setup ssh tunneling

How to Setup SSH Tunneling

If you need to connect to another system on a network, then you will need to expose it to publicly accessible network which may risk your data and files. One of the solutions to overcome this problem, is to setup an SSH tunneling between the source and destination systems. It is a fast and secure way to transfer data between two systems. It is also known as port forwarding. In SSH tunneling, a secure and encrypted SSH connection is made between client-server using which data can be transferred securely. In this article, we will learn how to setup SSH tunneling in Linux. You can use these steps on almost every Linux distribution such as RHEL/CentOS/Fedora/Debian & Ubuntu.


How to Setup SSH Tunneling

Here are the steps to setup SSH tunneling in Linux. Please note, when you run the commands below the first time, you may be asked if you trust the remote source. Enter yes to proceed. Also, almost every Linux system has ssh pre-installed so you don’t need to install or setup anything in this case.


What is SSH Tunneling

Before we setup SSH tunneling, it is important to understand what is SSH tunneling, and the different types of tunneling available. SSH tunneling is basically a way to send data through an encrypted channel. This data can be encrypted or unencrypted. Nevertheless, it is secure since the channel of transmission itself is secure & encrypted. It is used for file transfer, database connection, intranet connections across systems which use firewalls. It is also used for VPN systems. You can also use it to bypass firewall and access restricted content in some cases. There are three types of SSH tunneling:

  • Local Port Forwarding
  • Remote Post Forwarding
  • Dynamic Port Forwarding


1. Local Forwarding

In this case, the we will forward a port from local client machine to remote server machine and from there the connection is made to destination machine. In this case, when you send request to local client’s forwarded port, it will send the data to remote server via SSH tunnel. From there onwards, the connection is forwarded by remote server to destination machine.

It is used to connect to a remote system behind a firewall or in intranet to remote system. For example, you can use this method to connect to a remote database whose database port is not publicly accessible.

Let us say you want to connect to remote service running on port 8000 via local port 8080. In such cases, open terminal and run the following command to tunnel connection from local to remote port. Replace 127.0.0.1 with local IP address or domain, and www.example.com with remote IP address or domain.

$ ssh 127.0.0.1 -L 8080:www.example.com:3000 

Now when you send requests to local port 8080 it will be forwarded to remote port 3000.

Alternatively, you may use the following command.

$ ssh -L [LOCAL_IP:]LOCAL_PORT:DESTINATION:DESTINATION_PORT [USER@]SSH_SERVER

In the above command, [LOCAL_IP]:LOCAL_PORT is the local IP address-port combination, and DESTINATION:DESTINATION_PORT is the remote ip address-port combination. [USER@]SSH_SERVER is the user credential for remote login.

If you want to the above tunnel to run in background use -f option.

$ ssh -f 127.0.0.1 -L 8080:www.example.com:3000


2. Remote Forwarding

In this case, we connect to local machine from remote machine, via SSH tunneling. By default, SSH does not support remote port forwarding. So you need to open its configuration file in a text editor.

$ sudo vim /etc/ssh/sshd_config 

Add/Modify the GatewayPorts to Yes.

GatewayPorts yes

Save and exit the file. Restart the server.

$ sudo systemctl restart sshd

Once you have enabled remote port forwarding, run the following command.

$ ssh -R [REMOTE:]REMOTE_PORT:DESTINATION:DESTINATION_PORT [USER@]SSH_SERVER

In the above command,

  • [REMOTE:]REMOTE_PORT – IP address & port number of the remote SSH server.
  • DESTINATION:DESTINATION_PORT – IP address & port of local machine.
  • [USER@]SERVER_IP – The remote SSH username and IP address

Here is an example to listen to port 8080 for incoming traffic from remote IP 54.43.32.21 and direct it to local port 3000.

$ ssh -R 54.43.32.21:8080:127.0.0.1:3000 -N -f user@remote.host 

If you omit the remote IP address 54.43.32.21 and the colon (:) after it, then SSH will listen to all incoming traffic on port 8080.

It is used to give access to systems outside your network/firewall. So be careful, before you do this.


3. Dynamic Port Forwarding

In above 2 examples, you can tunnel only a single local port. In dynamic port forwarding, you can tunnel between a range of local and remote ports. We can enable dynamic port forwarding via -D option in ssh command, as shown below.

$ ssh -D [LOCAL_IP:]LOCAL_PORT [USER@]SSH_SERVER

In the above command,

  • [LOCAL_IP:]LOCAL_PORT – IP address & port number of the local machine.
  • [USER@]SERVER_IP – Remote IP address and username.

That’s it. In this article, we have learnt what is SSH tunneling (port forwarding) and the different types of port forwarding.

Also read:

Leave a Reply

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