systemd
🤷♂️
analyze slow boot times
list how long it took for systemd units to initialize:
systemd-analyze blameoutput example:
2min 158ms systemd-networkd-wait-online.serviceDNS
all things DNS
change dns resolver
get current status and dns server:
resolvectl statuschange to another DNS server:
edit /etc/systemd/resolved.conf and change the DNS line. for example:
[Resolve]
DNS=1.1.1.1 8.8.8.8if you want to use DNS over TLS:
[Resolve]
DNS=1.1.1.1#1dot1dot1dot1.cloudflare-dns.com
DNSOverTLS=Yesrestart service:
sudo systemctl restart systemd-resolved.serviceverify again (see above)
change hostname
sudo hostnamectl set-hostname my-awesome-hostdepending on your setup you might still need to update your /etc/hosts file as well
clear dns cache
resolvectl flush-cachesverify:
resolvectl statisticslogs: journalctl / systemd journals
- see also: journalctl
configure journal / log usage
it might be a good idea to limit the disk usage of your systemd journals. To do this, edit the file: /etc/systemd/journald.conf
control how much disk space the journal may use up at most:
SystemMaxUse=1Gcontrol how large individual journal files may grow at most:
SystemMaxFileSize=100Mand restart things
systemctl daemon-reload # for good measure
systemctl restart systemd-journald.servicelog disk usage
check current disk usage of systemd journals:
journalctl --disk-usageclean up some old logs:
journalctl --vacuum-size=100Mtimers (better cronjobs!)
todo.. write something somewhere about the use of systemd-timers instead of cronjobs.
create your own timer / cronjob
create your service unit:
place it somewhere in /etc/systemd/system/restic_backup.service for example
[Unit]
Description=restic systemd service
[Service]
ExecStart=/usr/local/bin/backup.sh
[Install]
WantedBy=multi-user.targetcreate your timer:
place it in the same folder but name it .timer, like this: /etc/systemd/system/restic_backup.timer
[Unit]
Description=restic systemd timer
[Timer]
OnUnitActiveSec=24h
RandomizedDelaySec=1h
[Install]
WantedBy=multi-user.targetthere are many different ways when the service should run, like with OnUnitActiveSec every 24 hours including a randomized delay of 1h.
here’s the documentation for more ways when system should execute something
reload and enable the service:
systemctl daemon-reload
systemctl enable restic_backup.timerdisable timer
systemctl disable name.timerlist timers
systemctl list-timersremove / clear dead or dangling timers
I removed a service and timer through ansible and reloaded systemd, but the timer was still showing up when running systemd list-timers. This worked to clean it up:
systemctl reset-failedworking with services
debugging: read and follow service logs output
read logs:
journalctl -u your-name.servicetail log:
journalctl -f -u your-name.servicedisable / mask service, prevent from starting & unmask
disabling a service prevents it from being started automatically on boot but it can still be interacted with, or started by other services. masking a service means it can’t even be started using systemd, it’ll essentially create a symlink to /dev/null
mask / unmask services
list masked services:
systemctl list-unit-files | grep maskedmask a service / prevent service from being started at all:
systemctl mask wpa_supplicant.servicethe output will also tell you that the service has been linked to /dev/null:
Created symlink /etc/systemd/system/wpa_supplicant.service → /dev/null.unmask a service:
systemctl unmask bluetooth.servicedisable a service
disable a service from starting automatically:
it can be started manually, for example by NetworkManager
systemctl disable wpa_supplicant.servicelist active services
the service might be active, but exited already since it was just initializing or checking something
systemctl list-units --type=service --state=activelist running services
systemctl list-units --type=service --state=running