Candid photograph of a Linux server terminal with a soft olive-green glow against a dark slate background, conveying a calm technical atmosphere.

Step-by-step guides for system administrators — covering command-line basics, web server setup, and preparation material for technical interviews.

Browse Tutorials

Reverse SSH Tunnels for Remote Access on Linux Servers

Remote access to a machine at the edge of a network is bread-and-butter work for system administrators, but the direct route is often blocked. Australian home NBN connections typically sit behind carrier-grade NAT, so port forwarding on the router accomplishes nothing until you buy a static IP from Telstra or Optus. Field gear on a 4G link in the Pilbara may have an address nobody can route to at all.

A reverse SSH tunnel flips the problem around. Instead of waiting for an inbound connection, the internal machine dials out to a public host you control and asks that host to listen on your behalf. This guide builds one from scratch on RHEL, CentOS, Fedora and Debian-family systems, covering the relay host, the systemd unit, and the checks that confirm traffic is genuinely flowing.

When a reverse tunnel earns its keep

Most direct SSH attempts fail for one of three reasons: no public address, no inbound firewall path, or a NAT device that simply will not forward. A reverse tunnel removes all three, because the only network requirement is an outbound SSH connection from the private side, which firewalls and corporate networks allow by default.

For a tradie running a small server in a warehouse outside Adelaide, or an ops team patching a control box at a mine site, the tunnel to a VPS in Sydney or Melbourne is often the only workable option. A small instance costs a few tens of dollars a month, which is cheaper than the phone calls involved in getting a static IP negotiated with a network team.

It also suits temporary work. Expose a Grafana dashboard or a Jenkins instance on a high port, hand the link to a colleague, then close the tunnel. Nothing needs to be permanently exposed, and no firewall rule survives the session.

How the reverse forward actually works

Local port forwarding uses -L, and reverse forwarding uses -R. With -R, the SSH daemon on the public host opens a listening socket, and every connection arriving at that socket is carried back down the existing encrypted session to a target on the private machine. The port looks public on the relay, yet the service actually lives inside your own network.

The path runs from the private client to sshd on the VPS, then through the SSH client back to the target service. Because the traffic rides an authenticated SSH session, it is encrypted end to end and inherits the security of the account that established it.

The trade-off is that the relay must be configured to permit forwarding, and anyone who can reach that public port can reach the service. Binding to loopback on the relay and adding a second authentication layer closes most of that gap.

Preparing the public relay

Choose a VPS with a fixed address in an Australian region. Sydney and Melbourne are the usual picks; pick the one with lower round-trip latency to your own location rather than to Sydney by default, since a Brisbane admin will see less jitter against a Melbourne host than a Singapore one. Install openssh-server, create a dedicated unprivileged user, and place your public key in that user's authorized_keys with restrictive permissions.

On the relay, sshd_config needs AllowTcpForwarding yes. Add GatewayPorts clientspecified only when you genuinely intend to bind to a non-loopback address, since the default of no restricts the listener to 127.0.0.1. Set PermitRootLogin no and turn off password authentication before exposing the host. Restart the daemon and confirm the configuration with sshd -t before reloading.

If you plan to put a status page or a small landing page in front of the relay, terminate TLS there. The web server tutorials at Linuxtpoint's web server category walk through Nginx server blocks and Let's Encrypt certificates, which is a tidy way to give the tunnel a memorable hostname instead of a bare port number.

Opening the tunnel from the private host

Test it by hand first so you can see errors in the foreground:

ssh -N -R 2222:localhost:22 tunnel@relay.example.au

Leave that running in a second terminal, then connect with ssh -p 2222 you@relay.example.au. On the relay, ss -tlnp | grep 2222 should show the listener. If it does not appear, check the server log for remote port forwarding failed for listen port.

Once it works, wrap it in autossh so a flaky link heals itself, then move it into a systemd unit with Restart=always and RestartSec=10. Keep the key in a file with chmod 600, and pass -i explicitly rather than relying on an agent that may not survive a reboot.

Teams juggling several sites often want a central view of which tunnels are up and a way to generate the connection strings and key pairs without hand-editing units. This piece on automated tunnel management describes that pattern of auto-generated configuration, which suits a fleet of remote boxes more than a single hand-run command.

Keeping the link alive and locked down

Persistence has two halves. On the client, autossh plus ServerAliveInterval 30 and ServerAliveCountMax 3 detects a dead session and rebuilds it. On the relay, enabling ClientAliveInterval and TCP keepalives stops half-open connections from piling up in the socket table. A systemd unit with Restart=always covers the case where the tunnel process itself is killed.

Security matters more here than on a normal server, because the relay is public by design. Use key-only authentication, disable password login, and add restrict or permitopen entries to authorized_keys so a compromised relay-side key cannot pivot further into your internal network. Binding to 127.0.0.1 on the relay and reaching the service through a second SSH hop limits exposure considerably.

The Australian Signals Directorate's Essential Eight guidance pushes regular patching, multi-factor authentication and application allow-listing, which applies cleanly to this setup: patch the relay's SSH daemon, protect your own accounts with MFA, and allow-list only the services that need forwarding. For the wider hardening picture, the Linux administration tutorials cover setting up fail2ban, key rotation and audit logging in reasonable detail.

Checking a tunnel that misbehaves

When traffic stalls, work through it in order rather than changing three things at once:

A few habits keep a tunnel healthy over months:

The practical takeaway is that a reverse tunnel gives you a public path into a private network without touching the router, and the whole arrangement reduces to two things worth getting right: an outbound-only SSH connection managed by autossh and systemd, and a relay host that binds to loopback, accepts keys only, and forwards nothing beyond the ports you explicitly allow. Get those two right and you have a reliable way into a home lab, a field server or a client site that no carrier-grade NAT can hide.