Skip to content

systemd

🤷‍♂️

analyze slow boot times

list how long it took for systemd units to initialize:

systemd-analyze blame

output example:

2min 158ms systemd-networkd-wait-online.service

DNS

all things DNS

change dns resolver

get current status and dns server:

resolvectl status

change 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.8

if you want to use DNS over TLS:

[Resolve]
DNS=1.1.1.1#1dot1dot1dot1.cloudflare-dns.com
DNSOverTLS=Yes

restart service:

sudo systemctl restart systemd-resolved.service

verify again (see above)

change hostname

sudo hostnamectl set-hostname my-awesome-host

depending on your setup you might still need to update your /etc/hosts file as well

clear dns cache

resolvectl flush-caches

verify:

resolvectl statistics

logs: journalctl / systemd journals

read journalctl logs / syslog

the following flags can usually be combined, so -f -u <service> works as well.

follow all new logs (tail -f):

journalctl -f

display last 100 lines and follow new logs:

journalctl -n 100 -f

read logs from a specific service/unit:

journalctl -u your-name.service

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=1G

control how large individual journal files may grow at most:

SystemMaxFileSize=100M

and restart things

systemctl daemon-reload # for good measure
systemctl restart systemd-journald.service

log disk usage

check current disk usage of systemd journals:

journalctl --disk-usage

clean up some old logs:

journalctl --vacuum-size=100M

timers (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.target

create 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.target

there 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.timer

disable timer

systemctl disable name.timer

list timers

systemctl list-timers

remove / 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-failed

working with services

debugging: read and follow service logs output

read logs:

journalctl -u your-name.service

tail log:

journalctl -f -u your-name.service

disable / mask service, prevent from starting & unmask

using disable stops the service from starting automatically, but it can be started manually. mask prevents the service from being started at all by creating a symlink to /dev/null

list masked services:

systemctl list-unit-files | grep masked

disable a service from starting automatically:

it can be started manually, for example by NetworkManager

systemctl disable wpa_supplicant.service

prevent service from being started at all:

systemctl mask wpa_supplicant.service

the output will also tell you that the service has been linked to /dev/null:

Created symlink /etc/systemd/system/wpa_supplicant.service  /dev/null.

list active services

the service might be active, but exited already since it was just initializing or checking something

systemctl list-units --type=service --state=active

list running services

systemctl list-units --type=service --state=running