An SSH login can work perfectly while the service you wanted to reach remains unavailable. That’s because port forwarding adds another connection beyond SSH itself—and the destination is reached from a specific side of the tunnel.

The quickest way to understand the three common forwarding options is to ask: Where does the listener run, and which machine connects to the destination?

Local forwarding: -L

Use local forwarding when your computer needs to access a service reachable from the SSH server, such as a private database or an internal dashboard.

ssh -N -L 127.0.0.1:15432:db.internal:5432 user@bastion 

This creates a listener on your computer at 127.0.0.1:15432. When a client connects to it, SSH carries the connection to the server, and the server connects to db.internal:5432.

For example, configure a database client to connect to 127.0.0.1:15432. The database does not need to be exposed directly to your laptop or the public internet; it only needs to be reachable from bastion.

The command’s destination hostname is resolved from the server side. If the server cannot reach or resolve db.internal, the tunnel can establish but database connections will still fail.

Remote forwarding: -R

Use remote forwarding when a machine you control can connect outbound to an SSH server, but the server needs to reach a service running on that machine.

ssh -N -R 19090:127.0.0.1:3000 user@ssh-server 

Here, the SSH server listens on port 19090. Connections to that listener travel back through SSH, and the SSH client connects to 127.0.0.1:3000 on its own machine. If you start a development server locally on port 3000, you can test the forwarded service from a shell on the SSH server:

curl -I http://127.0.0.1:19090/ 

The 127.0.0.1 in the -R destination refers to the SSH client’s loopback interface, not the server’s. This is a common source of confusion: with -L, the server connects to the destination; with -R, the client does.

Remote forwards normally listen on the server’s loopback interface by default. That means a successful -R does not automatically make your local service public. Avoid changing SSH server settings to create a wildcard listener unless broader access is genuinely required and appropriately controlled.

Dynamic forwarding: -D

Dynamic forwarding creates a local SOCKS proxy. Instead of specifying one destination in the SSH command, a SOCKS-aware application selects a destination for each connection.

ssh -N -D 127.0.0.1:11080 user@ssh-server 

Configure an application to use a SOCKS proxy at 127.0.0.1:11080. The SSH server then makes the connection to the requested destination. For curl, this sends the hostname through the proxy for resolution on the server side:

curl --socks5-hostname 127.0.0.1:11080 http://internal.service/ 

Dynamic forwarding is not a full-device VPN. It applies to applications configured to use the SOCKS proxy; other application traffic continues to use its normal network route.

Keep the security boundary in mind

These commands bind their listeners to 127.0.0.1, keeping them local to the machine where each listener runs. That’s a sensible default: binding a SOCKS proxy or remote forward to a broadly reachable interface can give other machines access to services through your SSH connection.

SSH encrypts traffic between the SSH client and server. It does not necessarily encrypt the separate connection from the server to the destination service, and it does not add authentication to that service. Keep the destination’s own security controls in place.

The SSH server can also restrict forwarding. Options such as AllowTcpForwarding, PermitOpen, and PermitListen may limit whether a forward is allowed and which destinations or listening ports it can use. If you’re setting up a new server, check that SSH is enabled and reachable first.

Debug a tunnel one hop at a time

A tunnel involves several separate checks: SSH authentication, creation of the listening socket, connectivity from the forwarding endpoint to the destination, and the application’s response. A working SSH login confirms only the first part.

For a local forward, test the destination from the SSH server’s network. For a remote forward, test the target from the SSH client’s machine. For SOCKS, confirm that the application is actually using SOCKS; when hostname resolution needs to happen through the SSH server, use an option such as curl --socks5-hostname.

A few common symptoms point to useful checks:

  • bind: Address already in use: another process is using the requested listening port. Choose a different port or identify the existing listener.
  • Remote forwarding fails to start: the remote port may already be occupied, or server policy may prohibit the forward.
  • The tunnel starts, but the request fails: check that the destination service is running and reachable from the correct side of the tunnel.
  • The reverse tunnel works on the server but not another computer: the server-side listener may be loopback-only, as expected by default.

Use ssh -vv for more detail about connection and forwarding setup. Debug output can include hostnames, usernames, and endpoints, so redact sensitive information before sharing it. If SSH itself cannot reach the server, diagnose that connection separately; these SSH connection-refused checks can help distinguish a tunnel problem from an unavailable SSH service.

Quick mental model

  • -L: your machine listens; the SSH server connects to the destination.
  • -R: the SSH server listens; your machine connects to the destination.
  • -D: your machine listens as a SOCKS proxy; the server connects to destinations requested by SOCKS-aware applications.

Once you know where the listener lives and which side makes the onward connection, choosing a forwarding mode—and troubleshooting a failed tunnel—gets much easier.

I originally published a more detailed version of this guide on the ShadeTerm blog.

I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.