Guide

SSH dynamic port forwarding (ssh -D)

Dynamic port forwarding turns the SSH client into a SOCKS proxy: one local port, and each application connection names its own destination, which the SSH server then connects to.

Short answer

ssh -D [bind_address:]port user@server makes the SSH client listen on a local port and act as a SOCKS proxy. An application that uses the proxy names a destination for each connection; the request goes through the encrypted SSH connection and the SSH server connects to that destination. One listener reaches any TCP host the server can reach. Applications must be configured to use it.

The commands on this page are standard OpenSSH. Transputty, the Windows SSH tunnel manager this site belongs to, is in development with no public build yet; see the release status.

What is dynamic port forwarding?

A local forward has its destination fixed when the tunnel is created. That is fine for one database, and tedious for twenty internal web sites. Dynamic forwarding removes the fixed destination. It is the third type of SSH port forwarding, and the "dynamic" refers to the destination being decided at connection time.

browser --SOCKS--> 127.0.0.1:1080  ==SSH (encrypted)==>  bastion.example.com  -->  wiki.internal:443
curl ----SOCKS-->  (ssh listens)                           (sshd connects out)  -->  10.0.5.20:8080
                                                                                -->  any host:port it can reach

How does the mechanism work?

Each connection goes through four steps:

  1. An application connects to the local listener and starts a SOCKS handshake. In SOCKS5 it sends a CONNECT request containing the destination, either as an IP address or as a host name, and the port.
  2. The SSH client reads the request. It does not connect anywhere itself. It asks the SSH server to open a forwarding channel to that host and port, the same direct-tcpip channel type that a local forward uses.
  3. The SSH server resolves the name if one was given, makes the TCP connection, and reports success or failure. The SSH client passes the result to the application as the SOCKS reply.
  4. From then on the bytes are relayed in both directions until either side closes.

Two things follow from this design. First, the SOCKS protocol is spoken only between the application and the SSH client on your machine; the SSH server never sees SOCKS and needs no proxy software. Second, when the application sends a host name instead of an address, the lookup happens on the SSH server. Whether your application does that is a client setting, and it is the main subject of using an SSH SOCKS proxy.

According to the ssh manual, OpenSSH's listener supports the SOCKS4 and SOCKS5 protocols. It does not ask proxy clients for a username or password, which is one reason the listener is loopback-only by default.

The syntax

ssh -D [bind_address:]port user@server
Part Meaning
bind_address Optional. The local address to listen on. Left out, the listener follows the client's GatewayPorts setting, which means loopback by default
port The local port for the SOCKS listener. 1080 is conventional

The usual command for a proxy with no shell:

ssh -N -D 1080 deploy@bastion.example.com

-N means no remote command is run; stop the proxy with Ctrl+C. Add -o ExitOnForwardFailure=yes so ssh exits instead of continuing without a proxy when port 1080 is already taken. A quick test from another terminal:

curl --socks5-hostname 127.0.0.1:1080 https://example.com/

Binding the proxy to anything other than loopback (-D 0.0.0.0:1080, or -g) hands every machine that can reach the port an unauthenticated proxy into the server's network. Avoid it unless the network is yours and you mean to.

Only root can listen on ports below 1024 on Unix-like systems. -D can be combined with -L, -R and -J on one command line.

DynamicForward in ssh_config

Host office-proxy
    HostName bastion.example.com
    User deploy
    DynamicForward 1080
    ExitOnForwardFailure yes
    ServerAliveInterval 30

ssh -N office-proxy then starts the proxy. DynamicForward accepts the same [bind_address:]port form as -D, and can be given more than once.

How does -D differ from -L?

Local forward (-L) Dynamic forward (-D)
Listeners One per destination One for everything
Destination Fixed in the tunnel definition Sent by the application for each connection
What the application must support Nothing: it connects to a normal host and port The SOCKS protocol
Application configuration Change the server address to 127.0.0.1:port Set a SOCKS proxy; addresses stay the real ones
Host names and TLS certificates The application sees localhost, so certificate names do not match The application uses real names, so HTTPS and virtual hosts work normally
Destination name lookups Always by the SSH server By the SSH server if the application sends names; otherwise locally
Best for One database, one service, tools with no proxy support Web browsing, many hosts, APIs spread across a private network

A rule of thumb: if you can name the one host and port you need, use -L. If you find yourself adding a fourth -L, or the target is a web application, use -D.

What does the server need?

Very little:

  • AllowTcpForwarding must be yes, all or local. Dynamic forwards open the same outgoing channels as local forwards, so a server that permits -L permits -D.
  • PermitOpen, if set, restricts the destinations exactly as it does for local forwards. A server with PermitOpen db.internal:5432 will refuse everything else a SOCKS client asks for.
  • The server's own network position decides what is reachable. Its firewall rules, routes and DNS apply to every proxied connection, and destinations see the server's address as the source.

GatewayPorts in sshd_config is not involved; it concerns remote forwards.

Limits

  • TCP only. OpenSSH's SOCKS listener handles TCP connections. It does not relay UDP, so UDP-based protocols do not go through it. ICMP (ping) does not either.
  • Per-application. Nothing uses the proxy until it is told to. A browser may be configured while a command-line tool beside it still connects directly. Programs with no SOCKS support need a local forward or a wrapper tool.
  • Not a system VPN. There is no virtual network adapter and no route. See the tunnel-versus-VPN table in the port forwarding overview.
  • No proxy authentication. Whoever can connect to the listener can use it, which is why it should stay on loopback.
  • The server operator can see destinations. Proxied connections originate on the SSH server, so its administrator and its network can observe where they go.
  • It lasts as long as the SSH connection. When the connection drops, the listener goes away with it and applications get connection errors until the proxy is restarted. See keeping an SSH tunnel connected.

Performance notes

Dynamic forwarding performs like any other SSH forwarding, with a few characteristics worth understanding. No figures are given here because they depend entirely on the two networks and the machines involved.

  • Everything shares one TCP connection. A browser may open dozens of connections through the proxy; inside SSH each is a separate channel with its own flow control, but all of them ride the single client-to-server TCP connection. On a link that loses packets, one stalled segment delays every channel.
  • The path is longer. Traffic goes from you to the SSH server and then to the destination. If the server is far from you or from the destination, latency rises accordingly.
  • Throughput is bounded by the slower leg, and by the CPU cost of encryption at both ends, which is small on current hardware with current ciphers.
  • Compression (-C) is situational. It can help on slow links carrying compressible data, and it costs CPU for no gain on traffic that is already compressed or encrypted, which includes most web traffic.

Using the proxy

Setting up applications is its own topic: SOCKS5 versus SOCKS4, the socks5h scheme, Firefox and Chrome settings, curl, git, and how to confirm DNS is not leaking. All of that is in using an SSH SOCKS proxy, and a start-to-finish walk-through is in how to set up an SSH SOCKS proxy.

How Transputty handles dynamic forwards

Transputty is in development and has no public build yet. In the current internal build for Windows 11, a dynamic forward is a rule of kind "SOCKS (-D)". It has a listen address and port and no destination fields. A "SOCKS" preset creates one on 127.0.0.1:1080, and a SOCKS rule can sit beside local and remote rules on the same connection.

A rule of kind 'SOCKS (-D)' in the editor: a listen address and port, no destination fields. This one listens on 127.0.0.1:1081 beside a local and a remote rule on the same connection. The app is connected to local test servers with fictional host names.

The proxy speaks SOCKS5 and handles CONNECT requests. Host names that an application sends are passed to the SSH server unresolved, and the inspector labels the rule "SOCKS5 proxy · DNS resolved on the server". As with OpenSSH, that holds only when the application sends names. During a reconnect the SOCKS port stays bound, as described in automatic reconnection.

Limits, some of which differ from OpenSSH: SOCKS4 clients are not accepted, there is no UDP and no SOCKS BIND, the app offers no proxy username and password, and the listener is loopback-only.

Common questions

What does ssh -D 1080 do?

ssh -D 1080 makes the SSH client listen on port 1080 of your computer and behave as a SOCKS proxy. An application configured to use that proxy tells it which host and port it wants, the request travels through the SSH connection, and the SSH server opens the connection. One listener serves any number of destinations, with no need to list them in advance.

What is the difference between ssh -L and ssh -D?

A local forward (-L) ties one local port to one fixed destination, and any program can use it without knowing about proxies. A dynamic forward (-D) is one local port that reaches any destination, but the program must speak the SOCKS protocol to say where it wants to go. Use -L for a single database or service and -D for browsing or many hosts.

Does dynamic port forwarding need anything installed on the server?

No. The SOCKS proxy runs inside the SSH client on your computer. The server only receives ordinary forwarding requests, the same kind a local forward produces. It needs AllowTcpForwarding set to yes, all or local, and its PermitOpen setting, if the administrator has used it, must allow the destinations.

Is ssh -D a VPN?

No. ssh -D is a proxy for TCP connections made by applications you configure to use it. Programs that are not configured connect directly as before, UDP is not carried, and nothing changes in your routing table. It gives VPN-like reach to a browser or command-line tool, not to the whole operating system.

Which port should I use for ssh -D?

Any free port works; on Linux and macOS, ports below 1024 need root. Port 1080 is the port registered for SOCKS and is what most examples and tools assume, so it is the convenient choice. If something else already uses it, pick another, such as 1081, and enter that number in the application's proxy settings.

Sources and further reading

Transputty keeps SSH tunnels running on Windows

Local, remote and SOCKS5 forwards in one window, automatic reconnection with ports that stay bound, and a clear reason when something fails. In development; no public build yet.