SSH Tunnel Explained: Secure Port Forwarding Made Simple

The Problem

If you've ever needed to connect from one machine to another while preserving its ports and data (or simply want to forward specific port), then you've likely wondered how to do this securely with SSH. This tutorial will guide you through the process of setting up a secure SSH tunnel, also known as an SSH port forwarding or SSH tunneling.

Why Use SSH Tunneling?

SSH provides a way to securely connect from one computer to another over a network connection. Using SSH for port forwarding allows you to forward traffic not only through the network but can also be performed across non-secure channels such as Telnet, which is typically insecure (and hence its name). This enables you to access remote systems or services on your local machine using a secure tunnel.

The Process

Step 1: Set Up SSH Tunneling

First, we need an SSH client configured with the correct host keys for our destination. We can do this by editing the known_hosts file and adding the public key of our target system:

# Edit ~/.ssh/known_hosts and add the public key of your target system (usually provided in ssh-agent)

Step 2: Forward Ports

Now, to forward a specific port on your local machine to a remote server. For example, if you want to access SSH from your local machine, type:

$ ssh -R 3000:localhost:22 target_server_ip_or_address

Step 3: Use Your Tunnel

Once set up, you can now access your target host via SSH by connecting directly from your local machine:

$ ssh -p 3000 user@target_server IP

Shell Commands Recap

Creating an SSH Tunnel

$ ssh -L 80:remote_host_ip_or_address:80 user@localhost

Accessing the Remote System via SSH

$ ssh -p 3000 user@target_server_ip_or_address

If you want to connect directly:

ssh -T user@localhost


## Quick Answer: An Example Scenario

You’re working in a local development environment, and you need access to production services that are not accessible via standard SSH. You can forward your traffic from port 3000 on your local machine to the same port on a remote server where these services are running:
$ ssh -R 3000:localhost:22 target_server_ip_or_address

Connect directly via SSH, connecting to port 3000 of your local machine instead:

ssh -p 3000 user@target_server IP


## Troubleshooting Common Issues

Issue: No Output or Incorrect Port Forwarding

If there’s no output when executing the command, check whether you’re running an SSH client without the correct host key. Ensure that your known_hosts file is up-to-date with the public keys of your target systems.

Issue: Firewall Blockage on Destination Host

Make sure to allow SSH traffic through the firewall on both your local machine and remote host. This can be done by adjusting network rules or enabling services like fail2ban, which may need to be configured to allow exceptions for secure connections.

Conclusion

Setting up an SSH tunnel is a simple yet powerful technique that enables you to securely forward traffic across networks without needing direct connection access to the destination. By understanding and utilizing this method effectively, you can streamline your development workflow by accessing production services on remote machines without compromising security.