Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rkmax/d73b8110cede81c8e13fd5f673d93681 to your computer and use it in GitHub Desktop.
Save rkmax/d73b8110cede81c8e13fd5f673d93681 to your computer and use it in GitHub Desktop.
SSH Tunnel Cheat Sheet
# $LOCAL_IP: 'localhost' or machine from local network
# $LOCAL_PORT: open port on local machine
# $REMOTE_IP: remote localhost or IP from remote network
# $REMOTE_PORT: open port on remote site

# Forward Tunnel: map port from remote machine/network on local machine
ssh -L $LOCAL_PORT:$REMOTE_IP:$REMOTE_PORT $USER@$SERVER

# Reverse Tunnel: make local port accessable to remote machine
ssh -R $REMOTE_PORT:$LOCAL_IP:$LOCAL_PORT $USER@$SERVER
@rtp4jc
Copy link

rtp4jc commented Jul 25, 2022

I love this cheat sheet. Thanks @rkmax for making this. I'm going to embellish this a little for people with less experience by adding some example values and other commands.

You can set the variables mentioned above ($LOCAL_IP, $LOCAL_PORT, etc) with the export command:

export LOCAL_IP="localhost"

Note: I left out the dollar sign on purpose. You only use the dollar sign when you access the value of the variable.

The LOCAL_IP address will most likely have a value of 'localhost' as mentioned above. This can also be an IP address of a device on the local network.

The REMOTE_IP is not the server address, it's the IP address local to the server address. You will probably want this set to 'localhost'. However, if the SERVER address pointed to a computer (say, computer A) that had computer B in its local network, you could set it to the local IP address of computer B on computer A's local network (Ex: 192.168.1.X or 10.0.0.X) and you could access ports from computer B.

The REMOTE_PORT is not the SSH port on the server, it's the port you want to forward to the LOCAL_PORT. If you need a custom SSH port (other than 22), you'll need to add -p <port_number> to the end of the commands in this cheat sheet.

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