Created
August 5, 2025 19:18
-
-
Save sjmf/6ad576684fd12f59a6f8e969cfedb9cc to your computer and use it in GitHub Desktop.
Systemd configuration for persistent ssh reverse tunnel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=SSH tunnel service SSH on local port 22 to 22000 on remote host | |
Wants=network-online.target | |
After=network-online.target | |
StartLimitIntervalSec=0 | |
[Service] | |
User=tunnel | |
Group=tunnel | |
Type=simple | |
Restart=always | |
RestartSec=60 | |
ExecStart=/usr/bin/ssh -o UserKnownHostsFile=/home/tunnel/.ssh/known_hosts -o ExitOnForwardFailure=yes -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -NTR 22000:localhost:22 [email protected] -i /home/tunnel/.ssh/id_rsa | |
[Install] | |
WantedBy=multi-user.target |
The remote address can be bound publicly using the bind: -R *:22000:localhost:22
, but this increases attack surface.
Instead, one can use an SSH config (.ssh/config
) to bounce off localhost on the remote:
Host dm-iarc-01
Hostname localhost
Port 22000
User change_me
# Connect via remote host
ProxyCommand ssh [email protected] nc -w 5 $(echo %h|cut -d%% -f1) %p
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To set up the user (as root, or prefix
sudo
):Create passwordless ssh keys and copy to the remote:
Bring up systemd service:
The command
sudo journalctl -exu ssh.remote.service
will show logs. Add -v to the command to view verbose.Ideally, remove the password for the remote tunnel user:
passwd --delete tunnel