SSH Config Tips: RemoteFoward and LocalForward

Suppose you have a local laptop without a public IP named A (192.168.1.7) and a remote server that has a public IP name named B (172.16.0.77) and another remote server C (172.16.0.78) which is in the same LAN with B. You can connect to B directly by ssh, and you cannot connect to C directly because C doesn't have an external IP address.

  1. You want to map port 5001 on A to the same port on B to make you local service temporarily public), you should add this line in laptop A's .ssh/config file:

     RemoteForward 5001 localhost:5001 #here localhost refers to A
    
  2. You want to map port 3306 on Host B (Remote machine) to the 3306 port on Host A (your laptop), you should add this line in laptop A's .ssh/config file:

     LocalForward 3306 localhost:3306  # here localhost refers to B
    
  3. You want to map port 22 on machine C to the 8045 port on A, add this line:

     LocalFoward 8045 172.16.0.78:22
    

Finally, the following code shows the final configuration in your ~/.ssh/config file

            Host B
		HostName b.somedomain.com
		Port 22
		RemoteForward 5001 localhost:5001
		LocalForward 3306 localhost:3306
		LocalForward 8045 172.16.0.78:22

Thus, after you get connected with Host B through this config file,

  • You can tell your friends or colleague to visit your local website by visiting http://b.somedomain.com:5001.
  • You can get connected to Host C directly by running ssh -p 8045 username@localhost command.
  • You can also directly visit the locally deployed MySQL service on Host B by connecting your local laptop's 3306 port.

Comments

  1. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.