15

I've recently started using the amazing Termux terminal app and linux environment. It provides a wealth of full-function terminal utilities. It would be great to be able to access these utilities from other consoles, especially when connected via ssh or adb shell. I've managed to make some functionality available by setting my PATH and LD_LIBRARY_PATH environment variables to the same values used by Termux's shell, but there are permissions problems and it seems messy.

Is there a way to ssh in to a Termux session? Termux doesn't seem to provide an ssh server package. Even if it did, presumably that server, and thus the ssh session granted by it, would run as a separate user from the termux user, resulting in similar permissions problems.

Is there a supported way to access the Termux user environment from other consoles?

3
  • I looked at various tutorials and I could only figure out how to connect via USB (wired) tether. I couldn't figure out how to get a wireless IP for the sshd server in Termux. If anyone is having trouble finding the IP address of your phone, plug it into your PC (with USB cable) and then look for the "Default Route" on the new wired connection on your PC, that's the only way I could get it working. Also you may need "whoami" and/or "passwd" in Termux to get the username. Port defaults to 8022. PS: Tried sshuttle through the Termux sshd and my ISP still throttles it down to pathetic .5 Mbps :-/
    – PJ Brunet
    Jun 12, 2020 at 23:52
  • @PJBrunet what did you try on termux to get the Ip Adress? Nov 12, 2020 at 16:31
  • For getting the ip of a device, Linux-Know-How might help. As a starter I recommend Lpic1. Nov 12, 2020 at 16:39

5 Answers 5

16

Termux does provide the openssh package, which contains both the ssh client and the sshd server.

Install the package with: apt install openssh, then start the server with sshd - it will run on port 8022 by default, so connect to it with ssh -p 8022 DEVICE_IP, and you can find the device wifi ip using ip addr list wlan0.

Password authentication is not supported, so you need to setup $HOME/.ssh/authorized_keys.

After you have connected you will run as the normal Termux user with environment variables setup correctly.

5
  • 1
    Wow, they should really mention that in the package description, which just says that it is a "Secure shell for logging in to a remote machine".
    – intuited
    Sep 1, 2016 at 17:23
  • 12
    Password authentication is supported. You need to run passwd to set a new password for your SSH server. However, what doesn't work is usernames (i.e. everyone can log in with every username with that one password), AFAIK. Jun 4, 2019 at 21:29
  • 2
    You can launch sshd on every boot using wiki.termux.com/wiki/Termux:Boot
    – fuzzyTew
    May 9, 2020 at 19:59
  • "wlan0" didn't give me a useable IP address
    – PJ Brunet
    Jun 12, 2020 at 21:10
  • @MAChitgarha your point is essential, otherwise there is no chance to connect. Well done! Nov 12, 2020 at 16:37
3

You need to install openssh in termux by running

pkg install openssh

and then, on the device you want to ssh from, generate a Public ID Key,

ssh-keygen

and just confirm and down change anything, then do

cat ~/.ssh/id_rsa.pub

and copy the output into ~/.ssh/authorized_keys and restart the sshd server and try to ssh,

if you encounter any errors, check the authorized_keys file and makesure your public key is all on one line

You have to do these steps above because Termux sshd doesnt support password logins, (IE, ssh user@ip_address then entering a password)

you have to ssh into termux using

ssh termux-ip-here -p 8022
1
  • Password authentication IS now supported. Nov 7, 2020 at 12:50
3

On your knees and bow,

Ip_Address_Of_Remote_Host = 192.168.1.20

(Assuming you have generated a key on the termux machine using

ssh-keygen -t rsa

There is a space inbetween ssh-keygen and -t and copied the key to Remote_Host using that machines log-on details

ssh-copy-id [email protected]).

There is a space inbetween ssh-copy-id and user

You should now be able to log on to the remote host without using a password.

ssh [email protected]

The rsa key generated above is not the key you place into the authorized_keys file on the termux machine.

The issue is getting the

id_rsa.pub

file from your Remote machine to the termux

~/.ssh/authorized_keys

file on the Local system (phone) the solution I used was scp, this command confuses a lot of people but if viewed from the position that it is just a cp command it's mysticism vanishes

cp file_to_copy where_to_copy_file

This command is run from the Termux machine (phone)

scp [email protected]:/home/user/.ssh/id_rsa.pub ~/.ssh/authorized_keys

There is a space inbetween id_rsa.pub and ~/

The path to your .ssh directory may be different just make sure it is the correct path you enter into he above command.

If you still have trouble logging on navigate to the

/data/data/com.termux/files/usr/etc/ssh/sshd_config file

Activate these two key pairs

ChallengeResponseAuthentication no

PubkeyAuthentication yes

1
  • For anybody with a slight understanding of SSH this should be straight forward for those who are new to GNU/Linux paths are what you need to be focused on. My next port of call is to see if X forwarding is possible for then it opens up the possibility of using the productivity suits on your desktop but writing to the termux machine, let me expand you will be able to use LibraOffice software on the termux machine but have it displayed on the desktop without lagging/latency this is the power of GNU/Linux. Jul 21, 2018 at 12:06
