9

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?

12

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.

  • 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 '16 at 17:23
  • Why can't I connect from a remote client with ssh on a smartphone that has sshd termux and is connected to internet with an ip (which I get from curl ifconfig.me) on port 8022. Is the problem related to the internet provider, that there are many clients on one ip? – user3123159 Dec 20 '17 at 13:40
  • 3
    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. – MAChitgarha Jun 4 at 21:29
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
3

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 your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS

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 your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS:~/.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 your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS "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 your_desktop_ssh_user@YOUR.DESKTOP.IP.ADDRESS

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 /
2

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 user@192.168.1.20).

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 user@192.168.1.20

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 user@192.168.1.20:/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

  • 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. – Michael Hughes Jul 21 '18 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.