Chapter 3. Managing services with systemd
3.1. Introduction to systemd
Systemd is a system and service manager for Linux operating systems. It is designed to be backwards compatible with SysV init scripts, and provides a number of features such as parallel startup of system services at boot time, on-demand activation of daemons, or dependency-based service control logic. Starting with Red Hat Enterprise Linux 7, systemd replaced Upstart as the default init system.
Systemd introduces the concept of systemd units. These units are represented by unit configuration files located in one of the directories listed in the following table.
Table 3.1. Systemd unit files locations
Directory | Description |
---|---|
| Systemd unit files distributed with installed RPM packages. |
| Systemd unit files created at run time. This directory takes precedence over the directory with installed service unit files. |
|
Systemd unit files created by |
The units encapsulate information about:
- System services
- Listening sockets
- Other objects that are relevant to the init system
For a complete list of available systemd unit types, see the following table.
Table 3.2. Available systemd unit types
Unit Type | File Extension | Description |
---|---|---|
Service unit |
| A system service. |
Target unit |
| A group of systemd units. |
Automount unit |
| A file system automount point. |
Device unit |
| A device file recognized by the kernel. |
Mount unit |
| A file system mount point. |
Path unit |
| A file or directory in a file system. |
Scope unit |
| An externally created process. |
Slice unit |
| A group of hierarchically organized units that manage system processes. |
Socket unit |
| An inter-process communication socket. |
Swap unit |
| A swap device or a swap file. |
Timer unit |
| A systemd timer. |
Overriding the default systemd configuration using system.conf
The default configuration of systemd is defined during the compilation and it can be found in the systemd configuration file at /etc/systemd/system.conf
. Use this file if you want to deviate from those defaults and override selected default values for systemd units globally.
For example, to override the default value of the timeout limit, which is set to 90 seconds, use the DefaultTimeoutStartSec
parameter to input the required value in seconds.
DefaultTimeoutStartSec=required value
For further information, see Example 3.11, “Changing the timeout limit”.
3.1.1. Main features
The systemd system and service manager provides the following main features:
Socket-based activation — At boot time, systemd creates listening sockets for all system services that support this type of activation, and passes the sockets to these services as soon as they are started. This not only allows systemd to start services in parallel, but also makes it possible to restart a service without losing any message sent to it while it is unavailable: the corresponding socket remains accessible and all messages are queued.
Systemd uses socket units for socket-based activation.
- Bus-based activation — System services that use D-Bus for inter-process communication can be started on-demand the first time a client application attempts to communicate with them. Systemd uses D-Bus service files for bus-based activation.
- Device-based activation — System services that support device-based activation can be started on-demand when a particular type of hardware is plugged in or becomes available. Systemd uses device units for device-based activation.
- Path-based activation — System services that support path-based activation can be started on-demand when a particular file or directory changes its state. Systemd uses path units for path-based activation.
- Mount and automount point management — Systemd monitors and manages mount and automount points. Systemd uses mount units for mount points and automount units for automount points.
- Aggressive parallelization — Because of the use of socket-based activation, systemd can start system services in parallel as soon as all listening sockets are in place. In combination with system services that support on-demand activation, parallel activation significantly reduces the time required to boot the system.
- Transactional unit activation logic — Before activating or deactivating a unit, systemd calculates its dependencies, creates a temporary transaction, and verifies that this transaction is consistent. If a transaction is inconsistent, systemd automatically attempts to correct it and remove non-essential jobs from it before reporting an error.
- Backwards compatibility with SysV init — Systemd supports SysV init scripts as described in the Linux Standard Base Core Specification, which eases the upgrade path to systemd service units.
3.1.2. Compatibility changes
The systemd system and service manager is designed to be mostly compatible with SysV init and Upstart. The following are the most notable compatibility changes with regards to Red Hat Enterprise Linux 6 system that used SysV init:
-
Systemd has only limited support for runlevels. It provides a number of target units that can be directly mapped to these runlevels and for compatibility reasons, it is also distributed with the earlier
runlevel
command. Not all systemd targets can be directly mapped to runlevels, however, and as a consequence, this command might returnN
to indicate an unknown runlevel. It is recommended that you avoid using therunlevel
command if possible.
For more information about systemd targets and their comparison with runlevels, see Section 3.3, “Working with systemd targets”. The
systemctl
utility does not support custom commands. In addition to standard commands such asstart
,stop
, andstatus
, authors of SysV init scripts could implement support for any number of arbitrary commands in order to provide additional functionality. For example, the init script foriptables
could be executed with thepanic
command, which immediately enabled panic mode and reconfigured the system to start dropping all incoming and outgoing packets. This is not supported in systemd and thesystemctl
only accepts documented commands.For more information about the
systemctl
utility and its comparison with the earlierservice
utility, see Table 3.3, “Comparison of the service utility with systemctl”.-
The
systemctl
utility does not communicate with services that have not been started by systemd. When systemd starts a system service, it stores the ID of its main process in order to keep track of it. Thesystemctl
utility then uses this PID to query and manage the service. Consequently, if a user starts a particular daemon directly on the command line,systemctl
is unable to determine its current status or stop it. -
Systemd stops only running services. Previously, when the shutdown sequence was initiated, Red Hat Enterprise Linux 6 and earlier releases of the system used symbolic links located in the
/etc/rc0.d/
directory to stop all available system services regardless of their status. With systemd , only running services are stopped on shutdown. -
System services are unable to read from the standard input stream. When systemd starts a service, it connects its standard input to
/dev/null
to prevent any interaction with the user. -
System services do not inherit any context (such as the
HOME
andPATH
environment variables) from the invoking user and their session. Each service runs in a clean execution context. - When loading a SysV init script, systemd reads dependency information encoded in the Linux Standard Base (LSB) header and interprets it at run time.
- All operations on service units are subject to a default timeout of 5 minutes to prevent a malfunctioning service from freezing the system. This value is hardcoded for services that are generated from initscripts and cannot be changed. However, individual configuration files can be used to specify a longer timeout value per service, see Example 3.11, “Changing the timeout limit”.
For a detailed list of compatibility changes introduced with systemd, see the Migration Planning Guide for Red Hat Enterprise Linux 7.
3.2. Managing system services
Previous versions of Red Hat Enterprise Linux, which were distributed with SysV init or Upstart, used init scripts located in the /etc/rc.d/init.d/
directory. These init scripts were typically written in Bash, and allowed the system administrator to control the state of services and daemons in their system. Starting with Red Hat Enterprise Linux 7, these init scripts have been replaced with service units.
Service units end with the .service
file extension and serve a similar purpose as init scripts. To view, start, stop, restart, enable, or disable system services, use the systemctl
command as described in Comparison of the service utility with systemctl, Comparison of the chkconfig utility with systemctl, and further in this section. The service
and chkconfig
commands are still available in the system and work as expected, but are only included for compatibility reasons and should be avoided.
Table 3.3. Comparison of the service utility with systemctl
service | systemctl | Description |
---|---|---|
|
| Starts a service. |
|
| Stops a service. |
|
| Restarts a service. |
|
| Restarts a service only if it is running. |
|
| Reloads configuration. |
|
| Checks if a service is running. |
|
| Displays the status of all services. |
Table 3.4. Comparison of the chkconfig utility with systemctl
chkconfig | systemctl | Description |
---|---|---|
|
| Enables a service. |
|
| Disables a service. |
|
| Checks if a service is enabled. |
|
| Lists all services and checks if they are enabled. |
|
| Lists services that are ordered to start before the specified unit. |
|
| Lists services that are ordered to start after the specified unit. |
Specifying service units
For clarity, all command examples in the rest of this section use full unit names with the .service
file extension, for example:
# systemctl stop nfs-server.service
However, the file extension can be omitted, in which case the systemctl
utility assumes the argument is a service unit. The following command is equivalent to the one above:
# systemctl stop nfs-server
Additionally, some units have alias names. Those names can have shorter names than units, which can be used instead of the actual unit names. To find all aliases that can be used for a particular unit, use:
# systemctl show nfs-server.service -p Names
Behavior of systemctl in a chroot environment
If you change the root directory using the chroot
command, most systemctl
commands refuse to perform any action. The reason for this is that the systemd
process and the user that used the chroot
command do not have the same view of the filesystem. This happens, for example, when systemctl
is invoked from a kickstart
file.
The exception to this are unit file commands such as the systemctl enable
and systemctl disable
commands. These commands do not need a running system and do not affect running processes, but they do affect unit files. Therefore, you can run these commands even in chroot
environment. For example, to enable the httpd
service on a system under the /srv/website1/
directory:
# chroot /srv/website1 # systemctl enable httpd.service Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service, pointing to /usr/lib/systemd/system/httpd.service.
3.2.1. Listing services
To list all currently loaded service units, type the following at a shell prompt:
systemctl list-units --type service
For each service unit file, this command displays its full name (UNIT
) followed by a note whether the unit file has been loaded (LOAD
), its high-level (ACTIVE
) and low-level (SUB
) unit file activation state, and a short description (DESCRIPTION
).
By default, the systemctl list-units
command displays only active units. If you want to list all loaded units regardless of their state, run this command with the --all
or -a
command line option:
systemctl list-units --type service --all
You can also list all available service units to see if they are enabled. To do so, type:
systemctl list-unit-files --type service
For each service unit, this command displays its full name (UNIT FILE
) followed by information whether the service unit is enabled or not (STATE
). For information on how to determine the status of individual service units, see Displaying service status.
Example 3.1. Listing services
To list all currently loaded service units, run the following command:
$ systemctl list-units --type service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded active exited Harvest vmcores for ABRT
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
…
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tog-pegasus.service loaded active running OpenPegasus CIM Server
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
46 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'
To list all installed service unit files to determine if they are enabled, type:
$ systemctl list-unit-files --type service UNIT FILE STATE abrt-ccpp.service enabled abrt-oops.service enabled abrt-vmcore.service enabled abrt-xorg.service enabled abrtd.service enabled … wpa_supplicant.service disabled ypbind.service disabled 208 unit files listed.
3.2.2. Displaying service status
To display detailed information about a service unit that corresponds to a system service, type the following at a shell prompt:
systemctl status name.service
Replace name with the name of the service unit you want to inspect (for example, gdm
). This command displays the name of the selected service unit followed by its short description, one or more fields described in Table 3.5, “Available service unit information”, and if it is executed by the root
user, also the most recent log entries.
Table 3.5. Available service unit information
Field | Description |
---|---|
| Information whether the service unit has been loaded, the absolute path to the unit file, and a note whether the unit is enabled. |
| Information whether the service unit is running followed by a time stamp. |
| The PID of the corresponding system service followed by its name. |
| Additional information about the corresponding system service. |
| Additional information about related processes. |
| Additional information about related Control Groups (cgroups). |
To only verify that a particular service unit is running, run the following command:
systemctl is-active name.service
Similarly, to determine whether a particular service unit is enabled, type:
systemctl is-enabled name.service
Note that both systemctl is-active
and systemctl is-enabled
return an exit status of 0
if the specified service unit is running or enabled. For information on how to list all currently loaded service units, see Listing services.
Example 3.2. Displaying service status
The service unit for the GNOME Display Manager is named gdm.service
. To determine the current status of this service unit, type the following at a shell prompt:
# systemctl status gdm.service gdm.service - GNOME Display Manager Loaded: loaded (/usr/lib/systemd/system/gdm.service; enabled) Active: active (running) since Thu 2013-10-17 17:31:23 CEST; 5min ago Main PID: 1029 (gdm) CGroup: /system.slice/gdm.service ├─1029 /usr/sbin/gdm ├─1037 /usr/libexec/gdm-simple-slave --display-id /org/gno… └─1047 /usr/bin/Xorg :0 -background none -verbose -auth /r… Oct 17 17:31:23 localhost systemd[1]: Started GNOME Display Manager.
Example 3.3. Displaying services ordered to start before a service
To determine what services are ordered to start before the specified service, type the following at a shell prompt:
# systemctl list-dependencies --after gdm.service gdm.service ├─dbus.socket ├─getty@tty1.service ├─livesys.service ├─plymouth-quit.service ├─system.slice ├─systemd-journald.socket ├─systemd-user-sessions.service └─basic.target [output truncated]
Example 3.4. Displaying services ordered to start after a service
To determine what services are ordered to start after the specified service, type the following at a shell prompt:
# systemctl list-dependencies --before gdm.service gdm.service ├─dracut-shutdown.service ├─graphical.target │ ├─systemd-readahead-done.service │ ├─systemd-readahead-done.timer │ └─systemd-update-utmp-runlevel.service └─shutdown.target ├─systemd-reboot.service └─final.target └─systemd-reboot.service
3.2.3. Starting a service
To start a service unit that corresponds to a system service, type the following at a shell prompt as root
:
systemctl start name.service
Replace name with the name of the service unit you want to start (for example, gdm
). This command starts the selected service unit in the current session. For information on how to enable a service unit to be started at boot time, see Enabling a service. For information on how to determine the status of a certain service unit, see Displaying service status.
Example 3.5. Starting a service
The service unit for the Apache HTTP Server is named httpd.service
. To activate this service unit and start the httpd
daemon in the current session, run the following command as root
:
# systemctl start httpd.service
3.2.4. Stopping a service
To stop a service unit that corresponds to a system service, type the following at a shell prompt as root
:
systemctl stop name.service
Replace name with the name of the service unit you want to stop (for example, bluetooth
). This command stops the selected service unit in the current session. For information on how to disable a service unit and prevent it from being started at boot time, see Disabling a service. For information on how to determine the status of a certain service unit, see Displaying service status.
Example 3.6. Stopping a service
The service unit for the bluetoothd
daemon is named bluetooth.service
. To deactivate this service unit and stop the bluetoothd
daemon in the current session, run the following command as root
:
# systemctl stop bluetooth.service
3.2.5. Restarting a service
To restart a service unit that corresponds to a system service, type the following at a shell prompt as root
:
systemctl restart name.service
Replace name with the name of the service unit you want to restart (for example, httpd
). This command stops the selected service unit in the current session and immediately starts it again. Importantly, if the selected service unit is not running, this command starts it too. To tell systemd to restart a service unit only if the corresponding service is already running, run the following command as root
:
systemctl try-restart name.service
Certain system services also allow you to reload their configuration without interrupting their execution. To do so, type as root
:
systemctl reload name.service
Note that system services that do not support this feature ignore this command altogether. For convenience, the systemctl
command also supports the reload-or-restart
and reload-or-try-restart
commands that restart such services instead. For information on how to determine the status of a certain service unit, see Displaying service status.
Example 3.7. Restarting a service
In order to prevent users from encountering unnecessary error messages or partially rendered web pages, the Apache HTTP Server allows you to edit and reload its configuration without the need to restart it and interrupt actively processed requests. To do so, type the following at a shell prompt as root
:
# systemctl reload httpd.service
3.2.6. Enabling a service
To configure a service unit that corresponds to a system service to be automatically started at boot time, type the following at a shell prompt as root
:
systemctl enable name.service
Replace name with the name of the service unit you want to enable (for example, httpd
). This command reads the [Install]
section of the selected service unit and creates appropriate symbolic links to the /usr/lib/systemd/system/name.service
file in the /etc/systemd/system/
directory and its subdirectories. This command does not, however, rewrite links that already exist. If you want to ensure that the symbolic links are re-created, use the following command as root
:
systemctl reenable name.service
This command disables the selected service unit and immediately enables it again. For information on how to determine whether a certain service unit is enabled to start at boot time, see Displaying service status. For information on how to start a service in the current session, see Starting a service.
Example 3.8. Enabling a service
To configure the Apache HTTP Server to start automatically at boot time, run the following command as root
:
# systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
3.2.7. Disabling a service
To prevent a service unit that corresponds to a system service from being automatically started at boot time, type the following at a shell prompt as root
:
systemctl disable name.service
Replace name with the name of the service unit you want to disable (for example, bluetooth
). This command reads the [Install]
section of the selected service unit and removes appropriate symbolic links to the /usr/lib/systemd/system/name.service
file from the /etc/systemd/system/
directory and its subdirectories. In addition, you can mask any service unit to prevent it from being started manually or by another service. To do so, run the following command as root
:
systemctl mask name.service
This command replaces the /etc/systemd/system/name.service
file with a symbolic link to /dev/null
, rendering the actual unit file inaccessible to systemd. To revert this action and unmask a service unit, type as root
:
systemctl unmask name.service
For information on how to determine whether a certain service unit is enabled to start at boot time, see Displaying service status. For information on how to stop a service in the current session, see Stopping a service.
Example 3.9. Disabling a service
Example 3.6, “Stopping a service” illustrates how to stop the bluetooth.service
unit in the current session. To prevent this service unit from starting at boot time, type the following at a shell prompt as root
:
# systemctl disable bluetooth.service Removed symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service. Removed symlink /etc/systemd/system/dbus-org.bluez.service.
3.2.8. Starting a conflicting service
In systemd, positive and negative dependencies between services exist. Starting particular service may require starting one or more other services (positive dependency) or stopping one or more services (negative dependency).
When you attempt to start a new service, systemd resolves all dependencies automatically. Note that this is done without explicit notification to the user. If you are already running a service, and you attempt to start another service with a negative dependency, the first service is automatically stopped.
For example, if you are running the postfix
service, and you try to start the sendmail
service, systemd first automatically stops postfix
, because these two services are conflicting and cannot run on the same port.
3.3. Working with systemd targets
Systemd targets are represented by target units. Target units file ends with the .target
file extension and their only purpose is to group together other systemd units through a chain of dependencies. For example, the graphical.target unit
, which is used to start a graphical session, starts system services such as the GNOME Display Manager (gdm.service)
or Accounts Service (accounts-daemon.service)
and also activates the multi-user.target unit
. Similarly, the multi-user.target unit starts other essential system services such as NetworkManager (NetworkManager.service)
or D-Bus (dbus.service)
and activates another target unit named basic.target.
This section includes procedures to implement while working with systemd
targets.
3.3.1. Difference between SysV runlevels and systemd targets
The previous versions of Red Hat Enterprise Linux were distributed with SysV init or Upstart, and implemented a predefined set of runlevels that represented specific modes of operation. These runlevels were numbered from 0 to 6 and were defined by a selection of system services to be run when a particular runlevel was enabled by the system administrator. Starting with Red Hat Enterprise Linux 7, the concept of runlevels has been replaced with systemd targets.
Red Hat Enterprise Linux 7 was distributed with a number of predefined targets that are more or less similar to the standard set of runlevels from the previous releases. For compatibility reasons, it also provides aliases for these targets that directly maps to the SysV runlevels.
The following table provides a complete list of SysV runlevels and their corresponding systemd targets:
Table 3.6. Comparison of SysV runlevels with systemd targets
Runlevel | Target Units | Description |
---|---|---|
|
| Shut down and power off the system. |
|
| Set up a rescue shell. |
|
| Set up a non-graphical multi-user system. |
|
| Set up a non-graphical multi-user system. |
|
| Set up a non-graphical multi-user system. |
|
| Set up a graphical multi-user system. |
|
| Shut down and reboot the system. |
The following table compares the SysV init commands with systemctl. Use the systemctl utility to view, change, or configure systemd targets:
The runlevel
and telinit
commands are still available in the system and work as expected, but are only included for compatibility reasons and should be avoided.
Table 3.7. Comparison of SysV init commands with systemctl
Old Command | New Command | Description |
---|---|---|
|
| Lists currently loaded target units. |
|
| Changes the current target. |
Additional resources
- man sysv init
- man upstart init
- man systemctl
3.3.2. Viewing the default target
The default target unit is represented by the /etc/systemd/system/default.target
file.
Procedure
To determine which target unit is used by default:
$ systemctl get-default graphical.target
To determine the default target using the symbolic link:
$ ls -l /lib/systemd/system/default.target
3.3.3. Viewing the target units
By default, the systemctl list-units
command displays only active units.
Procedure
To list all loaded units regardless of their state:
$ systemctl list-units --type target --all
To list all currently loaded target units:
$ systemctl list-units --type target UNIT LOAD ACTIVE SUB DESCRIPTION basic.target loaded active active Basic System cryptsetup.target loaded active active Encrypted Volumes getty.target loaded active active Login Prompts graphical.target loaded active active Graphical Interface local-fs-pre.target loaded active active Local File Systems (Pre) local-fs.target loaded active active Local File Systems multi-user.target loaded active active Multi-User System network.target loaded active active Network paths.target loaded active active Paths remote-fs.target loaded active active Remote File Systems sockets.target loaded active active Sockets sound.target loaded active active Sound Card spice-vdagentd.target loaded active active Agent daemon for Spice guests swap.target loaded active active Swap sysinit.target loaded active active System Initialization time-sync.target loaded active active System Time Synchronized timers.target loaded active active Timers LOAD = Reflects whether the unit definition was properly loaded. ACTIVE = The high-level unit activation state, i.e. generalization of SUB. SUB = The low-level unit activation state, values depend on unit type. 17 loaded units listed.
3.3.4. Changing the default target
The default target unit is represented by the /etc/systemd/system/default.target
file. The following procedure describes how to change the default target by using the systemctl command:
Procedure
To determine the default target unit:
# systemctl get-default
To configure the system to use a different target unit by default:
# systemctl set-default multi-user.target rm /etc/systemd/system/default.target ln -s /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target
This command replaces the
/etc/systemd/system/default.target
file with a symbolic link to/usr/lib/systemd/system/name.target
, where name is the name of the target unit you want to use. Replace multi-user with the name of the target unit you want to use by default.Reboot
# reboot
3.3.5. Changing the default target using symbolic link
The following procedure describes how to change the default target by creating a symbolic link to the target.
Procedure
To determine the default target unit:
# ls /lib/systemd/system/default.target -l
To create a symbolic link:
# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target
Reboot the system:
# reboot
Verification steps
Verify the newly created default.target:
$ systemctl get-default multi-user.target
3.3.6. Changing the current target
This procedure explains how to change the target unit in the current session using the systemctl command.
Procedure
To change to a different target unit in the current session:
# systemctl isolate multi-user.target
This command starts the target unit named multi-user and all dependent units, and immediately stops all others.
Replace multi-user with the name of the target unit you want to use by default.
Verification steps
Verify the newly created default.target:
$ systemctl get-default multi-user.target
3.3.7. Booting to rescue mode
Rescue mode provides a convenient single-user environment and allows you to repair your system in situations when it is unable to complete a regular booting process. In rescue mode, the system attempts to mount all local file systems and start some important system services, but it does not activate network interfaces or allow more users to be logged into the system at the same time.
Procedure
To change the current target and enter rescue mode in the current session:
# systemctl rescue Broadcast message from root@localhost on pts/0 (Fri 2013-10-25 18:23:15 CEST): The system is going down to rescue mode NOW!
This command is similar to
systemctl isolate rescue.target
, but it also sends an informative message to all users that are currently logged into the system.To prevent
systemd
from sending a message, run the following command with the--no-wall
command-line option:# systemctl --no-wall rescue
3.3.8. Booting to emergency mode
Emergency mode provides the most minimal environment possible and allows you to repair your system even in situations when the system is unable to enter rescue mode. In emergency mode, the system mounts the root file system only for reading, does not attempt to mount any other local file systems, does not activate network interfaces, and only starts a few essential services.
Procedure
To change the current target and enter emergency mode:
# systemctl emergency
This command is similar to systemctl isolate emergency.target
, but it also sends an informative message to all users that are currently logged into the system.
To prevent systemd from sending this message, run the following command with the --no-wall
command-line option: # systemctl --no-wall emergency
3.4. Shutting down, suspending, and hibernating the system
In Red Hat Enterprise Linux 7, the systemctl
utility replaced a number of power management commands used in previous versions of Red Hat Enterprise Linux. The commands listed in Table 3.8, “Comparison of power management commands with systemctl” are still available in the system for compatibility reasons, but it is advised that you use systemctl
when possible.
Table 3.8. Comparison of power management commands with systemctl
Old Command | New Command | Description |
---|---|---|
|
| Halts the system. |
|
| Powers off the system. |
|
| Restarts the system. |
|
| Suspends the system. |
|
| Hibernates the system. |
|
| Hibernates and suspends the system. |
3.4.1. Shutting down the system
The systemctl
utility provides commands for shutting down the system, however the traditional shutdown
command is also supported. Although the shutdown
command will call the systemctl
utility to perform the shutdown, it has an advantage in that it also supports a time argument. This is particularly useful for scheduled maintenance and to allow more time for users to react to the warning that a system shutdown has been scheduled. The option to cancel the shutdown can also be an advantage.
Using systemctl commands
To shut down the system and power off the machine, type the following at a shell prompt as root
:
systemctl poweroff
To shut down and halt the system without powering off the machine, run the following command as root
:
systemctl halt
By default, running either of these commands causes systemd to send an informative message to all users that are currently logged into the system. To prevent systemd from sending this message, run the selected command with the --no-wall
command line option, for example:
systemctl --no-wall poweroff
Using the shutdown command
To shut down the system and power off the machine at a certain time, use a command in the following format as root
:
shutdown --poweroff hh:mm
Where hh:mm is the time in 24 hour clock format. The /run/nologin
file is created 5 minutes before system shutdown to prevent new logins. When a time argument is used, an optional message, the wall message, can be appended to the command.
To shut down and halt the system after a delay, without powering off the machine, use a command in the following format as root
:
shutdown --halt +m
Where +m is the delay time in minutes. The now
keyword is an alias for +0
.
A pending shutdown can be canceled by the root
user as follows:
shutdown -c
See the shutdown(8)
manual page for further command options.
3.4.2. Restarting the system
To restart the system, run the following command as root
:
systemctl reboot
By default, this command causes systemd to send an informative message to all users that are currently logged into the system. To prevent systemd from sending this message, run this command with the --no-wall
command line option:
systemctl --no-wall reboot
3.4.3. Suspending the system
To suspend the system, type the following at a shell prompt as root
:
systemctl suspend
This command saves the system state in RAM and with the exception of the RAM module, powers off most of the devices in the machine. When you turn the machine back on, the system then restores its state from RAM without having to boot again. Because the system state is saved in RAM and not on the hard disk, restoring the system from suspend mode is significantly faster than restoring it from hibernation, but as a consequence, a suspended system state is also vulnerable to power outages.
For information on how to hibernate the system, see Section 3.4.4, “Hibernating the system”.
3.4.4. Hibernating the system
To hibernate the system, type the following at a shell prompt as root
:
systemctl hibernate
This command saves the system state on the hard disk drive and powers off the machine. When you turn the machine back on, the system then restores its state from the saved data without having to boot again. Because the system state is saved on the hard disk and not in RAM, the machine does not have to maintain electrical power to the RAM module, but as a consequence, restoring the system from hibernation is significantly slower than restoring it from suspend mode.
To hibernate and suspend the system, run the following command as root
:
systemctl hybrid-sleep
For information on how to suspend the system, see Section 3.4.3, “Suspending the system”.
3.5. Working with systemd unit files
This chapter includes the description of systemd unit files. The following sections show you how to:
- Create custom unit files
- Convert SysV init scripts to unit files
- Modify existing unit files
- Work with instantiated units
3.5.1. Introduction to unit files
A unit file contains configuration directives that describe the unit and define its behavior. Several systemctl
commands work with unit files in the background. To make finer adjustments, system administrator must edit or create unit files manually. Table 3.1, “Systemd unit files locations” lists three main directories where unit files are stored on the system, the /etc/systemd/system/
directory is reserved for unit files created or customized by the system administrator.
Unit file names take the following form:
unit_name.type_extension
Here, unit_name stands for the name of the unit and type_extension identifies the unit type, see Table 3.2, “Available systemd unit types” for a complete list of unit types. For example, there usually is sshd.service
as well as sshd.socket
unit present on your system.
Unit files can be supplemented with a directory for additional configuration files. For example, to add custom configuration options to sshd.service
, create the sshd.service.d/custom.conf
file and insert additional directives there. For more information on configuration directories, see Modifying existing unit files.
Also, the sshd.service.wants/
and sshd.service.requires/
directories can be created. These directories contain symbolic links to unit files that are dependencies of the sshd
service. The symbolic links are automatically created either during installation according to [Install] unit file options or at runtime based on [Unit] options. It is also possible to create these directories and symbolic links manually. For more details on [Install] and [Unit] options, see the tables below.
Many unit file options can be set using the so called unit specifiers – wildcard strings that are dynamically replaced with unit parameters when the unit file is loaded. This enables creation of generic unit files that serve as templates for generating instantiated units. See Working with instantiated units for details.
3.5.2. Unit file structure
Unit files typically consist of three sections:
-
The
[Unit]
section — contains generic options that are not dependent on the type of the unit. These options provide unit description, specify the unit’s behavior, and set dependencies to other units. For a list of most frequently used [Unit] options, see Table 3.9, “Important [Unit] section options”. -
The
[Unit type]
section — if a unit has type-specific directives, these are grouped under a section named after the unit type. For example, service unit files contain the[Service]
section. -
The
[Install]
section — contains information about unit installation used bysystemctl enable
anddisable
commands. For a list of options for the[Install]
section, see Table 3.11, “Important [Install] section options”.
3.5.2.1. Important [Unit] section options
The following tables lists important options of the [Unit] section.
Table 3.9. Important [Unit] section options
Option[a]] section, see the systemd.unit(5) manual page.] | Description |
---|---|
|
A meaningful description of the unit. This text is displayed for example in the output of the |
| Provides a list of URIs referencing documentation for the unit. |
|
Defines the order in which units are started. The unit starts only after the units specified in |
|
Configures dependencies on other units. The units listed in |
|
Configures weaker dependencies than |
|
Configures negative dependencies, an opposite to |
[a]
For a complete list of options configurable in the [[Unit
[b]
In most cases, it is sufficient to set only the ordering dependencies with After and Before unit file options. If you also set a requirement dependency with Wants (recommended) or Requires , the ordering dependency still needs to be specified. That is because ordering and requirement dependencies work independently from each other.
|
3.5.2.2. Important [Service] section options
The following tables lists important options of the [Service] section.
Table 3.10. Important [Service] section options
Option[a]] section, see the systemd.service(5) manual page.] | Description |
---|---|
|
Configures the unit process startup type that affects the functionality of
*
*
*
*
*
* |
|
Specifies commands or scripts to be executed when the unit is started. |
| Specifies commands or scripts to be executed when the unit is stopped. |
| Specifies commands or scripts to be executed when the unit is reloaded. |
|
With this option enabled, the service is restarted after its process exits, with the exception of a clean stop by the |
|
If set to True, the service is considered active even when all its processes exited. Default value is False. This option is especially useful if |
[a]
For a complete list of options configurable in the [[Service
|
3.5.2.3. Important [Install] section options
The following tables lists important options of the [Install] section.
Table 3.11. Important [Install] section options
Option[a]] section, see the systemd.unit(5) manual page.] | Description |
---|---|
|
Provides a space-separated list of additional names for the unit. Most |
|
A list of units that depend on the unit. When this unit is enabled, the units listed in |
|
A list of units that weakly depend on the unit. When this unit is enabled, the units listed in |
| Specifies a list of units to be installed or uninstalled along with the unit. |
| Limited to instantiated units, this option specifies the default instance for which the unit is enabled. See Working with instantiated units |
[a]
For a complete list of options configurable in the [[Install
|
3.5.3. Creating custom unit files
There are several use cases for creating unit files from scratch: you could run a custom daemon, create a second instance of some existing service (as in Creating a second instance of the sshd service), or import a SysV init script (more in Converting SysV init scripts to unit files). On the other hand, if you intend just to modify or extend the behavior of an existing unit, use the instructions from Modifying existing unit files. The following procedure describes the general process of creating a custom service.
Procedure
-
Prepare the executable file with the custom service. This can be a custom-created script, or an executable delivered by a software provider. If required, prepare a PID file to hold a constant PID for the main process of the custom service. It is also possible to include environment files to store shell variables for the service. Make sure the source script is executable (by executing the
chmod a+x
) and is not interactive. Create a unit file in the
/etc/systemd/system/
directory and make sure it has correct file permissions. Execute asroot
:touch /etc/systemd/system/name.service
chmod 664 /etc/systemd/system/name.service
Replace name with a name of the service to be created. Note that file does not need to be executable.
Open the
name.service
file created in the previous step, and add the service configuration options. There is a variety of options that can be used depending on the type of service you wish to create, see Unit file structure. The following is an example unit configuration for a network-related service:[Unit] Description=service_description After=network.target [Service] ExecStart=path_to_executable Type=forking PIDFile=path_to_pidfile [Install] WantedBy=default.target
Where:
-
service_description is an informative description that is displayed in journal log files and in the output of the
systemctl status
command. -
the
After
setting ensures that the service is started only after the network is running. Add a space-separated list of other relevant services or targets. - path_to_executable stands for the path to the actual service executable.
-
Type=forking
is used for daemons that make the fork system call. The main process of the service is created with the PID specified in path_to_pidfile. Find other startup types in Table 3.10, “Important [Service] section options”. -
WantedBy
states the target or targets that the service should be started under. Think of these targets as of a replacement of the older concept of runlevels.
-
service_description is an informative description that is displayed in journal log files and in the output of the
Notify systemd that a new
name.service
file exists by executing the following command asroot
:systemctl daemon-reload
systemctl start name.service
WarningAlways run the
systemctl daemon-reload
command after creating new unit files or modifying existing unit files. Otherwise, thesystemctl start
orsystemctl enable
commands could fail due to a mismatch between states of systemd and actual service unit files on disk. Note, that on systems with a large number of units this can take a long time, as the state of each unit has to be serialized and subsequently deserialized during the reload.
3.5.3.1. Creating a custom unit file by using the second instance of the sshd service
System Administrators often need to configure and run multiple instances of a service. This is done by creating copies of the original service configuration files and modifying certain parameters to avoid conflicts with the primary instance of the service. The following procedure shows how to create a second instance of the sshd
service.
Procedure
Create a copy of the
sshd_config
file that will be used by the second daemon:# cp /etc/ssh/sshd{,-second}_config
Edit the
sshd-second_config
file created in the previous step to assign a different port number and PID file to the second daemon:Port 22220 PidFile /var/run/sshd-second.pid
See the
sshd_config
(5) manual page for more information onPort
andPidFile
options. Make sure the port you choose is not in use by any other service. The PID file does not have to exist before running the service, it is generated automatically on service start.Create a copy of the systemd unit file for the
sshd
service:# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/sshd-second.service
Alter the
sshd-second.service
created in the previous step as follows:Modify the
Description
option:Description=OpenSSH server second instance daemon
Add sshd.service to services specified in the
After
option, so that the second instance starts only after the first one has already started:After=syslog.target network.target auditd.service sshd.service
- The first instance of sshd includes key generation, therefore remove the ExecStartPre=/usr/sbin/sshd-keygen line.
Add the
-f /etc/ssh/sshd-second_config
parameter to thesshd
command, so that the alternative configuration file is used:ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd-second_config $OPTIONS
After the above modifications, the sshd-second.service should look as follows:
[Unit] Description=OpenSSH server second instance daemon After=syslog.target network.target auditd.service sshd.service [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd-second_config $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
If using SELinux, add the port for the second instance of sshd to SSH ports, otherwise the second instance of sshd will be rejected to bind to the port:
# semanage port -a -t ssh_port_t -p tcp 22220
Enable sshd-second.service, so that it starts automatically upon boot:
# systemctl enable sshd-second.service
-
Verify if the sshd-second.service is running by using the
systemctl status
command. Verify if the port is enabled correctly by connecting to the service:
$
ssh -p 22220 user@server
If the firewall is in use, make sure that it is configured appropriately in order to allow connections to the second instance of sshd.
3.5.3.2. Choosing a target for ordering and dependencies of custom unit files
To learn how to properly choose a target for ordering and dependencies of your custom unit files, see the following articles:
Additional information with some real-world examples of cases triggered by the ordering and dependencies in a unit file is available in Red Hat Knowledgebase article Is there any useful information about writing unit files?
If you want to set limits for services started by systemd
, see the Red Hat Knowledgebase article How to set limits for services in RHEL 7 and systemd. These limits need to be set in the service’s unit file. Note that systemd
ignores limits set in the /etc/security/limits.conf
and /etc/security/limits.d/*.conf
configuration files. The limits defined in these files are set by PAM when starting a login session, but daemons started by systemd
do not use PAM login sessions.
3.5.4. Converting SysV init scripts to unit files
Before taking time to convert a SysV init script to a unit file, make sure that the conversion was not already done elsewhere. All core services installed on Red Hat Enterprise Linux come with default unit files, and the same applies for many third-party software packages.
Converting an init script to a unit file requires analyzing the script and extracting the necessary information from it. Based on this data you can create a unit file. As init scripts can vary greatly depending on the type of the service, you might need to employ more configuration options for translation than outlined in this chapter. Note that some levels of customization that were available with init scripts are no longer supported by systemd units.
The majority of information needed for conversion is provided in the script’s header. The following example shows the opening section of the init script used to start the postfix
service on Red Hat Enterprise Linux 6:
!/bin/bash # postfix Postfix Mail Transfer Agent # chkconfig: 2345 80 30 # description: Postfix is a Mail Transport Agent, which is the program that moves mail from one machine to another. # processname: master # pidfile: /var/spool/postfix/pid/master.pid # config: /etc/postfix/main.cf # config: /etc/postfix/master.cf BEGIN INIT INFO # Provides: postfix MTA # Required-Start: $local_fs $network $remote_fs # Required-Stop: $local_fs $network $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: start and stop postfix # Description: Postfix is a Mail Transport Agent, which is the program that moves mail from one machine to another. # END INIT INFO
In the above example, only lines starting with # chkconfig and # description are mandatory, so you might not find the rest in different init files. The text enclosed between the BEGIN INIT INFO and END INIT INFO lines is called Linux Standard Base (LSB) header. If specified, LSB headers contain directives defining the service description, dependencies, and default runlevels. What follows is an overview of analytic tasks aiming to collect the data needed for a new unit file. The postfix init script is used as an example.
3.5.4.1. Finding the systemd service description
You can find descriptive information about the script on the line starting with #description. Use this description together with the service name in the Description
option in the [Unit] section of the unit file. The LSB header might contain similar data on the #Short-Description and #Description lines.
3.5.4.2. Finding the systemd service dependencies
The LSB header might contain several directives that form dependencies between services. Most of them are translatable to systemd unit options, see Table 3.12, “Dependency options from the LSB header”
Table 3.12. Dependency options from the LSB header
LSB Option | Description | Unit File Equivalent |
---|---|---|
| Specifies the boot facility name of the service, that can be referenced in other init scripts (with the "$" prefix). This is no longer needed as unit files refer to other units by their file names. | – |
|
Contains boot facility names of required services. This is translated as an ordering dependency, boot facility names are replaced with unit file names of corresponding services or targets they belong to. For example, in case of |
|
| Constitutes weaker dependencies than Required-Start. Failed Should-Start dependencies do not affect the service startup. |
|
| Constitute negative dependencies. |
|
3.5.4.3. Finding default targets of the service
The line starting with #chkconfig contains three numerical values. The most important is the first number that represents the default runlevels in which the service is started. Map these runlevels to equivalent systemd targets. Then list these targets in the WantedBy
option in the [Install] section of the unit file. For example, postfix
was previously started in runlevels 2, 3, 4, and 5, which translates to multi-user.target and graphical.target. Note that the graphical.target depends on multiuser.target, therefore it is not necessary to specify both. You might find information on default and forbidden runlevels also at #Default-Start and #Default-Stop lines in the LSB header.
The other two values specified on the #chkconfig line represent startup and shutdown priorities of the init script. These values are interpreted by systemd if it loads the init script, but there is no unit file equivalent.
3.5.4.4. Finding files used by the service
Init scripts require loading a function library from a dedicated directory and allow importing configuration, environment, and PID files. Environment variables are specified on the line starting with #config in the init script header, which translates to the EnvironmentFile
unit file option. The PID file specified on the #pidfile init script line is imported to the unit file with the PIDFile
option.
The key information that is not included in the init script header is the path to the service executable, and potentially some other files required by the service. In previous versions of Red Hat Enterprise Linux, init scripts used a Bash case statement to define the behavior of the service on default actions, such as start, stop, or restart, as well as custom-defined actions. The following excerpt from the postfix
init script shows the block of code to be executed at service start.
conf_check() { [ -x /usr/sbin/postfix ] || exit 5 [ -d /etc/postfix ] || exit 6 [ -d /var/spool/postfix ] || exit 5 } make_aliasesdb() { if [ "$(/usr/sbin/postconf -h alias_database)" == "hash:/etc/aliases" ] then # /etc/aliases.db might be used by other MTA, make sure nothing # has touched it since our last newaliases call [ /etc/aliases -nt /etc/aliases.db ] || [ "$ALIASESDB_STAMP" -nt /etc/aliases.db ] || [ "$ALIASESDB_STAMP" -ot /etc/aliases.db ] || return /usr/bin/newaliases touch -r /etc/aliases.db "$ALIASESDB_STAMP" else /usr/bin/newaliases fi } start() { [ "$EUID" != "0" ] && exit 4 # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 1 conf_check # Start daemons. echo -n $"Starting postfix: " make_aliasesdb >/dev/null 2>&1 [ -x $CHROOT_UPDATE ] && $CHROOT_UPDATE /usr/sbin/postfix start 2>/dev/null 1>&2 && success || failure $"$prog start" RETVAL=$? [ $RETVAL -eq 0 ] && touch $lockfile echo return $RETVAL }
The extensibility of the init script allowed specifying two custom functions, conf_check()
and make_aliasesdb()
, that are called from the start()
function block. On closer look, several external files and directories are mentioned in the above code: the main service executable /usr/sbin/postfix
, the /etc/postfix/
and /var/spool/postfix/
configuration directories, as well as the /usr/sbin/postconf/
directory.
Systemd supports only the predefined actions, but enables executing custom executables with ExecStart
, ExecStartPre
, ExecStartPost
, ExecStop
, and ExecReload
options. The /usr/sbin/postfix
together with supporting scripts are executed on service start. Converting complex init scripts requires understanding the purpose of every statement in the script. Some of the statements are specific to the operating system version, therefore you do not need to translate them. On the other hand, some adjustments might be needed in the new environment, both in unit file as well as in the service executable and supporting files.
3.5.5. Modifying existing unit files
Services installed on the system come with default unit files that are stored in the /usr/lib/systemd/system/
directory. System Administrators should not modify these files directly, therefore any customization must be confined to configuration files in the /etc/systemd/system/
directory.
Procedure
Depending on the extent of the required changes, pick one of the following approaches:
-
Create a directory for supplementary configuration files at
/etc/systemd/system/unit.d/
. This method is recommended for most use cases. It enables extending the default configuration with additional functionality, while still referring to the original unit file. Changes to the default unit introduced with a package upgrade are therefore applied automatically. See Extending the default unit configuration for more information. -
Create a copy of the original unit file
/usr/lib/systemd/system/
in/etc/systemd/system/
and make changes there. The copy overrides the original file, therefore changes introduced with the package update are not applied. This method is useful for making significant unit changes that should persist regardless of package updates. See Overriding the default unit configuration for details.
-
Create a directory for supplementary configuration files at
-
To return to the default configuration of the unit, delete custom-created configuration files in
/etc/systemd/system/
. To apply changes to unit files without rebooting the system, execute:
systemctl daemon-reload
The
daemon-reload
option reloads all unit files and recreates the entire dependency tree, which is needed to immediately apply any change to a unit file. As an alternative, you can achieve the same result with the following command, which must be executed under theroot
user:init q
If the modified unit file belongs to a running service, this service must be restarted to accept new settings:
systemctl restart name.service
To modify properties, such as dependencies or timeouts, of a service that is handled by a SysV initscript, do not modify the initscript itself. Instead, create a systemd
drop-in configuration file for the service as described in Extending the default unit configuration and Overriding the default unit configuration. Then manage this service in the same way as a normal systemd
service.
For example, to extend the configuration of the network
service, do not modify the /etc/rc.d/init.d/network
initscript file. Instead, create new directory /etc/systemd/system/network.service.d/
and a systemd
drop-in file /etc/systemd/system/network.service.d/my_config.conf
. Then, put the modified values into the drop-in file. Note: systemd
knows the network
service as network.service
, which is why the created directory must be called network.service.d
3.5.5.1. Extending the default unit configuration
This section describes how to extend the default unit file with additional configuration options.
Procedure
To extend the default unit file with additional configuration options, first create a configuration directory in
/etc/systemd/system/
. If extending a service unit, execute the following command asroot
:mkdir /etc/systemd/system/name.service.d/
Replace name with the name of the service you want to extend. The above syntax applies to all unit types.
Create a configuration file in the directory made in the previous step. Note that the file name must end with the .conf suffix. Type:
touch /etc/systemd/system/name.service.d/config_name.conf
Replace config_name with the name of the configuration file. This file adheres to the normal unit file structure, therefore all directives must be specified under appropriate sections, see Unit file structure.
For example, to add a custom dependency, create a configuration file with the following content:
[Unit] Requires=new_dependency After=new_dependency
Where new_dependency stands for the unit to be marked as a dependency. Another example is a configuration file that restarts the service after its main process exited, with a delay of 30 seconds:
[Service] Restart=always RestartSec=30
It is recommended to create small configuration files focused only on one task. Such files can be easily moved or linked to configuration directories of other services.
To apply changes made to the unit, execute as
root
:systemctl daemon-reload
systemctl restart name.service
Example 3.10. Extending the httpd.service configuration
To modify the httpd.service unit so that a custom shell script is automatically executed when starting the Apache service, perform the following steps.
Create a directory and a custom configuration file:
# mkdir /etc/systemd/system/httpd.service.d/
# touch /etc/systemd/system/httpd.service.d/custom_script.conf
Provided that the script you want to start automatically with Apache is located at
/usr/local/bin/custom.sh
, insert the following text to thecustom_script.conf
file:[Service] ExecStartPost=/usr/local/bin/custom.sh
To apply the unit changes, execute:
# systemctl daemon-reload
# systemctl restart httpd.service
The configuration files from configuration directories in /etc/systemd/system/
take precedence over unit files in /usr/lib/systemd/system/
. Therefore, if the configuration files contain an option that can be specified only once, such as Description
or ExecStart
, the default value of this option is overridden. Note that in the output of the systemd-delta
command, described in Monitoring overridden units, such units are always marked as [EXTENDED], even though in sum, certain options are actually overridden.
3.5.5.2. Overridding the default unit configuration
This section describes how to override the default unit configuration.
Procedure
To make changes that will persist after updating the package that provides the unit file, first copy the file to the
/etc/systemd/system/
directory. To do so, execute the following command asroot
:cp /usr/lib/systemd/system/name.service /etc/systemd/system/name.service
Where name stands for the name of the service unit you wish to modify. The above syntax applies to all unit types.
Open the copied file with a text editor, and make the desired changes. To apply the unit changes, execute as
root
:systemctl daemon-reload
systemctl restart name.service
Example 3.11. Changing the timeout limit
You can specify a timeout value per service to prevent a malfunctioning service from freezing the system. Otherwise, timeout is set by default to 90 seconds for normal services and to 300 seconds for SysV-compatible services.
For example, to extend timeout limit for the httpd
service:
Copy the
httpd
unit file to the/etc/systemd/system/
directory:cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/httpd.service
Open file
/etc/systemd/system/httpd.service
and specify theTimeoutStartUSec
value in the[Service]
section:… [Service] … PrivateTmp=true TimeoutStartSec=10 [Install] WantedBy=multi-user.target …
Reload the
systemd
daemon:systemctl daemon-reload
Optional. Verify the new timeout value:
systemctl show httpd -p TimeoutStartUSec
To change the timeout limit globally, input the DefaultTimeoutStartSec
in the /etc/systemd/system.conf
file.
3.5.5.3. Monitoring overriden units
This section describes how to display an overview of overridden or modified unit files.
Procedure
To display an overview of overridden or modified unit files, use the following command:
systemd-delta
For example, the output of the above command can look as follows:
[EQUIVALENT] /etc/systemd/system/default.target → /usr/lib/systemd/system/default.target [OVERRIDDEN] /etc/systemd/system/autofs.service → /usr/lib/systemd/system/autofs.service --- /usr/lib/systemd/system/autofs.service 2014-10-16 21:30:39.000000000 -0400 + /etc/systemd/system/autofs.service 2014-11-21 10:00:58.513568275 -0500 @@ -8,7 +8,8 @@ EnvironmentFile=-/etc/sysconfig/autofs ExecStart=/usr/sbin/automount $OPTIONS --pid-file /run/autofs.pid ExecReload=/usr/bin/kill -HUP $MAINPID -TimeoutSec=180 +TimeoutSec=240 +Restart=Always [Install] WantedBy=multi-user.target [MASKED] /etc/systemd/system/cups.service → /usr/lib/systemd/system/cups.service [EXTENDED] /usr/lib/systemd/system/sssd.service → /etc/systemd/system/sssd.service.d/journal.conf 4 overridden configuration files found.
3.5.6. Working with instantiated units
It is possible to instantiate multiple units from a single template configuration file at runtime. The "@" character is used to mark the template and to associate units with it. Instantiated units can be started from another unit file (using Requires
or Wants
options), or with the systemctl start
command. Instantiated service units are named the following way:
template_name@instance_name.service
Where template_name stands for the name of the template configuration file. Replace instance_name with the name for the unit instance. Several instances can point to the same template file with configuration options common for all instances of the unit. Template unit name has the form of:
unit_name@.service
For example, the following Wants
setting in a unit file:
Wants=getty@ttyA.service getty@ttyB.service
first makes systemd search for given service units. If no such units are found, the part between "@" and the type suffix is ignored and systemd searches for the getty@.service
file, reads the configuration from it, and starts the services.
For example, the getty@.service
template contains the following directives:
[Unit] Description=Getty on %I … [Service] ExecStart=-/sbin/agetty --noclear %I $TERM …
When the getty@ttyA.service and getty@ttyB.service are instantiated from the above template, Description
= is resolved as Getty on ttyA and Getty on ttyB.
3.5.6.1. Important unit specifiers
Wildcard characters, called unit specifiers, can be used in any unit configuration file. Unit specifiers substitute certain unit parameters and are interpreted at runtime. Table 3.13, “Important unit specifiers” lists unit specifiers that are particularly useful for template units.
Table 3.13. Important unit specifiers
Unit Specifier | Meaning | Description |
---|---|---|
| Full unit name |
Stands for the full unit name including the type suffix. |
| Prefix name | Stands for a unit name with type suffix removed. For instantiated units %p stands for the part of the unit name before the "@" character. |
| Instance name |
Is the part of the instantiated unit name between the "@" character and the type suffix. |
| Host name | Stands for the hostname of the running system at the point in time the unit configuration is loaded. |
| Runtime directory |
Represents the runtime directory, which is either |
For a complete list of unit specifiers, see the systemd.unit(5)
manual page.
3.6. Optimizing systemd to shorten the boot time
There is a list of systemd unit files that are enabled by default. System services that are defined by these unit files are automatically run at boot, which influences the boot time.
This section describes:
- The tools to examine system boot performance.
- The purpose of systemd units enabled by default, and circumstances under which you can safely disable such systemd units in order to shorten the boot time.
3.6.1. Examining system boot performance
To examine system boot performance, you can use the systemd-analyze
command. This command has many options available. However, this section covers only the selected ones that may be important for systemd tuning in order to shorten the boot time.
For a complete list and detailed description of all options, see the systemd-analyze
man page.
Prerequisites
Before starting to examine systemd in order to tune the boot time, you may want to list all enabled services:
$ systemctl list-unit-files --state=enabled
Analyzing overall boot time
Procedure
- For the overall information about the time that the last successful boot took, use:
$ systemd-analyze
Analyzing unit initialization time
Procedure
- For the information about the initialization time of each systemd unit, use:
$ systemd-analyze blame
The output lists the units in descending order according to the time they took to initialize during the last successful boot.
Identifying critical units
Procedure
- To identify the units that took most time to initialize at the last successful boot, use:
$ systemd-analyze critical-chain
The output highlights the units that critically slow down the boot with the red color.
Figure 3.1. The output of the systemd-analyze critical-chain command

3.6.2. A guide to selecting services that can be safely disabled
If you find the boot time of your system long, you can shorten it by disabling some of the services enabled on boot by default.
To list such services, run:
$ systemctl list-unit-files --state=enabled
To disable a service, run:
# systemctl disable service_name
However, certain services must stay enabled in order that your operating system is safe and functions in the way you need.
You can use the table below as a guide to selecting the services that you can safely disable. The table lists all services enabled by default on a minimal installation of Red Hat Enterprise Linux 8, and for each service it states whether this service can be safely disabled.
The table also provides more information about the circumstances under which the service can be disabled, or the reason why you should not disable the service.
Table 3.14. Services enabled by default on a minimal installation of RHEL 8
Service name | Can it be disabled? | More information |
---|---|---|
auditd.service | yes |
Disable |
autovt@.service | no | This service runs only when it is really needed, so it does not need to be disabled. |
crond.service | yes | Be aware that no items from crontab will run if you disable crond.service. |
dbus-org.fedoraproject.FirewallD1.service | yes |
A symlink to |
dbus-org.freedesktop.NetworkManager.service | yes |
A symlink to |
dbus-org.freedesktop.nm-dispatcher.service | yes |
A symlink to |
firewalld.service | yes |
Disable |
getty@.service | no | This service runs only when it is really needed, so it does not need to be disabled. |
import-state.service | yes |
Disable |
irqbalance.service | yes |
Disable |
kdump.service | yes |
Disable |
loadmodules.service | yes |
This service is not started unless the |
lvm2-monitor.service | yes |
Disable |
microcode.service | no | Do not be disable the service because it provides updates of the microcode software in CPU. |
NetworkManager-dispatcher.service | yes |
Disable |
NetworkManager-wait-online.service | yes |
Disable |
NetworkManager.service | yes |
Disable |
nis-domainname.service | yes |
Disable |
rhsmcertd.service | no | |
rngd.service | yes |
Disable |
rsyslog.service | yes |
Disable |
selinux-autorelabel-mark.service | yes |
Disable |
sshd.service | yes |
Disable |
sssd.service | yes |
Disable |
syslog.service | yes |
An alias for |
tuned.service | yes |
Disable |
lvm2-lvmpolld.socket | yes |
Disable |
dnf-makecache.timer | yes |
Disable |
unbound-anchor.timer | yes |
Disable |
To find more information about a service, you can run one of the following commands:
$ systemctl cat <service_name>
$ systemctl help <service_name>
The systemctl cat
command provides the content of the service file located under /usr/lib/systemd/system/<service>
, as well as all applicable overrides. The applicable overrides include unit file overrides from the /etc/systemd/system/<service>
file or drop-in files from a corresponding unit.type.d
directory.
For more information on drop-in files, see the systemd.unit
man page.
The systemctl help
command shows the man page of the particular service.
3.7. Additional Resources
For more information on systemd and its usage on Red Hat Enterprise Linux, see the resources listed below.
3.7.1. Installed Documentation
-
systemctl
(1) — The manual page for thesystemctl
command line utility provides a complete list of supported options and commands. -
systemd
(1) — The manual page for thesystemd
system and service manager provides more information about its concepts and documents available command line options and environment variables, supported configuration files and directories, recognized signals, and available kernel options. -
systemd-delta
(1) — The manual page for thesystemd-delta
utility that allows to find extended and overridden configuration files. -
systemd.directives(7)
— The manual page namedsystemd.directives
provides detailed information about systemd directives. -
systemd.unit
(5) — The manual page namedsystemd.unit
provides detailed information about systemd unit files and documents all available configuration options. -
systemd.service
(5) — The manual page namedsystemd.service
documents the format of service unit files. -
systemd.target
(5) — The manual page namedsystemd.target
documents the format of target unit files. -
systemd.kill
(5) — The manual page namedsystemd.kill
documents the configuration of the process killing procedure.
3.7.2. Online Documentation
- systemd Home Page — The project home page provides more information about systemd.