SSH
SSH Tunnels & Port Forwarding: Local, Remote, and Dynamic Explained
SSH tunneling is one of the most useful but least understood features of OpenSSH. This guide explains local forwarding for accessing internal services, remote forwarding for exposing local services to remote networks, and dynamic SOCKS proxying for routing browser traffic through a server.
Local Port Forwarding: Accessing Private Services Securely
Local port forwarding creates a listening socket on your local machine that forwards traffic through the SSH connection to a destination on the remote network. The classic use case is accessing a PostgreSQL database that only accepts connections from `localhost` on the remote server.
The command `ssh -L 5432:localhost:5432 user@server` binds port 5432 on your local machine and forwards it to port 5432 on the remote server's localhost. Your local `psql -h localhost -p 5432` then connects through the tunnel as if it were running on the server itself.
For databases on a separate private subnet that the SSH host can reach but you cannot, use the target's private IP: `ssh -L 5432:10.0.1.50:5432 user@jumphost`. This routes traffic from your laptop through the jump host to the database server, all over a single encrypted SSH connection.
Remote Port Forwarding: Exposing Local Services
Remote port forwarding works in the opposite direction — it opens a port on the remote server and forwards connections back to a port on your local machine. This is useful for exposing a local webhook receiver to an external service during development without deploying to a staging server.
The command `ssh -R 8080:localhost:3000 user@server` opens port 8080 on the server and forwards any connection to it to your local port 3000. The remote service can now reach your locally running application at `http://localhost:8080` from the server's perspective.
Combining `GatewayPorts yes` in sshd_config on the server allows the remote-forwarded port to accept connections from external IPs (not just server localhost), effectively making your local service temporarily accessible from the internet.
Dynamic SOCKS Proxy for Full Network Routing
Dynamic forwarding creates a SOCKS5 proxy on your local machine that routes all traffic through the SSH connection and out from the remote server's network perspective. This is the full-featured alternative to a VPN for temporary access to private networks.
The command `ssh -D 1080 user@jumphost` creates a SOCKS5 proxy on local port 1080. Configure your browser or application to use `SOCKS5 localhost:1080` as the proxy. All traffic then exits from the remote server's network, allowing access to internal services as if you were on the internal network.
ShellMate tunnel manager plugin visualizes and manages all active local, remote, and dynamic forwarding rules as persistent named tunnels — eliminating the need to remember and re-type forwarding flags for every session.
Related ShellMate resources
Questions about SSH Tunnels & Port Forwarding
What is SSH local port forwarding?
Local port forwarding binds a port on your local machine and forwards connections through the SSH tunnel to a target host and port reachable from the remote server. It is commonly used to access private databases, Redis instances, and internal web services without a VPN.
What is the difference between local and remote SSH port forwarding?
Local forwarding (ssh -L) opens a port on your local machine and forwards to the remote side. Remote forwarding (ssh -R) opens a port on the remote server and forwards connections back to your local machine. Local forwarding is for accessing remote services; remote forwarding is for exposing local services.
Can I manage SSH tunnels without command-line flags?
Yes. ShellMate tunnel manager plugin lets you create, name, and persist SSH tunnel configurations alongside host profiles. Tunnels start automatically when you connect to a host, without requiring manual -L or -D flags.
Put the guide into practice
Organize hosts, choose a deliberate credential path, and keep SSH work visible in a focused desktop workspace.