systemd:Linux系统管理的进化与实践

2026-01-02 12:56:06 · 作者: AI Assistant · 浏览: 5

systemd 是 Linux 系统中用于启动和管理守护进程的工具,已成为大多数发行版的标准配置。本文将深入探讨其核心命令和配置管理,帮助读者掌握系统管理与开发的关键技能。

Systemd 是 Linux 系统中用于启动和管理守护进程的工具,已成为大多数发行版的标准配置。它解决了传统 init 进程在启动时间和脚本复杂性方面的不足,提供了更强大的功能和更简单的管理方式。本文将详细介绍 systemd 的基本命令和配置管理,帮助读者更好地理解和使用这一工具。

系统管理命令

systemd 提供了一系列强大的命令,用于管理系统和其各种单元(Unit)。以下是一些常用命令及其用途:

systemctl

systemctl 是 systemd 的主命令,用于管理系统的服务、目标、挂载点等。它提供了多种操作,如启动、停止、重启服务,以及管理系统的启动和关闭。

  • 重启系统 bash $ sudo systemctl reboot
  • 关闭系统,切断电源 bash $ sudo systemctl poweroff
  • 停止 CPU 工作 bash $ sudo systemctl halt
  • 暂停系统 bash $ sudo systemctl suspend
  • 让系统进入冬眠状态 bash $ sudo systemctl hibernate
  • 让系统进入交互式休眠状态 bash $ sudo systemctl hybrid-sleep
  • 启动进入救援状态(单用户状态) bash $ sudo systemctl rescue

systemd-analyze

systemd-analyze 命令用于查看启动时间,帮助分析系统的性能。

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

hostnamectl

hostnamectl 命令用于查看和设置主机名。

  • 显示当前主机的信息 bash $ hostnamectl
  • 设置主机名 bash $ sudo hostnamectl set-hostname rhel7

localectl

localectl 命令用于查看和设置本地化参数,如语言和键盘布局。

  • 查看本地化设置 bash $ localectl
  • 设置本地化参数 bash $ sudo localectl set-locale LANG=en_GB.utf8 $ sudo localectl set-keymap en_GB

timedatectl

timedatectl 命令用于查看和设置系统时区和时间。

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

loginctl

loginctl 命令用于查看当前登录的用户和会话信息。

  • 列出当前 session bash $ loginctl list-sessions
  • 列出当前登录用户 bash $ loginctl list-users
  • 列出指定用户的信息 bash $ loginctl show-user ruanyf

Unit 的概念与管理

在 systemd 中,所有系统资源都被抽象为 Unit。Unit 是 systemd 管理的基本单元,分为12种类型,主要包括系统服务(Service unit)、目标(Target unit)、硬件设备(Device unit)、文件系统挂载点(Mount unit)等。

Unit 的状态

systemctl status 命令用于查看系统状态和单个 Unit 的状态。此外,systemctl 还提供了三个查询状态的简单方法,主要供脚本内部的判断语句使用。

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

Unit 管理命令

以下是一些常用的 Unit 管理命令,用于启动和停止 Unit(主要是 service):

  • 立即启动一个服务 bash $ sudo systemctl start apache.service
  • 立即停止一个服务 bash $ sudo systemctl stop apache.service
  • 重启一个服务 bash $ sudo systemctl restart apache.service
  • 杀死一个服务的所有子进程 bash $ sudo systemctl kill apache.service
  • 重新加载一个服务的配置文件 bash $ sudo systemctl reload apache.service
  • 重载所有修改过的配置文件 bash $ sudo systemctl daemon-reload
  • 显示某个 Unit 的所有底层参数 bash $ systemctl show httpd.service
  • 显示某个 Unit 的指定属性的值 bash $ systemctl show -p CPUShares httpd.service
  • 设置某个 Unit 的指定属性 bash $ sudo systemctl set-property httpd.service CPUShares=500

依赖关系

Unit 之间存在依赖关系,这意味着在启动某个 Unit 时,Systemd 会同时启动其依赖的其他 Unit。systemctl list-dependencies 命令可以列出一个 Unit 的所有依赖。

  • 列出一个 Unit 的所有依赖 bash $ systemctl list-dependencies nginx.service
  • 列出所有依赖,包括 Target 类型 bash $ systemctl list-dependencies --all nginx.service

Unit 的配置文件

每个 Unit 都有一个配置文件,用于定义如何启动和管理该 Unit。systemd 默认从 /etc/systemd/system/ 读取配置文件,但其中大部分文件是符号链接,指向 /usr/lib/systemd/system/

配置文件的状态

systemctl list-unit-files 命令用于列出所有配置文件,可以查看它们的状态,包括是否启用、禁用、静态或被屏蔽。

  • 列出所有配置文件 bash $ systemctl list-unit-files
  • 列出指定类型的配置文件 bash $ systemctl list-unit-files --type=service

配置文件的格式

配置文件是普通的文本文件,可以用文本编辑器打开。systemctl cat 命令可以查看配置文件的内容。

  • 查看配置文件的内容 bash $ systemctl cat atd.service

配置文件的区块

配置文件通常分为几个区块,每个区块的第一行用方括号表示区别名,如 [Unit][Service]。每个区块内部是一些等号连接的键值对。

  • [Unit] 区块
  • Description:简短描述
  • Documentation:文档地址
  • Requires:当前 Unit 依赖的其他 Unit
  • Wants:与当前 Unit 配合的其他 Unit
  • BindsTo:与 Requires 类似,它指定的 Unit 如果退出,会导致当前 Unit 停止运行
  • Before:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之后启动
  • After:如果该字段指定的 Unit 也要启动,那么必须在当前 Unit 之前启动
  • Conflicts:这里指定的 Unit 不能与当前 Unit 同时运行
  • Condition...:当前 Unit 运行必须满足的条件,否则不会运行
  • Assert...:当前 Unit 运行必须满足的条件,否则会报启动失败

  • [Install] 区块

  • WantedBy:当前 Unit 激活时符号链接会放入 /etc/systemd/system 目录下面以 Target 名 + .wants 后缀构成的子目录中
  • RequiredBy:当前 Unit 激活时,符号链接会放入 /etc/systemd/system 目录下面以 Target 名 + .required 后缀构成的子目录中
  • Alias:当前 Unit 可用于启动的别名
  • Also:当前 Unit 激活(enable)时,会被同时激活的其他 Unit

  • [Service] 区块

  • Type:定义启动时的进程行为
    • Type=simple:默认值,执行 ExecStart 指定的命令,启动主进程
    • Type=forking:以 fork 方式从父进程创建子进程,创建后父进程会立即退出
    • Type=oneshot:一次性进程,Systemd 会等当前服务退出,再继续往下执行
    • Type=dbus:当前服务通过 D-Bus 启动
    • Type=notify:当前服务启动完毕,会通知 Systemd,再继续往下执行
    • Type=idle:若有其他任务执行完毕,当前服务才会运行
  • ExecStart:启动当前服务的命令
  • ExecStartPre:启动当前服务之前执行的命令
  • ExecStartPost:启动当前服务之后执行的命令
  • ExecReload:重启当前服务时执行的命令
  • ExecStop:停止当前服务时执行的命令
  • ExecStopPost:停止当前服务之后执行的命令
  • RestartSec:自动重启当前服务间隔的秒数
  • Restart:定义何种情况 Systemd 会自动重启当前服务,可能的值包括 alwayson-successon-failureon-abnormalon-aborton-watchdog
  • TimeoutSec:定义 Systemd 停止当前服务之前等待的秒数
  • Environment:指定环境变量

Target 的概念与管理

Target 是 systemd 中用于管理一组 Unit 的机制,类似于传统的 RunLevel。使用 Target 可以方便地管理多个 Unit 的启动和关闭,而无需逐一操作每个服务。

查看 Target 信息

  • 查看当前系统的所有 Target bash $ systemctl list-unit-files --type=target
  • 查看一个 Target 包含的所有 Unit bash $ systemctl list-dependencies multi-user.target
  • 查看启动时的默认 Target bash $ systemctl get-default
  • 设置启动时的默认 Target bash $ sudo systemctl set-default multi-user.target
  • 切换 Target 时,默认不关闭前一个 Target 启动的进程,systemctl isolate 命令改变这种行为,关闭前一个 Target 里面所有不属于后一个 Target 的进程 bash $ sudo systemctl isolate multi-user.target

Target 与传统 RunLevel 的对应关系

  • Traditional runlevel:1(单用户模式)、2(多用户模式,无网络)、3(多用户模式,有网络)、4(未使用)、5(图形界面)、6(关机)
  • New targetmulti-user.target(对应传统 runlevel 3)、graphical.target(对应传统 runlevel 5)、rescue.target(对应单用户模式)

实践与最佳实践

在实际应用中,掌握 systemd 的命令和配置文件是至关重要的。以下是一些最佳实践和注意事项:

配置文件管理

  • 启用服务 bash $ sudo systemctl enable [email protected]
  • 禁用服务 bash $ sudo systemctl disable [email protected]

依赖关系管理

  • 查看某个服务的依赖关系 bash $ systemctl list-dependencies nginx.service
  • 查看所有依赖,包括 Target 类型 bash $ systemctl list-dependencies --all nginx.service

启动和停止服务

  • 启动服务 bash $ sudo systemctl start apache.service
  • 停止服务 bash $ sudo systemctl stop apache.service
  • 重启服务 bash $ sudo systemctl restart apache.service
  • 杀死服务的所有子进程 bash $ sudo systemctl kill apache.service
  • 重新加载配置文件 bash $ sudo systemctl daemon-reload $ sudo systemctl restart httpd.service

配置文件的格式

  • 查看配置文件 bash $ systemctl cat atd.service

依赖关系

  • 查看某个服务的依赖关系 bash $ systemctl list-dependencies nginx.service
  • 查看所有依赖,包括 Target 类型 bash $ systemctl list-dependencies --all nginx.service

结论

systemd 是 Linux 系统中用于启动和管理守护进程的工具,已成为大多数发行版的标准配置。它解决了传统 init 进程在启动时间和脚本复杂性方面的不足,提供了更强大的功能和更简单的管理方式。掌握 systemd 的命令和配置文件是至关重要的,可以帮助读者更好地理解和使用这一工具进行系统管理和开发。通过合理配置和使用 systemd,可以显著提高系统的稳定性和性能。

关键字列表: systemd, systemctl, Unit, Target, 配置文件, 依赖关系, 启动耗时, 进程管理, 系统服务, 符号链接, 环境变量, 重启间隔