Skip to content

Instantly share code, notes, and snippets.

@ialhashim
Last active April 2, 2024 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ialhashim/65f634304497f107cdff98bd3cc1228f to your computer and use it in GitHub Desktop.
Save ialhashim/65f634304497f107cdff98bd3cc1228f to your computer and use it in GitHub Desktop.
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. (optionally) Enable password login in /etc/ssh/sshd_config by setting PasswordAuthentication yes and sudo passwd $USER.
  5. Enable GatewayPorts yes and restart sshd. E.g. sudo nano /etc/ssh/sshd_config then sudo systemctl restart ssh.service.
  6. 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
Copy link

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
Copy link

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