What does OpenSSH's dynamic forwarding (ssh -D) provide, and how does it differ from a fixed -L forward?
answer
- the application names the destination
- SOCKS4 and SOCKS5, RFC 1928
- one channel per CONNECT
- where names get resolved
- packets need a tun device
basics
~10 sssh -D turns the client into a SOCKS4/SOCKS5 proxy: each application connection names its own destination, which the SSH server then connects to. A -L forward fixes one destination per listening port.
solid answer
~40 sWith `ssh -N -D 1080 [email protected]` the **ssh client** listens on local port 1080 and speaks SOCKS4 and SOCKS5 (RFC 1928). A browser set to use that proxy sends a CONNECT request naming the host and port it wants; `ssh` turns each request into a `direct-tcpip` channel, exactly like a `-L` connection, and `sshd` on the gateway connects onward. So `-D` is a `-L` whose destination is chosen per connection by the application, which is why one command can route a browser through the office network. If the application sends **host names** rather than addresses in its SOCKS5 requests, the names are resolved on the server's side. It carries TCP only; moving whole IP packets, UDP included, needs `-w` with tun devices and the server's `PermitTunnel`, which defaults to `no`.
code
bash · 5 lines# SOCKS proxy on the laptop's loopback port 1080, no remote shell
ssh -N -D 1080 [email protected]
# Layer 3 tun forwarding: tun0 on both ends; the server must set PermitTunnel
ssh -N -w 0:0 [email protected]go deeper
Remember that -D makes your own ssh client a SOCKS proxy on a local port, and that the application, not the command line, picks each destination.
Explain the path: SOCKS CONNECT, then a direct-tcpip channel, then sshd connecting onward. Be precise about when names are resolved remotely and why only TCP is carried.
Know the limits in production: TCP only, the SOCKS port's bind address, the server-side switches that cover -D, and what -w with PermitTunnel costs compared with a real VPN.
Judge when ad-hoc SOCKS tunnels are acceptable access paths and when the need for routed, audited, multi-user access argues for a VPN design instead.
## From one destination to any destination A local forward (`ssh -L 15432:db.internal.example:5432 ...`) binds one local port to **one** destination chosen when you start `ssh`. That is fine for a single database, but a browser that should reach dozens of internal sites through the office network would need a forward per site, and the pages would still contain links to the real names. **Dynamic forwarding** removes the fixed destination. `ssh -D` opens a listening port on the client and runs a **SOCKS server** on it; the OpenSSH manual page says SOCKS4 and SOCKS5 are supported. SOCKS is a small proxy protocol (SOCKS5 is RFC 1928, conventionally on TCP port 1080) in which the *application* tells the proxy where it wants to go before any data flows. ```bash ssh -N -D 1080 [email protected] ``` The browser is then configured with a SOCKS5 proxy at `localhost:1080`. ## How a SOCKS request becomes an SSH channel 1. The browser connects to `localhost:1080` and negotiates a SOCKS method. 2. It sends a **CONNECT** request whose destination is either an IP address or a domain name (`ATYP` X'03' in RFC 1928) plus a port. 3. The `ssh` client reads that destination and opens a **`direct-tcpip`** channel (RFC 4254 section 7.2) naming it, the same channel type a `-L` connection uses. 4. `sshd` on the gateway applies its forwarding rules, connects to the destination from its own network, and the bytes flow. Because every `-D` connection is a `direct-tcpip` channel, the server cannot tell it apart from a `-L` connection. `AllowTcpForwarding local` (from the client's perspective) and `PermitOpen` govern both, and `AllowTcpForwarding no` stops both. ## Name resolution: where DNS happens The answer depends on the application: - If it sends a **domain name** in the SOCKS5 request, `ssh` passes the name in the channel open and the **server** resolves it. Internal names that only the office DNS knows then work, and the laptop's local resolver never sees the lookups. - If it resolves the name **locally** and sends an address, the lookup has already happened on the laptop's network. Internal-only names fail, and the queries leak to the local resolver. Most SOCKS-capable applications have a setting that chooses between the two; "the tunnel resolves DNS" is true only when the application sends names. ## What -D does not carry - **TCP only.** Each connection becomes a `direct-tcpip` channel, which is a TCP stream; UDP and ICMP traffic from the application is not carried. - **Applications that do not speak SOCKS** cannot use it unless something wraps them. - **No address on the remote network** for the laptop: the far side sees connections from the gateway, not from a routed client. The listener is bound like a local forward: loopback by default, the client's `GatewayPorts` (or `-g`) or an explicit bind address to widen it. An open SOCKS port on a shared network lets anyone there use your tunnel. ## When you need packets: -w and tun devices OpenSSH can also forward whole **IP packets** (layer 3) or **Ethernet frames** (layer 2) between `tun` devices with `ssh -w local_tun[:remote_tun]`. It uses OpenSSH's `[email protected]` channel type and carries any IP protocol, UDP and ICMP included. It costs more: - the server must allow it with `PermitTunnel` (`yes`, `point-to-point`, `ethernet`); the default is `no`; - the user needs permission to open the tun device on both ends, which in practice usually means root; - you assign the interface addresses and routes yourself; SSH only moves packets between the two devices; - every packet rides inside one TCP connection, so loss on the outer connection delays everything inside it. | | `-L` | `-D` | `-w` | |---|---|---|---| | Destination | fixed at start | chosen per connection by the application | any address routed into the tun device | | Carries | TCP | TCP | IP packets or Ethernet frames | | Channel | `direct-tcpip` | `direct-tcpip` | `[email protected]` | | Server switch | `AllowTcpForwarding`, `PermitOpen` | the same | `PermitTunnel` (default `no`) | | Client privilege | none for high ports | none for high ports | tun device access | All three are **ad-hoc** tools for one person and one session. Designing a site-to-site or remote-access VPN is a different subject, with its own gateways, addressing and routing.
- What does `ssh -R 1080 [email protected]`, with only a port, do?It is the reverse of `-D`. With no explicit destination, OpenSSH's manual says `ssh` acts as a SOCKS4/5 proxy for remote clients: `sshd` listens on port 1080 on the server, and each connection there is carried back to the client, which connects to whatever destination the SOCKS request names from the client's network.
- Can an sshd administrator tell a `-D` connection from a `-L` one?Not from the protocol. Both arrive as `direct-tcpip` channel opens naming a host and port, so `AllowTcpForwarding` (the `local` value covers both) and `PermitOpen` treat them identically. The only difference is how many distinct destinations one client asks for over time.
- Thinks sshd on the server runs the SOCKS proxy for -D.
- Believes -D always resolves DNS names on the far side.
- Claims -D carries UDP because SOCKS5 defines UDP ASSOCIATE.
- Says -w works out of the box like -L and -D.
- Calls -D a VPN that gives the laptop an address on the remote network.