User systemd service
| Ubuntu 22.04
All of users’ systemd have to place in directory ~/.config/systemd/user/
If you want start user systemd unit when user login, run:
systemctl --user enable <service>
List of available types of systemd units:
systemctl -t help
Available unit types:
service
mount
swap
socket
target
device
automount
timer
path
slice
scopeCreate user systemd file to make ssh connection:
Name “hetzner” was already created in
~/.ssh/config:... host hetzner Hostname 159.69.152.138 Port 22 user goto IdentityFile /home/goto/.ssh/id_rsa ...
cat <<EOF > ~/.config/systemd/user/ssh-hetznder-sockd5.service
[Unit]
Description=Setup a secure tunnel to %I
After=network.target
[Service]
Environment="TARGET=hetzner"
# EnvironmentFile=/etc/default/secure-tunnel@%i
ExecStart=/usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -D 1080 $TARGET
# Restart every >2 seconds to avoid StartLimitInterval failure
RestartSec=5
Restart=always
EOFAnd reload systemd:
systemctl --user daemon-reloadEnable and start systemd service:
systemctl --user enable --now ssh-hetznder-sockd5Check that service alive:
$ systemctl --user status ssh-hetznder-sockd5
● ssh-hetznder-sockd5.service - Setup a secure tunnel to
Loaded: loaded (/home/goto/.config/systemd/user/ssh-hetznder-sockd5.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2025-01-07 15:28:57 MSK; 5s ago
Main PID: 643630 (ssh)
Tasks: 1 (limit: 18149)
Memory: 1.8M
CPU: 54ms
CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/ssh-hetznder-sockd5.service
└─643630 /usr/bin/ssh -NT -o ServerAliveInterval=60 -o ExitOnForwardFailure=yes -D 1080 hetzner