)
Linux Systemd服务管理从入门到实战自定义服务全套命令在 Linux 运维工作中服务管理是重中之重。开机自启、服务启停、配置重载、守护进程托管、异常重启全部依赖 Systemd 机制。CentOS 7 / Ubuntu 16.04 全部默认采用 Systemd 管理系统与服务彻底替代传统 init 机制。本文从零讲解 Systemd 原理、Unit 单元、全套 systemctl 命令、配置文件详解最后带大家手写开发一个自定义守护服务适合实训、面试、工作落地一、Systemd 是什么核心优势1.1 Windows 与 Linux 自启对比Windows 开机自动启动程序、后台服务可通过命令services.msc图形化管理。LinuxCentOS7使用Systemd统一管理所有系统服务、开机流程、守护进程。1.2 Systemd 核心特点并行启动无依赖的服务全部并行启动大幅加快开机速度按需启动很多服务仅做标记不真正占用资源被访问时才真正启动统一管理进程、服务、挂载、定时、设备全部抽象为 Unit 管理自动重启支持服务崩溃自动重启保障业务常驻Systemd 的设计目标为系统启动、服务生命周期、资源管控提供一套完整解决方案。1.3 系统两大顶级进程PID 1 / PID 2内核空间kthreaddPID2负责内核层所有后台线程用户空间systemdPID1所有用户进程的父进程接管系统所有服务与进程管理二、服务与守护进程Daemon通俗理解2.1 核心概念区分服务业务层面的名称对外提供能力Web服务、数据库服务、培训服务守护进程后台常驻程序真正干活的进程静默运行、对外响应请求2.2 经典类比互联网业务服务Web 网站服务守护进程httpd / nginx2.3 实操httpd 服务与守护进程对应关系# 安装httpd服务[rootcentos7 ~18:33:14]# yum -y install httpd# 启动服务[rootcentos7 ~18:58:09]# systemctl start httpd# 查看服务守护进程树[rootcentos7 ~18:58:56]# ps -C httpd fPID TTY STAT TIME COMMAND4887? Ss0:00 /usr/sbin/httpd-DFOREGROUND4893? S0:00\_ /usr/sbin/httpd-DFOREGROUND4894? S0:00\_ /usr/sbin/httpd-DFOREGROUND4895? S0:00\_ /usr/sbin/httpd-DFOREGROUND4896? S0:00\_ /usr/sbin/httpd-DFOREGROUND4897? S0:00\_ /usr/sbin/httpd-DFOREGROUND可见httpd 服务由主进程多个子守护进程共同提供服务。三、Systemd 架构核心守护进程systemdPID1后台常驻全权管理系统资源用户操作工具systemctl运维唯一交互命令实现所有服务管理操作四、Systemd 十大 Unit 单元类型Systemd 将系统所有可管理资源抽象为Unit 单元不同后缀对应不同功能。单元类型后缀作用说明Service.service系统服务管理最常用Socket.socket进程间通信套接字Target.target运行级别、服务组管理Timer.timer定时任务替代crontabDevice.device内核硬件设备管理Mount.mount文件系统挂载管理Swap.swap交换分区管理Automount.automount自动挂载Path.path监控文件变化触发服务Slice.slice系统资源限制与分组五、Unit 查看命令大全5.1 查看各类单元列表# 查看已加载所有unit[fangbingcentos7 ~19:02:54]$ systemctl list-units UNIT LOAD ACTIVE SUB DESCRIPTION proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Poi sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active plugged VMware_Virtual_IDE_CDROM_Drive Cent sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged VMware_Virtual_S1sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged VMware_Virtual_S2sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged VMware_Virtual_S# 只查看定时器单元-t指定类型[fangbingcentos7 ~19:04:26]$ systemctl list-units-ttimer UNIT LOAD ACTIVE SUB DESCRIPTION systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories unbound-anchor.timer loaded active waiting daily update of the root trust anchorforDNSSEC 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.# 查看所有服务含开启/关闭[fangbingcentos7 ~19:04:57]$ systemctl list-units--typeservice--allUNIT LOAD ACTIVE SUB DESCRIPTION abrt-ccpp.service loaded active exited Install ABRT coredump hook abrt-oops.service loaded active running ABRT kernel log watcher abrt-vmcore.service loaded inactive dead Harvest vmcoresforABRT abrt-xorg.service loaded active running ABRT Xorg log watcher abrtd.service loaded active running ABRT Automated Bug Reporting Tool accounts-daemon.service loaded active running Accounts Service apparmor.service not-found inactive dead apparmor.service# 查看系统所有单元配置文件[fangbingcentos7 ~19:07:08]$ systemctl list-unit-files UNIT FILE STATE proc-sys-fs-binfmt_misc.automount static dev-hugepages.mount static dev-mqueue.mount static proc-fs-nfsd.mount static proc-sys-fs-binfmt_misc.mount static run-vmblock\x2dfuse.mount disabled# 查看启动失败的服务当前没有失败单元服务[fangbingcentos7 ~19:10:09]$ systemctl--failed--typeservice0loadedunitslisted. Pass--allto see loaded but inactive units, too. To show all installed unit files usesystemctl list-unit-files.# 查看单个服务详细信息[fangbingcentos7 ~19:11:05]$ systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded(/usr/lib/systemd/system/sshd.service;enabled;vendor preset: enabled)Active: active(running)since 三2026-07-2216:59:17 CST;2h 14min ago Docs: man:sshd(8)man:sshd_config(5)Main PID:1219(sshd)Tasks:1CGroup: /system.slice/sshd.service └─1219 /usr/sbin/sshd-D7月2216:59:17 centos7.fangbing.cloud systemd[1]: Starting OpenSSH server daemon...7月2216:59:17 centos7.fangbing.cloud sshd[1219]: Server listening on0.0.0.0 port22.7月2216:59:17 centos7.fangbing.cloud sshd[1219]: Server listening on :: port22.7月2216:59:17 centos7.fangbing.cloud systemd[1]: Started OpenSSH server daemon.7月2216:59:38 centos7.fangbing.cloud sshd[1898]: Accepted passwordforfangbing from10.1.8.1 port50556ssh27月2218:12:09 centos7.fangbing.cloud sshd[3646]: Accepted passwordforroot from10.1.8.1 port59563ssh27月2218:16:08 centos7.fangbing.cloud sshd[3791]: Accepted passwordforroot from10.1.8.1 port59695ssh25.2 字段含义说明UNIT单元名称LOAD配置文件是否成功加载解析ACTIVE高级别运行状态SUB详细子状态DESCRIPTION服务描述5.3 服务状态关键字释义loaded配置已加载active(running)正在运行active(exited)一次性任务执行完毕active(waiting)运行中、等待事件触发inactive已停止enabled开机自启开启disabled开机自启关闭static不可手动启动依赖别的服务触发六、systemctl 服务控制核心命令生产常用命令功能说明systemctl status 服务名查看服务状态、日志、PIDsystemctl start 服务名启动服务systemctl stop 服务名停止服务systemctl restart 服务名重启服务stopstartsystemctl reload 服务名重载配置不重启主进程systemctl enable 服务名设置开机自启systemctl disable 服务名关闭开机自启systemctl mask 服务名彻底禁用服务无法启动systemctl unmask 服务名解除禁用systemctl is-enabled 服务名查看是否开机自启6.1 实操演示sshd 服务启停、自启管理# 停止服务[rootcentos7 ~18:59:09]# systemctl stop sshd.service[rootcentos7 ~19:15:54]# systemctl is-active sshdinactive# 启动服务[rootcentos7 ~19:16:11]# systemctl start sshd[rootcentos7 ~19:16:43]# systemctl is-active sshdactive# 重启服务相当于stop再start[rootcentos7 ~19:16:54]# systemctl restart sshd# 重载配置修改sshd_config后使用重新加载服务服务对应的主进程不会重启只会重新加载一次配置文件[rootcentos7 ~19:17:32]# systemctl reload sshd# 关闭开机自启[rootcentos7 ~19:18:21]# systemctl disable sshdRemoved symlink /etc/systemd/system/multi-user.target.wants/sshd.service.[rootcentos7 ~19:20:01]# systemctl is-enabled sshddisabled# 开启开机自启[rootcentos7 ~19:20:22]# systemctl enable sshdCreated symlink from /etc/systemd/system/multi-user.target.wants/sshd.service to /usr/lib/systemd/system/sshd.service.[rootcentos7 ~19:21:11]# systemctl is-enabled sshdenabled# 彻底禁用服务锁死[rootcentos7 ~19:21:25]# systemctl mask sshdCreated symlink from /etc/systemd/system/sshd.service to /dev/null.# 重启验证无法远程连接且无法statr原因是服务被禁用后将无法start因为服务的配置文件指向/dev/null[rootcentos7 ~19:22:01]# reboot[rootcentos7 ~19:24:28]# systemctl status sshd● sshd.service Loaded: masked(/dev/null;bad)Active: inactive(dead)7月2219:23:05 centos7.fangbing.cloud systemd[1]: Cannotadddependency job... Warning: sshd.service changed on disk. Runsystemctl daemon-reloadto reload units. Hint: Some lines were ellipsized, use-lto showinfull.# 解锁服务可以开启服务[rootcentos7 ~19:24:43]# systemctl unmask sshd[rootcentos7 ~19:26:17]# systemctl start sshd[rootcentos7 ~19:27:30]# 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-2219:27:30 CST;3s ago Docs: man:sshd(8)man:sshd_config(5)Main PID:2790(sshd)Tasks:1CGroup: /system.slice/sshd.service └─2790 /usr/sbin/sshd-D7月2219:27:30 centos7.fangbing.cloud systemd[1]: Starting OpenSSH server d...7月2219:27:30 centos7.fangbing.cloud sshd[2790]: Server listening on0.0.0...7月2219:27:30 centos7.fangbing.cloud sshd[2790]: Server listening on :: po...7月2219:27:30 centos7.fangbing.cloud systemd[1]: Started OpenSSH server da... Hint: Some lines were ellipsized, use-lto showinfull.七、Systemd 配置文件路径与详解7.1 配置文件优先级/etc/systemd/system/自定义配置优先级最高/usr/lib/systemd/system/系统默认、软件自带配置7.2 标准 service 文件三大模块详解以 sshd 为例[Unit] 区域服务描述与依赖Description服务功能描述Documentation帮助文档路径After依赖服务指定在哪些服务启动完毕再启动当前服务Wants弱依赖依赖服务失败不影响当前服务启动[Service] 区域服务运行参数Typenotify服务启动后主动通知 systemd 就绪EnvironmentFile加载环境变量配置文件ExecStart服务启动命令ExecReload重载配置命令KillMode进程杀死模式Restarton-failure异常退出自动重启RestartSec重启等待间隔时间[Install] 区域开机自启配置WantedBymulti-user.target多用户模式下开机启动八、企业级实战自定义开发 study 守护服务需求开发一个自定义 Linux 守护服务后台永久运行每5秒写入一条日志模拟业务常驻进程。8.1 编写业务脚本[rootcentos7 ~19:27:44]# vim /usr/local/bin/study脚本内容#!/bin/bash# 第一行内容是脚本的 解释器声明shebang指定该脚本使用 /bin/bash 作为解释器执行。系统会根据这一行找到对应的 shell 程序来解析后续命令。# 启动一个无限循环while 是循环关键字true 是一个永远为真的条件因此这个循环会一直执行下去直到被外部终止如 CtrlC。whiletrue# 循环体的开始标记do 和后面的 done 之间的内容是循环中重复执行的命令。do# 执行 date 命令获取当前系统时间并通过 $(...) 捕获其输出将结果赋值给变量 DATE。DATE$(date)# echo 命令输出字符串其中 $DATE 会被替换为变量的值# 是追加重定向符号将输出内容追加到 /var/log/study.log 文件中# 最终输出内容类似 Fri Oct 31 10:00:00 CST 2025: IM studying [ Linux ]。echo$DATE: IM studying [ Linux ]/var/log/study.log# 让脚本暂停执行 5 秒sleep 命令用于延迟单位默认为秒避免循环执行过快。sleep5# 循环体的结束标记与前面的 while 和 do 配合标志着一次循环的结束。done8.2 赋予执行权限[rootcentos7 ~19:30:57]# chmod x /usr/local/bin/study8.3 新建 systemd 服务配置文件[rootcentos7 ~19:31:49]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/studyd.service[rootcentos7 ~19:34:24]# vim /etc/systemd/system/studyd.service精简配置内容[Unit]Descriptionstudy server daemon[Service]ExecStart/usr/local/bin/study[Install]WantedBymulti-user.target8.4 重载配置、启动并开机自启# 重载unit配置[rootcentos7 ~19:35:44]# systemctl daemon-reload# 开机自启并立即启动服务[rootcentos7 ~19:36:15]# systemctl enable studyd --now# 查看服务状态[rootcentos7 ~19:36:40]# systemctl status studyd.service● studyd.service - study server daemon Loaded: loaded(/etc/systemd/system/studyd.service;enabled;vendor preset: disabled)Active: active(running)since 三2026-07-2219:23:12 CST;13min ago Main PID:719(study)CGroup: /system.slice/studyd.service ├─719/bin/bash /usr/local/bin/study └─3306sleep57月2219:23:12 centos7.fangbing.cloud systemd[1]: Started study server daemon.8.5 验证日志自动输出[rootcentos7 ~19:46:55]# tail -f /var/log/study.log2026年 07月22日 星期三19:46:35 CST: IM studying [ Linux ] 2026年 07月 22日 星期三 19:46:40 CST: IM studying[Linux]2026年 07月22日 星期三19:46:45 CST: IM studying [ Linux ] 2026年 07月 22日 星期三 19:46:50 CST: IM studying[Linux]2026年 07月22日 星期三19:46:55 CST: IM studying [ Linux ] 2026年 07月 22日 星期三 19:47:00 CST: IM studying[Linux]效果每5秒自动新增一行时间日志服务永久后台守护运行。九、总结1. Systemd 是 CentOS7 统一服务管理方案并行启动、按需加载、自动重启极大提升运维效率。2. 服务是业务概念守护进程是真实运行的后台进程二者一一对应。3. 熟练掌握systemctl 启停、自启、禁用、重载是运维基础能力。ux ]2026年 07月 22日 星期三 19:46:45 CST: I’M studying [ Linux ]2026年 07月 22日 星期三 19:46:50 CST: I’M studying [ Linux ]2026年 07月 22日 星期三 19:46:55 CST: I’M studying [ Linux ]2026年 07月 22日 星期三 19:47:00 CST: I’M studying [ Linux ]效果每5秒自动新增一行时间日志服务永久后台守护运行。 ## 九、总结 1\. Systemd 是 CentOS7\ 统一服务管理方案**并行启动、按需加载、自动重启**极大提升运维效率。 2\. 服务是业务概念守护进程是真实运行的后台进程二者一一对应。 3\. 熟练掌握 **systemctl 启停、自启、禁用、重载** 是运维基础能力。 4\. 掌握 service 配置文件三大模块可**自定义开发任意后台守护服务**适配企业自定义业务进程托管需求。