admin_grundlagen:systemd_service_unit_files
Dies ist eine alte Version des Dokuments!
Inhaltsverzeichnis
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
Syntax des Unit Files prüfen:
systemd-analyze verify nc.service
Systemd Konfiguration neu laden 2)
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
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)
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
2)
nicht nötig, wenn das Unit File mit
systemctl edit angelegt oder bearbeitet wurde 3)
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.1742462435.txt.gz · Zuletzt geändert: 2025/03/20 09:20 von natureshadow2
