Skip to content

Instantly share code, notes, and snippets.

@MParvin
Last active July 13, 2025 08:03
Show Gist options
  • Save MParvin/7f2ee9423c95f3fb79356f044ea13738 to your computer and use it in GitHub Desktop.
Save MParvin/7f2ee9423c95f3fb79356f044ea13738 to your computer and use it in GitHub Desktop.
SSH tunnel systemd service
[Unit]
Description=SSH Tunnel
After=network.target
[Service]
Type=forking
EnvironmentFile=/etc/default/ssh_tunnel
ExecStart=/usr/bin/ssh -fN -D "$SOCKS_PORT" -i "$SSH_KEY" -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes "$USER"@"$SSH_SERVER"
ExecStartPost=/bin/bash -c "/usr/bin/curl --silent --fail --max-time 10 -x socks5h://localhost:$SOCKS_PORT http://ifconfig.me/ip || /bin/kill -9 -- $MAINPID"
ExecReload=/bin/kill -1 -- $MAINPID
ExecStop=/bin/kill -9 -- $MAINPID
Restart=always
RestartSec=5
KillMode=process
[Install]
WantedBy=multi-user.target
@MParvin
Copy link
Author

MParvin commented Jul 3, 2023

Content of /etc/default/ssh_tunnel

SSH_SERVER=
USER=
SOCKS_PORT=
SSH_KEY=

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