Sub topic 12
SSH tunnels for single services
This chapter assumes you know basic IP addressing, the difference between TCP and UDP, and how to use a Linux command line. It also assumes you have read the earlier chapters on encapsulation and key exchange.
SSH is normally used to get a remote shell. It can also carry other TCP connections inside its encrypted channel. This gives you a tunnel for one service at a time, with no VPN software and no virtual network interface.
How SSH forwarding works
An SSH connection is authenticated and encrypted using the key exchange you saw earlier. SSH can open extra channels inside that one connection. Each channel carries one TCP stream.
Forwarding differs from the tunnels in earlier chapters. GRE, IPsec and WireGuard wrap IP packets. SSH forwarding wraps TCP byte streams. The SSH client listens on a port, accepts a TCP connection, and passes the bytes to the SSH server over the encrypted channel. The server then opens a new TCP connection to the real destination.
Because of this design, SSH forwarding carries only TCP. It does not carry UDP or ping. It also does not change your routing table.
Local forwarding
Local forwarding makes a remote service appear on a port of your own machine.
ssh -L 8080:intranet.example.com:80 [email protected]The parts of -L 8080:intranet.example.com:80 mean:
8080is the port the SSH client listens on, on your machine.intranet.example.com:80is the destination, as the bastion host sees it.[email protected]is the SSH server that makes the final connection.
After this, a browser pointed at http://localhost:8080 reaches the internal web server. The traffic is encrypted between your machine and the bastion. From the bastion to intranet.example.com it is a normal TCP connection, so it is not protected by SSH.
By default the client listens only on the loopback address, so other machines on your network cannot use the forward. Do not widen this with -L 0.0.0.0:8080:... unless you want to share the forward with others.
Remote forwarding
Remote forwarding goes the other way. It opens a port on the SSH server and sends connections back to your side.
ssh -R 9000:localhost:3000 [email protected]Anyone who can reach port 9000 on the bastion (by default only the bastion itself) is connected to port 3000 on your machine. This is useful for exposing a development server temporarily. It also lets a user open a path into a network from the inside, so administrators often restrict it with AllowTcpForwarding or GatewayPorts in sshd_config.
Dynamic forwarding as a SOCKS proxy
Dynamic forwarding avoids naming one destination in advance.
ssh -D 1080 [email protected]The client now acts as a SOCKS proxy on localhost:1080. An application configured to use it names the destination for each connection, and the bastion connects there. This behaves like a per-application full tunnel. If the application sends its DNS lookups through the proxy (for example, SOCKS5 with remote DNS), name resolution also happens on the bastion side. Otherwise DNS leaks out of your local network.
Comparing the three forms
| Option | Listens on | Connects to | Typical use |
|---|---|---|---|
| `-L` | Your machine | One fixed destination, via the server | Reach an internal web or database port |
| `-R` | The SSH server | One fixed destination, via your machine | Expose a local service |
| `-D` | Your machine | Any destination the application asks for | Proxy a browser through the server |
Limits and common errors
- Only TCP is carried. For UDP, such as DNS or VoIP, you need a real VPN.
- TCP inside TCP can behave badly on lossy links. Both layers retransmit, which causes delay. This is one reason SSH is a poor choice for a general VPN.
bind: Address already in usemeans another program already holds the local port. Choose a different one.- If a forward does nothing, check that the server permits it.
AllowTcpForwarding noblocks all forwards. - Add
-Nto open the tunnel without a shell, and-fto send it to the background.
Exercise
You run ssh -L 5433:db.internal:5432 [email protected]. On your laptop you then run psql -h localhost -p 5433. Which machine makes the TCP connection to port 5432 on db.internal, and is the segment between that machine and db.internal encrypted by SSH?
Show answer
The jump host (jump.example.com) opens the connection to db.internal:5432. SSH encrypts only the segment between your laptop and the jump host. The segment from the jump host to db.internal is an ordinary TCP connection, unless the database protocol itself uses TLS.