termux / termux-app

Termux - a terminal emulator application for Android OS extendible by variety of packages.
https://f-droid.org/en/packages/com.termux
Other
32.97k stars 3.49k forks source link

access from adb shell? #77

Closed anarcat closed 8 years ago

anarcat commented 8 years ago

is there a way to access the termux binaries from the ADB shell?

root@ville:/data/data/com.termux/files # ./usr/bin/tmux
CANNOT LINK EXECUTABLE DEPENDENCIES: library "libutil.so" not found

is there a way to enter the termux environment from ADB?

i thought chroot could work, but it doesn't either:

root@ville:/data/data/com.termux/files # echo $(ls usr/bin/)
am applets apt apt-cache apt-config apt-get apt-key ash bash busybox chsh dalvikvm dash df dpkg dpkg-deb dpkg-divert dpkg-query dpkg-split dpkg-trigger getprop gpg gpg-zip gpgsplit gpgv ip logcat ping ping6 pm sh su termux-elf-cleaner termux-fix-shebang termux-open-url termux-reload-settings termux-setup-storage tmux
root@ville:/data/data/com.termux/files # ./usr/bin/bash
CANNOT LINK EXECUTABLE DEPENDENCIES: library "libandroid-support.so" not found
1|root@ville:/data/data/com.termux/files # chroot . ./usr/bin/bash
chroot: can't execute './usr/bin/bash': No such file or directory
127|root@ville:/data/data/com.termux/files # chroot . /usr/bin/bash
chroot: can't execute '/usr/bin/bash': No such file or directory
127|root@ville:/data/data/com.termux/files # chroot . /data/data/com.termux/files/usr/bin/bash
chroot: can't execute '/data/data/com.termux/files/usr/bin/bash': No such file or directory
127|root@ville:/data/data/com.termux/files # chroot /data/data/com.termux/files /usr/bin/bash
chroot: can't execute '/usr/bin/bash': No such file or directory
127|root@ville:/data/data/com.termux/files #
fornwall commented 8 years ago

You need the LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib environment variable setup for the system linker to find the libraries. You could try with just

export LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib

Another solution could be to start telnetd on the device (it uses port 8023 by default), then use adb port forwarding to allow a telnet connection.

anarcat commented 8 years ago

thanks! maybe a section in the help pages could be added for this?

palxex commented 7 years ago

@fornwall anyway to make chroot work?

gene-pavlovsky commented 7 years ago

That's what I got so far (on my rooted phone):

/data/data/com.termux/files/home/bin/termux-shell.sh (created in Termux):

#!/system/bin/sh
export PREFIX='/data/data/com.termux/files/usr'
export HOME='/data/data/com.termux/files/home'
export LD_LIBRARY_PATH='/data/data/com.termux/files/usr/lib'
export PATH="/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets:$PATH"
export LANG='en_US.UTF-8'
export SHELL='/data/data/com.termux/files/usr/bin/bash'
cd "$HOME"
exec "$SHELL" -l

/data/home/bin/termux.sh (/data/home and everything owned by shell user, created in adb shell):

#!/system/bin/sh
su $(stat -c %u /data/data/com.termux) /data/data/com.termux/files/home/bin/termux-shell.sh

From my laptop:

C:\cygwin64\home\gene # adb shell /data/home/bin/termux.sh
bash-4.4$ apt --version
apt --version
apt 1.2.12 (arm)
bash-4.4$ pwd
pwd
/data/data/com.termux/files/home
bash-4.4$ termux-chroot
termux-chroot
bash-4.4$

termux-chroot works. Problems: Terminal is "dumb" - Tab, Ctrl-R, Ctrl-D etc. don't work. Even if I set TERM=xterm or TERM=linux, it doesn't do anything. Any ideas about that?

andresmrm commented 7 years ago

I tried following @gene-pavlovsky tip, but I can't use anything network related from that user. With curl I get Couldn't resolve host. Trying to start dropbear I get Error resolving: No address associated with hostname. With apt: 7 - No address associated with hostname.

