视频加载失败

Linux 系统服务管理:SysV init 与 systemd

2420 字
12 分钟
Linux 系统服务管理:SysV init 与 systemd

【一】由来#

传统 SysV init 通过 /etc/init.d/ 启动脚本管理服务;许多 systemd 系统仍保留 service 命令和这些脚本作为兼容接口。

Terminal window
sudo /etc/init.d/apache2 start
#或者
service apache2 start

这种方法有两个缺点:

一是依赖关系表达能力有限,传统启动脚本常按既定顺序执行,容易拉长启动时间。

二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。

【二】Systemd概述#

Systemd就是为了解决这些问题而诞生的。

它的设计目标是,为系统的启动和管理提供一套完整的解决方案,根据Linux惯例,字母d是守护进程(daemon)的缩写,Systemd这个名字的含义,就是它要守护整个系统。

Systemd概述(图 1)
Systemd概述(图 1)

(上图为Systemd作者Lennart Poettering)

在采用 systemd 的发行版中,systemd 通常作为 PID 1 运行,并负责启动和监管大部分系统服务。并非所有 Linux 发行版都使用 systemd;也存在 OpenRC、runit、SysV init 等实现。

Terminal window
systemctl --version #命令查看Systemd的版本。
Terminal window
linux@ubuntu:~$ systemctl --version
systemd 237
+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL
+XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybrid

Systemd的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,不符合 KISS(Keep It Simple, Stupid,保持简单)原则;这是对设计取舍的一种观点。

【三】系统管理#

【1】systemctl#

systemd 是一套初始化与服务管理组件;systemctl 是其主要管理命令。

systemctl 是systemd的主命令,用于管理系统

Terminal window
#重启系统
sudo systemctl reboot
#关闭系统,切断电源
sudo systemctl poweroff
# 停止系统但不一定切断电源(行为取决于硬件与固件)
sudo systemctl halt
#暂停系统
sudo systemctl suspend
#让系统进入冬眠状态
sudo systemctl hibernate
#让系统进入交互式休眠状态
sudo systemctl hybrid-sleep
# 进入救援模式;将影响当前会话,应在本地控制台谨慎执行
sudo systemctl rescue

【2】systemd-analyze#

systemd-analyze 命令用于查看启动耗时

Terminal window
#查看启动耗时
systemd-analyze
#查看每个服务的启动耗时
systemd-analyze blame
#显示瀑布状的启动过程流
systemd-analyze critical-chain
#显示指定服务的启动流
systemd-analyze critical-chain atd.service

【3】hostnamectl#

hostnamectl 命令用于查看当前主机的信息

Terminal window
#显示当前主机的信息
hostnamectl
#设置主机名。
sudo hostnamectl set-hostname rhel7

【4】localectl#

localectl 命令用于查看本地化设置

Terminal window
#查看本地化设置
localectl
#设置本地化参数。
sudo localectl set-locale LANG=en_GB.utf8
sudo localectl set-keymap en_GB

【5】timedatectl#

timedatectl 命令用于查看当前时区设置

Terminal window
# 查看当前时区设置
timedatectl
#显示所有可用的时区
timedatectl list-timezones
#设置当前时区
sudo timedatectl set-timezone America/New_York
sudo timedatectl set-time YYYY-MM-DD
sudo timedatectl set-time HH:MM:SS

【6】loginctl#

loginctl 命令用于查看当前登录的用户

Terminal window
# 列出当前session
loginctl list-sessions
#列出当前登录用户
loginctl list-users
#列出显示指定用户的信息
loginctl show-user ruanyf

【四】Unit#

【1】含义#

systemd 管理的对象统称为 Unit(单元)。常见类型如下;具体支持的类型与 systemd 版本有关。

Service unit:系统服务

Target unit:多个Unit构成的一个组

Device Unit:硬件设备

Mount Unit:文件系统的挂载点

Automount Unit:自动挂载点

Path Unit:文件或路径

Scope Unit:不是由Systemd启动的外部进程

Slice Unit:进程组

Socket Unit:进程间通信的socket

Swap Unit:swap文件

Timer Unit:定时器

systemctl list-units命令可以查看当前系统的所有Unit。

Terminal window
#列出正在运行的Unit
systemctl list-units
#列出所有Unit,包括没有找到配置文件的或者启动失败的
systemctl list-units --all
#列出所有没有运行的Unit
systemctl list-units --all --state=inactive
#列出所有加载失败的Unit
systemctl list-units --failed
#列出所有正在运行的、类型为service的Unit
systemctl list-units --type=service

【2】Unit的状态#

systemctl status命令用于查看系统状态和单个Unit的状态。

Terminal window
#显示系统状态
systemctl status
#显示单个Unit的状态
systemctl status bluetooth.service
Terminal window
linux@ubuntu:~$ systemctl status bluetooth.service
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2024-09-24 11:05:44 CST; 9h ago
Docs: man:bluetoothd(8)
Main PID: 963 (bluetoothd)
Status: "Running"
Tasks: 1 (limit: 4551)
Memory: 1.8M
CPU: 75ms
CGroup: /system.slice/bluetooth.service
└─963 /usr/lib/bluetooth/bluetoothd
9月 24 11:05:44 ubuntu systemd[1]: Starting Bluetooth service...
9月 24 11:05:44 ubuntu bluetoothd[963]: Bluetooth daemon 5.64
9月 24 11:05:44 ubuntu bluetoothd[963]: Starting SDP server
9月 24 11:05:44 ubuntu systemd[1]: Started Bluetooth service.
9月 24 11:05:44 ubuntu bluetoothd[963]: Bluetooth management interface 1.22 initialized

除了status命令,systemctl还提供了三个查询状态的简单方法,主要供脚本内部的判断语句使用。

Terminal window
#显示某个Unit是否正在运行
systemctl is-active application.service
#显示某个Unit是否处于启动失败状态
systemctl is-failed application.service
#显示某个Unit服务是否建立了启动链接
systemctl is-enabled application.service

【3】Unit管理#

对于用户来说,最常用的是下面这些命令,用于启动和停止Unit(主要是service)。

Terminal window
#立即启动一个服务
sudo systemctl start apache.service
#立即停止一个服务
sudo systemctl stop apache.service
#重启一个服务
sudo systemctl restart apache.service
# 向服务的控制组发送信号;会终止进程,应谨慎使用
sudo systemctl kill apache.service
#重新加载一个服务的配置文件
sudo systemctl reload apache.service
#重载所有修改过的配置文件
sudo systemctl daemon-reload
#显示某个 Unit 的所有底层参数
systemctl show httpd.service
#显示某个 Unit 的指定属性的值
systemctl show -p CPUWeight httpd.service
#设置某个 Unit 的指定属性
sudo systemctl set-property httpd.service CPUWeight=500

【4】依赖关系#

Unit之间存在依赖关系:A依赖于B,就意味着Systemd在启动A的时候,同时会去启动B。

systemctl list-dependencies命令列出一个Unit的所有依赖。

Terminal window
systemctl list-dependencies nginx.service

上面命令的输出结果之中,有些依赖是Target类型(详见下文),默认不会展开显示。如果要展开Target,就需要使用—all参数。

Terminal window
systemctl list-dependencies --all nginx.service

【五】服务管理实例:SSH 服务#

【1】查看系统是否安装ssh服务#

Terminal window
linux@ubuntu:~$ apt policy openssh-server
ssh:
已安装:(无)
候选: 1:7.6p1-4ubuntu0.7
版本列表:
1:7.6p1-4ubuntu0.7 500
500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main amd64 Packages
500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main i386 Packages
1:7.6p1-4ubuntu0.5 500
500 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 Packages
500 http://mirrors.yun-idc.com/ubuntu bionic-security/main i386 Packages
1:7.6p1-4 500
500 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 Packages
500 http://mirrors.yun-idc.com/ubuntu bionic/main i386 Packages

【2】安装ssh服务#

Terminal window
linux@ubuntu:~$ sudo apt install openssh-server # 安装 SSH 服务端;客户端命令通常由 openssh-client 提供

【3】查看 SSH 服务是否启动#

Ubuntu/Debian 的服务单元通常名为 ssh.service,而 RHEL 系发行版常为 sshd.service。先用 systemctl list-unit-files | grep -E '^ssh(d)?\.service' 确认本机名称。下例以 Ubuntu 的 ssh.service 为准。

Terminal window
linux@ubuntu:~$ systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-08-29 09:22:52 CST; 57s ago
Main PID: 2815 (sshd)
Tasks: 1 (limit: 2283)
CGroup: /system.slice/ssh.service
└─2815 /usr/sbin/sshd -D
8月 29 09:22:52 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
8月 29 09:22:52 ubuntu sshd[2815]: Server listening on 0.0.0.0 port 22.
8月 29 09:22:52 ubuntu sshd[2815]: Server listening on :: port 22.
8月 29 09:22:52 ubuntu systemd[1]: Started OpenBSD Secure Shell server.

【4】关闭服务#

注意: 若正通过 SSH 远程连接该主机,停止或禁用 SSH 服务会使后续连接失败。请确认具备本地控制台或其他恢复方式。

Terminal window
linux@ubuntu:~$ sudo systemctl stop ssh.service
linux@ubuntu:~$ systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2022-08-29 09:25:36 CST; 3s ago
Process: 2815 ExecStart=/usr/sbin/sshd -D $SSHD_OPTS (code=exited, status=0/SUCCESS)
Main PID: 2815 (code=exited, status=0/SUCCESS)
8月 29 09:22:52 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
8月 29 09:22:52 ubuntu sshd[2815]: Server listening on 0.0.0.0 port 22.
8月 29 09:22:52 ubuntu sshd[2815]: Server listening on :: port 22.
8月 29 09:22:52 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
8月 29 09:25:36 ubuntu systemd[1]: Stopping OpenBSD Secure Shell server...
8月 29 09:25:36 ubuntu sshd[2815]: Received signal 15; terminating.
8月 29 09:25:36 ubuntu systemd[1]: Stopped OpenBSD Secure Shell server.

【5】关闭自动运行服务#

Terminal window
linux@ubuntu:~$ sudo systemctl disable ssh.service
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable ssh
Removed /etc/systemd/system/sshd.service.
linux@ubuntu:~$
linux@ubuntu:~$ sudo systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; disabled; vendor preset: enabled)
Active: inactive (dead)
8月 29 09:22:52 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
8月 29 09:22:52 ubuntu sshd[2815]: Server listening on 0.0.0.0 port 22.
8月 29 09:22:52 ubuntu sshd[2815]: Server listening on :: port 22.
8月 29 09:22:52 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
8月 29 09:25:36 ubuntu systemd[1]: Stopping OpenBSD Secure Shell server...
8月 29 09:25:36 ubuntu sshd[2815]: Received signal 15; terminating.
8月 29 09:25:36 ubuntu systemd[1]: Stopped OpenBSD Secure Shell server.
linux@ubuntu:~$ sudo reboot # 系统重启后,查看 SSH 服务状态
linux@ubuntu:~$ systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; disabled; vendor preset: enabled)
Active: inactive (dead)

【6】设置开机自动运行服务#

Terminal window
linux@ubuntu:~$ sudo systemctl enable --now ssh.service # 设置开机启动并立即启动服务
Synchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable ssh
Created symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.
linux@ubuntu:~$ systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: inactive (dead)
linux@ubuntu:~$ sudo reboot # 系统重启后,查看 SSH 服务状态
linux@ubuntu:~$ systemctl status ssh.service
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2022-08-29 09:36:30 CST; 31s ago
Process: 967 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Process: 961 ExecReload=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Process: 762 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 780 (sshd)
Tasks: 1 (limit: 2283)
CGroup: /system.slice/ssh.service
└─780 /usr/sbin/sshd -D
8月 29 09:36:30 ubuntu systemd[1]: Starting OpenBSD Secure Shell server...
8月 29 09:36:30 ubuntu sshd[780]: Server listening on 0.0.0.0 port 22.
8月 29 09:36:30 ubuntu sshd[780]: Server listening on :: port 22.
8月 29 09:36:30 ubuntu systemd[1]: Started OpenBSD Secure Shell server.
8月 29 09:36:30 ubuntu systemd[1]: Reloading OpenBSD Secure Shell server.
8月 29 09:36:30 ubuntu systemd[1]: Reloaded OpenBSD Secure Shell server.
8月 29 09:36:30 ubuntu sshd[780]: Received SIGHUP; restarting.
8月 29 09:36:30 ubuntu sshd[780]: Server listening on 0.0.0.0 port 22.
8月 29 09:36:30 ubuntu sshd[780]: Server listening on :: port 22.

【补充】验证与故障排查#

用 systemctl is-active 服务名 与 systemctl is-enabled 服务名 分别检查当前运行和开机启动状态;两者互不等同。启动失败时运行 systemctl status 服务名、journalctl -u 服务名 -b。修改 unit 文件后先 sudo systemctl daemon-reload 再重启服务。

参考资料#

systemctl 手册;systemd.service 手册。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!

打赏
Linux 系统服务管理:SysV init 与 systemd
https://firefly.cuteleaf.cn/posts/circuit-design/linux/1-base-system/130系统服务管理/
作者
Dream
发布于
2026-03-05
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
Dream
Hello, I'm Dream.
公告
欢迎来到我的博客!爱学习爱进步。
分类
标签
最新动态
站点统计
文章
367
分类
6
标签
201
总字数
804,820
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Vercel
博客版本
Firefly v6.16.8
文章许可
CC BY-NC-SA 4.0
文章目录