Skip to content

Instantly share code, notes, and snippets.

@ethanc8
Created January 9, 2024 03:11
Show Gist options
  • Save ethanc8/e0a251a7f4a8e2c6ff5467375c7d7af3 to your computer and use it in GitHub Desktop.
Save ethanc8/e0a251a7f4a8e2c6ff5467375c7d7af3 to your computer and use it in GitHub Desktop.
Quick tip: How to SSH between your Linux laptop and your Android phone using Termux

Quick tip: How to SSH between your Linux laptop and your Android phone using Termux

If you've ever wanted to write code on your Android phone, or work on files on your laptop while you're standing around somewhere you can't pull out your laptop, this is the way to do it!

Set up

On your Android phone, please install F-Droid. You'll need to download the APK and open it. If it asks you whether you want to install an "unknown app", allow it. Then, in F-Droid, search for "Termux" and install the "Termux" app.

Alternatively, you can download the Termux APK by itself, but it won't auto-update.

Now, you're ready to follow with this guide. Please follow this guide on a secure network, as it will temporarily leave your devices open to network attacks. Once you've finished the setup process, it will be safe to use on insecure networks.

Please do not ever share the files id_rsa, id_rsa.pub, or the directory ~/.ssh with anyone. This will create a security risk.

SSH from GNU/Linux to Termux

  1. In Termux:
pkg up
pkg install openssh
passwd # set a password
ssh-keygen -A
sshd
ifconfig
# Use the output of ifconfig to find the IP address
  1. In GNU/Linux:
# Change this to the IP address of the Android phone
androidip=143.195.81.101
ssh-keygen -t rsa -b 2048 -f id_rsa
ssh-copy-id -p 8022 -i id_rsa $androidip
cp id_rsa ~/.ssh/id_rsa
cp id_rsa.pub ~/.ssh/id_rsa.pub

If you add a passphrase, then you need to use it every time you ssh. Otherwise, you'll be able to log in without a passphrase.

  1. In Termux:
sed -i "s/PasswordAuthentication yes/PasswordAuthentication no/" $PREFIX/etc/ssh/sshd_config
pkill sshd; sshd

SSH from Termux to GNU/Linux

  1. In GNU/Linux:
sudo apt install openssh-server
sudo systemctl enable ssh
hostname -i
# Use the output of ifconfig to find the IP address
  1. In Termux:
# Change this to the IP address of the GNU/Linux desktop
desktopip=143.195.87.67
ssh-keygen -t rsa -b 2048 -f id_rsa
ssh-copy-id -p 22 -i id_rsa ethan@$desktopip
cp id_rsa ~/.ssh/id_rsa
cp id_rsa.pub ~/.ssh/id_rsa.pub

If you add a passphrase, then you need to use it every time you ssh. Otherwise, you'll be able to log in without a passphrase.

  1. In GNU/Linux:
sudo sed -i "s/PasswordAuthentication yes/PasswordAuthentication no/" /etc/ssh/sshd_config
sudo systemctl restart ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment