什么是 Systemd 和 Systemctl?
Systemd 是一套系统管理守护进程、工具和库的集合,用于替代传统的 System V init 守护进程。它作为类 UNIX 系统的中央管理和配置平台,在大多数主流 Linux 发行版中已成为标准配置。
Systemctl 则是用于控制 systemd 系统和服务管理器的实用工具,是与 systemd 交互的主要命令行接口。
1. Systemd 与 Systemctl 基础入门
1.1 检查系统是否安装了 systemd 及其当前版本
bash
systemctl --version
示例输出:
systemd 252 (252-51.el9)
+PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN -IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY +P11KIT -QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD -BPF_FRAMEWORK +XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified
1.2 检查 systemd 和 systemctl 的安装位置
bash
whereis systemd
whereis systemctl
示例输出:
systemd: /usr/lib/systemd /usr/lib64/systemd /etc/systemd /usr/share/systemd /usr/share/man/man1/systemd.1.gz
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz
1.3 检查 systemd 是否正在运行
bash
ps -eaf | grep systemd
该输出显示系统中所有正在运行的与 systemd 相关的进程,其中 PID 为 1 的 /usr/lib/systemd/systemd
进程是系统的初始化进程(init 进程),它是所有其他进程的父进程。
1.4 分析 systemd 启动过程
bash
systemd-analyze
示例输出:
Startup finished in 5.033s (firmware) + 6.556s (loader) + 851ms (kernel) + 1.518s (initrd) + 36.634s (userspace) = 50.594s
graphical.target reached after 36.601s in userspace.
1.5 分析启动时每个进程所花费的时间
bash
systemd-analyze blame
1.6 分析启动时的关键链
bash
systemd-analyze critical-chain
1.7 列出所有可用单元
bash
systemctl list-unit-files
重要提示 :Systemctl 接受服务(.service
)、挂载点(.mount
)、套接字(.socket
)和设备(.device
)作为单元。
1.8 列出所有正在运行的单元
bash
systemctl list-units
1.9 列出所有故障单元
bash
systemctl --failed
1.10 检查某个单元是否已启用
bash
systemctl is-enabled crond.service
1.11 检查某个单元或服务是否正在运行
bash
systemctl status firewalld.service
2. 使用 Systemctl 控制和管理服务
2.1 列出所有服务
bash
systemctl list-unit-files --type=service
2.2 服务的启动、重启、停止、重新加载和状态检查
以 httpd.service 为例:
bash
systemctl start httpd.service # 启动服务
systemctl restart httpd.service # 重启服务
systemctl stop httpd.service # 停止服务
systemctl reload httpd.service # 重新加载配置
systemctl status httpd.service # 检查状态
注意:使用 start、restart、stop 和 reload 命令时,终端不会显示输出,只有 status 命令会显示输出。
2.3 服务的激活和开机自启配置
bash
systemctl is-active httpd.service # 检查是否活跃
systemctl enable httpd.service # 启用开机自启
systemctl disable httpd.service # 禁用开机自启
2.4 服务的屏蔽和取消屏蔽
bash
systemctl mask httpd.service # 屏蔽服务(使其无法启动)
systemctl unmask httpd.service # 取消屏蔽服务
2.5 终止服务
bash
systemctl kill httpd
systemctl status httpd
3. 使用 Systemctl 控制和管理挂载点
3.1 列出所有系统挂载点
bash
systemctl list-unit-files --type=mount
3.2 挂载点的管理操作
bash
systemctl start tmp.mount
systemctl stop tmp.mount
systemctl restart tmp.mount
systemctl reload tmp.mount
systemctl status tmp.mount
3.3 挂载点的激活和开机自启配置
bash
systemctl is-active tmp.mount
systemctl enable tmp.mount
systemctl disable tmp.mount
3.4 挂载点的屏蔽和取消屏蔽
bash
systemctl mask tmp.mount
systemctl unmask tmp.mount
4. 使用 Systemctl 控制和管理套接字
4.1 列出所有可用的系统套接字
bash
systemctl list-unit-files --type=socket
4.2 套接字的管理操作
以 cups.socket 为例:
bash
systemctl start cups.socket
systemctl restart cups.socket
systemctl stop cups.socket
systemctl reload cups.socket
systemctl status cups.socket
4.3 套接字的激活和开机自启配置
bash
systemctl is-active cups.socket
systemctl enable cups.socket
systemctl disable cups.socket
4.4 套接字的屏蔽和取消屏蔽
bash
systemctl mask cups.socket
systemctl unmask cups.socket
5. 高级系统控制功能
5.1 获取服务的当前 CPU 份额
bash
systemctl show -p CPUShares httpd.service
注意:默认情况下,每个服务的 CPUShare = 1024。
5.2 限制服务的 CPU 份额
bash
systemctl set-property httpd.service CPUShares=2000
systemctl show -p CPUShares httpd.service
注意:设置 CPUShare 后,会创建以服务名称命名的目录(httpd.service.d),其中包含配置文件。
5.3 检查服务的所有配置详细信息
bash
systemctl show httpd
5.4 分析服务的关键链
bash
systemd-analyze critical-chain httpd.service
5.5 获取服务的依赖项列表
bash
systemctl list-dependencies httpd.service
5.6 按层次结构列出控制组(cgroups)
bash
systemd-cgls
5.7 根据资源使用情况列出控制组
bash
systemd-cgtop
6. 控制系统运行级别
6.1 启动系统救援模式
bash
systemctl rescue
6.2 进入紧急模式
bash
systemctl emergency
6.3 查看当前运行级别
bash
systemctl get-default
6.4 切换到运行级别 5(图形模式)
bash
systemctl isolate runlevel5.target
# 或
systemctl isolate graphical.target
6.5 切换到运行级别 3(多用户模式)
bash
systemctl isolate runlevel3.target
# 或
systemctl isolate multiuser.target
6.6 设置默认运行级别
bash
systemctl set-default runlevel3.target # 多用户模式
systemctl set-default runlevel5.target # 图形模式
6.7 系统电源管理
bash
systemctl reboot # 重启
systemctl halt # 关机
systemctl suspend # 挂起(待机)
systemctl hibernate # 休眠
systemctl hybrid-sleep # 混合睡眠
运行级别说明
- Runlevel 0:关闭并切断系统电源
- Runlevel 1:救援/单用户维护模式
- Runlevel 3:多用户,非图形系统
- Runlevel 4:多用户,非图形系统(通常未使用或自定义)
- Runlevel 5:多用户,图形系统
- Runlevel 6:关闭并重新启动机器