2

These are Android commands to run on Termux and setup a SSH client and server, allowing to connect from your desktop to your Android and vice-versa. This does not cover the SSH server and client set up on your desktop computer.

Using the SSH server https://termux.com/ssh.html

apt update
apt upgrade
apt install openssh

Here you can create a password for your keys and use it to connect to your computer, if you like a password. Otherwise, leave them in blank and no passwords are required, other than your DESKTOP user computer password.

How can I set up password-less SSH login? https://askubuntu.com/questions/46930/how-can-i-set-up-password-less-ssh-login

ssh-keygen -b 4096 -t rsa

This allows you to connect from your ANDROID to your DESKTOP by SSH connection without your ANDROID user password (as passwords are not supported by termux sshd server)

3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id https://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

This allows you to connect from your DESKTOP to your ANDROID by SSH connection without your ANDROID user password (if you want to use your's computer user password, skip this command)

  1. First Option Accessing termux user environment from other consoles Accessing termux user environment from other consoles

    scp [email protected]:~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
    
  2. Second Option How do I add SSH Keys to authorized_keys file? https://askubuntu.com/questions/46424/how-do-i-add-ssh-keys-to-authorized-keys-file

    cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
    

Scripts and notes for setting up and using SSHd on your Android device using Termux https://github.com/tomhiggins/TermuxSSHDsetup

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

ssh blank passphrase but still asking for it https://serverfault.com/questions/845623/ssh-blank-passphrase-but-still-asking-for-it

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa.pub
chmod 600 ~/.ssh/known_hosts
chmod 600 ~/.ssh/authorized_keys

Start the server by executing sshd (which will start it on the default port 8022) and run logcat -s 'syslog:*' to view the log output from the server process. Execute pkill sshd to stop the server.

logcat -s 'syslog:*'
pkill sshd
sshd

Finally, connecting from one to another and vice-versa:

Connecting from your ANDROID to your DESKTOP (the usage of your DESKTOP user password can be optional)

ssh [email protected]

Connecting from your DESKTOP to your ANDROID (must not to use your ANDROID user password)

Since Termux is a single-user system you may connect as any user - regardless of your specified username you will login as the only Termux user available.

The Termux sshd binary does not support password logins, so a key needs to be authorized in ~/.ssh/authorized_keys before connecting. Use ssh-keygen to generate a new one if desired.

ssh -p 8022 -i ~/.ssh/id_rsa IP.OF.YOUR.ANDROID

(ANDROID) If you still have trouble logging on navigate to file and activate these two key pairs: /data/data/com.termux/files/usr/etc/ssh/sshd_config

ChallengeResponseAuthentication no
PubkeyAuthentication yes

Extra: Locking and unlocking the file system from read-only to read-write and vice-versa.

How to copy files to an Android emulator’s data directory with ‘adb push’ https://alvinalexander.com/android/how-copy-files-android-device-emulator-adb-push-read-only-error

mount -o rw,remount rootfs /

Revert the changes back after finishing the changes!

cat /proc/mounts
mount -o ro,remount rootfs /
0

As an alternative way, I build termux app and then adb shell to access termux console from PC without requiring root.

The prefix below of [PC]~ $, [ADB]~ $ and [ADB's BASH]~ $ indicates where the command type from.

  1. In Android settings, enable Developer options, toggle on USB debugging, and uninstall existing termux app for all users. And of course, the existing termux data will loss.
  2. Clone termux source code, [PC]~ $ git clone https://github.com/termux/termux-app
  3. Open that project in Android Studio, plug-in phone cable, select phone device from menu then direct run or build it as debug apk which allow you use run-as without root.
  4. [PC]~ $ adb shell
  5. [ADB]~ $ run-as com.termux
  6. [ADB]~ $ cd files/usr/bin
  7. If not yet install bash and vim, either go to Android device to install, or [ADB]~ $ export PATH=/data/data/com.termux/files/usr/bin:$PATH first then pkg install bash and pkg install vim.
  8. ./bash to launch bash shell session.
  9. [ADB's BASH]~ $ vim ~/.bashrc and put this 2 lines which set environment when launch new bash session: PATH=/data/data/com.termux/files/usr/bin:$PATH and export LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
  10. [ADB's BASH]~ $ . ~/.bashrc to take effect immediately.
  11. Use rsync command, e.g. [ADB's BASH]~ $ rsync -rvtP --remove-source-files source_dir foo@IP:/home/foo/Downloads/ to transfer file between Android and PC. (Note: Source directory don't trailing with /, and need find source_dir -type d -empty -delete after done to remove empty directories) . Alternatively use [PC]~ $ adb exec-out run-as com.termux cat '/data/user/0/com.termux/.bashrc' > ~/Downloads/.bashrc to pull file from Android to PC.
  12. To quit, Ctrl+D or [ADB's BASH]~ $ exit. [ADB]~ $ ./bash to re-enter new bash session.
  13. For unknown reason, first time access will no internet, you need launch termux app in android device manually once, after that can exit termux app.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .