Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons
Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is
available at
http://creativecommons.org/licenses/by-sa/3.0/
. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must
provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert,
Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift,
Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States
and other countries.
Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.
XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States
and/or other countries.
MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and
other countries.
Node.js ® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the
official Joyent Node.js open source or commercial project.
The OpenStack ® Word Mark and OpenStack logo are either registered trademarks/service marks
or trademarks/service marks of the OpenStack Foundation, in the United States and other
countries and are used with the OpenStack Foundation's permission. We are not affiliated with,
endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
Abstract
Modify the systemd unit files and extend the default configuration, examine the system boot
performance and optimize systemd to shorten the boot time.
Table of Contents
Table of Contents
. . . . . . . . . .OPEN
MAKING . . . . . . SOURCE
. . . . . . . . . .MORE
. . . . . . .INCLUSIVE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3. . . . . . . . . . . . .
. . . . . . . . . . . . . FEEDBACK
PROVIDING . . . . . . . . . . . . ON
. . . .RED
. . . . .HAT
. . . . .DOCUMENTATION
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . . . . .
.CHAPTER
. . . . . . . . . . 1.. .WORKING
. . . . . . . . . . .WITH
. . . . . .SYSTEMD
. . . . . . . . . . .UNIT
. . . . . FILES
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5. . . . . . . . . . . . .
1.1. INTRODUCTION TO UNIT FILES 5
1.2. SYSTEMD UNIT FILES LOCATIONS 5
1.3. UNIT FILE STRUCTURE 6
1.4. IMPORTANT [UNIT] SECTION OPTIONS 6
1.5. IMPORTANT [SERVICE] SECTION OPTIONS 7
1.6. IMPORTANT [INSTALL] SECTION OPTIONS 9
1.7. CREATING CUSTOM UNIT FILES 9
1.8. CREATING A CUSTOM UNIT FILE BY USING THE SECOND INSTANCE OF THE SSHD SERVICE 11
1.9. FINDING THE SYSTEMD SERVICE DESCRIPTION 12
1.10. FINDING THE SYSTEMD SERVICE DEPENDENCIES 12
1.11. FINDING DEFAULT TARGETS OF THE SERVICE 13
1.12. FINDING FILES USED BY THE SERVICE 13
1.13. MODIFYING EXISTING UNIT FILES 15
1.14. EXTENDING THE DEFAULT UNIT CONFIGURATION 16
1.15. OVERRIDING THE DEFAULT UNIT CONFIGURATION 17
1.16. CHANGING THE TIMEOUT LIMIT 17
1.17. MONITORING OVERRIDDEN UNITS 18
1.18. WORKING WITH INSTANTIATED UNITS 19
1.19. IMPORTANT UNIT SPECIFIERS 19
1.20. ADDITIONAL RESOURCES 20
.CHAPTER
. . . . . . . . . . 2.
. . OPTIMIZING
. . . . . . . . . . . . . .SYSTEMD
. . . . . . . . . . .TO
. . . SHORTEN
. . . . . . . . . . . THE
. . . . .BOOT
. . . . . . .TIME
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
..............
2.1. EXAMINING SYSTEM BOOT PERFORMANCE 21
2.2. A GUIDE TO SELECTING SERVICES THAT CAN BE SAFELY DISABLED 22
2.3. ADDITIONAL RESOURCES 25
1
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
2
MAKING OPEN SOURCE MORE INCLUSIVE
3
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
4. Enter your suggestion for improvement in the Description field. Include links to the relevant
parts of the documentation.
4
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
<unit_name>.<type_extension>
Here, unit_name stands for the name of the unit and type_extension identifies the unit type.
For example, you can find an sshd.service as well as an 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.
The systemd system and service manager can also create the sshd.service.wants/ and
sshd.service.requires/ directories. These directories contain symbolic links to unit files that are
dependencies of the sshd service. systemd creates the symbolic links automatically either during
installation according to [Install] unit file options or at runtime based on [Unit] options. You can also
create these directories and symbolic links manually.
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 .
5
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
Directory Description
The default configuration of systemd is defined during the compilation and you can find the
configuration in the /etc/systemd/system.conf file. By editing this file, you can modify the default
configuration by overriding 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
Additional resources
6
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
After [b] Defines the order in which units are started. The unit
starts only after the units specified in After are
active. Unlike Requires, After does not explicitly
activate the specified units. The Before option has
the opposite functionality to After .
[a] For a complete list of options configurable in the [Unit] section, see the systemd.unit(5) manual page.
[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.
7
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
8
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
[a] For a complete list of options configurable in the [Service] section, see the systemd.service(5) manual page.
RequiredBy A list of units that depend on the unit. When this unit
is enabled, the units listed in RequiredBy gain a
Require dependency on the unit.
[a] For a complete list of options configurable in the [Install] section, see the systemd.unit(5) manual page.
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 .
Procedure
1. To create a custom service, prepare the executable file with the service. The file can contain 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. You can also include
9
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
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.
2. Create a unit file in the /etc/systemd/system/ directory and make sure it has correct file
permissions. Execute as root:
# touch /etc/systemd/system/<name>.service
Replace <name> with a name of the service you want to created. Note that the file does not
need to be executable.
3. Open the created <name>.service file and add the service configuration options. You can use
various options 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
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.
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
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.
# systemctl daemon-reload
10
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
WARNING
Procedure
To create a second instance of the sshd service:
1. Create a copy of the sshd_config file that the second daemon will use:
# cp /etc/ssh/sshd{,-second}_config
2. 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 about Port and PidFile 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.
3. 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
b. Add sshd.service to services specified in the After option, so that the second instance
starts only after the first one has already started:
11
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
e. After the modifications, the sshd-second.service unit file contains the following settings:
[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
5. 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:
Make sure you configure firewall to allow connections to the second instance of sshd.
12
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
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.
13
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
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.
14
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
Procedure
1. Depending on the extent of the required changes, pick one of the following approaches:
2. To return to the default configuration of the unit, delete custom-created configuration files in
the /etc/systemd/system/ directory.
# 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:
# init q
4. If the modified unit file belongs to a running service, restart the service:
IMPORTANT
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
15
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
Procedure
# mkdir /etc/systemd/system/<name>.service.d/
Replace <name> with the name of the service you want to extend. The syntax applies to all unit
types.
# 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 and you have to specify all directives in the 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>
The <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
Create small configuration files focused only on one task. Such files can be easily moved or
linked to configuration directories of other services.
# systemctl daemon-reload
# systemctl restart <name>.service
To modify the httpd.service unit so that a custom shell script is automatically executed when
starting the Apache service, perform the following steps.
# mkdir /etc/systemd/system/httpd.service.d/
16
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
# touch /etc/systemd/system/httpd.service.d/custom_script.conf
2. Specify the script you want to execute after the main service process by inserting the
following text to the custom_script.conf file:
[Service]
ExecStartPost=/usr/local/bin/custom.sh
# systemctl daemon-reload
NOTE
Procedure
1. Copy the unit file to the /etc/systemd/system/ directory by entering the following command as
root:
# cp /usr/lib/systemd/system/<name>.service /etc/systemd/system/<name>.service
2. Open the copied file with a text editor, and make changes.
# systemctl daemon-reload
# systemctl restart <name>.service
Procedure
To extend timeout limit for the httpd service:
17
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
# cp /usr/lib/systemd/system/httpd.service /etc/systemd/system/httpd.service
...
[Service]
...
PrivateTmp=true
TimeoutStartSec=10
[Install]
WantedBy=multi-user.target
...
# systemctl daemon-reload
NOTE
Procedure
# systemd-delta
18
CHAPTER 1. WORKING WITH SYSTEMD UNIT FILES
+TimeoutSec=240
+Restart=Always
[Install]
WantedBy=multi-user.target
<template_name>@<instance_name>.service
The <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
[email protected] [email protected]
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 [email protected] file, reads the configuration
from it, and starts the services.
[Unit]
Description=Getty on %I
...
[Service]
ExecStart=-/sbin/agetty --noclear %I $TERM
...
When the [email protected] and [email protected] are instantiated from the above template,
Description= is resolved as Getty on ttyA and Getty on ttyB.
19
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
For a complete list of unit specifiers, see the systemd.unit(5) manual page.
How to write a service unit file which enforces that particular services have to be started
How to decide what dependencies a systemd service unit definition should have
20
CHAPTER 2. OPTIMIZING SYSTEMD TO SHORTEN THE BOOT TIME
Prerequisites
Optional: Before you examine systemd to tune the boot time, list all enabled services:
Procedure
Choose the information you want to analyze:
Analyze the information about the time that the last successful boot took:
$ systemd-analyze
$ systemd-analyze blame
The output lists the units in descending order according to the time they took to initialize during
the last successful boot.
Identify critical units that took the longest time to initialize at the last successful boot:
$ systemd-analyze critical-chain
The output highlights the units that critically slow down the boot with the red color.
Additional resources
Disable a service:
Certain services must stay enabled so that your operating system is safe and functions in the way you
need.
Refer to the following table 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.
22
CHAPTER 2. OPTIMIZING SYSTEMD TO SHORTEN THE BOOT TIME
auditd.service yes Disable auditd.service only if you do not need audit messages
from the kernel. Be aware that if you disable auditd.service, the
/var/log/audit/audit.log file is not produced. Consequently, you
are not able to retroactively review some commonly-reviewed
actions or events, such as user logins, service starts or password
changes. Also note that auditd has two parts: a kernel part, and a
service itself. By using the systemctl disable auditd command,
you only disable the service, but not the kernel part. To disable
system auditing in its entirety, set audit=0 on kernel command
line.
[email protected] 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.
[email protected] no This service runs only when it is really needed, so it does not need
to be disabled.
irqbalance.service yes Disable irqbalance.service only if you have just one CPU. Do not
disable irqbalance.service on systems with multiple CPUs.
kdump.service yes Disable kdump.service only if you do not need reports from
kernel crashes.
23
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
rhsmcertd.service no
rngd.service yes Disable rngd.service only if you do not need much entropy on
your system, or you do not have any sort of hardware generator.
Note that the service is necessary in environments that require a
lot of good entropy, such as systems used for generation of X.509
certificates (for example the FreeIPA server).
rsyslog.service yes Disable rsyslog.service only if you do not need persistent logs,
or you set systemd-journald to persistent mode.
sshd.service yes Disable sshd.service only if you do not need remote logins by
OpenSSH server.
24
CHAPTER 2. OPTIMIZING SYSTEMD TO SHORTEN THE BOOT TIME
sssd.service yes Disable sssd.service only if there are no users who log in the
system over the network (for example by using LDAP or Kerberos).
Red Hat recommends to disable all sssd-* units if you disable
sssd.service.
To find more information about a service, use one of the following commands:
Additional resources
The systemd help command that shows the man page of a particular service
25
Red Hat Enterprise Linux 9 Using systemd unit files to customize and optimize your system
26