Linux 系统服务管理:SysV init 与 systemd
- 1Linux 系统概述:历史、发行版与 Ubuntu
- 2Linux引入
- 3Ubuntu Linux 安装:镜像、虚拟机与初始化
- 4Linux之系统服务
- 5Shell 基础:命令行与常用命令
- 6Linux之进程管理
- 7Vim 编辑器:模式、编辑与配置
- 8Linux之计划任务
- 9Linux 环境变量:作用域、配置与生效
- 10Linux初识
- 11Linux 软件包管理:RPM、DEB 与 APT
- 12Linux之网络
- 13Linux 用户与权限基础
- 14Linux之Shell
- 15Linux 文件管理:目录、文件类型、权限与常用操作
- 16Linux之文件管理
- 17Linux 磁盘管理:分区、格式化、挂载与空间查看
- 18Linux之用户管理
- 19Linux 进程管理:ps、kill、pstree 与作业控制
- 20Linux之基本权限
- 21Linux 网络管理:接口、路由、连通性与 NetworkManager
- 22Linux之特殊权限
- 23Linux 时间管理:date、hwclock、timedatectl 与时区
- 24Linux之软件管理
- 25Linux 系统服务管理:SysV init 与 systemd本文
- 26修改Linux登录时提示信息
- 27Shell 编程:变量、流程控制、函数与脚本
- 28【VIM编辑功能总结】
- 29Makefile 与 CMake:构建自动化基础
- 30简洁好用的Linux运维管理面板
- 31Linux 常用服务搭建:TFTP、NFS、Samba 与 SSH
- 32Linux 安装 fd , 替代 find 命令
- 33Linux知识点总结
【一】由来
传统 SysV init 通过 /etc/init.d/ 启动脚本管理服务;许多 systemd 系统仍保留 service 命令和这些脚本作为兼容接口。
sudo /etc/init.d/apache2 start#或者service apache2 start这种方法有两个缺点:
一是依赖关系表达能力有限,传统启动脚本常按既定顺序执行,容易拉长启动时间。
二是启动脚本复杂。init进程只是执行启动脚本,不管其他事情。脚本需要自己处理各种情况,这往往使得脚本变得很长。
【二】Systemd概述
Systemd就是为了解决这些问题而诞生的。
它的设计目标是,为系统的启动和管理提供一套完整的解决方案,根据Linux惯例,字母d是守护进程(daemon)的缩写,Systemd这个名字的含义,就是它要守护整个系统。

(上图为Systemd作者Lennart Poettering)
在采用 systemd 的发行版中,systemd 通常作为 PID 1 运行,并负责启动和监管大部分系统服务。并非所有 Linux 发行版都使用 systemd;也存在 OpenRC、runit、SysV init 等实现。
systemctl --version #命令查看Systemd的版本。linux@ubuntu:~$ systemctl --versionsystemd 237+PAM +AUDIT +SELINUX +IMA +APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL+XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD -IDN2 +IDN -PCRE2 default-hierarchy=hybridSystemd的优点是功能强大,使用方便,缺点是体系庞大,非常复杂。事实上,现在还有很多人反对使用Systemd,理由就是它过于复杂,与操作系统的其他部分强耦合,不符合 KISS(Keep It Simple, Stupid,保持简单)原则;这是对设计取舍的一种观点。
【三】系统管理
【1】systemctl
systemd 是一套初始化与服务管理组件;systemctl 是其主要管理命令。
systemctl 是systemd的主命令,用于管理系统
#重启系统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 命令用于查看启动耗时
#查看启动耗时systemd-analyze#查看每个服务的启动耗时systemd-analyze blame#显示瀑布状的启动过程流systemd-analyze critical-chain#显示指定服务的启动流systemd-analyze critical-chain atd.service【3】hostnamectl
hostnamectl 命令用于查看当前主机的信息
#显示当前主机的信息hostnamectl#设置主机名。sudo hostnamectl set-hostname rhel7【4】localectl
localectl 命令用于查看本地化设置
#查看本地化设置localectl#设置本地化参数。sudo localectl set-locale LANG=en_GB.utf8sudo localectl set-keymap en_GB【5】timedatectl
timedatectl 命令用于查看当前时区设置
# 查看当前时区设置timedatectl#显示所有可用的时区timedatectl list-timezones#设置当前时区sudo timedatectl set-timezone America/New_Yorksudo timedatectl set-time YYYY-MM-DDsudo timedatectl set-time HH:MM:SS【6】loginctl
loginctl 命令用于查看当前登录的用户
# 列出当前sessionloginctl 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。
#列出正在运行的Unitsystemctl list-units#列出所有Unit,包括没有找到配置文件的或者启动失败的systemctl list-units --all#列出所有没有运行的Unitsystemctl list-units --all --state=inactive#列出所有加载失败的Unitsystemctl list-units --failed#列出所有正在运行的、类型为service的Unitsystemctl list-units --type=service【2】Unit的状态
systemctl status命令用于查看系统状态和单个Unit的状态。
#显示系统状态systemctl status#显示单个Unit的状态systemctl status bluetooth.servicelinux@ubuntu:~$ systemctl status bluetooth.service● bluetooth.service - Bluetooth serviceLoaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)Active: active (running) since Tue 2024-09-24 11:05:44 CST; 9h agoDocs: man:bluetoothd(8)Main PID: 963 (bluetoothd)Status: "Running"Tasks: 1 (limit: 4551)Memory: 1.8MCPU: 75msCGroup: /system.slice/bluetooth.service└─963 /usr/lib/bluetooth/bluetoothd9月 24 11:05:44 ubuntu systemd[1]: Starting Bluetooth service...9月 24 11:05:44 ubuntu bluetoothd[963]: Bluetooth daemon 5.649月 24 11:05:44 ubuntu bluetoothd[963]: Starting SDP server9月 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还提供了三个查询状态的简单方法,主要供脚本内部的判断语句使用。
#显示某个Unit是否正在运行systemctl is-active application.service#显示某个Unit是否处于启动失败状态systemctl is-failed application.service#显示某个Unit服务是否建立了启动链接systemctl is-enabled application.service【3】Unit管理
对于用户来说,最常用的是下面这些命令,用于启动和停止Unit(主要是service)。
#立即启动一个服务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的所有依赖。
systemctl list-dependencies nginx.service上面命令的输出结果之中,有些依赖是Target类型(详见下文),默认不会展开显示。如果要展开Target,就需要使用—all参数。
systemctl list-dependencies --all nginx.service【五】服务管理实例:SSH 服务
【1】查看系统是否安装ssh服务
linux@ubuntu:~$ apt policy openssh-serverssh:已安装:(无)候选: 1:7.6p1-4ubuntu0.7版本列表:1:7.6p1-4ubuntu0.7 500500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main amd64 Packages500 http://mirrors.yun-idc.com/ubuntu bionic-updates/main i386 Packages1:7.6p1-4ubuntu0.5 500500 http://mirrors.yun-idc.com/ubuntu bionic-security/main amd64 Packages500 http://mirrors.yun-idc.com/ubuntu bionic-security/main i386 Packages1:7.6p1-4 500500 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 Packages500 http://mirrors.yun-idc.com/ubuntu bionic/main i386 Packages【2】安装ssh服务
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 为准。
linux@ubuntu:~$ systemctl status ssh.service● ssh.service - OpenBSD Secure Shell serverLoaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2022-08-29 09:22:52 CST; 57s agoMain PID: 2815 (sshd)Tasks: 1 (limit: 2283)CGroup: /system.slice/ssh.service└─2815 /usr/sbin/sshd -D8月 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 服务会使后续连接失败。请确认具备本地控制台或其他恢复方式。
linux@ubuntu:~$ sudo systemctl stop ssh.servicelinux@ubuntu:~$ systemctl status ssh.service● ssh.service - OpenBSD Secure Shell serverLoaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)Active: inactive (dead) since Mon 2022-08-29 09:25:36 CST; 3s agoProcess: 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】关闭自动运行服务
linux@ubuntu:~$ sudo systemctl disable ssh.serviceSynchronizing state of ssh.service with SysV service script with /lib/systemd/systemd-sysv-install.Executing: /lib/systemd/systemd-sysv-install disable sshRemoved /etc/systemd/system/sshd.service.linux@ubuntu:~$linux@ubuntu:~$ sudo systemctl status ssh.service● ssh.service - OpenBSD Secure Shell serverLoaded: 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 serverLoaded: loaded (/lib/systemd/system/ssh.service; disabled; vendor preset: enabled)Active: inactive (dead)【6】设置开机自动运行服务
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 sshCreated symlink /etc/systemd/system/sshd.service → /lib/systemd/system/ssh.service.linux@ubuntu:~$ systemctl status ssh.service● ssh.service - OpenBSD Secure Shell serverLoaded: 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 serverLoaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)Active: active (running) since Mon 2022-08-29 09:36:30 CST; 31s agoProcess: 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 -D8月 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 再重启服务。
参考资料
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!














