第 2 章 Managing systemd
As a system administrator, you can manage critical aspects of your system with systemd. Serving as a system and service manager for Linux operating systems, systemd software suite provides tools and services for controlling, reporting, and system initialization. Key features of systemd include:
- Parallel start of system services during boot
- On-demand activation of daemons
- Dependency-based service control logic
The basic object that systemd manages is a systemd unit, a representation of system resources and services. A systemd unit consists of a name, type and a configuration file that defines and manages a particular task. You can use unit files to configure system behavior. See the following examples of various systemd unit types:
- Service
- Controls and manages individual system services.
- Target
- Represents a group of units that define system states.
- Device
- Manages hardware devices and their availability.
- Mount
- Handles file system mounting.
- Timer
- Schedules tasks to run at specific intervals.
2.1. Managing system services with systemctl 复制链接链接已复制到粘贴板!
As a system administrator, you can manage system services by using the systemctl utility. You can perform various tasks, such as starting, stopping, restarting running services, enabling and disabling services to start at boot, listing available services, and displaying system services statuses.
2.1.1. Listing system services 复制链接链接已复制到粘贴板!
You can list all currently loaded service units and display the status of all available service units.
Procedure
Use the systemctl command to perform any of the following tasks:
List all currently loaded service units:
$ 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 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, or a 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'By default, the
systemctl list-unitscommand displays only active units. For each service unit file, the command provides an overview of the following parameters:UNIT- The full name of the service unit
LOAD- The load state of the configuration file
ACTIVEorSUB- The current high-level and low-level unit file activation state
DESCRIPTION- A short description of the unit’s purpose and functionality
List all loaded units regardless of their state, by using the following command with the
--allor-acommand line option:$ systemctl list-units --type service --allList the status (enabled or disabled) of all available service units:
$ systemctl list-unit-files --type service UNIT FILE STATE abrt-ccpp.service enabled abrt-oops.service enabled abrtd.service enabled ... wpa_supplicant.service disabled ypbind.service disabled 208 unit files listed.For each service unit, this command displays:
UNIT FILE- The full name of the service unit
STATE- The information whether the service unit is enabled or disabled to start automatically during boot
2.1.2. Displaying system service status 复制链接链接已复制到粘贴板!
You can inspect any service unit to get detailed information and verify the state of the service, whether it is enabled to start during boot or currently running. You can also view services that are ordered to start after or before a particular service unit.
Procedure
Display detailed information about a service unit that corresponds to a system service:
$ systemctl status <name>.serviceReplace
<name>with the name of the service unit you want to inspect (for example,gdm).This command displays the following information:
- The name of the selected service unit followed by a short description
- One or more fields described in Available service unit information
-
The execution of the service unit: if the unit is executed by the
rootuser The most recent log entries
Expand 表 2.1. Available service unit information Field Description LoadedInformation whether the service unit has been loaded, the absolute path to the unit file, and a note whether the unit is enabled to start during boot.
ActiveInformation whether the service unit is running followed by a time stamp.
Main PIDThe process ID and the name of the corresponding system service.
StatusAdditional information about the corresponding system service.
ProcessAdditional information about related processes.
CGroupAdditional information about related control groups (
cgroups).
Verify that a particular service unit is running:
$ systemctl is-active <name>.serviceDetermine whether a particular service unit is enabled to start during boot:
$ systemctl is-enabled <name>.service注意Both
systemctl is-activeandsystemctl is-enabledcommands return an exit status of0if the specified service unit is running or enabled.Check what services
systemdorders to start before the specified service unit# systemctl list-dependencies --after <name>.serviceFor example, to view the list of services ordered to start before
gdm, enter:# 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]Check what services
systemdorders to start after the specified service unit:# systemctl list-dependencies --before <name>.serviceFor example, to view the list of services
systemdorders to start aftergdm, enter:# 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
2.1.3. Starting and stopping a systemd unit 复制链接链接已复制到粘贴板!
You can start system service in the current session by using the systemctl start command.
Prerequisites
- You have the Root access.
Procedure
Start a system service in the current session:
# *systemctl start <systemd_unit> *Replace
<systemd_unit>with the name of the service unit you want to start (for example,httpd.service).注意In
systemd, positive and negative dependencies between services exist. Starting a 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,
systemdresolves all dependencies automatically, without explicit notification to the user. This means that 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
sendmailservice, and you attempt to start thepostfixservice,systemdfirst automatically stopssendmail, because these two services are conflicting and cannot run on the same port.
2.1.4. Stopping a system service 复制链接链接已复制到粘贴板!
If you want to stop a system service in the current session, use the systemctl stop command.
Prerequisites
- You have Root access
Procedure
Stop a system service:
# systemctl stop <name>.serviceReplace
<name>with the name of the service unit you want to stop (for example,bluetooth).
2.1.5. Restarting and reloading a system service 复制链接链接已复制到粘贴板!
You can restart system service in the current session by using the restart command to perform the following actions:
- Stop the selected service unit in the current session and immediately start it again.
- Restart a service unit only if the corresponding service is already running.
- Reload configuration of a system service without interrupting its execution.
Prerequisites
- You have the Root access.
Procedure
Restart a system service:
# systemctl restart <name>.serviceReplace
<name>with the name of the service unit you want to restart (for example,httpd).If the selected service unit is not running, this command starts it.
Restart a service unit only if the corresponding service is already running:
# systemctl try-restart <name>.serviceReload the configuration without interrupting service execution:
# systemctl reload <name>.service注意System services that do not support this feature, ignore this command. To restart such services, use the
reload-or-restartandreload-or-try-restartcommands instead.For more information, see the
systemctl(1)man page on your system.
2.1.6. Enabling a system service to start at boot 复制链接链接已复制到粘贴板!
You can enable a service to start automatically at boot, these changes apply with the next reboot.
Prerequisites
- You have Root access.
Procedure
Verify whether the unit is masked:
# systemctl status <systemd_unit>If the unit is masked, unmask it first:
# systemctl unmask <systemd_unit>Enable a service to start at boot time:
# systemctl enable <systemd_unit>Replace
<systemd_unit>with the name of the service unit you want to enable (for example,httpd).
Optionally, pass the --now option to the command to also start the unit right now.
2.1.7. Disabling a system service to start at boot 复制链接链接已复制到粘贴板!
You can prevent a service unit from starting automatically at boot time. If you disable a service, it will not start at boot, but you can start it manually. You can also mask a service, so that it cannot be started manually. Masking is a way of disabling a service that makes the service permanently unusable until it is unmasked again.
Prerequisites
- You have Root access.
Procedure
Disable a service to start at boot:
# systemctl disable <name>.serviceReplace
<name>with the name of the service unit you want to disable (for example,bluetooth). Optionally, pass the--nowcommand to also stop the service if it is currently running.Optional: To prevent that the unit can be accidentally started by an administrator or as a dependency of other units, mask the service:
# systemctl mask <name>.service