Just to be clear, I run:

adb shell
su $(stat -c %u /data/data/com.termux)
export PREFIX='/data/data/com.termux/files/usr'
export HOME='/data/data/com.termux/files/home'
export LD_LIBRARY_PATH='/data/data/com.termux/files/usr/lib'
export PATH="/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets:$PATH"
export LANG='en_US.UTF-8'
export SHELL='/data/data/com.termux/files/usr/bin/bash'
cd "$HOME"
exec "$SHELL" -l

curl works from the shell user, or from "normal" termux (typing at the terminal at the phone).

Using LineageOS 14.1 (Android 7.1.1).

nakov0301 commented 7 years ago

I have the same network problem as @andresmrm. Is there any solution to this?

lmsb commented 6 years ago

i have the same problem, TERM=dumb, any fix for that? Tab, Ctrl-R, Ctrl-D etc. don't work

tni commented 6 years ago

@andresmrm & @nakovdev:

This is probably a 'su' bug, the groups get dropped. From a termux shell: $ id uid=10077(u0_a77) gid=10077(u0_a77) groups=3003(inet),9997(everybody),50077(all_a77)

Using adb shell / su to change to u0_a77: $ id uid=10077(u0_a77) gid=10077(u0_a77) groups=10077(u0_a77) context=u:r:sudaemon:s0

\\

IME, adb shell is rather flaky and using ssh works much better. You can forward the sshd port over USB, using adb port forwarding: https://glow.li/technology/2016/09/20/access-termux-via-usb.html

suchipi commented 6 years ago

@tni's link has been moved here: https://glow.li/technology/2016/9/20/access-termux-via-usb/

untoreh commented 5 years ago

not sure if forwarding a port over adb is better than just ssh over usb tether

Gia90 commented 5 years ago

@andresmrm & @nakovdev:

This is probably a 'su' bug, the groups get dropped. From a termux shell: $ id uid=10077(u0_a77) gid=10077(u0_a77) groups=3003(inet),9997(everybody),50077(all_a77)

Using adb shell / su to change to u0_a77: $ id uid=10077(u0_a77) gid=10077(u0_a77) groups=10077(u0_a77) context=u:r:sudaemon:s0

I have a similar output when I run "id" in a termux shell:

$ id
uid=10077(u0_a77) gid=10077(u0_a77) groups=3003(inet),9997(everybody),20077(u0_a77_cache),50077(all_a77)

but the interesting part comes when I run "id" passing the termux user in the termux shell:

$ id u0_a77
uid=10077(u0_a77) gid=10077(u0_a77) groups=10077(u0_a77)

The user is not part of the "inet" group and that's why we are experiencing network problems. I'd like to understand why ...

ghost commented 5 years ago

The user is not part of the "inet" group

The necessary groups (like inet) are given dynamically on application start by Android OS. So, if you switched in ADB to Termux user, you probably won't have 'inet' group.

Gia90 commented 5 years ago

Thank you @xeffyr for the explanation. I also found this document which helped me understanding better the group management in Android: https://github.com/keesj/gomo/wiki/AndroidSecurityUserAndGroups

All this means that there will be no easy fix for this issue. I already have in mind a workaround for a project I am working on.. I'll post it here if it works :smile:

sultanahamer commented 5 years ago

@anarcat if you want to enter termux from computer, why not install ssh package in termux and just ssh from computer. You would have everything working.

May I know what do you want to achieve doing so?

anarcat commented 5 years ago

i did not want to have to setup network and a daemon to access the tmux facilities.

sultanahamer commented 5 years ago

You need not require big setup etc. You can do everything with a quick and simple one time setup.

Here is my setup

  1. install openssh(ssh client and server package) in termux using below command

    apt install openssh
  2. run sshd - starts ssh server

    sshd
  3. add your computer ssh key to ~/.ssh/authorized_keys(you can transfer the key using adb by creating a file using adb push or adb shell and then copying content from file manager app and paste or any other means like whatsapp(not so secure)). you can find your system key in ~/.ssh/id_rsa.pub

  4. I personally like connecting to my mobile using wifi(wifi over adb or ssh directly with ip) - but here in your case as you like to do it via adb, you can port forward with this command

    adb forward tcp:8022 tcp:8022
  5. All set and you can ssh into with command ssh -p 8022 root@localhost here use localhost if you did port forward else u can type the ip address of mobile if both computer and mobile are in same network.

from now on, whenever you want to ssh, open termux in mobile and type sshd. And then from laptop you can login either via portward using adb or wifi

Tip: I installed termux shortcuts app which will show me scripts in a simple widget. So I created a file with 1 line content

sshd

and named it start ssh. Created another one with content

killall shhd

and named it stop ssh So now I can start/stop sshd with a single tap than opening termux and typing it in.

Gia90 commented 5 years ago

In my case, I wanted to send command in the termux contest from a remote server with the least (possibly no) user interaction and client side configuration.

@sultanahamer your solution doesn't require complex configurations and it's definitely cleaner than the one I was working on, I think I will use yours :)

sultanahamer commented 5 years ago

@Gia90 for "In my case, I wanted to send command in the termux contest from a remote server with the least (possibly no) user interaction and client side configuration" In such case, I would use a twitter / telegram listening bot in termux. There are many of such scripts available online - you can try nodejs for the same (https://github.com/yagop/node-telegram-bot-api). So, just run them on termux and you can publish your messages from twitter / telegram. In case you dont want to use any 3rd party like twitter / telegram, create your own server in cloud, create your own server and write a client in termux that can listen to any commands from your server in cloud and execute commands in termux environment.

Gia90 commented 5 years ago

Thanks :) I will also consider this as a possible alternative.

verboze commented 5 years ago

Problems: Terminal is "dumb" - Tab, Ctrl-R, Ctrl-D etc. don't work. Even if I set TERM=xterm or TERM=linux, it doesn't do anything. Any ideas about that?

Thanks @gene-pavlovsky for the steps. To fix the issue mentioned above, I executed su as follows:

su -l $(stat -c %u /data/data/com.termux) -c /data/data/com.termux/files/home/bin/termuxshell

-l keeps the termux user environment, -c to execute the termux script.

verboze commented 5 years ago

The necessary groups (like inet) are given dynamically on application start by Android OS. So, if you switched in ADB to Termux user, you probably won't have 'inet' group.

Is there a way to assign this group while switching users? I've looked high and low, can't seem to come across a solution. My goal is to start sshd from the adb shell; I have a broken screen, so accessing the actual termux app is not an option for me :(

sultanahamer commented 5 years ago

@verboze why not try scrcpy if you have a broken screen. It would help you get everything done.

verboze commented 5 years ago

@sultanahamer I did NOT know about this tool! thanks a bunch, I'll give it a try!

Edit: worked like a charm! Thanks for the tip sultanahamer!

sultanahamer commented 5 years ago

@verboze you can try adb over wifi using command adb tcpip 5555 and after that, you can place your mobile in your bag and use it from computer.. :) with same scrcpy

foxundermoon commented 4 years ago
  1. add your computer ssh key to ~/.ssh/authorized_keys(you can transfer the key using adb by creating a file using adb push or adb shell and then copying content from file manager app and paste or any other means like whatsapp(not so secure)). you can find your system key in ~/.ssh/id_rsa.pub
  2. I personally like connecting to my mobile using wifi(wifi over adb or ssh directly with ip) - but here in your case as you like to do it via adb, you can port forward with this command

on my phone

adb shell
su
mkdir ~/.ssh
mkdir: '//.ssh': Read-only file system

cannot config authorized_keys

can run this code on phone to copy key

ssh user@linux_clinet "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys

actually the absolute path is

vi /data/data/com.termux/files/home/.ssh/authorized_keys
natethern commented 4 years ago

on my phone

adb shell
su
mkdir ~/.ssh
mkdir: '//.ssh': Read-only file system

cannot config authorized_keys

can run this code on phone to copy key

ssh user@linux_clinet "cat ~/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys

actually the absolute path is

vi /data/data/com.termux/files/home/.ssh/authorized_keys

@foxundermoon When you launch an adb shell, you are a different user (your username is "shell") than when you log into termux (your username is "u0_XYZ", where XYZ are assigned when the app is installed. Your $HOME and $PATH are also very different. There are various terminal emulator apps that give you access to a shell very similar to an adb shell.

Termux gives you a much more powerful shell (it is very close to a standard linux shell). You have many more tools and can install even more or compile your own. That is the whole point of termux.

The whole point of this issue/thread is that users are trying to use the adb shell as a starting point to remotely access the power of termux. Another, far more common, method of remote termux access is via ssh, but that is not what is being discussed here. Follow the termux wiki instructions to set up ssh - @sultanahamer's (very correct) comments are just to illustrate that setting up ssh is easy. But you have to know what "~" means and where to "adb push" a file and how to set its permissions and ...

DRSDavidSoft commented 4 years ago

Alternative method ROOT

On my rooted phone (running LineageOS), I always use the tsu binary to get root access from Termux. It is useful since it automatically sets all of the required environment variables

Running tsu -h provides the following:

tsu - a wrapper for su for Termux

Usage: tsu [-a|-e|-p|-s <shell>]

-s [</path/to/shell>]
    Use an alternate specified shell. `//usr` is expanded to $PREFIX.
-p
    Prepend /system/bin:/system/xbin to PATH and /system/lib{64} to LD_LIBRARY_PATH.
-a
    Append /system/bin:/system/xbin to PATH and /system/lib{64} to LD_LIBRARY_PATH.
-e
    Setup up some enviroment variables as when in Termux.

For details of the options see:
https://github.com/cswl/tsu

tsu -e

It seems that there is a -e flag, which specifically sets all the required environment variables needed for Termux.

Knowing this, I run /data/data/com.termux/files/usr/bin/tsu -e:

C:\Users\David
λ adb shell

ja3gxx:/ # /data/data/com.termux/files/usr/bin/tsu -e

While adb is in root. This gives me the same Termux root environment from adb.

Termux shell running under adb as root

Personally I find it useful, especially since it directly gives me a root terminal. I thought it could help others, too.

ihsakashi commented 3 years ago

tsu -e doesn't exist? @DRSDavidSoft

Erisa commented 3 years ago

As far as I understand it, tsu wraps around your su binary so it's up to the provider of your root access to supply the -e option?

As for tsu itself, pkg install tsu

gMan1990 commented 3 years ago

-l keeps the termux user environment, -c to execute the termux script.

How to fix: Cannot execute -c: Permission denied?

chandrashekarcn commented 3 years ago

Problems: Terminal is "dumb" - Tab, Ctrl-R, Ctrl-D etc. don't work. Even if I set TERM=xterm or TERM=linux, it doesn't do anything. Any ideas about that? solution : inputrc add requred to above file

rojenzaman commented 2 years ago

run adb shell as root:

adb root
adb shell

then:

/sbin/su -c PATH=/sbin/.magisk/busybox env -i  PREFIX=/data/data/com.termux/files/usr  LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so  HOME=/data/data/com.termux/files/home/.suroot  TMPDIR=/data/data/com.termux/files/home/.suroot/.tmp  ANDROID_DATA=/data  TERM=xterm-256color  ANDROID_ROOT=/system  PATH=/data/data/com.termux/files/usr/bin:/data/data/com.termux/files/usr/bin/applets:/system/bin:/system/xbin:/sbin:/sbin/bin  /data/data/com.termux/files/usr/bin/bash