Connecting to Android with SSH
Running an SSH server with Termux
Secure Shell Protocol (SSH) is a way to securely access a server for remote login or command execution. I recently wanted to use rsync for copying data to my Android phone. Since Android is based on Linux, it’s very easy to use standard Linux tools on your device.
To access your device with SSH, you need to run an SSH server. There are many ways to do this on Android, but the easiest way I found was running OpenSSH through Termux.
Termux is an Android application that provides a terminal emulator and many Linux tools. You can use it without root access and it lets you install and run many familiar Linux packages.
The easiest way to get Termux is from F-Droid. You can either install the F-Droid app and search for Termux, or just download/install the Termux APK directly from the F-Droid catalogue.
Once you have Termux installed, open it up, and you will be at a Bash command prompt.
Update and allow storage access
You should start by updating the package list:
1
$ pkg update
Next, grant Termux storage permissions so it can access your filesystem:
1
$ termux-setup-storage
(you will get a popup asking you to allow access)
Find your IP and username
You will need to know your device’s IP address and username to connect to it. The following commands will give you that information:
1
2
$ ifconfig
$ whoami
(make sure you are connected to your network via WiFi so you are getting a LAN IP)
Install OpenSSH and set a password
Next, install the OpenSSH server:
1
$ pkg install openssh
If you don’t want to mess with SSH keys, you can just set a password for connections:
1
$ passwd
Now, you can run the SSH server:
1
$ sshd
(it listens on port 8022 by default)
Connecting to your device with SSH
At this point, an SSH server is running that you connect to from any device on your network running an SSH client.
For example, on my Linux laptop, I can simply run:
1
ssh <username>@<ip> -p <port>
For example:
1
ssh u0_a142@10.0.0.200 -p 8022
After verifying the host identity and entering my password, I am remotely logged into the Android device and at a Bash shell prompt.
That’s it!