When a Linux machine sits behind a firewall or a home router, you cannot connect inbound to it. The solution in every case is the same idea: have the machine make an outbound connection to somewhere you can reach, and travel back down it. This guide covers four ways to do that, from a plain SSH tunnel to a managed mesh network.
The idea#
A firewall blocks unsolicited inbound connections. It does not block a connection the machine itself starts — that is how it browses the web. So the remote machine connects out to something with a public address, and that connection becomes a two-way path.
Home machine ----outbound---> Public relay <---you connect here
(no open ports) (has a public address)
Option 1: a reverse SSH tunnel#
You need one machine with a public address — a cheap virtual server is enough. From the machine behind the firewall:
ssh -N -R 2222:localhost:22 relayuser@relay.example.com
Reading the flags: -R 2222:localhost:22 means “open port 2222 on the relay, and forward anything arriving there to port 22 on this machine”. -N means do not run a command, just hold the tunnel open.
Then, from anywhere:
ssh -p 2222 homeuser@relay.example.com
By default the relay only listens on its loopback address, so you would first SSH to the relay and then connect locally. To listen on all interfaces, set this on the relay:
GatewayPorts clientspecified
ssh -N -R 0.0.0.0:2222:localhost:22 relayuser@relay.example.com
Keeping it up with autossh#
A plain SSH tunnel dies with the network and does not come back. autossh monitors it and reconnects:
sudo apt install autossh
autossh -M 0 -N -R 2222:localhost:22 relayuser@relay.example.com \
-o "ServerAliveInterval 30" \
-o "ServerAliveCountMax 3" \
-o "ExitOnForwardFailure yes"
ExitOnForwardFailure matters: without it, a failed forward leaves autossh believing everything is fine while the tunnel is not actually working.
Make it a service so it survives reboots:
[Unit]
Description=Reverse SSH tunnel
After=network-online.target
Wants=network-online.target
[Service]
User=tunnel
ExecStart=/usr/bin/autossh -M 0 -N -R 2222:localhost:22 \
-o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" \
-o "ExitOnForwardFailure yes" -i /home/tunnel/.ssh/id_ed25519 \
relayuser@relay.example.com
Restart=always
RestartSec=15
[Install]
WantedBy=multi-user.target
sudo systemctl enable --now reverse-tunnel
sudo journalctl -u reverse-tunnel -f
Use a dedicated user with a key that has no passphrase and can do nothing else. On the relay, restrict what that key is allowed to do:
restrict,port-forwarding,command="/bin/false" ssh-ed25519 AAAA... tunnel@home
That line in the relay’s authorized_keys permits forwarding and nothing else — no shell, no file transfer.
Option 2: Tailscale#
For most people this is now the right answer. It builds an encrypted private network between your machines, works through firewalls without configuration, and requires no public server at all.
curl -fsSL https://tailscale.com/install.sh | sh
sudo tailscale up
Run the same on your laptop, sign in on both, and they can reach each other by name:
tailscale status
ssh user@homeserver
No ports opened, no relay to maintain, and connections are usually direct rather than through anyone’s servers. The free tier covers personal use comfortably.
# Restrict which machines can reach it
sudo tailscale up --shields-up
# Share one machine's subnet with the network
sudo tailscale up --advertise-routes=192.168.1.0/24
Headscale is a self-hosted alternative if you would rather not depend on a third party for coordination.
Option 3: Cloudflare Tunnel#
Suited to exposing a web service rather than getting a shell:
cloudflared tunnel login
cloudflared tunnel create home
cloudflared tunnel route dns home app.example.com
tunnel: 6ff42ae2-765d-4adf-8112-31c55c1551ef
credentials-file: /root/.cloudflared/6ff42ae2.json
ingress:
- hostname: app.example.com
service: http://localhost:8080
- service: http_status:404
cloudflared tunnel run home
Your service becomes reachable at a public hostname with a valid certificate, and no inbound port is open on your network. Add Cloudflare Access in front of it if it should not be public.
Option 4: WireGuard#
If you want the underlying technology without a coordination service, WireGuard is what Tailscale is built on. It requires one endpoint with a public address and manual key exchange:
[Interface]
PrivateKey = <home machine private key>
Address = 10.10.0.2/24
[Peer]
PublicKey = <relay public key>
Endpoint = relay.example.com:51820
AllowedIPs = 10.10.0.0/24
PersistentKeepalive = 25
PersistentKeepalive is what keeps the path through the firewall open from the inside. Without it the connection works until it goes idle and then silently stops.
Comparing them#
| Setup effort | Needs a public server | Best for | |
|---|---|---|---|
| Reverse SSH | moderate | yes | occasional shell access |
| autossh service | moderate | yes | permanent shell access |
| Tailscale | minimal | no | almost everything |
| Cloudflare Tunnel | low | no | exposing a web service |
| WireGuard | higher | yes | full control, no third party |
Harden SSH either way#
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers youruser
MaxAuthTries 3
ClientAliveInterval 300
sudo sshd -t # check the config before applying
sudo systemctl reload sshd
Generate a modern key if you have not:
ssh-keygen -t ed25519 -C "you@example.com"
ssh-copy-id user@server
And install fail2ban if anything is exposed to the internet:
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
Useful SSH client configuration#
Host home
HostName relay.example.com
Port 2222
User homeuser
IdentityFile ~/.ssh/id_ed25519
ServerAliveInterval 60
Host home-direct
HostName homeserver
User homeuser
Then ssh home is enough. Multiplexing makes repeated connections instant:
Host *
ControlMaster auto
ControlPath ~/.ssh/control-%r@%h:%p
ControlPersist 10m
Questions people ask#
Is a reverse tunnel secure?
The tunnel is encrypted SSH. The risk is what you expose at the other end. Bind the forwarded port to loopback, restrict the tunnel key to port forwarding only, and use key authentication throughout.
Why does my tunnel keep dropping?
Usually a firewall or router dropping an idle connection. Set ServerAliveInterval so traffic flows periodically, and use autossh to reconnect when it does drop anyway.
Can I do this without any server of my own?
Yes — Tailscale and Cloudflare Tunnel both provide the coordination, so you need nothing with a public address.
What about port forwarding on the router?
It works if you control the router and have a stable address, and it exposes a port to the whole internet. The approaches here avoid that, which is why they are generally preferred now.
Where to go next#
- Command line basics — the shell skills these assume.
- Deployment and packaging — what you do once you can reach the machine.
- Docker command not found — a common next obstacle on a fresh server.