Termux-app: Add better support for root.

Created on 12 Mar 2016  Â·  34Comments  Â·  Source: termux/termux-app

Otherwise great, apps like tcpdump do not work at all because they need root access. using su from root does not preserve the enviroment, so it cannot find the binaries, and if you find them manually it cannot find the libs.

this would be really awesome.

Feedback needed enhancement

Most helpful comment

I started using Termux a couple weeks ago that I downloaded from F-Droid. I have been using it ever since. I noticed that root use was a problem so I searched online and came across this issues here and decided to solve the problem.

Check out my Github:

https://github.com/st42/termux-sudo.git

Here is the README.md

termux-sudo

A bash script that provides sudo for Termux
Termux is a terminal emulator and Linux environment for Android

Requirements

Rooted phone with su binary
SUDO WILL NOT WORK WITHOUT SU

Installing sudo

  1. Download sudo to phone and extract
  2. Open Termux and change to extraction directory
  3. Execute the following commands to place sudo into the correct directory with the proper permissions and ownership
cat sudo > /data/data/com.termux/files/usr/bin/sudo
chmod 700 /data/data/com.termux/files/usr/bin/sudo

Features

  • Sets up its environment automatically on first run, no need to do anything but use it
  • Creates a root folder .suroot in the Termux home folder with proper root permissions and ownership
  • Creates .bashrc file in root folder with proper PATH and LD_LIBRARY_PATH variables set so all binaries function correctly
  • Bash prompt PS1 variable is also set so you don't have bash-4.4# as prompt just #
  • Automatically creates .bash_history in root folder when you drop to a root shell so root shell history is preserved
  • Can be used like ordinary sudo (but only as root, no other user)
  • Can drop to root shell sudo su [-]
  • Runs built in Termux binaries and exteral binaries with optional arguments as root in current directory
  • Generates output in shell currently using
  • Can be used in other bash scripts
Usage:

sudo su [-]  
  Drop to root shell

sudo <command> [<args>]  
  Run command as root with optional arguments

This was inspired by the following:

https://github.com/cswl/tsu
https://gist.github.com/cswl/cd13971e644dc5ced7b2

All 34 comments

I had written a not so sudo script, which preserves environement and provides the user default shell...
https://gist.github.com/cswl/cd13971e644dc5ced7b2

Let me know what you think about it..

@cswl Looks interesting! Will look closer when I have more time.

Perhaps the su command can be likewise wrapped to make using it from Termux more seamless.

Tabe Kooistra has given this su alias:

alias su='/system/xbin/su --preserve-environment -c LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib exec /data/data/com.termux/files/usr/bin/bash --login'



@fornwall I was excited about Tabe Kooistra's aliasing but after adding it to my .bashrc file, su is invoked (as evidenced by the resultant toast from su) and breaks root at once by returning a $ prompt rather than a root shell prompt.


To query printenv and set while staying the alias, I had to escape su first to get a shell and found the environment isn't preserved at all. I'm fairly certain this is due to the comment I posted here after the issue was closed CANNOT LINK EXECUTABLE DEPENDENCIES: library "libgnustl_shared.so" not found #93

Again, my apologies. I segfaulted (=

Not sure how libraries works in Android, could we just link the termux lib path to the system libs? Then termux bins would work everywhere I imagine? More like an optware for Android than a terminal oasis :)

I threw in a su wrapper, which automatically uses user defined shell, and also allows to specify alternative shell..
If you guys wanna check it out.. https://github.com/cswl/tsu

It's my first time writing README.md .. so formatting is kinda screwed up right now...
I will be busy for a while, so will fix stuffs later.

@cswl Awesome!

It would be great if some people could try it up in different rooted devices/environments and report if it's working!

If it seems ok I guess we could add this as $PREFIX/bin/su in the default install (so running su will pick up this)? Or do you think it should be tsu?

It would be nice (if this wrapper script works well) to have it as the default su, so it works more "out of the box", and hopefully it wouldn't confuse/annoy people that Termux wraps the real su like this.

@fornwall

Well, tsu was intended to be a script to launch root shells (bash, zsh) available in Termux as root. I should really fix the README.

I'm not sure about the over-riding the su like that.
There are these options in the su binary from SuperSU. ( Cyanogenmod ships with it's own su implementation, but I can't test.)

 -cn, --context CONTEXT       switch to SELinux CONTEXT before invoking
 -mm, --mount-master          connect to a shell that can manipulate the
                               master mount namespace
  -mns, --mount-namespace PID  enter mount namespace used by PID

