Skip to content
SumGuy's Ramblings
Go back

SSH Tunneling: A Secure Conduit for Your Data

SSH tunneling, also known as SSH port forwarding, is a powerful technique that leverages the Secure Shell (SSH) protocol to create encrypted tunnels over a network. These tunnels securely transmit data between different systems, protecting it from prying eyes and unauthorized access.

How SSH Tunneling Works

Common Use Cases

Local Port Forwarding:

ssh -L 8080:localhost:80 user@remote_server

Access a web server running on port 80 of the remote server by connecting to localhost:8080 on your local machine.

Remote Port Forwarding:

ssh -R 8888:localhost:80 user@remote_server

Make a web server running on your local machine accessible via port 8888 on the remote server.

Dynamic Port Forwarding (SOCKS Proxy):

ssh -D 1080 user@remote_server

Configure your browser or application to use localhost:1080 as a SOCKS proxy, routing all your traffic through the SSH tunnel.

Reverse Tunneling (-R)

# On the server behind the NAT
ssh -R 8000:localhost:80 user@public_server

This command establishes a reverse tunnel, making a web server running on port 80 of the server behind the NAT accessible on the public server at port 8000.

Multiple Tunnels

ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@remote_server

This opens two tunnels: one for a web server on port 80, accessible locally at 8080, and another for a MySQL database on port 3306, accessible locally at 3306.

ProxyJump (SSH Chaining)

ssh -J user1@jump_host1,user2@jump_host2 user3@final_destination

This command first connects to jump_host1 as user1, then uses that connection to connect to jump_host2 as user2, and finally uses that connection to connect to final_destination as user3.

GatewayPorts (Sharing the Tunnel)

ssh -g -L 8080:localhost:80 user@remote_server

ControlMaster and ControlPath

ssh -o ControlMaster=auto -o ControlPath=~/.ssh/control_%h_%p_%r user@remote_server

Subsequent connections to the same server will reuse this control socket, speeding up connection establishment.

Best Practices and Tips

Gotchas and Troubleshooting

Let me know int he comments below if you’d like a deeper dive into any of these aspects, or if you have specific scenarios you’d like me to illustrate with SSH tunneling examples!


Share this post on:

Previous Post
Docker vs Podman: Key Differences
Next Post
Socat: The Swiss Army Knife of Networking