Skip to content

Instantly share code, notes, and snippets.

@ialhashim

ialhashim/readme.md

Last active Jan 11, 2021
Embed
What would you like to do?
Reverse port forwarding via an AWS instance

If you want to connect to a local workstation behind a firewall you can do that by bouncing traffic off of an AWS instance.

An example would be accessing a Jupyter Lab from the outside world to a machine with no static IP or behind a firewall.

The steps are:

  1. Create a paid AWS Ubuntu instance and save the '.pem' file.
  2. Make sure the AWS instance's Security Rules allows traffic in.
  3. Test that you can connect to this instance from the outside world (e.g. python3 -m http.server or jupyter lab --ip=*) then visit the instance using its public IP.
  4. Enable GatewayPorts yes as mentioned here and restart sshd as mentioned.
  5. On the local workstation (e.g. GPU workstation) run the following command: ssh -i "YOUR_PEM_FILE.pem" -R AWS_PUBLIC_IP:2222:localhost:8888 ubuntu@AWS_PUBLIC_IP

Now you can access the webserver or jupyter lab by going to http://AWS_PUBLIC_IP:2222.

Warning: this guide does not take care of the security aspects. Use at your own risk.

@FarisHijazi

This comment has been minimized.

Copy link

@FarisHijazi FarisHijazi commented Jan 11, 2021

If you're getting error 403 when connecting, allow remote access to jupyter notebook server (as shown in this guide)

jupyter notebook --generate-config
echo "c.NotebookApp.ip = '0.0.0.0'" >> ~/.jupyter/jupyter_notebook_config.py
jupyter notebook password # optional
@FarisHijazi

This comment has been minimized.

Copy link

@FarisHijazi FarisHijazi commented Jan 11, 2021

And since it tends to disconnect randomly, you may want to put it in a loop

sudo `while sleep 1; do ssh -i tunneling.pem -R ubuntu@AWS_PUBLIC_IP:2222:localhost:8888 ubuntu@AWS_PUBLIC_IP || sleep 1 ;done`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment