June 17, 2025
Understanding SSH Local Tunnels

Understanding SSH Local Tunnels

Secure Shell (SSH) is widely used to securely access remote servers and networks, but it can do much more than simply providing terminal access. One of its powerful features is the ability to create local tunnels via the -L option. SSH local forwarding allows you to securely tunnel traffic from your local machine to a remote service on a server, bypassing firewalls and securing your connections.

This article will explain what SSH local tunneling is, how to use it with the ssh -L command, and provide examples to help you leverage this feature in your daily work.

What is SSH Local Tunneling?

SSH local tunneling involves forwarding a local port to a remote server or service through an encrypted SSH connection. Essentially, it creates a secure “tunnel” from your local machine to a remote destination, and any traffic sent to a specified local port is forwarded to a remote server or service.

This technique can be extremely useful in several scenarios:

  • Accessing remote services: You can connect to a database, web server, or any internal service behind a firewall or NAT (Network Address Translation).
  • Bypassing network restrictions: If you’re in a network that blocks certain ports, you can tunnel traffic over port 22 (SSH), which is typically open.
  • Securing insecure protocols: You can encrypt traffic from applications that don’t natively support encryption (e.g., HTTP, MySQL).

The basic syntax for setting up an SSH local tunnel is:

ssh -L [local_port]:[remote_host]:[remote_port] [username]@[remote_host]

Where:

  • local_port: The port on your local machine that you want to forward.
  • remote_host: The destination host or server that your traffic will be forwarded to.
  • remote_port: The port on the remote host to which the traffic will be forwarded.
  • username: The SSH username for the remote system.
  • remote_host: The hostname or IP address of the remote system

How SSH Local Forwarding Works

When you use SSH with the -L option, the traffic sent to a local port is encrypted and forwarded over the SSH connection. The SSH server receives the traffic and redirects it to the specified remote host and port.

The flow looks like this:

  1. Local application (e.g., a web browser or database client): You configure the application to connect to localhost:[local_port].
  2. SSH client: SSH forwards the connection from your local port to the remote server over the SSH connection.
  3. Remote server: The SSH server receives the traffic and forwards it to the specified remote_host and remote_port.

This ensures that all traffic between your local machine and the remote service is encrypted, even if the remote service itself doesn’t support encryption natively.

Practical Examples of SSH Local Tunneling

Example : Accessing a Remote Web Server

Suppose you need to access a web server running on a remote machine, but the server is hosted on port 8000, which is blocked by a firewall. In this case, you can use SSH tunneling to securely remap the traffic to a different port that is open and accessible.

## run python server on your public server (8000 by default) 
python3 -m http.server 

## on your local machine create a local tunnel
ssh -L 6000:localhost:8000  username@<pubilc ip-address/domain name> 

## Now on your local machine remote port 8000 should be accessible at localhost port  6000
curl http://localhost:6000/

  • 6000: The local port on your machine where the web server will be available.
  • localhost:8000: The target address on the remote machine (the web server is running on port 8000 on the remote host).
  • user@remote_host: Your SSH login credentials for the remote host.

Once the SSH connection is established, you can open a web browser and visit http://localhost:6000. The browser’s traffic will be securely tunneled to the remote machine’s web server running on port 80.

Considerations and Best Practices

  1. Security: Since the traffic is encrypted, local tunneling enhances security. However, be cautious when tunneling sensitive data through public or untrusted SSH servers. Ensure that the remote SSH server is secure and that you’re using strong SSH keys for authentication.
  2. Firewall Configurations: Some firewalls may restrict or monitor traffic tunneled through SSH. Always verify with your network administrator if SSH tunneling is allowed in your environment.
  3. Multiple Tunnels: You can set up multiple local tunnels by adding additional -L options to the SSH command:
ssh -L 8080:localhost:80 -L 3306:localhost:3306 user@remote_host

This will forward both the web and database ports over the same SSH connection.

4. Background SSH Tunnels: You can run SSH tunneling in the background by adding the -f option, which will cause SSH to go into the background before executing the command:

ssh -f -L 8080:localhost:80 user@remote_host -N

The -N option tells SSH not to execute any commands on the remote server, just to create the tunnel.

Conclusion

SSH local tunneling is a versatile tool that can help you secure and bypass network restrictions. Whether you’re accessing a remote web service, securing database connections, or bypassing firewalls, the ssh -L command offers a simple and powerful solution for routing traffic through an encrypted tunnel. By mastering SSH local forwarding, you can improve your workflows and protect your connections, especially when working with remote or insecure networks.

Leave a Reply

Your email address will not be published. Required fields are marked *