Linuxhotel Wiki

Wie ging das nochmal?

Benutzer-Werkzeuge

Webseiten-Werkzeuge


admin_grundlagen:systemd_service_unit_files

Beispiel für ein Service Unit

Unit File anlegen:

systemctl edit --full --force nc.service
/etc/systemd/system/nc.service
[Service]
ExecStart=/usr/bin/nc -l -p 5000

1)

Syntax des Unit Files prüfen:

systemd-analyze verify nc.service

2)

Systemd Konfiguration neu laden 3)

systemctl daemon-reload

Service starten

systemctl start nc

Start überprüfen

systemctl status nc

Beschreibung hinzufügen:

systemctl edit --full nc.service
/etc/systemd/system/nc.service
[Unit]
Description=my netcat service
 
…

Beschreibung überprüfen

systemctl status nc

Beim Booten starten

systemctl edit --full nc.service
/etc/systemd/system/nc.service
…
 
[Install]
WantedBy=multi-user.target
systemctl enable nc

Nach beendeter Verbindung neustarten:

/etc/systemd/system/nc.service
…
 
[Service]
…
Restart=always

Beispiel für ein User Service Unit

Sinnloses Skript anlegen:

mkdir ~/bin
vim ~/bin/fillfs.sh
~/bin/fillfs.sh
#!/bin/bash
dd if=/dev/zero of=/tmp/zero oflag=append conv=notrunc bs=1M

Als Benutzer Unit File anlegen:

systemctl --user edit --force --full fillfs.service
~/.config/systemd/user/fillfs.service
[Service]
ExecStart=/home/nutzer45/bin/fill-fs.sh

Syntax des Unit Files überprüfen:

systemd-analyze --user verify fillfs.service

„Dienst“ starten:

systemctl --user start fillfs

Sehen was passiert:

systemctl --user status fillfs
journalctl --user -fu fillfs
watch -d df -h

vmstat daemon service unit

/etc/systemd/system/vmstatd.service
[Unit]
Description=VMSTAT as a Service
 
[Service]
Restart=on-failure
RestartSec=10
EnvironmentFile=/etc/default/vmstatd
#ExecStart=/bin/bash -c 'exec /usr/bin/vmstat -n -w -t 3 >> /tmp/vmstat.log'
ExecStart=/bin/bash -c 'exec /usr/bin/vmstat $VMPARAM $VMDELAY >> $VMLOGFILE'
 
[Install]
WantedBy=basic.target
/etc/default/vmstatd
VMPARAM="-n -w -t"
VMDELAY="5"
VMLOGFILE="/tmp/vmstat.log"

Dienste absichern

4)

TODO: noch nicht getestet

ProtectSystem=full

oder

ProtectSystem=strict
ReadWritePaths=/var/lib/…
ReadOnlyPaths=/var/lib/…
CapabilityBoundingSet=~CAP_SYS_ADMIN
SystemCallFilter=~@mount,~@swap,~@setuid,~@resources,~@reboot,~@privileged,~@obsolete,~@module
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectedControlGroups=true
PrivateTmp=true
PrivateDevices=true
SystemCallFilter=~@mount,~@swap,~@setuid,~@resources,~@reboot,~@privileged,~@obsolete,~@module,~@keyring,~@cpu-emulation,~@clock,~@chown
RestrictAddressFamilies=AF_UNIX,AF_INET,AF_INET6
RestrictNamespaces=true
RestrictRealtime=true
#PrivateUsers=
#MemoryDenyWriteExecute=
#IPAddressAllow=
#IPAddressDeny=
#AmbientCapabilitySet=

Für lokale Dienste (systlog ohne netzwerk, cron (falls keine Jobs auf das Netzwerk zugreifen sollen), …) zusätzlich:

PrivateNetwork=true
RestrictAddressFamilies=AF_UNIX

TODO: Für Dienste, die keine Daten dauerhaft speichern (ntpd? unbound?):

DynamicUser=yes
User=unbound
Group=unbound
1)
Lennart Poettering schreibt auf die Frage, warum alle Netzwerkdienste Socket Activation nutzen sollten: „robustness (because your service can go away, and then get restarted without clients noticing much), security (because priv code in pid1 binds the socket, and service can be entirely unpriv), resource use (because you can bind thousands of sockets, without having to pay upfront for starting the services behind), flexibility (because admins can configure .socket units in a *lot* of detail, and schedule binding of sockets precisely, for example bind certain IP sockets only after some network interface showed up and has been configured), simplicity (because as mentioned you don't need to configure deps anymore), compatibility with soft-reboot, … and the list goes on and on and on and on…“
2)
Fehlermeldung „Attempted to remove disk file system, and we can't allow that.“ in systemd Versionen vor v239 kann ignoriert werden: https://github.com/systemd/systemd/issues/8592
3)
nicht nötig, wenn das Unit File mit systemctl edit angelegt oder bearbeitet wurde
4)
Lennart Poettering schreibt: There's quite some stuff now we should enable wherever we can. Specifically ProtectSystem=, ProtectHome=, ProtectKernelTunables=, ProtectKernelModules=, ProtectedControlGroups=, PrivateUsers=, PrivateTmp=, PrivateDevices=, PrivateNetwork=, SystemCallFilter=, RestrictAddressFamilies=, RestrictNamespaces=, MemoryDenyWriteExecute=, RestrictRealtime= https://lwn.net/Articles/709764/
admin_grundlagen/systemd_service_unit_files.txt · Zuletzt geändert: 2025/09/02 13:23 von ingo_wichmann