Linux systemd 系统服务管理一、前言对比Windows 系统可通过services.msc打开服务管理器管理软件启停、开机自启CentOS 7 及以上版本统一使用systemd作为系统初始化与服务管理器是 Linux 下管理后台服务、开机自启的标准方案。二、systemd 基础概念1 systemd 优势并行启动无依赖服务大幅缩短开机时间按需激活服务不会提前常驻访问时才启动节约资源统一管理系统所有资源服务、挂载、定时任务等通过 CGroup 分组管理进程杀死服务不会残留子进程。### 2 系统两大核心初始进程用户空间一号进程systemdPID 1系统所有进程始祖负责系统初始化、服务管控内核空间进程kthreaddPID 2所有内核线程父进程。### 3 服务 vs 守护进程daemon区分服务业务层面统称对外提供功能守护进程后台长期运行、无终端依附的程序是服务底层实现。httpd 基础实操演示# 安装web服务软件包[rootcentos7~17:09:20]# yum install -y httpd# 启动httpd服务[rootcentos7~17:09:33]# systemctl start httpd# 树形查看httpd所有守护进程[rootcentos7~17:14:09]# ps -C httpd fPID TTY STAT TIME COMMAND3158? Ss0:00 /usr/sbin/httpd-DFOREGROUND3159? S0:00\_ /usr/sbin/httpd-DFOREGROUND3160? S0:00\_ /usr/sbin/httpd-DFOREGROUND3161? S0:00\_ /usr/sbin/httpd-DFOREGROUND3162? S0:00\_ /usr/sbin/httpd-DFOREGROUND3163? S0:00\_ /usr/sbin/httpd-DFOREGROUND主进程 PID3158其余为工作子进程全部属于 httpd 服务的守护进程。三、systemd 整体架构核心后台程序systemdPID 1负责调度、监控全部系统资源用户交互工具systemctl管理员操作服务的统一命令管理最小单元Unit所有系统资源都封装为 Unit 配置文件。## 四、systemd 十种 Unit 类型| Unit类型 | 文件后缀 | 作用说明 || ---- | ---- | ---- || Service |.service| 系统后台服务最常用httpd/sshd || Socket |.socket| 进程间通信套接字 || Target |.target| 模拟系统运行级别服务分组 || Timer |.timer| 定时任务替代 crontab || Device |.device| 内核识别硬件设备 || Mount |.mount| 文件系统挂载配置 || Automount |.automount| 文件系统自动挂载 || Swap |.swap| 交换分区配置 || Path |.path| 文件变动触发服务启动 || Slice |.slice| 进程资源限制分组 |五、Unit 列表查看命令1 查看当前已加载运行的所有 Unit[rootcentos7~17:16:11]# systemctl list-units输出字段说明UNIT单元名称LOAD配置文件是否成功解析加载ACTIVE单元整体激活状态SUB细分运行状态DESCRIPTION单元功能描述2 指定类型过滤 unit示例查看定时器 timerliuyifeicentos7 ~17:17:20$ systemctl list-units-ttimer UNIT LOAD ACTIVE SUB DESCRIPTION systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Director unbound-anchor.timer loaded active waiting daily update of the root trust anch LOADReflects whether the unit definition was properly loaded. ACTIVEThe high-level unit activation state, i.e. generalization of SUB. SUBThe low-level unit activation state, values depend on unit type.2loadedunitslisted. Pass--allto see loaded but inactive units, too. To show all installed unit files usesystemctl list-unit-files.3 查看所有 service 单元包含运行/停止liuyifeicentos7 ~17:18:30$ systemctl list-units--typeservice--all4 查看系统全部unit文件含未加载liuyifeicentos7 ~17:19:54 $ systemctl list-unit-files5 查看启动失败的异常服务liuyifeicentos7 ~17:20:39$ systemctl--failed--typeservice六、查看单个服务详细状态以 sshd 远程服务为例liuyifeicentos7 ~17:22:01$ systemctl status sshd ● sshd.service - OpenSSH server daemon Loaded: loaded(/usr/lib/systemd/system/sshd.service;enabled;vendor preset: enabled)Active: active(running)since 三2026-07-2216:54:52 CST;27min ago Docs: man:sshd(8)man:sshd_config(5)Main PID:1197(sshd)Tasks:1CGroup: /system.slice/sshd.service └─1197 /usr/sbin/sshd-D7月2216:54:52 centos7.liuyifei.cloud systemd[1]: Starting OpenSSH server daemon...7月2216:54:52 centos7.liuyifei.cloud sshd[1197]: Server listening on0.0.0.0 po....7月2216:54:52 centos7.liuyifei.cloud sshd[1197]: Server listening on :: port22.7月2216:54:52 centos7.liuyifei.cloud systemd[1]: Started OpenSSH server daemon.7月2216:54:54 centos7.liuyifei.cloud sshd[1443]: Accepted passwordforliuyifei...27月2217:02:36 centos7.liuyifei.cloud sshd[2875]: Accepted passwordforliuyifei...27月2217:02:39 centos7.liuyifei.cloud sshd[2924]: Accepted passwordforroot fro...2 Hint: Some lines were ellipsized, use-lto showinfull.状态关键字释义关键字含义loaded单元配置文件解析完成active(running)服务正在运行active(exited)一次性任务执行完毕active(waiting)运行中等待外部事件inactive服务已停止enabled开机自动启动disabled开机不启动static无法手动启动依赖其他单元触发七、systemctl 服务管理核心命令表命令功能描述systemctl status UNIT查看服务完整状态、PID、日志systemctl start UNIT立即启动服务systemctl stop UNIT停止运行中的服务systemctl restart UNIT重启服务先 stop 再 startsystemctl reload UNIT热重载配置不重启主进程systemctl enable UNIT设置开机自启加--now同步启动systemctl disable UNIT取消开机自启加--now同步停止systemctl is-enabled UNIT查询是否开机自启systemctl mask UNIT彻底锁定服务无法手动/开机启动systemctl unmask UNIT解除服务锁定恢复可用sshd 实操完整示例# 停止 ssh 远程服务现有连接会断开[rootcentos7~17:28:26]# systemctl stop sshd# 测试 ssh 连接连接失败[rootcentos7~17:29:10]# ssh liuyifei10.1.8.10ssh: connect tohost10.1.8.10 port22: Connection refused# 启动 ssh 服务[rootcentos7~17:30:25]# systemctl start sshd# 测试 ssh 连接恢复正常[rootcentos7 ~]# ssh laomacentos7 hostname# 重启服务[rootcentos7~18:33:22]# ssh liuyifei10.1.8.10liuyifei10.1.8.10s password: Last login: Wed Jul2217:27:172026from10.1.8.1 liuyifeicentos7 ~18:33:34$# 修改配置后重载不断开现有 ssh 会话[rootcentos7~18:35:21]# systemctl reload sshd# 关闭开机自启[rootcentos7~18:36:08]# systemctl disable sshdRemoved symlink /etc/systemd/system/multi-user.target.wants/sshd.service.[rootcentos7~18:37:01]# systemctl is-enabled sshddisabled# 重启系统验证不自启[rootcentos7~18:37:20]# reboot# 开启开机自启[rootcentos7~18:42:30]# systemctl enable sshdCreated symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.[rootcentos7~18:43:42]# systemctl start sshd[rootcentos7~18:44:18]# systemctl is-enabled sshdenabled# 彻底屏蔽ssh服务无法start启动[rootcentos7~18:48:13]# systemctl mask sshdCreated symlink from /etc/systemd/system/sshd.service to /dev/null.[rootcentos7~18:51:49]# systemctl stop sshd[rootcentos7~18:52:01]# systemctl start sshdFailed to start sshd.service: Unit is masked.# 解除屏蔽[rootcentos7~18:52:25]# systemctl unmask sshdRemoved symlink /etc/systemd/system/sshd.service.八、service单元配置文件详解配置文件优先级从上至下优先级递减/etc/systemd/system/xxx.service管理员自定义优先级最高/usr/lib/systemd/system/xxx.service软件安装自带默认配置。三大配置区块以 sshd.service 为例[Unit] 区块服务基础信息、依赖、启动顺序[Unit] # 服务描述 DescriptionOpenSSH server daemon # 内置帮助文档 Documentationman:sshd(8) man:sshd_config(5) # 必须在网络、密钥服务启动后再启动 sshd Afternetwork.target sshd-keygen.service # 弱依赖密钥服务失败不影响 sshd 运行 Wantssshd-keygen.service[Service] 区块运行规则、启停指令、重启策略[Service] # 服务类型启动后主动通知systemd就绪 Typenotify # 外部环境变量文件 EnvironmentFile/etc/sysconfig # 前台启动命令 ExecStart/usr/sbin/sshd -D $OPTIONS # 重载配置执行指令发送HUP信号 ExecReload/bin/kill -HUP $MAINPID # 仅杀死主进程保留已建立连接 KillModeprocess # 程序崩溃自动重启手动停止不重启 Restarton-failure # 崩溃后延迟42秒再重启避免高频重启 RestartSec42s[Install] 区块定义开机启动目标[Install] # 多用户命令行模式下自动启动 WantedBymulti-user.target九、实战自定义 study 系统服务需求编写循环脚本每5秒写入时间日志到/var/log/study.log注册为systemd 系统服务开机自启。步骤 1编写全局执行脚本[rootcentos7~19:00:39]# vim /usr/local/bin/study#!/bin/bash# 无限循环whiletruedo# 获取当前时间DATE$(date)# 追加写入日志echo$DATE: IM studying [ Linux ]/var/log/study.log# 延迟 5 秒sleep5done# 添加执行权限[rootcentos7 ~19:00:51]# chmod x /usr/local/bin/study步骤2创建自定义服务单元文件复制 sshd 配置作为模板简化修改[rootcentos7~19:00:42]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/studyd.service[rootcentos7~19:18:54]# vim /etc/systemd/system/studyd.service精简配置内容[Unit] Descriptionstudy server daemon [Service] ExecStart/usr/local/bin/study [Install] WantedBymulti-user.target步骤 3重载 systemd 识别新配置修改/新增 service 文件后必须执行否则不生效[rootcentos7~19:26:26]# systemctl daemon-reload步骤 4设置开机自启并立即启动服务[rootcentos7~19:33:14]# systemctl enable studyd --now步骤 5查看 studyd 服务运行状态[rootcentos7~19:34:34]# systemctl status studyd● studyd.service - study server daemon Loaded: loaded(/etc/systemd/system/studyd.service;enabled;vendor preset: disabled)Active: active(running)since 三2026-07-2219:34:34 CST;25s ago Main PID:3956(study)Tasks:2CGroup: /system.slice/studyd.service ├─3956 /bin/bash /usr/local/bin/study └─3970sleep57月2219:34:34 centos7.liuyifei.cloud systemd[1]: Started study server daemon.步骤6实时验证日志输出liuyifeicentos7 ~19:34:00$tail-f/var/log/study.log2026年 07月22日 星期三19:35:14 CST: IM studying [ Linux ] 2026年 07月 22日 星期三 19:35:19 CST: IM studying[Linux]2026年 07月22日 星期三19:35:24 CST: IM studying [ Linux ] 2026年 07月 22日 星期三 19:35:29 CST: IM studying[Linux]2026年 07月22日 星期三19:35:34 CST: IM studying [ Linux ] 2026年 07月 22日 星期三 19:35:39 CST: IM studying[Linux]2026年 07月22日 星期三19:35:44 CST: IM studying[Linux]日志每 5 秒持续新增自定义服务部署完成。十、总结CentOS 7 使用 systemd 管理系统与后台服务所有管理资源统一抽象为 Unit.service 单元用来管控守护进程。服务配置分为软件自带与管理员自定义两个目录拥有不同优先级。依靠 systemctl 工具可以完成服务启停、自启管理、配置重载等运维操作。我们能够自行编写业务脚本创建对应的 service 配置文件将普通脚本注册为系统服务实现开机自启、标准化托管。同时需要区分修改配置文件和修改业务脚本对应的不同生效方式理解 disable 与 mask 的管控差异。