如何编写自定义ls命令并在已Root安卓终端运行及C代码部署疑问
ls Command on Rooted Android Great job getting your custom ls code written! Let's walk through how to get it running on your rooted Android device, and answer your question about deploying to /system/bin.
First: Compile Your C Code for Android
Android uses different CPU architectures (most commonly ARM64 or 32-bit ARM) than desktop x86 systems, so you need to compile your code to match your device's hardware. Here are two straightforward ways:
Option 1: Cross-Compile on a Desktop (Linux/macOS)
- Grab the Android NDK (Native Development Kit) — you can get it via Android Studio or as a standalone download.
- Identify your device's architecture: Run
uname -min a rooted terminal (output likeaarch64for ARM64,armv7lfor 32-bit ARM). - Use the NDK's toolchain to compile:
- For ARM64:
aarch64-linux-android29-clang your_ls.c -o custom_ls(replace29with your device's Android API level if needed) - For 32-bit ARM:
armv7a-linux-androideabi29-clang your_ls.c -o custom_ls
- For ARM64:
Option 2: Compile Directly on Android (Easier for Testing)
Use Termux, a powerful terminal emulator for Android:
- Install Termux from F-Droid (the Google Play version is outdated).
- Open Termux and install the compiler:
pkg install clang - Copy your C code into Termux's storage:
cp /sdcard/your_ls.c ~/ - Compile:
clang your_ls.c -o custom_ls
Deploy the Binary to Your Rooted Device
If you cross-compiled on a desktop, transfer the binary to your device via ADB:adb push custom_ls /sdcard/
Install It to /system/bin (Or Run It Directly)
Your question: Is putting it in /system/bin enough? Mostly yes, but there are critical steps to make it work properly:
- Gain root access: Open your terminal emulator and run
su(grant root permissions when prompted). - Remount
/systemas read-write: By default,/systemis read-only to protect system files. Run:mount -o rw,remount /system
Note: Some newer devices use/system_rootinstead — check withmount | grep /systemif this fails. - Copy the binary:
cp /sdcard/custom_ls /system/bin/(adjust the source path if you compiled in Termux, e.g.,cp ~/custom_ls /system/bin/) - Set correct permissions: The binary needs to be executable by all users:
chmod 755 /system/bin/custom_ls - Remount
/systemback to read-only: Restore the device's default security state:mount -o ro,remount /system
Run Your Custom ls
Now, open any terminal emulator (rooted or not) and type custom_ls — it should run just like the built-in ls command!
Alternative: Run Without Modifying /system
If you don't want to alter the system directory, you can run the binary directly:
- Navigate to its folder:
cd /sdcard/ - Execute it:
./custom_ls
Or add its path to yourPATHtemporarily:export PATH=$PATH:/sdcard/, then just typecustom_ls.
Troubleshooting Tips
- "Exec format error": Your binary's architecture doesn't match your device's. Double-check the compiler toolchain you used.
- "Permission denied": Ensure you set
755permissions, and temporarily disable SELinux if needed:su -c setenforce 0(note: this reduces security, so only use for testing). - Unexpected behavior: Android uses Bionic libc, which is mostly POSIX-compliant but has minor differences from glibc. Verify your code uses Android-compatible functions.
内容的提问来源于stack exchange,提问作者NoNoob

