5 Essential Port Forwarding Techniques Every Developer Should Know
Port forwarding is one of the most powerful yet underutilized features of SSH. It allows you to create encrypted tunnels between your local machine and a remote server, enabling secure access to services that are otherwise locked behind firewalls or restricted networks. In this article, we cover five essential port forwarding techniques that every developer should have in their toolkit.
1. Local Port Forwarding
Local port forwarding lets you access a remote service as if it were running on your local machine. For example, if a database server on port 5432 is only accessible from the remote host, you can run ssh -L 5432:localhost:5432 user@remote to tunnel it to your local port 5432. This is invaluable for development workflows where you need to connect a local IDE or GUI tool to a remote database securely.
2. Remote Port Forwarding
Remote port forwarding works in reverse: it exposes a service running on your local machine to the remote server. This is useful when you want to share a local development server with a colleague or allow a remote server to call back to your local application. The command ssh -R 8080:localhost:3000 user@remote makes your local port 3000 available on the remote server's port 8080.
3. Dynamic Port Forwarding (SOCKS Proxy)
Dynamic port forwarding creates a SOCKS proxy on your local machine that routes all traffic through the SSH tunnel. Run ssh -D 1080 user@remote and configure your browser or application to use localhost:1080 as a SOCKS5 proxy. This is particularly useful for accessing geo-restricted content or browsing securely on untrusted networks.
4. Multi-hop Tunneling
Sometimes you need to reach a server that is only accessible through an intermediary host (a jump server or bastion host). SSH supports multi-hop connections using the -J flag: ssh -J user@bastion user@internal-server. You can chain multiple jump hosts together, and SSH handles the tunnel setup automatically. SSH Bridge makes configuring these multi-hop connections visual and straightforward.
5. Persistent Tunnels with Autossh
For tunnels that need to stay alive continuously, autossh monitors your SSH connection and automatically restarts it if it drops. Combined with systemd or launchd, you can create reliable, always-on tunnels for services like monitoring dashboards or webhook endpoints. This eliminates the frustration of manually reconnecting dropped tunnels during long-running operations.