Fixing `Unit ssh-guard.service not found` after installing a systemd service on Debian 12
I wrote a tiny service to restart a script that keeps dying on a home server running Debian 12. The unit file was fine, or so I thought, but systemctl kept saying it didn't exist. This is a setup mistake I'd made before and still made, so I'm writing it down.
## The error
$ sudo systemctl start ssh-guard.service
Failed to start ssh-guard.service: Unit ssh-guard.service not found.$ sudo systemctl status ssh-guard.service
Unit ssh-guard.service could not be found.
I could see the file right there:
$ ls -l ssh-guard.service
-rw-r--r-- 1 me me 412 Apr 12 20:31 ssh-guard.service
It was in my home directory. That's the first problem. systemd doesn't scan your home directory.
## Where systemd actually looks
On Debian 12 there are two common places:
/etc/systemd/system/ # your own units and overrides
/lib/systemd/system/ # units shipped by packages
System units you write go in /etc/systemd/system. The distinction matters because /lib is managed by dpkg and gets overwritten on package upgrades. Mine went to /etc:
sudo cp ssh-guard.service /etc/systemd/system/
sudo chmod 644 /etc/systemd/system/ssh-guard.service
The unit itself was simple:
[Unit]
Description=Restart watcher
After=network.target[Service]
Type=simple
ExecStart=/usr/local/bin/ssh-guard.sh
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
A couple of people have asked me why I bothered with a service at all for a shell script. Fair. The reason is that the script needs to survive a reboot and restart on failure, and cron doesn't give me either without extra glue. systemd is overkill for a home box but it's less overkill than a cron job with a lockfile and a mailto I never read.
## The step I always forget
Even with the file in the right place, systemctl won't see it until you reload the manager:
sudo systemctl daemon-reload
sudo systemctl enable --now ssh-guard.service
Created symlink /etc/systemd/system/multi-user.target.wants/ssh-guard.service -> /etc/systemd/system/ssh-guard.service.$ systemctl is-active ssh-guard.service
active
Without daemon-reload you get the same "Unit not found" error and it looks like the file is still in the wrong place. I spent ten minutes checking paths before I remembered it.
## When the error means something else
If the unit is found but fails to start, the message changes:
$ journalctl -u ssh-guard.service -n 20 --no-pager
Apr 12 20:44:03 home systemd[1]: ssh-guard.service: Failed at step EXEC spawning /usr/local/bin/ssh-guard.sh: Permission denied
That one is a missing execute bit, chmod +x on the script. Different error, different fix. systemctl status plus journalctl -u covers both cases and tells you which one you've got.
The other status worth knowing is systemctl show ssh-guard.service -p MainPID -p ExecMainStatus, which prints the raw exit status without the human formatting. If the service exits cleanly but immediately, you'll see ExecMainStatus=0 and no error anywhere, which usually means the script forked and the parent exited. For a Type=simple service that's a common footgun. Adding Type=forking or dropping the background & fixes it.
## A small gotcha with the filename
I'd originally named the file ssh-guard@.service, which is a template unit and needs an instance name. Calling systemctl start ssh-guard.service will never find it because the unit is ssh-guard@something.service. If your unit name ends in @, that's probably your problem. Renaming mine to a plain unit saved me another round of confusion.