====== Beispiel für ein Service Unit ====== Unit File anlegen: systemctl edit --full --force nc.service [Service] ExecStart=/usr/bin/nc -l -p 5000 Syntax des Unit Files prüfen: systemd-analyze verify nc.service (( 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 )) Systemd Konfiguration neu laden (( nicht nötig, wenn das Unit File mit ''systemctl edit'' angelegt oder bearbeitet wurde )) systemctl daemon-reload Service starten systemctl start nc Start überprüfen systemctl status nc Beschreibung hinzufügen: systemctl edit --full nc.service [Unit] Description=my netcat service … Beschreibung überprüfen systemctl status nc Beim Booten starten systemctl edit --full nc.service … [Install] WantedBy=multi-user.target systemctl enable nc ====== Beispiel für ein User Service Unit ====== Sinnloses Skript anlegen: mkdir ~/bin vim ~/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 [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 ===== [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 VMPARAM="-n -w -t" VMDELAY="5" VMLOGFILE="/tmp/vmstat.log" ====== Dienste absichern ====== ((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/)) 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