Linux serverlinux web serverNETWORK ADMINISTRATIONS

Redhat Linux 9: Quản lý các tùy chọn trong file cấu hình systemd unit

Link playlist
Red Hat Enterprise Linux Administration

2023 12 02 21 53 45

Managing Unit Options
When working with Systemd unit files, you risk getting
overwhelmed by its many options. Every unit file can be
configured with different options. To figure out which options
are available for a specific unit, use the systemctl show
command. For instance, the systemctl show sshd command
shows all Systemd options that can be configured in the
sshd.service unit, including their current default values.
Example 11-8 shows the output of this command.
Example 11-8 Showing Available Options with systemctl show

When changing unit files to apply options, you need to make
sure that the changes are written to /etc/systemd/system, which
is the location where custom unit files should be created. The
recommended way to do so is to use the systemctl edit
command. This command creates a subdirectory in
/etc/systemd/system for the service that you are editing; for
example, if you use systemctl edit sshd.service, you get a
directory with the name /etc/systemd/systemd/sshd.service.d in
which a file with the name override.conf is created. All settings
that are applied in this file overwrite any existing settings in the
service file in /usr/lib/systemd/system. In Exercise 11-2 you
learn how to apply changes to Systemd units.
Tip
By default, Systemd uses the nano editor. Not
everybody likes that very much (including me). If
you want vim to be used instead of nano, edit the
/root/.bash_profile file to include the following line:
export SYSTEMD_EDITOR=”/bin/vim” and add
this line to the ~/.bashrc file. After you log in again,
vim will be used as the default editor. If you would
rather use /bin/vim as the default editor for all
commands that need an external editor (including
systemctl), you may also include export
EDITOR=”/bin/vim” instead.
Exercise 11-2 Changing Unit Configuration
1. From a root shell, type dnf -y install httpd to install the
Apache web server package.
2. Use systemctl cat httpd.service to show the current
configuration of the unit file that starts the Apache web
server.
3. Type systemctl show httpd.service to get an overview of
available configuration options for this unit file.
4. Type export SYSTEMD_EDITOR=/bin/vim to ensure you use
vim as the default editor for the duration of this session.
(Optionally, add this line to ~/.bashrc to make it persistent.)
5. Use systemctl edit httpd.service to change the default
configuration, and add a [Service] section that includes the
Restart=always and RestartSec=5s lines.
6. Enter systemctl daemon-reload to ensure that Systemd
picks up the new configuration.
7. Type systemctl start httpd to start the httpd service and
systemctl status sshd to verify that the sshd service is
indeed running.
8. Use killall httpd to kill the httpd process.
9. Type systemctl status httpd and then repeat after 5
seconds. You’ll notice that the httpd process gets
automatically restarted.
Summary
In this chapter you learned how to work with Systemd. You
read how to manage Systemd service state and how to change
different options in Systemd. In the next chapter you’ll learn
how to schedule tasks using the cron and at services.

source

by Le Hoang Long Long

linux web server