A Reverse SSH tunnel is a networking method for accessing a machine behind a NAT router or restrictive firewall by establishing an outbound connection to a remote server that forwards incoming traffic back to the client. Instead of an external host initiating an inbound connection to a private network, the internal client opens the SSH session and instructs the remote server to bind a listening port and relay traffic through that encrypted channel. This setup allows administrators, developers, and automated systems to reach private hosts without modifying local router configurations or requesting a public IP address.
How it works
Standard SSH port forwarding pushes traffic from your local machine to a remote destination. A reverse tunnel flips that direction. When the private client runs the ssh -R command, it authenticates with a publicly reachable intermediary server—often called a bastion host or jump server—and asks the SSH daemon to open a listening socket on a specified remote port.
Once the socket is active, any TCP connection arriving at that port on the public server is encapsulated inside the existing outbound SSH session and routed back to a target host and port accessible from the client. Because firewalls and NAT gateways almost always allow outbound TCP traffic on port 22 or 443, the connection passes through without requiring inbound port forwarding rules on intermediate routers. To prevent unattended prompts from blocking automated connections, administrators rely on non-interactive key pairs as covered in SSH Key Configuration: A Professional Guide to Secure Access.
Example
Suppose you are developing a web application on a local workstation running on port 3000 and need to expose it temporarily to a teammate or webhook provider via a public VPS. You can initiate the tunnel from the local workstation:
ssh -N -R 8080:localhost:3000 [email protected]
In this command, -N tells SSH not to execute a remote shell, while -R 8080:localhost:3000 binds port 8080 on the public server (203.0.113.10) and forwards incoming requests to localhost:3000 on your workstation. For long-lived deployments such as remote IoT fleets or unattended servers, wrapping the command in autossh ensures the tunnel automatically reconnects if the network drops.
What matters when choosing a server or hosting
The intermediary VPS acts as the public entry point for every tunneled session, so its network configuration and reliability directly affect usability:
- Dedicated IPv4/IPv6 addressing: Ensure the hosting plan provides a static public IP address so client reconnection scripts do not break after a reboot.
- Full SSH daemon control: Managed shared hosting rarely permits custom
sshd_configedits. Choose a VPS or cloud instance with root access so you can enableGatewayPortswhen external clients need to reach the forwarded port directly. - Low latency and predictable bandwidth: Because all traffic hairpins through the relay server, hosting the VPS geographically close to both the client and the end users minimizes round-trip latency.
- Firewall flexibility: A cloud firewall or host-level packet filter lets you restrict the exposed tunnel port to trusted source IP addresses rather than leaving it open to the entire internet.
Common mistakes
- Leaving GatewayPorts disabled: By default, OpenSSH binds remote forwarded ports to the server's loopback interface (
127.0.0.1). External traffic will be refused unless you setGatewayPorts yesorGatewayPorts clientspecifiedin/etc/ssh/sshd_configand reload the service. - Using password authentication in background services: Automated tunnels managed by systemd or
autosshwill hang or fail silently if the server prompts for a password. Always configure dedicated key pairs following SSH Key Configuration: A Professional Guide to Secure Access. - Ignoring TCP keepalive settings: Stateful NAT tables frequently drop idle connections. Without
ServerAliveIntervalconfigured on the client, a tunnel can appear connected while silently dropping packets.
FAQ
Is a reverse SSH tunnel secure?
Yes, the traffic between the client and the relay server is encrypted using standard SSH protocols. However, if you bind the forwarded port to a public interface using GatewayPorts, anyone who can reach that port on the server can interact with your local service, so you should enforce firewall rules or application-level authentication.
How does a reverse SSH tunnel differ from a VPN?
A VPN operates at the network or link layer, routing broad IP subnets between devices. A reverse SSH tunnel forwards specific TCP ports over a single user-space SSH connection, making it lighter to deploy when you only need to expose one or two local services.
Why does my tunnel stay open in the process list after disconnecting?
If a network drop occurs without a clean TCP reset, the server may keep the old port locked until a timeout expires. Enabling ClientAliveInterval on the server and ExitOnForwardFailure=yes on the client prevents stale port bindings from blocking reconnections.