Systemctl 与 Systemd 全面指南:Linux 系统服务管理详解

什么是 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:关闭并重新启动机器
相关推荐
是小胡嘛5 小时前
C++之Any类的模拟实现
linux·开发语言·c++
口袋物联6 小时前
设计模式之工厂模式在 C 语言中的应用(含 Linux 内核实例)
linux·c语言·设计模式·简单工厂模式
qq_479875436 小时前
X-Macros(1)
linux·服务器·windows
笨笨聊运维8 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
jun_bai8 小时前
python写的文件备份网盘程序
运维·服务器·网络
爱吃牛肉的大老虎8 小时前
网络传输架构之gRPC讲解
网络·架构
Warren988 小时前
Python自动化测试全栈面试
服务器·网络·数据库·mysql·ubuntu·面试·职场和发展
HIT_Weston9 小时前
39、【Ubuntu】【远程开发】拉出内网 Web 服务:构建静态网页(二)
linux·前端·ubuntu
欢喜躲在眉梢里9 小时前
CANN 异构计算架构实操指南:从环境部署到 AI 任务加速全流程
运维·服务器·人工智能·ai·架构·计算
云飞云共享云桌面10 小时前
无需配置传统电脑——智能装备工厂10个SolidWorks共享一台工作站
运维·服务器·前端·网络·算法·电脑