Skip to main content

Setting up SSH on Termux

Entering commands with the Android touch screen can be a pain. You can either connect a keyboard or setup SSH to remotely connect from a computer instead. This also allows you transfer files to your device using a SFTP client.

Enabling SSH

  1. Open Termux and make sure your packages are up to date: pkg update
  2. Install SSHD: pkg install openssh
  3. Set a password: passwd
  4. Get your username: whoami
  5. Get your ip address: ifconfig
    On most wifi devices, it should be the inet value under wlan0.
  6. Start sshd: sshd
    If you get a "no hostkeys available" error, run ssh-keygen -A and then try sshd again.

Connecting to SSH

On your computer use a ssh client to connect on port 8022. Windows 11, Linux, and Mac computers should be able to use the ssh command in their terminal/command line: ssh username@ipaddress -p 8022. Windows 10 and below may need to install an SSH client such as PuTTY or Windows Terminal.

Keeping SSHD Running

SSHD will remain running in Termux so long as the app remains running, even in the background. You'll see it in the Android notification area if it's running (with the >_ icon) and in the Android task switcher. If the app closes completely, you will need to re-run sshd to start it again, but there are a few ways around this.

Option 1: Enable Wakelock

On the notification for Termux in Android's notification area, there's a button called Acquire Wakelock. Enabling Wakelock prevents Android from entering "deep sleep" however this also can be a drain on the battery when the device is not in use. You can toggle this on and off as needed, but Android might still shut down the app after wakelock is disabled, requiring you to restart SSHD later. The button in the notification will change to Release Wakelock when wakelock is enabled.

You can also use the commands termux-wake-lock and termux-wake-unlock to turn it on and off.

Personally I choose not use wakelock as I often forget to disable it and end up draining too much battery while the device is in my bag or pocket.

Option 2: Run SSHD on Termux start

This method will automatically run the SSHD command when you start Termux. This is my preferred method as it only takes starting the app to get it running when I need it.

  1. Start Termux (or login to SSH) and create a ~/.bashrc script file with nano (or whatever text editor you prefer): nano ~/.bashrc
  2. Add the SSHD command: sshd
    You can also add the termux-wake-lock command if you want to enable wakelock too.
  3. Press Ctrl+X to exit, Y to save the file, and Enter to confirm the file name,

Option 3: Run SSHD on Android boot using Termux:Boot

This is similar to Option 2, but runs SSHD (and optionally enables wakelock) when Android boots up.

  1. Download Termux:Boot from F-Droid.
  2. Run the Termux:Boot app (separate from the main Termux app) once.
  3. Start Termux (or login to SSH) and create the ~/.termux/boot directory: mkdir ~/.termux/boot
  4. Create a script file in the new directory with nano (or whatever text editor you prefer): nano ~/.termux/boot/sshd
    Add the commands (enabling wakelock is optional).
    #!/data/data/com.termux/files/usr/bin/sh
    termux-wake-lock
    sshd
  5. Press Ctrl+X to exit, Y to save the file, and Enter to confirm the file name,