The Linux Page

Creating and sharing a public key from Termux on Android

How to connect to a server from your Termux terminal

I found that good documentation from Termux, about Remote Access. It mentions [S]FTP and SSH. The main problem to use SSH is that you need to have access to the public key and... how do you copy the public key from your Termux terminal to your server?

What I've found is that you can use ssh-copy-id with a local VPS on the same network. When I'm at home, I have my phone on a LAN IP address and I can access my VPS servers... at least those that allow for a password login.

So what I've done is something like this:

1. Make sure that your VPS SSH server is still allowing password login

vim /etc/ssh/sshd_config

Then search for the PasswordAuthentication parameter and make sure it says "yes" (if commented out, that's the default):

PasswordAuthentication yes

2. Determine the IP address of that VPS server

ip address

You can also run that command in Termux to make sure both computers are on the same network.

3. Copy the Key

Use the ssh-copy-id command like so:

ssh-copy-id -i .ssh/my_secret_key me@vps

The -i option allows you to select which key you want to copy. If you used the default one, then there is no need to specify the key. Also, you can use cd first if you prefer to avoid typing the path.

The me part is your username on the destination computer. My Termux gives me a user name such as u1_123, so not something I'd have on my VPS.

The VPS is the IP address of your VPS (i.e. 192.168.0.2). If you somehow have a DNS, you could enter the computer name. I don't go that far on my LAN, though... (not yet)

4. Verify Copy

To make sure that you did in deed copy your new key, you can either check the .ssh/authorized_keys (see point 5. below to do so), or probably even better, try to connect with SSH. However, connecting with SSH should as you for your key passphrase, not your user password. If you used the same for both, it is slightly harder to distinguish the difference. When the SSH key fails, the message you end up with is:

me@vps's password:_

This means the remote SSH server is asking you for your remote account password. In other words, your key failed. If you have a passphrase, you entered it first and then SSH also asks you for your password, again, the key failed (i.e. you need to enter your passphrase to unlock your key, then SSH can test with your key, and finally if that fails, the SSH server can revert back to asking your for your password).

5. Retrieve the Key

Log in your VPS and get the key by editing or using cat like:

cat .ssh/authorized_keys

Assuming it was the very first time you used the ssh-copy-id command, the Termux public key is going to be the last line in that file. Otherwise you can quickly compare the keys using the following in Termux:

cat .ssh/my_secret_key.pub

Then you can find the key you wanted in the authorized_keys file.

Copy that key on the server(s) you want to be able to access with SSH.