Whenever I want to securely proxy my web traffic through my VPS I just run this script in the terminal. There’s nothing terribly complicated about it – just a simple chain of commands to create a SOCKS proxy through an SSH tunnel. As it is targeted specifically to my current operating system of choice, Ubuntu (14.04), it might not work on other Linux flavors. But since Ubuntu is an ever-popular OS, I thought I’d post this snippet for posterity.
Just create a script file called proxy.sh and copy the following code:
#!/usr/bin/env bash #---------------------------------------------------------------# # Create a SOCKS proxy on a remote server through an SSH tunnel #---------------------------------------------------------------# REMOTE='myusername@myremotehost' HOST='localhost' PORT=5222 set -e function clear_proxy_settings() { echo 'Clearing system proxy settings' gsettings set org.gnome.system.proxy mode 'none' } trap clear_proxy_settings EXIT # catch exit signals echo 'Setting system proxy settings to '${HOST}':'${PORT} gsettings set org.gnome.system.proxy.socks host $HOST gsettings set org.gnome.system.proxy.socks port $PORT gsettings set org.gnome.system.proxy mode 'manual' echo 'Opening SSH tunnel' ssh -N -D $PORT $REMOTE trap - EXIT # reset the trap clear_proxy_settings # call cleanup manually exit 0
Make sure to edit the line REMOTE='myusername@myremotehost'
to suit your specific case. And, of course, ensure that the script is executable: chmod +x proxy.sh
. Then, to run:
./proxy.sh
In your browser, go to whatsmyip.org to check that your exposed IP is indeed that of your remote server. If all is working correctly, you should now be securely tunneling your traffic through your remote server!
The script will run indefinitely until you terminate it with CTRL-C
.