Skip to content

Instantly share code, notes, and snippets.

@Youngv
Forked from devmaars/Termux SSH Tutorial.md
Created April 23, 2025 06:01
Show Gist options
  • Save Youngv/d5c3e6ddd6899527187242dad3b6d51a to your computer and use it in GitHub Desktop.
Save Youngv/d5c3e6ddd6899527187242dad3b6d51a to your computer and use it in GitHub Desktop.
This guide will show you how to set up an ssh server on your phone using Termux and connect to it from your computer. This is useful if you want to access your phone's files or run commands on your phone from your computer.

How to ssh to termux the right way

Termux

Requirements

  • Termux (From F-Droid)
  • ADB (Android Debug Bridge)
  • USB Cable (Wireless connection is also possible but it's slower)

Steps

  1. Install ssh server in Termux by running pkg update && pkg install openssh.

  2. Start the ssh server by running sshd. also you can run run termux-wake-lock to keep the server running in the background.

  3. Set a password for the user by running passwd.

  4. Get your username by running whoami.

  5. On your computer make sure your phone is visible to ADB by running adb devices. If it's not visible, you may need to enable USB debugging in Developer Options.

  6. On your computer forward the ssh port to your computer by running adb forward tcp:8022 tcp:8022. (ssh uses port 22 by default, but you can use any port you want)

  7. On your computer, install ssh by running sudo apt install openssh-client (for Ubuntu/Debian) or sudo pacman -S openssh (for Arch/Manjaro).

  8. Generate a key pair by running ssh-keygen -t rsa you can press enter to use the default location and no passphrase.

  9. Copy the public key to the phone by running ssh-copy-id -p 8022 <username>@localhost. Replace <username> with the username you got in step 4. you will be asked for the password you set in step 3.

  10. Connect to the ssh server by running ssh -p 8022 <username>@localhost.

11.Bonus: You can create a ssh config file to make it easier to connect to the server. Create a file called ~/.ssh/config with the following content:

Host termux
    HostName localhost
    Port 8022
    User <username>

Now you can connect to the server by running ssh termux. note with this technique you will be required to forward the port every time you connect your phone to the computer every time you disconnect the phone from the computer.

we can automate this process by creating a udev rule that forwards the port every time the phone is connected to the computer.

  1. Create a file called /etc/udev/rules.d/99-android.rules with the following content:
SUBSYSTEM=="usb", ATTR{idVendor}=="<your phone's vendor id>", ATTR{idProduct}=="<your phone's product id>", RUN+="/usr/bin/adb forward tcp:8022 tcp:8022"

Replace <your phone's vendor id> and <your phone's product id> with the vendor id and product id of your phone. You can get these values by running lsusb and looking for your phone.

  1. Reload the udev rules by running sudo udevadm control --reload-rules.

  2. Unplug and plug your phone back in.

  3. Now you can connect to the server by running ssh termux every time you connect your phone to the computer.

Or if you want a simpler solution you can create this script under ./local/bin/termux-prepare-ssh.sh

#!/usr/bin/env bash

DEFAULT_PORT=8022

KEYEVENT_HOME=3
KEYEVENT_ENTER=66

adb shell am start -n com.termux/.app.TermuxActivity
sleep 1

adb shell input text "sshd"
adb shell input keyevent $KEYEVENT_ENTER

adb shell input text "termux-wake-lock"
adb shell input keyevent $KEYEVENT_ENTER

adb shell input keyevent $KEYEVENT_HOME

adb forward tcp:$DEFAULT_PORT tcp:$DEFAULT_PORT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment