Systemd Introduction Tutorial: Commands Section

Systemd Tutorial: Practical Guide

Systemd Introduction Tutorial: Commands Section

Author: Ruan Yifeng

Date: March 7, 2016

Systemd is a Linux system utility used to start Process guardian Has become the standard configuration for most distributions.

This article introduces its basic usage, divided into two parts. Today, we introduce its main commands. Next article Introduce its practical application. img

Origin

In history, Linux boot Always adopt init Process.

The following command is used to start the service.

This method has two drawbacks.

Firstly, the startup time is long. init Processes are initiated sequentially; the next process will only start after the previous one has completed.

The second issue is that the startup script is complex. init The process merely executes the startup script, ignoring other matters. The script must handle various situations itself, often making the script lengthy.

II. Overview of Systemd

Systemd was born to address these issues. Its design goal is to provide a comprehensive solution for system boot and management.

According to Linux convention, letters d It's an abbreviation for daemon. The name "Systemd" signifies that it is meant to be a guardian for the entire system.

img

(As shown above is the author of Systemd) Lennart Poettering

No longer needed with Systemd. init Done. Systemd has replaced initd It becomes the first process of the system (PID equals 1), and all other processes are its children.

The command above checks the version of Systemd.

Systemd 的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用 Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,违反"keep simple, keep stupid"的 Unix philosophy img

(上图 shows the Systemd architecture diagram)

Section 3: System Management

Systemd isn't a single command; it's a suite of commands involving various aspects of system management.

3.1 systemctl

systemctl It's the main command of Systemd, used for system management.

3.2 systemd-analyze

systemd-analyze Command to view startup time.

3.3 hostnamectl

hostnamectl The command is used to view the current host's information.

3.4 localectl

localectl Command is used to view localization settings.

3.5 timedatectl

timedatectl Command to view current timezone setting.

3.6 loginctl

loginctl The command is used to view the currently logged-in user.

fourth, unit

4.1 Meaning

Systemd manages all system resources. Different resources are collectively called Units (units).

Unit is divided into 12 types in total.

  • Service unit: system service
  • Target unit: A group composed of multiple units
  • Device Unit: Hardware Device
  • Mount Unit: Filesystem Mount Point
  • Automount Unit: Auto-mount Point
  • Path Unit: File or Path
  • Scope Unit: External process not started by Systemd
  • Slice Unit: Process Group
  • Snapshot Unit: Systemd snapshot, can revert to a specific snapshot
  • Socket Unit: a socket for inter-process communication
  • Swap Unit: swap file
  • Timer Unit: Timer

systemctl list-units Command can view all Units in the current system.

4.2 Unit status

systemctl status Commands are used to view system and individual Unit status.

Except for status Command. systemctl Provided three simple methods for querying statuses, mainly for use in script internal conditional statements.

4.3 Unit Management

For users, the most commonly used commands are as follows, for starting and stopping Unit (mainly services).

4.4 Dependency Relationship

Dependencies exist between units: A depends on B, meaning Systemd will start B alongside A.

systemctl list-dependencies List all dependencies of a Unit.

In the output of the above command, some dependencies are of the Target type (see below). By default, they are not expanded. To expand the Targets, you need to use --all Parameters.

V. Unit Configuration File

5.1 Overview

Each Unit has a configuration file that tells Systemd how to start the Unit.

Systemd is set to read from directory /etc/systemd/system/ Read the configuration file. However, most of the files stored in it are symbolic links, pointing to directories. /usr/lib/systemd/system/ The actual configuration file is located in that directory.

systemctl enable The command is used to establish symbolic link relationships between the two directories.

If the configuration file is set to start on boot, systemctl enable Command is akin to activating boot-up startup.

In contrast, systemctl disable The command is used to unlink symbolic links between two directories, equivalent to disabling boot-up startup.

The file extension of the configuration file indicates the type of the Unit, for example. sshd.socket If omitted, the default suffix for Systemd is: ".service" .service Therefore, sshd Be interpreted as sshd.service

5.2 Configuration File Status

systemctl list-unit-files Command is used to list all configuration files.

This command outputs a list.

This list shows the status of each configuration file, which totals four types.

  • Enabled: Link established
  • disabled: No startup link established.
  • static: This configuration file does not have [Install] Partly (unexecutable), it can only serve as a dependency for other configuration files.
  • masked: This configuration file is prohibited from creating startup links.

Note that the configuration file status does not indicate whether the Unit is running. This must be executed as mentioned earlier. systemctl status Command.

Once the configuration file is modified, you must reload the file for SystemD and restart it, otherwise the changes will not take effect.

5.3 Configuration file format

Configuration files are plain text files, which can be opened with a text editor.

systemctl cat Commands can view the contents of configuration files.

As shown above, the configuration file is divided into several sections. The first line of each section is a distinguished name enclosed in square brackets, such as: [Unit] Note, the block names and field names in the configuration file are case-sensitive.

Each block contains key-value pairs connected by equals signs.

5.4 Configuration File Sections

[Unit] The block typically is the first block in a configuration file, used to define the metadata of a Unit and its relationships with other Units. Its main fields are as follows.

  • Dsecription : Brief description
  • Documentation Document address
  • Requires The other Units currently dependent on this Unit will fail to start if they are not running.
  • Wants Other Units that work with the current Unit won't cause a failure if they're not running.
  • BindsTo : With Requires Similar, if the specified Unit exits, it will stop the current Unit from running.
  • Before If the specified Unit also needs to be started, it must be initiated after the current Unit.
  • After If the Unit specified by this field also needs to be started, it must be initiated before the current Unit.
  • Conflicts The specified Unit cannot run concurrently with the current Unit.
  • Condition... Current Unit operation must meet certain conditions, otherwise it will not run.
  • Assert... Current Unit operation must meet certain conditions, otherwise, startup failure will occur.

[Install] Typically the last section of a configuration file, used to define how to start and whether to boot up at system startup. Its main fields are as follows.

  • WantedBy Its value is one or more Targets, and when the current Unit is enabled, symbolic links will be placed into /etc/systemd/system Below the table of contents, list the "Target Name + " .wants subdirectory formed by suffixes
  • RequiredBy Its value is one or more Targets, and when the current Unit is activated, symbolic links will be placed into /etc/systemd/system Below the table of contents, list the "Target Name + " .required subdirectory formed by suffixes
  • Alias : Current Unit's Aliases Available for Startup
  • Also Other Units activated when the current Unit is enabled.

[Service] The block used for Service configuration; only Unit of Service type has this block. Its main fields are as follows.

  • Type Define the process behavior at startup. It has the following values.
  • Type=simple Default, execute ExecStart Specified command, start main process
  • Type=forking Create a child process from the parent process using the fork method, and the parent process will exit immediately after creation.
  • Type=oneshot One-time process, Systemd will wait for the current service to exit before continuing.
  • Type=dbus The current service is started via D-Bus.
  • Type=notify Service started successfully, notification will be sent. Systemd Continue executing downward
  • Type=idle If other tasks are completed, the current service will run.
  • ExecStart Command to start the current service
  • ExecStartPre Run commands before starting the current service
  • ExecStartPost Start current service command
  • ExecReload Command executed when restarting the current service
  • ExecStop Command executed when stopping the current service
  • ExecStopPost Stop commands after service shutdown
  • RestartSec Interval for automatic restart of the current service in seconds
  • Restart Define what conditions cause Systemd to automatically restart the current service. Possible values include: always Always restarting. on-success on-failure on-abnormal on-abort on-watchdog
  • TimeoutSec The number of seconds that Systemd waits before stopping the current service.
  • Environment Set environment variable

Complete field list of Unit configuration file, please refer to Official document

Six, Target

When starting a computer, a large number of Units need to be initiated. It would be very inconvenient to list all the Units required for each startup. Systemd's solution is to use Targets.

简单说,Target 就是一个 Unit 组,包含许多相关的 Unit 。启动某个 Target 的时候,Systemd 就会启动里面所有的 Unit。从这个意义上说,Target这个概念类似于"状态点",启动某个 Target 就好比启动到某种状态。

Traditional init In the boot mode, there's a concept of RunLevel, which is similar to the function of Target. The difference is that RunLevel is mutually exclusive, meaning it's impossible to start multiple RunLevels simultaneously, whereas multiple Targets can be started concurrently.

The correspondence between Target and traditional RunLevel is as follows.

It is with init The main differences between the processes are as follows.

Default RunLevel In (the) /etc/inittab File settings) are now replaced by the default Target, located at /etc/systemd/system/default.target , usually symbol links to graphical.target (Graphical User Interface) or multi-user.target Multi-user command-line

(2) Location of the startup script formerly /etc/init.d Contents, symbolic links to different RunLevel directories (e.g.) /etc/rc3.d /etc/rc5.d (etc.), now stored in /lib/systemd/system With /etc/systemd/system Table of Contents.

(3) Configuration file location formerly init The process configuration file is /etc/inittab Configuration files for various services are stored in /etc/sysconfig Contents. The current configuration files are mainly stored in: /lib/systemd Contents, at /etc/systemd Modifications in the catalog can override the original settings.

7. Log Management

Systemd manages the startup logs for all Units. The benefit is that you can only use journalctl A command to view all logs (kernel logs and application logs). The log configuration file is /etc/systemd/journald.conf

journalctl Powerful and versatile.

(End)

Document Information

  • Copyright Statement: Free to redistribute - Non-commercial - No derivatives - Keep attribution Creative Commons 3.0 License )
  • Date of Publication: Mar. 7, 2016

You recently used:

收藏: favorite Menu QQ