├── install.sh ├── README.md └── sudo /install.sh: -------------------------------------------------------------------------------- 1 | #!/data/data/com.termux/files/usr/bin/bash 2 | 3 | # Setup 4 | # updating pkg 5 | clear 6 | echo ' Updating pkg...' 7 | sleep 3;clear 8 | apt update;clear 9 | 10 | # Installing ncurses-utils to avoiding eror 11 | echo ' Installing addons to avoiding eror...' 12 | sleep 3;clear 13 | apt install ncurses-utils;clear 14 | 15 | 16 | echo " Setup Preferences!" 17 | sleep 3;clear 18 | ####################### 19 | 20 | # Setup sudo 21 | cat sudo > /data/data/com.termux/files/usr/bin/sudo 22 | 23 | # Giving permission 24 | chmod 700 /data/data/com.termux/files/usr/bin/sudo 25 | 26 | # Exit 27 | echo ' Exiting...' 28 | sleep 3;clear 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Termux 2 | A bash script that provides sudo for Termux 3 | Termux is a terminal emulator and Linux environment for Android 4 | 5 | **Requirements** 6 | 7 | Rooted phone with su binary 8 | SUDO WILL NOT WORK WITHOUT SU 9 | 10 | # Install 11 | 1. ```cd superuser``` 12 | 2. ```chmod +x install.sh``` 13 | 3. ```./install.sh``` 14 | 15 | **Features** 16 | 17 | - Sets up its environment automatically on first run, no need to do anything but use it 18 | - Creates a root folder ```.suroot``` in the Termux home folder with proper root permissions and ownership 19 | - Creates ```.bashrc``` file in root folder with proper PATH and LD_LIBRARY_PATH variables set so all binaries function correctly 20 | - Bash prompt PS1 variable is also set so you don't have ```bash-4.4#``` as prompt just ```#``` 21 | - Automatically creates ```.bash_history``` in root folder when you drop to a root shell so root shell history is preserved 22 | - Can be used like ordinary sudo (but only as root, no other user) 23 | - Can drop to root shell ```sudo su [-]``` 24 | - Runs built in Termux binaries and exteral binaries with optional arguments as root in current directory 25 | - Generates output in shell currently using 26 | - Can be used in other bash scripts 27 | - [option] Can turn off colored error messages be editing the variable ```colored``` at the beginning of sudo file 28 | ``` 29 | Usage: 30 | 31 | sudo su [-] 32 | Drop to root shell 33 | 34 | sudo [] 35 | Run command as root with optional arguments 36 | ``` 37 | 38 | **This was inspired by the following:** 39 | 40 | https://github.com/cswl/tsu 41 | https://gist.github.com/cswl/cd13971e644dc5ced7b2 42 | 43 | **Copy paste from** 44 | 45 | https://gitlab.com/st42/sudo-termux 46 | 47 | **Script kiddies in bellow :D** 48 | 49 | https://github.com/twentysvns/ 50 | 51 | # Thanks very much 52 | 53 | 54 | -------------------------------------------------------------------------------- /sudo: -------------------------------------------------------------------------------- 1 | #!/data/data/com.termux/files/usr/bin/bash 2 | 3 | #set colored=true to turn on colored error messages 4 | #set colored=false to turn off colored error messages 5 | colored=true 6 | 7 | #red=1 green=2 yellow=3 8 | color() { 9 | if [ $colored == "true" ]; then 10 | echo "$(tput setaf $1)${*:2}$(tput sgr0)" 11 | else 12 | echo "${*:2}" 13 | fi 14 | } 15 | 16 | show_usage() { 17 | echo -e "\n`color 3 Usage:`\n" 18 | echo 'sudo su [-]' 19 | echo -e " `color 2 Drop to root shell`\n" 20 | echo 'sudo []' 21 | echo -e " `color 2 Run command as root with optional arguments`\n" 22 | exit 23 | } 24 | 25 | SYSBIN=/system/bin 26 | SYSXBIN=/system/xbin 27 | BB=$SYSXBIN/busybox 28 | PRE=/data/data/com.termux/files 29 | ROOT_HOME=$PRE/home/.suroot 30 | BINPRE=$PRE/usr/bin 31 | LDLP="export LD_LIBRARY_PATH=$PRE/usr/lib" 32 | CMDLINE="PATH=$PATH:$SYSXBIN:$SYSBIN;$LDLP;HOME=$ROOT_HOME;cd $PWD" 33 | 34 | if [ -x /magisk/.core/bin/su ]; then 35 | SU=/magisk/.core/bin/su 36 | elif [ -x /sbin/su ]; then 37 | SU=/sbin/su 38 | elif [ -x $SYSXBIN/su ]; then 39 | SU=$SYSXBIN/su 40 | elif [ -x /su/bin/su ]; then 41 | SU=/su/bin/su 42 | else 43 | echo -e "\n`color 1 su` executable not found" 44 | echo -e "`color 1 sudo` requires `color 1 su`\n" 45 | exit 46 | fi 47 | 48 | if [ ! -d $ROOT_HOME ]; then 49 | if [ -x $BB ] && [ $($BB --list | grep -w mount) == "mount" ]; then 50 | MOUNTEX="$BB mount" 51 | elif [ -x $SYSBIN/mount ]; then 52 | MOUNTEX="$SYSBIN/mount" 53 | else 54 | echo -e "\nCannot find `color 1 mount` executable" 55 | echo -e "`color 2 Unable to setup sudo`\n" 56 | exit 57 | fi 58 | MOUNT_RW="$MOUNTEX -o rw,remount,rw /system" 59 | MOUNT_RO="$MOUNTEX -o ro,remount,ro /system" 60 | if [ -x "/sbin/magisk" ]; then 61 | unset LD_LIBRARY_PATH 62 | $SU -c "$CMDLINE;$MOUNT_RW" 63 | $SU -c "$CMDLINE;mkdir $ROOT_HOME" 64 | $SU -c "$CMDLINE;chmod 700 $ROOT_HOME" 65 | BASHRC="'PS1=\"# \"\nexport TERM=$TERM\n$LDLP\nexport PATH=$PATH:$SYSXBIN:$SYSBIN'" 66 | $SU -c "$CMDLINE;echo -e $BASHRC > $ROOT_HOME/.bashrc" 67 | $SU -c "$CMDLINE;chmod 700 $ROOT_HOME/.bashrc" 68 | $SU -c "$CMDLINE;$MOUNT_RO" 69 | else 70 | $SU -c "$MOUNT_RW" 71 | $SU -c "mkdir $ROOT_HOME" 72 | $SU -c "chmod 700 $ROOT_HOME" 73 | BASHRC="'PS1=\"# \"\nexport TERM=$TERM\n$LDLP\nexport PATH=$PATH:$SYSXBIN:$SYSBIN'" 74 | $SU -c "echo -e $BASHRC > $ROOT_HOME/.bashrc" 75 | $SU -c "chmod 700 $ROOT_HOME/.bashrc" 76 | $SU -c "$MOUNT_RO" 77 | fi 78 | fi 79 | 80 | ARGS=$(printf '%q ' "$@") 81 | 82 | if [ -z "$*" ]; then 83 | show_usage 84 | elif [ $1 == "su" ]; then 85 | CMDLINE="$CMDLINE;$BINPRE/bash" 86 | elif [ -x "$BINPRE/$1" ]; then 87 | CMDLINE="$CMDLINE;$BINPRE/$ARGS" 88 | elif [ -x $SYSBIN/$1 ] || [ -x $SYSXBIN/$1 ] || [ -x $1 ]; then 89 | CMDLINE="$CMDLINE;$ARGS" 90 | else 91 | echo -e "\nCommand `color 1 $1` not found" 92 | echo -e "`color 2 Check your spelling and try again`\n" 93 | fi 94 | 95 | pre_env_chk=`$SU --help|grep -e --preserve-environment` 96 | 97 | if [ -x "/sbin/magisk" ]; then 98 | unset LD_LIBRARY_PATH 99 | fi 100 | 101 | if [ -n "$pre_env_chk" ]; then 102 | $SU --preserve-environment -c "$CMDLINE" 103 | else 104 | $SU -c "$CMDLINE" 105 | fi 106 | # Reset echo 107 | stty sane 108 | --------------------------------------------------------------------------------