Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access from adb shell? #77

Closed
anarcat opened this issue Apr 3, 2016 · 32 comments
Closed

access from adb shell? #77

anarcat opened this issue Apr 3, 2016 · 32 comments

Comments

@anarcat
Copy link

anarcat commented Apr 3, 2016

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
Copy link
Member

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
Copy link
Author

anarcat commented Apr 12, 2016

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

@palxex
Copy link

palxex commented Sep 16, 2016

@fornwall anyway to make chroot work?

@gene-pavlovsky
Copy link

gene-pavlovsky commented Nov 14, 2016

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
Copy link

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
Copy link

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

@lmsb
Copy link

lmsb commented Oct 25, 2017

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

@tni
Copy link

tni commented Oct 27, 2017

@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
Copy link

suchipi commented Apr 24, 2018

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

@ghost
Copy link

ghost commented Jul 19, 2018

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

@Gia90
Copy link

Gia90 commented Sep 14, 2018

@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
Copy link

ghost commented Sep 14, 2018

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
Copy link

Gia90 commented Sep 15, 2018

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 😄

@sultanahamer
Copy link

@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
Copy link
Author

anarcat commented Oct 17, 2018

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

@sultanahamer
Copy link

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
  1. run sshd - starts ssh server
sshd
  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

adb forward tcp:8022 tcp:8022
  1. 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
Copy link

Gia90 commented Oct 18, 2018

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
Copy link

@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
Copy link

Gia90 commented Oct 21, 2018

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

@verboze
Copy link

verboze commented Dec 7, 2018

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
Copy link

verboze commented Dec 7, 2018

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
Copy link

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

@verboze
Copy link

verboze commented Dec 8, 2018

@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
Copy link

sultanahamer commented Dec 18, 2018

@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
Copy link

foxundermoon commented Sep 1, 2019

  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
Copy link

natethern commented Sep 20, 2019

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
Copy link

DRSDavidSoft commented Apr 11, 2020

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
Copy link

tsu -e doesn't exist? @DRSDavidSoft

@Erisa
Copy link

Erisa commented Sep 8, 2020

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
Copy link

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

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

@chandrashekarcn
Copy link

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
Copy link

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

@ghost ghost locked and limited conversation to collaborators Oct 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests