Working with Systemd

Nov 10, 2025

Wikipedia - systemd

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/system contains system-provided unit files, these should never be directly modified
  • /etc/systemd/system contains custom unit files
  • /run/systemd contains run-time auto-generated unit files
  • If systemctl edit {unit}.service is used to edit a system-provided unit file, a drop-in (override) will automatically be created in /usr/lib/systemd/system
  • systemctl show lists 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

Arch - Systemd/User

  • /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 mask links a unit to the /dev/null device, which ensures that it cannot be started
  • systemctl unmask removes 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

  1. Ensure the httpd service is automatically started
  2. Edit its configuration such that on failue, it will restart after 1 minute
  3. Ensure that an nginx service cannot be started on this machine
Solution
  1. systemctl enable --now httpd
  2. 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
  3. systemctl mask nginx

- Clint Jordan