How to Set Up a Reverse SSH Tunnel from Android to Access Your Home Network (No Root)

18 min read Learn step‑by‑step how to create a persistent reverse SSH tunnel from an Android phone using Termux, enabling secure access to home services without rooting the device. October 12, 2026 23:30 How to Set Up a Reverse SSH Tunnel from Android to Access Your Home Network (No Root)

Imagine you are travelling and need to reach a web interface that only exists on your home network – a NAS, a home automation dashboard, or a development server. A reverse SSH tunnel lets your Android phone act as a bridge, pulling a connection from your home server back to the phone, so you can reach those internal services from anywhere. This tutorial walks you through the entire process using only free, open‑source tools that run in Termux, without requiring root access.

Before You Start

  • Android version: 7.0 (Nougat) or newer. Newer versions have better battery‑optimisation controls.
  • Termux: Install the official app from F-Droid or the Play Store.
  • Remote server: A Linux box at home with a public IP or a dynamic DNS name, SSH port (default 22) reachable from the internet.
  • Network: Both your phone and the home server need outbound internet access. Wi‑Fi is preferred for stability.
  • Battery: Keep the phone plugged in or ensure the battery is >50 % for the initial setup.
  • Optional but recommended: Install Termux:Boot (free add‑on) to start the tunnel automatically after a reboot.
Why a reverse tunnel? A normal forward tunnel requires the client (your phone) to initiate a connection to the server each time you need access. With a reverse tunnel, the server initiates the connection to the phone once, and you can then connect to the server through the phone’s open port, bypassing NAT and firewall restrictions on the home side.

Step 1 – Install Termux and Required Packages

  1. Open the Play Store or F-Droid and install Termux.
  2. Launch Termux. The first run will download additional data – allow it to complete.
  3. Update package lists and upgrade existing packages:
    pkg update && pkg upgrade -y
  4. Install OpenSSH and autossh (for auto‑reconnect):
    pkg install openssh autossh -y
  5. Optional – install termux-api if you want to control the tunnel via Android intents later:
    pkg install termux-api -y

These packages are pure user‑space binaries, so no root is required. autossh monitors the tunnel and restarts it automatically if the connection drops.

Step 2 – Generate an SSH Key Pair on Android

  1. In Termux, create a hidden .ssh directory:
    mkdir -p ~/.ssh && chmod 700 ~/.ssh
  2. Generate a new RSA key (4096 bits is a good balance of security and speed):
    ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -N ""

    Leave the passphrase empty for automated scripts, but protect the private key with file permissions (600).

  3. Verify the key files exist:
    ls -l ~/.ssh

The public key is stored in ~/.ssh/id_rsa.pub. You will copy this to the home server so it can authenticate the phone without a password.

Step 3 – Add the Phone’s Public Key to Your Home Server

  1. From a computer that can SSH into your home server, run:
    ssh user@your-home-server "mkdir -p ~/.ssh && chmod 700 ~/.ssh"
  2. Copy the public key from the phone to the server. The easiest way is to use scp from Termux:
    scp ~/.ssh/id_rsa.pub user@your-home-server:~/phone_key.pub
  3. On the server, append the key to ~/.ssh/authorized_keys and set correct permissions:
    cat ~/phone_key.pub >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    rm ~/phone_key.pub
  4. Test password‑less login from the phone:
    ssh -i ~/.ssh/id_rsa user@your-home-server "echo connected"

    You should see connected without being prompted for a password.

If the connection fails, double‑check that the server’s sshd_config permits PubkeyAuthentication yes and that the authorized_keys file is owned by the user.

Step 4 – Create the Reverse SSH Tunnel Command

A reverse tunnel forwards a port on the remote server (REMOTE_PORT) to a port on the Android device (LOCAL_PORT). For example, to expose the phone’s local web server (port 8080) as localhost:9000 on the home server, use:

ssh -N -R 9000:127.0.0.1:8080 -i ~/.ssh/id_rsa user@your-home-server

Explanation of flags:

  • -N – do not execute a remote command; we only want port forwarding.
  • -R 9000:127.0.0.1:8080 – on the remote side, bind port 9000 to the phone’s 127.0.0.1:8080.
  • -i ~/.ssh/id_rsa – use the private key we generated.

Replace 9000 with any unused port on the home server, and 8080 with the local service you want to expose (e.g., a local web UI, an SSH daemon on the phone, etc.).

Step 5 – Keep the Tunnel Alive with autossh

Network interruptions are inevitable. autossh watches the tunnel and restarts it automatically. The basic command looks like this:

autossh -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" \
  -R 9000:127.0.0.1:8080 -i ~/.ssh/id_rsa user@your-home-server

Key options:

  • -M 0 disables the monitoring port (simpler for mobile use).
  • ServerAliveInterval 30 sends a keep‑alive packet every 30 seconds.
  • ServerAliveCountMax 3 tells SSH to consider the connection dead after three missed keep‑alives.

Save this command in a script file so you can launch it easily.

  1. Create a directory for scripts:
    mkdir -p ~/scripts
  2. Create start_reverse_tunnel.sh with the following content:
    #!/data/data/com.termux/files/usr/bin/bash
    
    # Adjust variables as needed
    REMOTE_USER="user"
    REMOTE_HOST="your-home-server"
    REMOTE_PORT=9000
    LOCAL_PORT=8080
    SSH_KEY="$HOME/.ssh/id_rsa"
    
    autossh -M 0 -N \
      -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" \
      -R ${REMOTE_PORT}:127.0.0.1:${LOCAL_PORT} \
      -i "${SSH_KEY}" ${REMOTE_USER}@${REMOTE_HOST}
    

    Make the script executable:

    chmod +x ~/scripts/start_reverse_tunnel.sh

Run the script with ~/scripts/start_reverse_tunnel.sh. You should see no output – the tunnel runs in the foreground. Press Ctrl+C to stop it.

Step 6 – Run the Tunnel in the Background

To keep the tunnel running after you close the Termux session, use nohup or Termux’s built‑in termux-wake-lock to prevent the device from sleeping.

  1. Install termux-wake-lock (it is part of the Termux app, just call it):
    termux-wake-lock
  2. Start the script in the background with nohup:
    nohup ~/scripts/start_reverse_tunnel.sh > ~/tunnel.log 2>&1 &
  3. Verify it is running:
    ps -ef | grep autossh

The tunnel.log file will contain any error messages. If you ever need to stop the tunnel, find the process ID (PID) from the ps output and kill it:

kill PID

Step 7 – Automate the Tunnel at Boot with Termux:Boot

Manually starting the tunnel each time is tedious. Termux:Boot runs scripts placed in ~/.termux/boot whenever the device finishes booting.

  1. Install the free Termux:Boot add‑on from F-Droid or Play Store.
  2. Create the boot directory:
    mkdir -p ~/.termux/boot
  3. Copy (or symlink) the tunnel script into the boot folder:
    ln -s ~/scripts/start_reverse_tunnel.sh ~/.termux/boot/start_reverse_tunnel.sh
  4. Make sure the script is still executable (the symlink inherits permissions):
    chmod +x ~/.termux/boot/start_reverse_tunnel.sh
  5. Reboot the phone and check the log file (~/tunnel.log) to confirm the tunnel started.

If the tunnel does not start, open Termux, run termux-wake-lock manually, and inspect tunnel.log for error messages. Common reasons are missing network connectivity at boot or battery‑optimisation killing Termux.

Step 8 – Verify the Tunnel from Your Home Server

From a computer on your home network (or via SSH into the server), test the forwarded port:

curl http://localhost:9000

If the Android phone is running a local web service on port 8080, you should see the HTML output. For a generic test, you can also use nc -zv localhost 9000 to check if the port is open.

Step 9 – Use the Tunnel to Reach Home Services

Now that the tunnel is stable, you can access any service that runs on the phone (or any other device reachable from the phone) by connecting to localhost:REMOTE_PORT on the home server. Some practical examples:

  • Local web UI: Expose a home‑assistant dashboard that is bound to 127.0.0.1:8123 on the phone, then reach it from anywhere via http://your-home-server:9000.
  • SSH into the phone: Forward the phone’s SSH daemon (if you installed one) with -R 2222:127.0.0.1:22. Then from any remote machine: ssh -p 2222 user@your-home-server.
  • File sharing: Run a tiny HTTP file server on Android (python -m http.server 8080) and fetch files through the reverse tunnel.

Troubleshooting Common Issues

1. Authentication Fails

  • Make sure the public key on the server matches the private key on the phone (ssh-keygen -lf ~/.ssh/id_rsa vs. ssh-keygen -lf ~/.ssh/authorized_keys).
  • Check /etc/ssh/sshd_config for PubkeyAuthentication yes and AuthorizedKeysFile .ssh/authorized_keys.
  • Ensure the .ssh directory and authorized_keys file have permissions 700 and 600 respectively.

2. Remote Port Already In Use

If the server reports “address already in use”, pick a different REMOTE_PORT (e.g., 9010). You can also list used ports with ss -ltnp on the server.

3. Tunnel Drops After a Few Minutes

  • Android’s battery‑optimisation may kill the Termux process. Open Settings → Apps → Termux → Battery → “Allow background activity”.
  • Use termux-wake-lock as shown earlier to keep the CPU awake while the tunnel is active.
  • Verify that ServerAliveInterval and ServerAliveCountMax are set; they prevent idle disconnections.

4. No Network on Boot

Some devices enable Wi‑Fi after the Termux:Boot script runs. Add a short wait loop at the start of your script:

while ! ping -c1 -W1 8.8.8.8 >/dev/null; do
  sleep 5
done

This loops until the device can reach the internet.

5. “Bad file descriptor” Errors

These often appear when the remote side tries to bind to a privileged port (< 1024). Use ports >1024 for the reverse side unless you run the SSH daemon as root on the server.

Security Considerations

  • Protect the private key: The file ~/.ssh/id_rsa should be readable only by you (chmod 600). Do not share it.
  • Limit remote port exposure: Bind the reverse tunnel to 127.0.0.1 on the server (default) so only local processes can reach it. Avoid -R *:PORT:… unless you explicitly need remote access.
  • Firewall rules: On the home server, ensure that the forwarded port is not open to the internet unless you intend it. Use ufw deny 9000/tcp or similar.
  • Rotate keys periodically: Every few months, generate a new key pair and update authorized_keys.

Edge Cases & Advanced Tips

Multiple Android Devices

If you have several phones, give each a distinct REMOTE_PORT (e.g., 9000, 9001, 9002). You can also use a unique identifier in the SSH key comment to keep track of which key belongs to which device.

Dynamic Home IP

When your home network’s public IP changes, a dynamic DNS service (e.g., DuckDNS) keeps a constant hostname. Update the REMOTE_HOST variable in the script to the DNS name.

Using a Non‑Standard SSH Port

If your home router forwards SSH on a port other than 22, add -p PORT to the autossh command:

autossh -p 2222 -M 0 -N ...

Running a Service on the Phone Without a Dedicated App

You can start a temporary HTTP server directly from Termux:

python -m http.server 8080 --bind 127.0.0.1
Then the reverse tunnel will expose that server to the home side.

Alternative to autossh

If autossh is unavailable, you can wrap the SSH command in a simple while true loop with a short sleep. However, autossh handles edge cases like “broken pipe” more gracefully.

Wrapping Up

By following the steps above you now have a reliable reverse SSH tunnel that starts automatically on your Android device and stays alive across network hiccups. This setup gives you secure, on‑demand access to any service running on the phone—or any other device reachable from the phone—without ever rooting the hardware or exposing ports to the public internet.

Remember to keep your SSH keys safe, monitor battery‑optimisation settings, and adjust firewall rules on your home server. With those safeguards in place, the reverse tunnel becomes a powerful, low‑maintenance bridge between your mobile world and your home network.

User Comments (0)

Add Comment
We'll never share your email with anyone else.