And su can also switch user, su 1000 will switch to that user id.

I can see how people would just want to type like sudo bash and expect it to work like in normal unixes, but then again Termux is not.
If your concern is about people asking the same thing again and again, maybe we could put it up in help section like "Termux and Root".

About testing, I will try posting it on Google+, so people can give feedback.

I started using Termux a couple weeks ago that I downloaded from F-Droid. I have been using it ever since. I noticed that root use was a problem so I searched online and came across this issues here and decided to solve the problem.

Check out my Github:

https://github.com/st42/termux-sudo.git

Here is the README.md

termux-sudo

A bash script that provides sudo for Termux
Termux is a terminal emulator and Linux environment for Android

Requirements

Rooted phone with su binary
SUDO WILL NOT WORK WITHOUT SU

Installing sudo

  1. Download sudo to phone and extract
  2. Open Termux and change to extraction directory
  3. Execute the following commands to place sudo into the correct directory with the proper permissions and ownership
cat sudo > /data/data/com.termux/files/usr/bin/sudo
chmod 700 /data/data/com.termux/files/usr/bin/sudo

Features

  • Sets up its environment automatically on first run, no need to do anything but use it
  • Creates a root folder .suroot in the Termux home folder with proper root permissions and ownership
  • Creates .bashrc file in root folder with proper PATH and LD_LIBRARY_PATH variables set so all binaries function correctly
  • Bash prompt PS1 variable is also set so you don't have bash-4.4# as prompt just #
  • Automatically creates .bash_history in root folder when you drop to a root shell so root shell history is preserved
  • Can be used like ordinary sudo (but only as root, no other user)
  • Can drop to root shell sudo su [-]
  • Runs built in Termux binaries and exteral binaries with optional arguments as root in current directory
  • Generates output in shell currently using
  • Can be used in other bash scripts
Usage:

sudo su [-]  
  Drop to root shell

sudo <command> [<args>]  
  Run command as root with optional arguments

This was inspired by the following:

https://github.com/cswl/tsu
https://gist.github.com/cswl/cd13971e644dc5ced7b2

Hi @st42 ,

I failed to get your script to work in my environment when special characters are involved, e.g. file names containing space.

A simple test case:

touch 'this is a file' && sudo ls 'this is a file'

Here is a script that I've been using for a little while that quotes arguments (against word splitting) FYR: https://gist.github.com/kennethho/bc7502dcf6c0b4a53ef85758f797fc8b

It is based on a random version of su script from termux-tools. Other than quoting (i.e. word splitting), I never really paid attention to other detail. It's just something FYR.

Hi @kennethho,
The problem has been fixed in the latest commit. Thanks for noticing this.

@st42 Is there a way to get this working to elevate permissions to the adb user shell on a unrooted phone? On android 6. All I really need is a workink logcat and till now I only seem to be able to get that through ADB in a termux terminal is will stop with

open '/dev/hwlog_switch' fail -1, 13. Permission denied

One my CM MM rooted phone and tablet this is working like a charm btw. Just saying. The other one I don't want to root for the SafetyNet stuff.I HATE GOOGLE.

@roelfrenkema As stated in the README.md termux-sudo requires a rooted phone. As far as accessing /dev/hwlog_switch it is owned by root and it's permissions aren't allowing you to access it from within the phone unless you have root access.

On CM12.1, these work as expected:

$ sudo svc wifi disable
$ sudo input keyevent 26

However, in CM13 the above commands don't work and I have to use these instead:

$ PATH=/system/bin su -c 'svc wifi disable'
$ PATH=/system/bin su -c 'input keyevent 26'

Strange.

@st42 I've added a su check to your script to support Magisk v9, sent you a pull request.

A benign but perplexing "sh: cd: /storage/emulated/0/foo: No such file or directory" shows up in the terminal when sudo is called in a working directory somewhere in my device's simulated sd card.

Adding this line to st42's sudo script:
PWD="$(sed 's|/storage/emulated|/mnt/shell/emulated|g' <<< "$PWD")"
anywhere before this line:
CMDLINE="PATH=$PATH:$SYSXBIN:$SYSBIN;$LDLP;HOME=$ROOT_HOME;cd $PWD"
made the problem go away.

@bdantas I cannot replicate any of the errors you speak of.
sudo svc wifi disable
sudo input keyevent 26
Both work on my device

Calling sudo from simulated sdcard also works.

