Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Last active July 22, 2022 21:25
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ashrithr/3a83b42e3b658e742177 to your computer and use it in GitHub Desktop.
Save ashrithr/3a83b42e3b658e742177 to your computer and use it in GitHub Desktop.
ssh tunneling and port forwarding

###Single hop tunelling:

ssh -f -N -L 9906:127.0.0.1:3306 user@dev.example.com

where,

  • -f puts ssh in background
  • -N makes it not execute a remote command

This will forward all local port 9906 traffic to port 3306 on the remote dev.example.com server

###Multi-Hop Tunelling:

Tunnel from localhost to host1 and from host1 to host2:

ssh -L 9999:localhost:9999 host1 ssh -L 9999:localhost:1234 -N host2

This will open a tunnel from localhost to host1 and another tunnel from host1 to host2. However the port 9999 to host2:1234 can be used by anyone on host1. This may or may not be a problem.

Another Example:

Assume you have you have a web server running on 10.1.0.93 in a private network on port 80 which is reachable by a gateway server 198.1.1.34, here is how to open the ssh tunnel:

ssh -L 80:localhost:80 root@198.1.1.34 -t ssh -L 80:localhost:80 root@10.1.0.93

Example SSH Config:

Host cwg
  HostName 198.0.218.179
  Port 22
  User root
  IdentityFile ~/.ssh/id_rsa

# Access cw sync on localhost:9292
# Enable: ssh -f -N cw_tunnel
Host cw_tunnel
  HostName 198.0.218.179
  User root
  IdentityFile ~/.ssh/id_rsa
  LocalForward 9292 127.0.0.1:9292

# auto tunelling to securehost (remote host) via jumphost (gateway)
# we tell ssh that when it establishes a connection to securehost to do so using
# the stdin/stdout of the ProxyCommand as a transport. The ProxyCommand then tells
# the system to first ssh to our bastion host and open a netcat connection to host
# %h (hostname supplied to ssh) on port %p (port supplied to ssh).
Host jumphost
  ProxyCommand none
Host securehost
  ProxyCommand ssh jumphost -W %h:%p
@shakibamoshiri
Copy link

Thanks for sharing it,
In this way is also possible

Host cwg
  HostName 198.0.218.179
  Port 22
  User root
  IdentityFile ~/.ssh/id_rsa

# Access cw sync on localhost:9292
# Enable: ssh -f -N cw_tunnel
Host cw_tunnel
  HostName 198.0.218.179
  User root
  IdentityFile ~/.ssh/id_rsa
  LocalForward 9292 127.0.0.1:9292

Host *
    Compression yes
    KeepAlive yes
    ServerAliveInterval 240

then on Terminal

ssh -fNTCD 9050 -J wg cw_tunnel
# which means 
# localhost => cwg => cw_tunnel

Test

curl --socks5-hostname localhost:9050 ifconfig.me; echo
# output should be IP address of endpoint e.g. cw_tunnel

@nk9
Copy link

nk9 commented Jul 22, 2022

In both of these examples, the IdentityFile, User, and HostName are repeated. Is it possible to avoid this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment