Units
SystemD unit files in Linux define how each part of a system behaves. The most common type is the Services, which control programs and background processes. Targets, on the other hand, define system states, such as multi-user or graphical environments.
Mounts handle file systems and ensure disks and networks shares are attached and available.
Timers trigger services to to run at specific times or intervals.
Services
Services manage daemons that run in the background; The .service files control how programs start, stop, and behave under different conditions.
The service unit file typically contains three sections:
[Unit]
# These lines define dependencies for the service
Requires=
Wants=
# These determine the order in which services start
Before=
After=
[Service]
# Defines process behavior
Type=
# Specifies the start and stop command/method
ExecStart=
ExecStop=
# Defines the non-root user account that owns this. Omit if root.
User=
[Install]
# Defines when the service should be started, and its conditions.
WantedBy=
RequiredBy=For example, if we create a section under Install that reads WantedBy=multi-user.target, the service will be started automatically when the system reaches the multi-user.target during the Linux Boot Process.
Timers
Timers are used to schedule tasks or replace what traditional cron jobs do. In general, .timer files are paired with .service files, and tells SystemD when a specific service should be run.
Mounts
These allow you to define how file systems are mounted. For example, we might have a unit file that has the Mount directive:
[Unit]
After=local-fs.target
[Mount]
What=/dev/sdb1
Where=/mnt/usbdrive
Type=ext4
Options=defaultsThis means that the unit will be processed after the file system is started.
This type of systemD unit is particularly helpful when other services depend on having access to a file system.
Targets
Targets define system states by grouping Units together.
Management utils
systemctl interacts with systemd to start/stop/restart systemd units (along with mask, enable, etc.).
hostnamectl is used to manage the hostname of a server, among other things.
sysctl is used to modify kernel params at runtime. We can see all current values with sysctl -a. Alternatively, we can set new params with sysctl -w key=value (which will not persist after boot). We can also load multiple configs at once from a file with sysctl -p; for persisent settings, we can update /etc/sysctl.conf.
Config utils
systemd-resolved is the service responsible for DNS name resolution on many Linux systems. It manages DNS server, search domains, and DNS caching. By default, it works with NetworkManager, Netplan, and systemd-networkd. The service generally runs in the background and interacts with /etc/resolv.conf (which itself is a symbolic link pointing to a stub resolver).
resolvectl is used to interact with systemd-resolved, and is supposed to replace older tools like dig, host, and nslookup. For example, we can test name resolution with resolvectl query <hostname>.
timedatectl is used to view and manage time and timezones.
Diagnostics
systemd-analyze is used to see basic data on how long the boot process took. We can use the blame argument to have detailed information about startup times for individual services.