Definition:

  • unit refers to a resource that systemd knows how to manage via daemons and configuration files
  • Default unit files stored at lib>systemd>system>
  • But config files (managed by sysadmin) are stored at etc>systemd>system>
    • settings in here override the default unit files
    • only change the necessary line in the section
  • Unit files include [sections] followed by directive=value statements.
    • Two common sections are [Unit] and [Install].
    • The [Unit] section typically manages the unit’s relationship to other units, including Wants= and Requires= directives.
    • The [Install] section specifies the results of enable and disable actions by the administrator.
      • This includes WantedBy= and RequiredBy= directives.
  • Multiple fields in a directive is separated by space, in order

[Unit] section directives:

  • Description=
  • Before= When multiple units are specified, this unit will start before any unit listed in this field
  • After= This unit will start after all listed
  • Requires= dependencies, if fail this unit fail
  • Wants= non-fatal dependencies

.service unit file:

  • Defines a Linux Service
  • Directives:
    • Type= Configures the startup type for the service. Values include simple, exec, forking, oneshot, dbus, notify, and idle.
    • User=Specifies the user under whose authority the service runs (usually root).
    • ExecStart=Executes commands along a specified absolute path upon startup to start a service
    • ExecStop=Executes commands along a specified absolute path upon shutdown to stop a service.

.timer unit file:

  • Manage time-based events, similar to Cron
  • Directives:
    • OnBootSec: (monotonic) Time spanning from a specific event, such as system startup.
      • displayed as DayOfWeek, Year-Month-Day, and as Hour:Minute:Second.
      • For example, a timer running daily at 3:00 a.m. displays as: OnCalendar=*-*-* 3:00:00
      • runs at 9:00 p.m. on the first Friday of the month: OnCalendar=Fri *-*-*1..7 21:00:00
    • OnCalendar: (realtime) time referenced on system clock

.mount unit file:

  • Mount for disk, similar to mount command but persistent
  • Mount points managed via systemd reference paths use a slightly different naming convention than ones managed via normal Linux paths. Where Linux paths contain forward slashes as delimiters, systemd .mount unit files use the dash character instead. A path that might normally read /mnt/projects/servers becomes mnt-projects-servers
  • Directives:
    • What= abs path of storage to mount
    • Where= abs path to mount point dir
    • Type= define the filesystem type (opt)
    • Options

.target unit file:

  • represent system startup configurations. These targets define what services start when the system starts, often by chaining multiple configuration files.
  • Directives:
    • default= The target to which the system boots by default
    • multi-user.target Starts the enable services and the system to the CLI.
    • graphical.target Starts the enable services and the system to the GUI.
    • network-online Starts the specified network services, and delays the target until network service is established.
  • Target can be show with systemctl command, systemctl get-default
  • The default.target unit file points to the selected target—either CLI (multi-user.target) or GUI (graphical.target). When the default startup option is changed, this file is modified.
    • To move the system to the graphical target, type: systemctl isolate graphical.target
    • To set the graphical target as the default, type: systemctl set-default graphical.target