Skip to content

Instantly share code, notes, and snippets.

@werelax
Last active July 21, 2016 11:24
Show Gist options
  • Save werelax/10fd83ce033aa61e4288b613a5541b89 to your computer and use it in GitHub Desktop.
Save werelax/10fd83ce033aa61e4288b613a5541b89 to your computer and use it in GitHub Desktop.
Forward and Reverse SSH Tunnels
# Map a local port to a target port through the remote server.
# In the example: localhost:9000 is tunneled to imgur.com:80 through example.com
# local:9000 -> exaple.com -> target:80
# you could browse to http://localhost:9000/ and browse imgur.com
# (won't see anything, because imgur has changed their stack, but the tunnel works)
ssh -L 9000:imgur.com:80 user@example.com
# Useful for: use a server as a proxy, secure a connection (ssh encrypts everything),
# jumping around a firewall or site blocker.
# Map a local port to a port on the remote server
# In the example, we're mapping the local port 9000 to 'localhost:5432',
# but that 'localhost' is from the point of view of the server. So...
# local:9000 -> example.com:5432
ssh -L 9000:localhost:5432 user@example.com
# Useful for: expose a service on the server, like a DB, as if it were
# a local server.
# Map a port on the server to a local port
# On this example, we're mapping SERVER's port 9000 to 'localhost:3000',
# in this case the 'localhost' is from your local machine pot of view. So..
# example.com:9000 -> local:3000
ssh -R 9000:localhost:3000 user@example.com
# Useful for: expose a local service to the internet through the server
# name. To show your local version of a page to someone. To answer to a webhook
# from your local machine.
# NOTE: Port forwarding is disabled on ssh by default. To enable it, add:
# GatewayPorts yes
# to /etc/ssh/sshd_config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment