Skip to content

Instantly share code, notes, and snippets.

@epilys
Created January 31, 2024 07:15
Show Gist options
  • Save epilys/922e3c0b8e608e6830eac3be2f0f7ff9 to your computer and use it in GitHub Desktop.
Save epilys/922e3c0b8e608e6830eac3be2f0f7ff9 to your computer and use it in GitHub Desktop.
systemd service to port forward service
[Unit]
Description=Setup a secure tunnel to a local API port on another server named my-other-server in ssh config
After=network-online.target
[Service]
SyslogIdentifier="my-api-tunnel"
ExecStartPre=/bin/sh -c "/usr/bin/ssh -O exit my-other-service; true"
ExecStop=/bin/sh -c "/usr/bin/ssh -O exit my-other-service; true"
ExecStart=/usr/bin/ssh -NT -o LogLevel=ERROR -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -L 8000:localhost:8000 my-other-service
# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment