>_ SSHDock
FeaturesHow it worksBlogGuidesAbout
Launch Terminal
Back to Journal

SSH port forwarding explained with real examples

AH
Ayan Hussain
Mar 31, 2026 · 6 min read

On this page

  • Local port forwarding
  • Remote port forwarding
  • Dynamic port forwarding (SOCKS proxy)
  • Keeping tunnels alive
  • Persistent tunnels with autossh
  • Forwarding through jump hosts
  • Troubleshooting common issues
  • Port forwarding from the browser

SSH port forwarding is one of those features most people learn about once and then forget, until they actually need to access a database behind a firewall or share a local development server with someone who is not on the same network. Then it becomes useful enough that you use it regularly.

There are three types. They do different things and people mix them up constantly.

Local port forwarding

Local forwarding takes traffic from a port on your local machine and sends it through the SSH connection to a destination the remote server can reach.

ssh -L 8080:localhost:3000 user@remote-server

Breaking down the syntax: -L local-port:destination-host:destination-port. The localhost in the middle refers to localhost as seen by the remote server, not your local machine.

This opens port 8080 on your local machine. Any traffic you send to localhost:8080 goes through the SSH tunnel to port 3000 on the remote server. Visit http://localhost:8080 in your browser and you see whatever is running on port 3000 on the remote machine, even though that port is not exposed to the internet.

A common use case: the remote server runs a web app or API in development mode that is not behind a public reverse proxy. You open the tunnel, test against localhost:8080, and close the tunnel when done.

Another use case is database access. This one is probably the most common application of local port forwarding:

ssh -L 5432:db.internal:5432 user@bastion-host

Here the destination is not localhost but db.internal, a hostname that the remote server can resolve but your machine cannot. The bastion host can reach the database on port 5432; your machine cannot. The tunnel makes it appear on your local port 5432. Your local database client (pgAdmin, TablePlus, DBeaver) connects to localhost:5432 and talks to the actual remote database through the tunnel.

This pattern keeps the database completely off the internet while still making it accessible for development and administration work.

Remote port forwarding

Remote forwarding is the reverse. It takes traffic arriving at a port on the remote server and forwards it to a destination your local machine can reach.

ssh -R 9090:localhost:3000 user@remote-server

Syntax: -R remote-port:destination-host:destination-port. This opens port 9090 on the remote server. Traffic arriving at port 9090 on the remote machine gets forwarded through the SSH connection to port 3000 on your local machine.

The use case: you are developing a web app locally on port 3000. You need to show it to a client, or test it with a webhook, or let a colleague review it. You SSH into a server with a public IP, set up remote forwarding, and suddenly your local dev server is accessible at http://remote-server:9090.

For this to work for connections from external clients (not just from the remote server itself), you need GatewayPorts yes in the server's /etc/ssh/sshd_config. Without this, the forwarded port only binds to 127.0.0.1 on the remote server, meaning only processes on that server can reach it.

sudo nano /etc/ssh/sshd_config
# Add or change:
GatewayPorts yes

Then reload: sudo systemctl reload sshd.

Remote forwarding is also the mechanism behind services like ngrok and Tailscale's funnel feature. Those services maintain a persistent SSH-like connection from their server to a client on your machine, and use remote forwarding to expose your local services. Understanding how remote forwarding works demystifies how those products operate.

Dynamic port forwarding (SOCKS proxy)

Dynamic forwarding creates a SOCKS proxy on your local machine that routes traffic through the SSH connection. Instead of forwarding a specific port to a specific destination, it lets the application decide where to connect.

ssh -D 1080 user@remote-server

This opens a SOCKS5 proxy on your local port 1080. Any application you configure to use this proxy will route its traffic through the remote server.

For a browser, go into the network settings and configure a SOCKS5 proxy pointing to 127.0.0.1:1080. All browser traffic will appear to come from the remote server's IP address. Useful for accessing geographically restricted content, for browsing from a trusted network when you are on an untrusted one, or for testing what your site looks like from a different region.

For system-wide routing, you can configure your OS network settings or use a tool like proxychains (Linux) to route specific applications through the SOCKS proxy.

Dynamic forwarding is less commonly known than local and remote forwarding, but it is the most flexible of the three. It essentially gives you a VPN-like capability using just SSH.

Keeping tunnels alive

By default, running an SSH tunnel command keeps your terminal occupied. When you close the terminal, the tunnel closes.

To run a tunnel in the background:

ssh -N -f -L 8080:localhost:3000 user@remote-server

-N tells SSH not to execute a remote command (you do not get a shell, just the tunnel). -f puts the process in the background. This is suitable for tunnels you set up and leave running for a work session.

To kill the tunnel when you are done:

# Find the SSH process
ps aux | grep ssh
# Kill it by PID
kill <PID>

Or more directly:

pkill -f "ssh -N -f -L 8080"

Persistent tunnels with autossh

If you depend on a tunnel daily, running it manually every time is error-prone. autossh monitors an SSH tunnel and restarts it if the connection drops:

autossh -N -L 5432:db.internal:5432 user@bastion-host

autossh keeps the tunnel running through network interruptions, sleep/wake cycles, and SSH server restarts. It is available through most package managers:

# Debian/Ubuntu
apt-get install autossh

# macOS
brew install autossh

For tunnels that need to survive reboots, wrap autossh in a systemd service. The unit file is straightforward and there are examples in the autossh documentation.

Forwarding through jump hosts

If the server you want to tunnel to is not directly accessible, you can chain port forwarding through a jump host. The modern way uses the -J flag:

ssh -J user@bastion -L 5432:db.internal:5432 user@private-server

This connects through bastion first, then establishes the tunnel from private-server. The local port 5432 on your machine ends up connected to db.internal:5432 as seen from private-server. The bastion is just an intermediate hop.

See our bastion host guide for more on setting up the network architecture.

Troubleshooting common issues

"channel 1: open failed: connect failed": The destination host or port is not reachable from the remote server. Check that the service is running on the destination and that the remote server can reach it. Test with telnet or nc from the remote server.

"bind: Address already in use": The local port you specified is already in use. Use a different local port, or find and kill the process using that port: lsof -i :8080.

Tunnel drops after idle period: The SSH connection is being timed out. Add ServerAliveInterval 60 and ServerAliveCountMax 3 to your SSH config or pass them with -o ServerAliveInterval=60.

Remote forwarding not accessible externally: You need GatewayPorts yes in sshd_config on the remote server.

Port forwarding from the browser

If you connect to your server through SSHDock, the jump host feature covers the common case of tunneling through a bastion to reach a private server. For local port forwarding specifically, browser clients have inherent limitations because browsers cannot bind arbitrary local ports. For complex tunnel setups, a native SSH client configured with ~/.ssh/config is more capable. For straightforward access to a server through a bastion, SSHDock handles it without any local configuration.

AH
Written by
Ayan Hussain

Full-Stack Developer and creator of SSHDock. Ayan builds browser-based developer tools and writes about SSH security, Linux server management, and modern web engineering.

About meGitHub

Keep reading

Sep 12, 2026

SSH config file: how to manage multiple servers cleanly

Stop typing long SSH commands. The ~/.ssh/config file lets you define aliases, set per-host options, and connect to any server with a two-word command.

Sep 12, 2026

How to copy files with rsync over SSH

rsync is the right tool for syncing directories, deploying websites, and backing up servers over SSH. Real examples for deployments, backups, and a reusable deploy script.

Sep 12, 2026

Git over SSH: how to use SSH with GitHub, GitLab, and Bitbucket

Set up SSH authentication for Git hosting services so you never type a password when pushing or pulling. Covers GitHub, GitLab, Bitbucket, multiple accounts, and Windows.

SSHDOCK

Full terminal in your browser. Local credential storage, jump host tunneling, live CPU/memory/disk metrics, and a mobile soft-key bar. Free to use. Built by developers, for developers.

© 2026 SSHDock

GitHub (@ayanhackspro)
App
Launch TerminalFeaturesHow it worksBlog & TutorialsChangelogGuides
Cloud Guides
AWS EC2DigitalOcean DropletGoogle Cloud Compute EngineMicrosoft Azure Virtual MachineLinode Compute InstanceVultr Cloud Compute
Legal & Contact
Privacy PolicyTerms of ServiceDisclaimerContact UsAbout Creator
Modern, minimal browser SSH client
TerminalSitemap