识别自动启动的系统进程
Systemd守护进程简介
在Red Hat Enterprise Linux (RHEL) 9.4 中,systemd 是用于启动、管理和停止守护进程的系统和服务管理器。使用 systemd 可以更有效地管理系统的启动过程以及服务的生命周期。
在红帽企业Linux中,第一个启动的进程(PID 1)是systemd守护进程。
服务单元描述
使用systemctl -t help查看systemd可用的单元类型。
[root@localhost ~]# systemctl -t help
Available unit types:
service
mount
swap
socket
target
device
automount
timer
path
slice
scope
.service 代表系统服务。比如常见的sshd、firewalld服务。
.socket代表systemd应监控的进程间通信IPC套接字。
.path用于将服务的激活推迟到特定文件系统更改发生之后。可以将路径单元用于使用假脱机目录的服务,如打印系统。
列出服务单元
使用systemctl命令来探索系统的当前状态。例如,以下命令会列举出所有服务。
[root@localhost ~]# systemctl list-units --type=service
查看服务状态
使用systemctl status name.type命令查看单元的状态。例如,使用systemctl status sshd.service查看sshd服务的运行状态。
systemctl命令status选项输出中的一些字段:
字段 | 描述 |
loaded | 服务单元是否已加载到内存中 |
active | 服务单元是否正在运行;若为是,它已运行了多久。 |
docs | 哪里可以找到有关该服务的更多信息 |
main PID | 服务的主进程ID,包含命令名称 |
status | 服务的更多信息 |
process | 相关进程的更多信息 |
CGroup | 相关控制组的更多信息 |
systemctl输出中的服务状态:
关键字 | 描述 |
loaded | 单元配置文件已被处理 |
active(running) | 服务正在运行,并有持续的进程 |
active(exited) | 服务成功完成了一次性配置 |
active(waiting) | 服务正在运行中,但在等待事件 |
inactive | 服务未在运行 |
enabled | 服务在系统引导时启动 |
disabled | 服务未设为在系统引导时启动 |
static | 服务无法启用,但可以由一启用的单元自动启动 |
验证服务的状态
systemctl命令可验证服务的具体状态。例如,使用systemctl命令is-active选项来验证服务单元是否处于活动状态。
[root@localhost ~]# systemctl is-active sshd.service
active
通常返回的状态是active或inactive。
运行systemctl命令is-enabled选项可验证服务单元是否已启用为在系统引导期间自动启动。
[root@localhost ~]# systemctl is-enabled sshd.service
enabled
通常返回的状态为enabled或disabled。
控制系统服务
启动和停止服务
使用systemctl status命令来验证服务的状态,即服务是在运行还是已停止状态。
要停止正在运行的服务,请使用systemctl命令stop选项。
[root@localhost ~]# systemctl stop firewalld.service
使用systemctl命令start选项启动已停止的服务。
[root@localhost ~]# systemctl start firewalld.service
重新启动和重新加载服务
当您重新启动正在运行的服务时,该服务首先停止运行,然后重新启动。在服务重新启动时,新进程在启动期间获得一个新的ID,因此进程ID会改变。
要重新启动正在运行的服务,请使用systemctl命令restart选项。
[root@localhost ~]# ps aux | grep firewalld
root 4347 0.1 0.5 352628 44084 ? Ssl 18:01 0:00 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid
root 4508 0.0 0.0 221664 2304 pts/1 S+ 18:05 0:00 grep --color=auto firewalld
[root@localhost ~]# systemctl restart firewalld.service
[root@localhost ~]# ps aux | grep firewalld
root 4528 19.0 0.5 352632 44272 ? Ssl 18:06 0:00 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid
root 4616 0.0 0.0 221664 2304 pts/1 S+ 18:06 0:00 grep --color=auto firewalld
一些服务无需重新启动即可重新加载其配置文件,这称为服务重新加载。重新加载服务不会更改与各项服务进程关联的进程ID。
要重新加载正在运行的服务,请使用systemctl命令reload选项。
[root@localhost ~]# ps aux | grep docker
root 1269 0.0 1.0 1995132 84968 ? Ssl 16:32 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 4620 0.0 0.0 221796 2304 pts/1 S+ 18:09 0:00 grep --color=auto docker
[root@localhost ~]# systemctl reload docker.service
[root@localhost ~]# ps aux | grep docker
root 1269 0.0 1.0 1995132 84968 ? Ssl 16:32 0:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root 4660 0.0 0.0 221796 2304 pts/1 S+ 18:09 0:00 grep --color=auto docker
列出单元依赖项
使用systemctl命令stop选项停止服务。
[root@localhost ~]# systemctl stop firewalld.service
systemctl list-dependencies UNIT命令显示启动服务单元所需的依赖项的层次结构映射。
要列出反向依赖项,请使用--reverse选项。
屏蔽和取消屏蔽服务
有时,系统中安装的不同服务之间可能彼此冲突。此时需要屏蔽服务。屏蔽操作会在配置目录中创建指向/dev/null文件的链接,该文件可阻止服务启动。要屏蔽某一服务,请使用systemctl命令mask选项,使用systemctl命令unmask选项可取消屏蔽服务单元。
使服务在系统引导时启动或停止
使用systemctl命令enable选项用于配置服务在系统启动时自动启动,确保服务在每次系统启动时都能自动运行。
[root@localhost ~]# systemctl enable firewalld.service
Created symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service → /usr/lib/systemd/system/firewalld.service.
Created symlink /etc/systemd/system/multi-user.target.wants/firewalld.service → /usr/lib/systemd/system/firewalld.service.
如果要启动服务并使其在引导期间自动启动,可以使用--now选项,避免了分别执行 systemctl enable 和 systemctl start 命令的繁琐。
要禁用服务自动启动,请使用systemctl disable选项。如果要同时停止服务,请使用systemctl disable --now命令。
Systemctl命令摘要
服务管理实用命令
命令 | 任务 |
systemctl status UNIT | 查看单元状态的详细信息 |
systemctl stop UNIT | 在运行中的系统上停止一项服务 |
systemctl start UNIT | 在运行中的系统上启动一项服务 |
systemctl restart UNIT | 在运行中的系统上重新启动一项服务 |
systemctl reload UNIT | 重新加载运行中服务的配置文件 |
systemctl mask UNIT | 禁用服务,使其无法启动或在系统引导时启动 |
systemctl unmask UNIT | 使屏蔽的服务变为可用 |
systemctl enable UNIT | 将服务配置为在系统引导时启动。使用--now选项也会启动该服务 |
systemctl disable UNIT | 禁止服务在系统引导时启动。使用--now选项也会停止该服务 |