With OpenSSH, what do local (-L) and remote (-R) port forwarding each do, and which side opens the listening port?
answer
- which end listens
- direct-tcpip versus tcpip-forward
- far end makes the onward connection
- loopback bind unless told otherwise
basics
~20 sLocal forwarding (-L) opens a listener on the client and carries each connection to a destination the SSH server connects to; remote forwarding (-R) makes the server listen and carries each connection back to a destination the client connects to.
solid answer
~40 sBoth carry TCP connections inside an existing SSH connection; they differ in which end listens. With `-L 15432:analytics-db.internal.example:5432` the **client** listens on port 15432, and every connection becomes a `direct-tcpip` channel (RFC 4254 section 7.2) asking the **server** to connect to the database, so that name is resolved and reached from the server's network. With `-R 8080:localhost:3000` the client sends a `tcpip-forward` global request (section 7.1); the **server** listens on 8080 and opens a `forwarded-tcpip` channel for each caller, which the client completes to its own port 3000. Both listeners bind to loopback by default, `-N` skips the remote shell and `-f` puts ssh in the background. Only the hop inside SSH is encrypted: the last leg from the far end to the destination is ordinary TCP.
code
bash · 5 lines# -L: the laptop listens on 127.0.0.1:15432; gw.example.com connects onward
ssh -N -L 15432:analytics-db.internal.example:5432 [email protected]
# -R: demo-vm.example.net listens on its loopback port 8080; the laptop connects onward
ssh -N -R 8080:localhost:3000 [email protected]go deeper
Recall the one rule: -L listens on your machine and the server connects onward; -R listens on the server and your machine connects onward. Be able to write both command lines.
Explain the RFC 4254 messages behind each direction: direct-tcpip for -L, the tcpip-forward request and forwarded-tcpip channels for -R, and why the destination is resolved at the far end.
Talk about what a forward exposes: the unencrypted last leg, loopback binds, and how AllowTcpForwarding, PermitOpen and PermitListen let a server owner decide which forwards exist at all.
Frame forwarding as a reachability decision: any account allowed to forward can reach whatever the server reaches, so weigh ad-hoc tunnels against brokered access paths with their own audit trail.
## One question decides everything: who listens SSH port forwarding carries ordinary **TCP connections** inside an SSH connection that is already authenticated and encrypted. The SSH connection protocol (RFC 4254) multiplexes many independent **channels** over that one connection, and each forwarded TCP connection gets a channel of its own. There are two directions, and the easiest way to keep them apart is to ask which end opens the listening socket: - **Local forwarding, `ssh -L`**: the **client** listens; the **server** makes the onward connection. - **Remote forwarding, `ssh -R`**: the **server** listens; the **client** makes the onward connection. "Local" and "remote" are named from the point of view of the `ssh` program you typed. ## Local forwarding: bring a remote service to your laptop A data engineer needs a private analytics database that only a gateway host can reach: ```bash ssh -N -L 15432:analytics-db.internal.example:5432 [email protected] ``` What happens: 1. `ssh` logs in to `gw.example.com` and opens a listening socket on the laptop, port 15432. 2. A database client on the laptop connects to `localhost:15432`. 3. `ssh` accepts it and sends `SSH_MSG_CHANNEL_OPEN` of type **`direct-tcpip`**, carrying the *host to connect* (`analytics-db.internal.example`) and the *port to connect* (5432), plus the originator's address. 4. `sshd` on the gateway decides whether to allow it, then opens a TCP connection to the database itself and relays bytes in both directions. The destination is resolved and connected **from the server**. That is why `-L 8080:localhost:80 [email protected]` reaches a web console bound to the *gateway's* loopback, not the laptop's. ## Remote forwarding: publish a laptop service on a far host For a demo, a webhook receiver on the laptop must be reachable on a cloud VM: ```bash ssh -N -R 8080:localhost:3000 [email protected] ``` Now the client sends `SSH_MSG_GLOBAL_REQUEST` **`tcpip-forward`** with an address and port to bind (RFC 4254 section 7.1). `sshd` on the VM opens the listener. Each connection to it makes the server send a **`forwarded-tcpip`** channel open back to the client, which connects to `localhost:3000` on the laptop. The client must reject a `forwarded-tcpip` open for a port it never asked to forward. A port of `0` asks the server to choose a free port and report it, and `cancel-tcpip-forward` tears the listener down. No inbound connection to the laptop is ever made: the laptop dialled out to the VM, and everything flows back through that connection. ## Side by side | | `-L` local | `-R` remote | |---|---|---| | Who listens | the `ssh` client | `sshd` on the server | | How it starts | nothing until a connection arrives, then `direct-tcpip` | `tcpip-forward` global request at login | | Per-connection channel | `direct-tcpip`, client to server | `forwarded-tcpip`, server to client | | Who connects onward | the server | the client | | Typical use | reach a private service from your laptop | expose a laptop service on a far host | | Default bind | client loopback | server loopback | ## Running a tunnel without a shell - **`-N`** asks for no remote command (the same as `SessionType none`), which is what you want for a forward-only connection. - **`-f`** sends `ssh` to the background just before command execution, after any password or passphrase prompt. - **`ExitOnForwardFailure yes`** makes `ssh` exit if it cannot set up a requested forward, for example because the port is taken; combined with `-f`, the client waits for remote forwards to be confirmed before backgrounding. It does not cover a later failure of the onward connection to the destination. Both listeners default to **loopback**. On the client, `GatewayPorts` in `ssh_config` (or `-g`) widens a local forward; on the server, `sshd`'s own `GatewayPorts` decides whether a remote forward may bind anywhere else. Binding a port below 1024 needs root on the side that listens. ## What the tunnel protects, and what it does not - The hop **between the two SSH endpoints** is encrypted and integrity-protected like the rest of the session. - The leg **from the far endpoint to the destination** is plain TCP. With `-L`, traffic from the gateway to the database crosses the internal network exactly as an unencrypted client would send it. - `sshd` sees and can restrict every forward: `AllowTcpForwarding` can allow `local`, `remote`, both or neither, and `PermitOpen` / `PermitListen` narrow the destinations and listeners. A server that allows forwarding without such limits therefore lets any user with a login reach whatever that server can reach. Static port mappings on a NAT router are a different mechanism: they rewrite packets at the network edge, while an SSH forward relays a connection through an authenticated session.
- In `ssh -L 8080:localhost:80 [email protected]`, whose localhost is meant?The SSH server's. The destination half of a `-L` specification travels in the `direct-tcpip` channel open and `sshd` connects to it, so `localhost:80` is the service on `gw.example.com`'s own loopback interface. That is a common way to reach an admin console that the server deliberately binds to loopback only.
- A `-L` forward starts cleanly, yet every connection to the local port is dropped at once. What is likely wrong?The listener is fine; the onward connection fails. Each connection opens a `direct-tcpip` channel, and `sshd` either refuses it (forwarding disabled, or a `PermitOpen` list without that destination) or cannot reach the host and port from its own network. `ExitOnForwardFailure` does not help here, because it only covers setting up the listener.
- How do you get the server to choose the port for a `-R` forward?Ask for port `0`, as in `-R 0:localhost:3000`. RFC 4254 has the server allocate a free unprivileged port and return it in `SSH_MSG_REQUEST_SUCCESS`; OpenSSH reports the allocated port at run time, and with `ssh -O forward` on a multiplexed connection it prints it to standard output.
- Says -L makes the remote server open the listening port.
- Thinks the -L destination is resolved and reached from the laptop.
- Claims an SSH tunnel encrypts traffic all the way to the destination service.
- Believes -R needs an inbound firewall opening towards the laptop.
- Assumes forwarded ports listen on every interface by default.