My device is running CyanogenMod 12.1 which is Android 5.1.1.

@bdantas
Check out https://termux.com/storage.html
This may help with your simulated sdcard issues

Even after running termux-setup-storage on CM12.1, I still have issues with sudo when using simulated sdcard. For the sake of those struggling with this as well, I will document the problem and two possible solutions here.

THE PROBLEM
I've noticed the following when working with files on my device's emulated sdcard:

$ touch /storage/emulated/0/test # works fine
$ sudo touch /storage/emulated/0/test # gives "No such file or directory"

$ touch /mnt/shell/emulated/0/test # gives "Permission denied"
$ sudo touch /mnt/shell/emulated/0/test # works fine

This creates the vexing situation where performing an operation on a file requires using a different path depending on whether or not the operation requires root (i.e., I need to use /storage/emulated/0/foo for non-root operation, but /mnt/shell/emulated/0/foo for root operation).

I found two solutions to this problem.

SOLUTION 1 (cleaner): Make /storage/emulated accessible to sudo
In st42's sudo script:

ARGS=$(printf '%q ' "$@") # this line is already in the sudo script. keep it.
# add this line:
ARGS="$(sed 's|/storage/emulated|/mnt/shell/emulated|g' <<< "$ARGS")" 

With solution 1, an operation on /storage/emulated/0/test always works, sudo or not.

SOLUTION 2 (hackier): Give regular user permission for /mnt/shell/emulated

  1. Install the free software (apache 2.0) apk "Universal Init.d" (https://github.com/Androguide/Universal-init.d) to emulate init.d function, since init.d (boot-time scripts) is disabled in CM12.1. After installing, run the app and give it root permission when it asks for it. Make sure to flip the switch to "on".
  2. Create /system/etc/init.d/20shellfolderperms, put this in it:
mount -o rw,remount /
chmod 777 /mnt/shell
mount -o ro,remount /
  1. Make 20shellfolderperms executable and reboot.

With solution 2, an operation on /mnt/shell/emulated/0/test always works, sudo or not.

Solution 1 is far superior as (amongst other things) it will work on phones where root is available but /system cannot be altered without making an verified boot path fail. There's a few really nasty boot implementations out there.

su 0 svc wifi enable # works fine

Some useful info: in LineageOS (and I think in CM13 also) the su binary does not preserve the environment (-m -p --preserve-environment). The code is not present in su and the related help text is commented out. Source here.

@vlad3-3-3 Interesting, thanks for sharing!

Got error when using termux-sudo by @st42 with node
env: can't execute 'node': Permission denied
is there any workaround ?

Muhammad,

I do not use node, but on any *nix (including termux/android) sudo only elevates privileges of the first command it encounters. For example, in

sudo foo | bar

Foo gets run as root but not bar. Something like this would have the expected effect:

sudo bash -c "foo | bar"

If you share your actual command that's giving the permission error I might be able to help you write the command in a way that will work.

On March 30, 2017 6:15:13 AM EDT, Muhammad Sayuti notifications@github.com wrote:

Got error when using termux-sudo by @st42 with node
'env: can't execute 'node': Permission denied'
is there any workaround ?

i only try to install using bower and npm and i think this issue is related to #240

I'm sorry, any news on this? i was trying to npm install berkeleydb but i got

../dist/configure: not found
make: *** [Makefile:12: config] Error 127

Any known solution?
Thanks

Has anyone figured out a way to get a clean root shell on lineageos 15.1? I have tried both the su from 14.1 and the su for 15.1 from this thread. Both give me root permissions, but both mess up the shells:

  • Ctrl+c always closes the su process and all of its children (even if they are shells)
  • Any command is echoed in the shell before showing the output of the command
  • The TERM variable is changed and even if this is fixed manually, I can't find a way to make cursor keys work or ncurses applications run as intended.

Is there any way to start a fresh shell that is decoupled from the su process but still outputs to my current terminal, once I can start a process as root?

@yeahnope Did you try with tsu it's available on termux repos.. so you can just

pkge install tsu

Maybe we can close this issue by now, tsu works for all my root needs at least.

Not work sudo it's show

"su executable not found
sudo requires su "

What do i ?

What do i ?

root your device. Commands tsu, tsudo, sudo, su useless and will not work if your device is not rooted.

I install sudo for wifi-hacker but it is show

"su executable not found
sudo requires su"

I install sudo for wifi-hacker but it is show

root your device and su/sudo will work !

Was this page helpful?
0 / 5 - 0 ratings