Introduction to Systemd¶
Systemd manages Linux system resources through units. A unit can represent a service, target, mount point, timer, socket, or another system resource.
1. Unit file directories¶
Unit files are usually stored in one of these directories, ordered by priority:
/etc/systemd/system: custom system or user configuration./run/systemd/system: runtime-generated configuration./usr/lib/systemd/system: unit files installed by the system or third-party software.
Systemd reads many default unit files from /usr/lib/systemd/system/, while /etc/systemd/system/ often contains custom files or symbolic links.
2. Unit and target¶
A Unit is the basic object managed by Systemd. A Target is a group of units, similar to a SysV-init runlevel.
Common service unit sections:
[Unit]: description and dependency metadata.[Service]: service lifecycle and runtime behavior.[Install]: how the unit is enabled and attached to a target.
Example service unit:
[Unit]
Description=Hello World
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill busybox1
ExecStartPre=-/usr/bin/docker rm busybox1
ExecStartPre=/usr/bin/docker pull busybox
ExecStart=/usr/bin/docker run --name busybox1 busybox /bin/sh -c
ExecStop="/usr/bin/docker stop busybox1"
ExecStopPost="/usr/bin/docker rm busybox1"
[Install]
WantedBy=multi-user.target
3. Common systemctl commands¶
systemctl list-units
systemctl list-units --all
systemctl list-units --failed
systemctl list-units --type=service
systemctl status bluetooth.service
sudo systemctl start apache.service
sudo systemctl stop apache.service
sudo systemctl restart apache.service
sudo systemctl daemon-reload
systemctl list-dependencies nginx.service
4. Target management¶
systemctl list-unit-files --type=target
systemctl list-dependencies multi-user.target
systemctl get-default
sudo systemctl set-default multi-user.target
sudo systemctl isolate multi-user.target
5. Logs¶
Systemd uses Journald for logs. Use journalctl to inspect system and service logs.
REF¶
[1]. https://cloud.tencent.com/developer/article/1516125
[2]. https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/chap-managing_services_with_systemd