DevOps - SSH tunneling and port forwarding

Tunneling is quick method for establishing connecting between local computer and a remote machine. Without any additional setup just using SSH protocol.

DevOps - SSH tunneling and port forwarding

What is SSH tunneling

Tunneling used to connect two machine and access the ports from the remote machine or local machine using ssh protocol. This method is widely used in administering server infrastructure management.

What you will learn

  • Connecting remote server and accessing its internal ports from local machine
  • Reverse SSH tunneling - Accessing local ports from the remote server

Access remote server port form the local machine

Connect to the remote server 10.10.10.1 and try to access it's internal port 8080
from the localhost:8000.

-L argument is used to connect as SSH tunnel.
ssh -L 8000:localhost:8080 [email protected]
Note: 10.10.10.1 is used for demonstrating purpose only. You should use your server IP instead.
SSH tunnel - connecting remote server internal port from the local machine
Created by mobilelabs

Now using localhost:8000 we can access the 10.10.10.1:8080 port.

Reverse SSH tunneling - Accessing local ports from the remote server

We have MySQL database 10.10.2.10:3306 in our on premise network which is not accessible by outside of the network. We need to connect this to web server in AWS Cloud 11.11.1.10.

-R argument is used to connect as SSH reverse tunnel.
ssh -R 3306:10.10.2.10:3307 [email protected]
Note: 11.11.1.10 is used for demonstrating purpose only. You should use your server IP instead.
SSH reverse tunnel - connecting local server port from the remote machine
Created by - mobilelabs

Now from AWS 11.11.1.10:3307 we can access the 10.10.2.10:3306 port.

How to forward all ports

You create simple proxy by forwarding all ports from source to destination server.

-D argument is used to forward dynamic ports.
ssh -D localhost:1337 [email protected]

Conclusion

SSH tunneling is simple but its very powerful, we have seen how to use ssh tunneling and reverse ssh tunneling.