Linux: Systemd
Basics
systemd
controls how services start, stop, and interact with the system during boot and runtime.
Units
Systemd units defines how each part of the system behaves
- Services: controls programs and background processes like web servers or networks service
- Targets: define system states such as multi-user or graphical environments
- Mounts: handle file systems and ensures disks and network shares are properly attached and available
- Timers: Trigger services to run at specific times or intervals
System mount unit files are stored in:
/etc/systemd/system/
/usr/lib/systemd/system/
Services
Services manage daemons and applications that run in the background. The service file controls how programs start, stop, and behave under different conditions. A service unit file usually contains 3 sections:
- [Unit]:
Requires=
andWants=
define dependencies.Before=
andAfter=
determine the order in which services start - [Service]:
Type=
defines process behavior.ExecStart=
specifies the start command.ExecStop
defines how to stop the service.Users=
defines non-root execution account. If omitted, the service starts with the root user. - [Install]:
WantedBy
andRequiredBy=
defines startup target
example:
[Unit]
Description= Start web app
After=network.target
[Service]
Type=simple
User=appuser
ExecStart=/usr/local/bin/webapp.sh
ExecStop=/bin/kill $MAINPID
[Install]
WantedBy=multi-user.target
Use the following commands to manage the service
systemctl start webapp # to start the service
systemctl enable webapp # to start the service automatically at boot time
systemctl restart webapp # to restart the service
systemctl disable webapp # to disable the service at boot time
systemctl stop webapp # to stop
systemctl status webapp # to check the service status
journalctl -u webapp # to view the service logs
Targets
Targets defines system states by grouping units together. The common targets include:
poweroff.target
rescue.target
multi-user.target
: non-graphical multi-user systemgraphical.target
: a full GUI session that includes everything in themulti-suer.target
reboot.target
network-online.target
: used when services must wait for full network connectivityemergency.target
here are some useful commands:
systemctl get-default # to see the current target
systemctl set-default graphical.target # to set the current target
systemctl isolate graphical.target # to immediately switch a target
systemctl list-units --type=target # to list all targets
When using WantedBy=multi-user.target
, you are telling that service to start when the system reaches that target.
Mounts
Defines and automate how file systems are mounted using systemd.
example:
[Unit]
Description=Mount external drive
After=local-fs.target
[Mount]
What=/dev/sdb1 # what location to mount
Where=/mnt/drive # mount point
Type=ext4 # file system type
Options=defaults # mount options
[Install]
WantedBy=multi-user.target
Timers
They are used to schedule tasks to replace or enhance what traditional cron jobs do. They are usually paired with a service file and tells systemd when that service should be started. OnBootSect=
directive defiles a delay after system boot before the timer activates. OnCalendar=
allows calendar-style scheduling.
example:
[Unit]
Description=Run backup every day at 2AM
[Timer]
OnCalendar=*-*-* 02:00:00 # run at specific time
Persistent=true
[Install]
WantedBy=timers.target
Useful commands are:
systemctl list-timers # to list active timers
systemctl start work.timer # to start a timer
systemctl enable work.timer # to enable a timer at boot
systemctl stop work.timer # to stop a timer
systemctl status work.timer # to check the status of a time
Management Utilities
systemctl
: It is used to manage systemd units.hostnamectl
: manages the system's hostnamesysctl
: manages the system's kernel
example:
hostnamectl set-hostname host1 # to set a permanent hostname of the system
sysctl -a # to view all parameters
sysctl <NAME> # to view a selected parameter
systctl -w <NAME>=<VALUE> # set a parameter
systemctl edit <UNIT> # to edit a systemd unit without changing the original unit file. ex: systemctl edit nginx.service
systemctl daemon-reload # to reload a systemd unit and apply any new or changed configurations
Configuration Utilities
systemd-resolved
is a background service that manages DNS resolution and caching for the system.resolvectl
is a command line utility for interacting withsystemd-resolved
.timedatectl
allows managing the system clock, timezone, and NTP synchronization.
Useful commands include:
resolvectl status # to see the status of systemd-resolved service
resolvectl query <HOSTNAME> # to see which ip address a hostname resolve to
timedatectl status # to check the system's clock configuration
timedatectl set-timezone <REGION/CITY> # to set the timezone of the system. ex: timedatectl set-timezone America/Chicago
timedatectl set-time "YYYY-MM-DD HH:MM:SS" # to set the system time
timedatectl set-ntp <true or false> # to set whether NTP should be used in the system or not
Diagnosis and Analysis Tools
systemd-analyze
reports total boot time and breaks it down into key stages such as firmware, kernel, and user space. It is useful for troubleshooting slow boot time
useful command:
systemd-analyze # to see the system's boot time
systemd-analyze blame # to list services take the longest to start
systemd-analyze security # to see security analysis of services