haase.dev/ blog

Systemd Remove Template Unit

July 18, 2020

Systemd's template unit files can be used to create multiple parameterized instances of the same unit. A common use-case is the configuration of multiple OpenVPN or Wireguard services.

I renamed my Wireguard configuration file, but the old systemd unit wg-quick@infra.service kept lurking around:

❯ systemctl list-units 'wg-quick*' --all
UNIT                   LOAD   ACTIVE   SUB    DESCRIPTION
wg-quick@infra.service loaded inactive dead   WireGuard via wg-quick(8) for infra
wg-quick@wg0.service   loaded active   exited WireGuard via wg-quick(8) for wg0
wg-quick.target        loaded inactive dead   WireGuard Tunnels via wg-quick(8)

Let's find the unit in the systemd configuration:

❯ find /etc/systemd/system -name 'wg-quick*'
/etc/systemd/system/multi-user.target.wants/wg-quick@wg0.service
/etc/systemd/system/multi-user.target.wants/wg-quick@infra.service

The file is just a symlink to the base unit:

lrwxrwxrwx 1 root root 37 Jan 11  2020 /etc/systemd/system/multi-user.target.wants/wg-quick@infra.service -> /lib/systemd/system/wg-quick@.service

Remove the file and reload the systemd configuration:

❯ rm /etc/systemd/system/multi-user.target.wants/wg-quick@infra.service
❯ systemctl daemon-reload