Skip to content
Back to Series SSH from Scratch
Networking

SSH Tunneling and Port Forwarding: Unlocking an Arbitrary Network

Use SSH -L, -R, and -D to forward ports securely: tunnel to internal databases, reach services in reverse, and set up dynamic SOCKS tunnels.

A
Alif Afriza Author
· · 4 min read
SSH Tunneling and Port Forwarding: Unlocking an Arbitrary Network

SSH tunneling is one of the main reasons SSH is called “an arbitrary network”. With a single command, you can reach services that should be closed off and safely bypass network restrictions. This is an advanced capability in our curriculum.

Basic Concepts

Port forwarding (or tunneling) forwards traffic from one port to another over an already encrypted SSH connection. The benefit: data travels securely, and services located on a private network can be reached without exposing them to the internet.

SSH has three main types of forwarding: local, remote, and dynamic.

Local Forwarding (-L)

Local forwarding directs a port on the local machine to a destination on the server side. The classic example: a database server (say MySQL) only listens on localhost on an internal server, and you want to access it from your laptop.

ssh -L 3306:localhost:3306 pengguna@server

On the local machine, port 3306 is now connected to localhost:3306 on the server. An application using that port appears to be running locally, even though the data travels over the SSH connection.

Common syntax: -L <port-lokal>:<host-tujuan>:<port-tujuan>.

Remote Forwarding (-R)

Remote forwarding goes the other way: a port on the server is forwarded to a service on the local machine. Useful for services running on a laptop or lab machine that you want to reach from an internet-facing server:

ssh -R 8080:localhost:3000 pengguna@server

Connections to server:8080 are forwarded to localhost:3000 on the local machine. This is commonly used when you need to expose a development app to a server for shared testing.

Dynamic Forwarding (-D) and SOCKS

Dynamic forwarding turns SSH into a SOCKS proxy: a browser or other tool that points its traffic at that port will forward it through the server, without needing to know the exact destination.

ssh -D 1080 pengguna@server

Set the browser to use SOCKS5 localhost:1080, and then all browsing requests go through the server. This creates a “lightweight VPN”: internet traffic appears to originate from the server and is encrypted in transit.

Jump Host: Hopping Past the Gateway

When a server can only be reached through a single gateway point (a bastion or jump host), you can forward through two hops at once:

ssh -J bastion pengguna@server-internal -L 8080:localhost:8080

The -J option (ProxyJump) is supported by modern clients. It can even be chained in sequence: -J a,b,c. This is covered in more depth in the jump host article.

Securing the Tunnel: Required Configuration

The hardening in the previous article turns off AllowTcpForwarding no. If you use tunnels, enable it:

AllowTcpForwarding yes

Remember: every tunnel is another path to a service. Keep the AllowUsers list narrow, so that tunnels can only be used by people who are allowed in.

Debugging Tunnels

When a tunnel doesn’t work, check in this order:

  1. Did the SSH connection succeed? (a failed hop is the clearest indication)
  2. Is the destination port open? Check from the server: ss -tlnp | grep 3306.
  3. Is the destination host being interpreted from the right location? A common mistake: localhost in a tunnel is interpreted from the server, not from the laptop.

Wrapping Up

SSH tunnels turn a single ordinary connection into an encrypted highway to internal services, SOCKS proxies, and access across firewalls. The key is to memorize the three types: local, remote, dynamic, plus jump host.

Up next in the curriculum: secure file transfers with scp, sftp, and rsync over ssh.

References

  • Man page ssh(1), sections -L, -R, -D, -J (OpenSSH).
  • Man page ssh_config(5), sections LocalForward, RemoteForward, DynamicForward, ProxyJump.
  • RFC 4254, The Secure Shell (SSH) Connection Protocol, rfc-editor.org/rfc/rfc4254 (channels and forwarding).
  • SSH, The Secure Shell: The Definitive Guide, O’Reilly Media (tunneling chapter).
Open for Contributors

Your ideas deserve to be read by thousands of people.

ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.

Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.

Discussion & Comments