systemd for persistent SOCKS proxy

systemd for persistent SOCKS proxy

I'd like one browser to connect through a VPN but my main internet connection (and other browsers) to go through my ISP.

I already have a VPN running on a VM on Proxmox and figured I could use it as a proxy.

But if the SSH connection dies the proxy would disconnect so I need a way of keeping the SSH connection from dying!  

Step 1: create /etc/systemd/system/anon-socks.service

[Unit]
Description=SSH socks proxy
After=network-online.target

[Service]
User=MYUSER
ExecStart=/usr/bin/ssh -N -D 9876 -q -i /home/MYUSER/.ssh/vm_pvt_key MYEXTERNALUSER@10.5.0.21
ExecStop=kill $(pgrep -f 'ssh -N -D 9876 -q')
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target
/etc/systemd/system/anon-socks.service

Make sure to update MYUSER to your username on the local machine, update -i /home/MYUSER/.ssh/vm_pvt_key MYEXTERNALUSER@10.5.0.21 to the appropriate connection.

Restart=always is what restarts the service should it be interrupted for any reason (loss of connection, you suspend and resume your computer, etc.). This is what makes the proxy "undying!"

Step 2: reload

sudo systemctl daemon-reload
Reload the unit files

Step 3: enable it

sudo systemctl enable --now anon-socks.service
Enable it, starting it now

Step 4: connect to proxy

about:preferences#general > Network Settings > Settings > Manual proxy configuration

Step 5: well, you're proxied now

(assuming the VPN is running on the server - you should confirm you're IP has changed)