Working with Systemd
Nov 10, 2025
Systemd units
-
Service units are used to start processes
-
Socket units monitor activity on a port and start the corresponding service unit when needed
-
Timer units are used to start services periodically
-
Path units can start service units when activity is detected in the file system
-
Mount units are used to mount file systems
-
Other unit types are available, though less relevant for RHCSA
- Service units are the most important for RHCSA
-
Service units are often used to start daemon processes (processes that run all the time)
-
Anything can run through systemd (Type=simple)
Frequently used commands:
systemctl list-units
systemctl list-unit-files
systemctl list-units --type=service --state=running
systemctl status {service}
systemctl restart {service}
systemctl enable {service}
systemctl disable {service}
systemctl daemon-reload
Modifying systemd unit configuration
/usr/lib/systemd/systemcontains system-provided unit files, these should never be directly modified/etc/systemd/systemcontains custom unit files/run/systemdcontains run-time auto-generated unit files- If
systemctl edit {unit}.serviceis used to edit a system-provided unit file, a drop-in (override) will automatically be created in/usr/lib/systemd/system systemctl showlists available parameters
Useful commands:
systemctl show
systemctl daemon-reload
man systemd.directives # this is over 20k lines long...
systemctl edit {unit}.service
Unit dependencies
systemctl list-dependencies {unit}
Add dependencies in the Unit section of service files. Example:
systemctl edit httpd
[Unit]
Requires=vsftpd.service
Now systemd will attempt to start vsftpd before httpd.
Systemd User Units
/usr/lib/systemd/user/where units provided by installed packages belong~/.local/share/systemd/user/where units of packages that have been installed in the home directory belong/etc/systemd/user/where system-wide user units are placed by the system administrator~/.config/systemd/user/where the user puts their own units
Masking services
- Some units cannot work simultaneously on the same system (for example, two different web servers listening on the same port)
systemctl masklinks a unit to the/dev/nulldevice, which ensures that it cannot be startedsystemctl unmaskremoves the mask
systemctl mask nginx.service
systemctl start nginx.service
Example: systemd can run anything
Example service
[Unit]
Description="sleepy service"
[Service]
Type=simple
ExecStart=/usr/bin/sleep inf
[Install]
WantedBy=multi-user.target
Lab Exercise
- Ensure the httpd service is automatically started
- Edit its configuration such that on failue, it will restart after 1 minute
- Ensure that an nginx service cannot be started on this machine
Solution
systemctl enable --now httpd- —
systemctl cat httpd` # to view current settings man systemctl.directives # to find restart parameter man systemctl.service # to find parameters under the service section systemctl edit httpd` # to add overrides[Service] Restart=on-failure RestartSec=60 systemctl mask nginx
- Clint Jordan