# Termux su -G Group/UID Pattern ## Context Termux has a safety mechanism that prevents the root user from directly running `apt`, `dpkg`, or `pkg`. This is by design — running package managers as root can break Termux's environment. However, some operations (like installing packages that require network access) need to run as the Termux app UID with the correct supplementary groups. The `su -G` pattern addresses this. ## The Pattern ```bash su -G 3003 $(stat -c %u /data/data/com.termux) -c "command" ``` Where: - `-G 3003` — adds the `AID_INET` group (GID 3003), which grants network access permissions - `$(stat -c %u /data/data/com.termux)` — gets the UID of the Termux app (e.g., `u0_a329`) - `-c "command"` — the command to execute as that UID with the specified group ## Common Use Cases 1. **Installing packages that need network access** when running from a root shell: ```bash su -G 3003 $(stat -c %u /data/data/com.termux) -c "pkg install openjdk-17" ``` 2. **Running apt/dpkg from root** without triggering Termux's safety lock: ```bash su -G 3003 $(stat -c %u /data/data/com.termux) -c "apt update && apt install aapt2" ``` ## Critical Limitation: No Shell Metacharacters The `su -G` command uses `execvp`-style execution. If the command string contains shell metacharacters (`;`, `&&`, `||`, `$()`, etc.), they may be interpreted by the shell and trigger SELinux domain restrictions. **Problem**: Commands with `;` or `&&` may fail with `Permission denied` or `inaccessible` because the shell tries to execute binaries in `/data/data/com.termux/...` under SELinux constraints. **Solution**: Use `env` with single commands instead of compound shell expressions: ```bash # BAD — may fail due to SELinux su -G 3003 $(stat -c %u /data/data/com.termux) -c "cmd1; cmd2" # GOOD — single command, no shell interpretation su -G 3003 $(stat -c %u /data/data/com.termux) -c "env VAR=val /path/to/binary args" ``` ## Requirements - **Magisk su** — The `-G` flag is Magisk-specific. Stock Android su (if any) or other root solutions may not support it. - **Root access** — The `su` command itself must be able to run (i.e., Magisk must grant root to the calling process). - **Termux app installed** — The UID lookup depends on `/data/data/com.termux` existing. ## When This Pattern Fails 1. **No Magisk** — The `-G` flag won't be recognized by non-Magisk su implementations. 2. **Magisk denies root** — If the calling app/process isn't in Magisk's superuser allowlist, `su` itself fails before reaching the `-G` handling. 3. **Wrong group number** — `3003` is `AID_INET` on most Android devices, but custom ROMs may use different GIDs. 4. **SELinux enforcing** — Even with correct UID/GID, some operations may still be blocked by SELinux policies. ## Related - See `termux-su-wrapper-analysis.md` for how Termux's `/data/data/com.termux/files/usr/bin/su` wrapper works - See `check_root.sh` for the full root diagnostic script