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
su -G 3003 $(stat -c %u /data/data/com.termux) -c "command"
Where:
-G 3003— adds theAID_INETgroup (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
Installing packages that need network access when running from a root shell:
su -G 3003 $(stat -c %u /data/data/com.termux) -c "pkg install openjdk-17"Running apt/dpkg from root without triggering Termux's safety lock:
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:
# 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
-Gflag is Magisk-specific. Stock Android su (if any) or other root solutions may not support it. - Root access — The
sucommand 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.termuxexisting.
When This Pattern Fails
- No Magisk — The
-Gflag won't be recognized by non-Magisk su implementations. - Magisk denies root — If the calling app/process isn't in Magisk's superuser allowlist,
suitself fails before reaching the-Ghandling. - Wrong group number —
3003isAID_INETon most Android devices, but custom ROMs may use different GIDs. - SELinux enforcing — Even with correct UID/GID, some operations may still be blocked by SELinux policies.
Related
- See
termux-su-wrapper-analysis.mdfor how Termux's/data/data/com.termux/files/usr/bin/suwrapper works - See
check_root.shfor the full root diagnostic script