Skip to content

Instantly share code, notes, and snippets.

@fabiosantoscode
Last active December 9, 2023 19:46
Show Gist options
  • Save fabiosantoscode/bcfe7165ca6dd97ba0bf to your computer and use it in GitHub Desktop.
Save fabiosantoscode/bcfe7165ca6dd97ba0bf to your computer and use it in GitHub Desktop.
Reverse tunnel. Connect to a public host somewhere and have it redirect all connections to your machine behind a NAT or firewall
# This enables a publicly available server to forward connections to your computer behind a NAT.
# So if you access http://xx.xx.xx.xx:8080/ on your browser, traffic is redirected to your machine behind a NAT.
# on your local host, type:
ssh -R xx.xx.xx.xx:8888:localhost:80 root@xx.xx.xx.xx
# now wait for your shell, and type:
socat TCP-LISTEN:8080,FORK TCP:127.0.0.1:8888
# This command outputs nothing, just keep it running. While you don't ^C, your tunnel is up and running!
# Address already in use? Change the ports you're using.
# Wanna change the ports? Read this.
# 80 is the port I want to access on my own machine which is behind a NAT
# 8080 is a port that the public server listens on. So if I access http://my-public-server:8880, I actually get to my own machine.
# xx.xx.xx.xx is the IP address of my server. I think you could use a hostname for this.
# 8888 is the port for the reverse tunnel. SSH will listen when my-public-server tries to access it, and forward connections to port 80 on my machine behind NAT.
@tamsanh
Copy link

tamsanh commented Mar 6, 2018

Great. Works perfectly.

For Address already in use errors, you can use SO_REUSEADDR. (In the following example, I use TCP4, but it should work with TCP).

socat TCP4-LISTEN: 8080,FORK,REUSEADDR TCP4:127.0.0.1:8888

@luftreich
Copy link

good !

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