Creating a reliable SSH tunnel on Android does not require rooting, but you do need a few basic conditions met. Preparing correctly saves time and avoids data‑loss or connectivity surprises later.
Settings > Apps > Termux > Permissions > Storage.pkg update && pkg upgrade -y
pkg install openssh autossh curl -y
openssh provides ssh and ssh-keygen.autossh automatically restarts the tunnel if the connection drops.curl will be handy for quick connectivity checks.ssh -V
autossh -V
Both commands should print version information.
A key pair is the most secure way to authenticate to your remote server. You can either generate a fresh pair inside Termux or copy an existing private key.
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519 -C "android@$(hostname)"
-t ed25519 creates a modern, compact key.-a 100 sets a high number of KDF rounds, making brute‑force attacks harder.-f defines the file name; we keep it in ~/.ssh/ which is the default location.~/.ssh/id_ed25519 – private key (keep secret).~/.ssh/id_ed25519.pub – public key (share with the server).id_rsa) from your PC to the Android device. The safest way is via a USB cable and adb push:
adb push id_rsa /sdcard/Download/
mv /sdcard/Download/id_rsa ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub. Otherwise you can extract it:
ssh-keygen -y -f ~/.ssh/id_ed25519 > ~/.ssh/id_ed25519.pub
Without the server recognising your public key, the tunnel will never authenticate.
cat ~/.ssh/id_ed25519.pub
~/.ssh/authorized_keys for the user you intend to tunnel as:
echo "paste‑the‑key‑here" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.ssh
Test the password‑less login from Termux:
ssh -i ~/.ssh/id_ed25519 user@your.server.com -p 22
If you are prompted for a passphrase only (or nothing at all), the key setup succeeded.
autossh for a persistent tunnelautossh monitors the health of the SSH connection and restarts it automatically. This is essential on mobile devices where network changes are frequent.
Typical use‑cases:
localhost:8080.For this tutorial we’ll set up a local forward from the remote SSH server’s port 80 to Android’s localhost:8080. Adjust the numbers to suit your needs.
Storing the full command in a script makes it easy to start/stop the tunnel and to add environment variables later.
cat > ~/start_tunnel.sh <<'EOF'
#!/data/data/com.termux/files/usr/bin/bash
# Autossh wrapper – keep the tunnel alive
# Replace user@host and ports as needed
AUTOSSH_PORT=0 # disables autossh’s monitor port (uses default)
export AUTOSSH_GATETIME=30 # seconds before autossh starts monitoring
autossh -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" \
-L 8080:127.0.0.1:80 \
-i ~/.ssh/id_ed25519 user@your.server.com -p 22
EOF
chmod +x ~/start_tunnel.sh
Explanation of key flags:
-M 0 disables the separate monitoring port; we rely on the built‑in keep‑alive options.-N tells SSH not to execute a remote command – we only want port forwarding.-o "ServerAliveInterval 30" sends a keep‑alive packet every 30 seconds.-o "ServerAliveCountMax 3" aborts after three missed keep‑alives, prompting autossh to restart.-L 8080:127.0.0.1:80 forwards remote port 80 to local port 8080.Start the tunnel and detach it from the terminal so it survives after you close Termux:
nohup ~/start_tunnel.sh > ~/autossh.log 2>&1 &
Check that the process is running:
ps -ef | grep autossh
You should see a line containing autossh -M 0 -N …. The log file ~/autossh.log will contain any connection messages.
Assuming the remote server hosts a web page on port 80, you can now access it locally:
curl http://127.0.0.1:8080
If you receive the HTML of the remote site, the tunnel is functional. You can also open a browser on Android and navigate to http://127.0.0.1:8080.
For remote‑forward scenarios, try connecting from the remote machine to the Android‑exposed port using the same ssh -R syntax.
~/autossh.log for messages like “Permission denied”.netstat -tuln | grep 8080 on Android (install net-tools if needed) to confirm the local listener exists.ServerAliveInterval to 60 seconds or add -o "TCPKeepAlive yes".pkill autossh or by killing the specific PID.Settings > Battery > Battery optimization > All apps > Termux > Don’t optimize.nohup … & line to Termux’s ~/.bashrc or use the Termux:Boot add‑on, which runs scripts at boot.~/.ssh/id_ed25519 and generate a new pair.ssh-agent inside Termux to cache the decrypted key for the session.
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519
With the tunnel in place, you now have a secure bridge from your Android device to any remote service that accepts SSH connections. The same pattern can be adapted for database ports, VNC, or even SOCKS5 proxying by changing the -L or -D arguments.
Enjoy the flexibility of a persistent SSH tunnel—no root required, and fully under your control.









