On LineageOS 23.2 `adb root`:

```bash
files/usr/bin/bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:$PATH; export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so; bash -i'
```
```
run-as: package not debuggable: com.termux
```

Can't I somehow force running a command as `com.termux` user thanks to `root`?
May 5, 6:23pm

On LineageOS (and most modern Android versions), the run-as command enforces SELinux and app sandboxing restrictions, even for the root user. The error run-as: package not debuggable: com.termux means that Termux is not marked as a debuggable app in its manifest, so run-as refuses to switch to its user context—even with root privileges.


Workarounds to Run Commands as com.termux User

1. Temporarily Make Termux Debuggable

  • Requires: Root access and a way to modify app manifests (e.g., apktool or smali patching).
  • Steps:
    1. Extract Termux’s APK:
      bash
      apktool d /data/app/com.termux-*/base.apk -o termux_mod
    2. Edit AndroidManifest.xml to add android:debuggable="true" to the <application> tag.
    3. Rebuild and reinstall the APK:
      bash
      apktool b termux_mod -o termux_debug.apk
      adb install -r termux_debug.apk
    4. Now run-as com.termux should work.
  • Downside: Requires rebuilding the APK, and updates may revert the change.

2. Use su to Switch to Termux’s UID

  • If Termux’s UID is known (e.g., u0_a123), you can use su to switch to it:
    bash
    su -c "su u0_a123 -c 'export PATH=/data/data/com.termux/files/usr/bin:\$PATH; bash -i'"
  • Find Termux’s UID:
    bash
    grep termux /data/system/packages.list
    or
    bash
    ls -n /data/data | grep com.termux
  • Note: This may still fail if SELinux denies the transition.

3. Directly Access Termux’s Files as Root

  • If your goal is to run scripts or access files in Termux’s directory, you can do so directly as root:
    bash
    cd /data/data/com.termux/files/usr/bin
    ./bash -lic 'export PATH=/data/data/com.termux/files/usr/bin:$PATH; bash -i'
  • Limitation: Some Termux-specific environment variables (e.g., $PREFIX) may not be set.

4. Use nsenter to Join Termux’s Namespace

  • If Termux is running, you can find its PID and join its namespace:
    bash
    pid=$(pgrep -u u0_a123 bash)  # Replace u0_a123 with Termux's UID
    nsenter -t $pid -m -u -i -n -p bash -c 'export PATH=/data/data/com.termux/files/usr/bin:$PATH; bash -i'
  • Note: Requires nsenter support in your kernel and may not work on all devices.

5. Patch run-as or SELinux Policy (Advanced)

  • For run-as: Some custom ROMs allow overriding the debuggable check by modifying the run-as binary.
  • For SELinux: You can try setting it to permissive mode:
    bash
    setenforce 0
    Then retry run-as com.termux. Warning: This weakens security.

Recommended Approach

  • If you just need to run Termux commands, use Termux itself (e.g., via termux-exec or a Termux session).
  • If you need to access Termux’s environment as root, use su + Termux’s UID (method 2).
  • If you need full Termux user context, rebuild Termux as debuggable (method 1).

May 5, 6:23pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more