- Termux (From F-Droid)
- ADB (Android Debug Bridge)
- USB Cable (Wireless connection is also possible but it's slower)
-
Install ssh server in Termux by running
pkg update && pkg install openssh
. -
Start the ssh server by running
sshd
. also you can run runtermux-wake-lock
to keep the server running in the background. -
Set a password for the user by running
passwd
. -
Get your username by running
whoami
. -
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. -
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) -
On your computer, install ssh by running
sudo apt install openssh-client
(for Ubuntu/Debian) orsudo pacman -S openssh
(for Arch/Manjaro). -
Generate a key pair by running
ssh-keygen -t rsa
you can press enter to use the default location and no passphrase. -
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. -
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.
- 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.
-
Reload the udev rules by running
sudo udevadm control --reload-rules
. -
Unplug and plug your phone back in.
-
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