Skip to content
Happy Programming Guide
Start learning
Coding Tools

Remote Access to a Linux Server Behind a Firewall

Reach a machine with no inbound ports open: reverse SSH tunnels, autossh, Tailscale and Cloudflare Tunnel, with the security settings each needs.

Teck cables entering a 18×36" junction box

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.

Output
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:

Terminal
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:

Terminal
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:

Output
GatewayPorts clientspecified
Terminal
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:

Terminal
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:

Output
[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
Terminal
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:

Output
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.

Terminal
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:

Terminal
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.

Terminal
# 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:

Terminal
cloudflared tunnel login
cloudflared tunnel create home
cloudflared tunnel route dns home app.example.com
Output
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
Terminal
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:

Output
[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#

Output
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers youruser
MaxAuthTries 3
ClientAliveInterval 300
Terminal
sudo sshd -t                    # check the config before applying
sudo systemctl reload sshd

Generate a modern key if you have not:

Terminal
ssh-keygen -t ed25519 -C "you@example.com"
ssh-copy-id user@server

And install fail2ban if anything is exposed to the internet:

Terminal
sudo apt install fail2ban
sudo systemctl enable --now fail2ban

Useful SSH client configuration#

Output
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:

Output
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 foundation for all of thisRead next

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *