SSH Tunneling - What it is and how you can set it up
Hello folks! This is my very first blog post out here on my own website! I am excited to be able to share my knowledge with you all and I hope that my content will help you add skills to your technical arsenal and learn in your technical journey. Today we will be exploring everything about SSH tunnelling and how you can use this to do some cool stuff. Let’s get to it!
What is SSH Tunneling?
SSH tunneling OR SSH port forwarding is simply a method where you use the SSH protocol to securely tunnel services or ports through a destination server. For instance, think of VPN. When you connect to your VPN, what you’re doing is to tunnel all of your traffic originating from your host computer to the destination server, essentially making it seem like your computer is part of this destination servers local network.
Some applications of SSH tunneling would be to access content that is geographically restricted or to bypass firewalls.
Types of SSH tunneling / Port Forwarding
There are multiple types of tunneling / ssh port forwarding:
Local Port Forwarding
Remote Port Forwarding
Dynamic Port Forwarding
Local Port Forwarding
Local port forwarding is used to forward a port on your local machine to the server machine that is accessible via SSH. This allows the client machine (your local machine) to listen on the port and forward all the network traffic to the server machine. Note that the destination traffic could be the SSH server or some other network destination that is accessible by the SSH server.
Typical use cases for local port forwarding are:
Sending traffic through a jump server
Connecting to a service that is only available on an internal network from the outside
Connecting to a remote file sharing service over the internet
A typical use case for example is that you have an application that is hosted on a machine in your office at say port 8080. You do not have access to this machine, but you do have access to an SSH server from your home computer. Say you want to access the application on your machine on port 8080, you would issue the following command on your terminal (linux or Mac):
$ ssh -L 127.0.0.1:8080:10.10.10.70:8080 [user]@[ssh_server_ip]
#127.0.0.1:8080 is your localhost port 8080.
#10.10.10.70:8080 is the application port hosted on the internal server in your office.
#[user]@[ssh_server_ip] is the username and ip of the ssh server that you have access to from your home machine.
Remote Port Forwarding
Remote port forwarding is exactly like local port forwarding, but the situation is reversed. For instance, say that you have an application running on your local machine, and want to make it accessible to a remote location (say your office network) via an SSH server. Note that in order to be able to do this, you will still need to have network connectivity to the SSH server. This use case is rare compared to local port forwarding.
$ ssh -R 9999:127.0.0.1:8080 [user]@[ssh_server_ip]
#9999 is the port that you want to use on the remote SSH server to tunnel traffic back to your local machine
#127.0.0.1:8080 is the socket on which your application is running
#[user]@[ssh_server_ip] is the username and ip of the ssh server that you have access to from your home machine
Note that there may be a requirement to set "GatewayPorts yes" in the sshd_config file on the SSH server if you want to connect to the forwarded port from outside the SSH server computer.
Dynamic Port Forwarding
Dynamic port forwarding will allow you to create a socket (IP + Port number) on your local host machine that desired applications can use to tunnel traffic through to a destination SSH Server, from where the traffic can be forwarded on to the actual destination. This is pretty much what a VPN connection will do as well.
What actually happens is that a SOCKS proxy is created between your host and the remote SSH server. Checkout this link if you want to learn more about SOCKS proxies.
ssh -D [local_port] [user]@[ssh_server_ip]
#[local_port] is the port that you want to create a socks proxy
#[user]@[ssh_server_ip] is the username and ip of the ssh server that you have access to from your home machine
As soon as you issue that command, you can configure your browser to use the SOCKS proxy in order to tunnel all your internet traffic through the SSH server. Check out this article if you would like to know how you can configure this in a chrome browser.
If you got to the end of this post, thank you so much for reading! I hope you have learned something new today! ☺️