公司动态

掌握Linux服务管理:systemd全面指南

📅 2026/7/23 4:16:58
掌握Linux服务管理:systemd全面指南
Linux 服务管理systemd 介绍基本概念CentOS 7 使用 Systemd 引导系统启动速度最快所有进程无论有无依赖关系则都是并行启动很多时候进程没有真正启动而是只有一个信号或者说是标记而已在真正利用的时候才会真正启动。Systemd为了解决上文的问题而诞生。它的目标是为系统的启动和管理提供一套完整的解决方案。系统引导程序用户空间systemdpid为1内核空间kthreaddpid为2服务从业务角度来称呼例如 web 服务数据库服务。守护进程daemonweb 服务器对外提供 web 服务由 web 相关的进程提供支持。以web服务为例服务web服务守护进程httpdsystemd 架构守护进程systemd由守护进程干活。工具systemctl提供给用户要、用来跟机器交互的工具。unit 类型systemctl 命令用于管理不同类型的系统对象这些对象称之为units。Service unit用于定义系统服务文件扩展名为**.service**例如httpd.serviceSocket unit用于标识进程间通信用的 socket文件文件扩展名为.socketTarget unit用于模拟实现“运行级别”文件扩展名为.targetTimer unit用于管理计划任务文件扩展名为.timerDevice unit用于定义内核识别的设备文件扩展名为.deviceMount unit用于定义文件系统挂载点文件扩展名为.mountSnapshot unit管理系统快照文件扩展名为.snapshotSwap unit用于标识swap设备文件扩展名为.swapAutomount unit文件系统的自动挂载点文件扩展名为.automountPath unit用于根据文件系统上特定对象的变化来启动其他服务文件扩展名为.pathSlice unit用于资源管理文件扩展名为.slice查看单个 unit 信息[rootcentos7 ~19:23:23]# systemctl status httpd.service● httpd.service - The Apache HTTP Server Loaded: loaded(/usr/lib/systemd/system/httpd.service;disabled;vendor preset: disabled)Active: active(running)since Wed2026-07-2219:23:23 CST;2s ago Docs: man:httpd(8)man:apachectl(8)Main PID:3769(httpd)...关键字概述loaded单元配置文件已处理active(running)正在运行active(exited)已成功完成一次性配置active(waiting)运行中正在等待事件inactive不再运行enabled系统引导时启动disabled系统引导时不启动static无法启动依赖其他单元启动# 安装软件包[rootcentos7 ~17:05:10]# yum install -y httpd# 启动服务[rootcentos7 ~17:45:50]# systemctl start httpd# 查看进程[rootcentos7 ~17:49:16]# ps -C httpd fPID TTY STAT TIME COMMAND3663? Ss0:00 /usr/sbin/httpd-DFOREGROUND3682? S0:00\_ /usr/sbin/httpd-DFOREGROUND3683? S0:00\_ /usr/sbin/httpd-DFOREGROUND3684? S0:00\_ /usr/sbin/httpd-DFOREGROUND3685? S0:00\_ /usr/sbin/httpd-DFOREGROUND3686? S0:00\_ /usr/sbin/httpd-DFOREGROUND控制系统服务# 停止服务[rootcentos7 ~18:53:10]# systemctl stop sshd.service# 客户端连接测试[rootcentos7 ~18:53:26]# ssh csrcentos7 hostnamessh: Could not resolvehostnamecentos7: Name orservicenot known# 启动服务[rootcentos7 ~18:54:10]# systemctl start sshd.service# 客户端连接测试[rootcentos7 ~18:54:39]# ssh csrcentos7 hostnamessh: Could not resolvehostnamecentos7: Name orservicenot know# 重启服务相当于stop再start[rootcentos7 ~18:57:46]# systemctl restart sshd.service# 一般用于配置文件变动后重新加载# 重新加载服务服务对应的主进程不会重启只会重新加载一次配置文件。[rootcentos7 ~18:58:03]# systemctl reload sshd.service# 禁止服务开机自启[rootcentos7 ~18:58:08]# systemctl disable sshd.serviceRemoved symlink /etc/systemd/system/multi-user.target.wants/sshd.service.[rootcentos7 ~18:58:39]# systemctl is-enabled sshddisabled# 重启系统验证[rootcentos7 ~18:58:51]# reboot# 设置服务开机自启[rootcentos7 ~19:07:46]# systemctl enable sshd.service[rootcentos7 ~19:09:31]# systemctl is-enabled sshdenabled[rootcentos7 ~19:09:43]# reboot# 禁用服务服务被禁用后将无法start因为服务的配置文件指向/dev/null[rootcentos7 ~19:10:19]# systemctl mask sshd.serviceCreated symlink from /etc/systemd/system/sshd.service to /dev/null.[rootcentos7 ~19:10:39]# systemctl start sshd.serviceFailed to start sshd.service: Unit is masked.[rootcentos7 ~19:11:03]## 取消禁用[rootcentos7 ~19:11:12]# systemctl unmask sshd.serviceRemoved symlink /etc/systemd/system/sshd.service.[rootcentos7 ~19:11:26]# systemctl start sshd.service[rootcentos7 ~19:11:29]#[rootcentos7 ~19:12:48]# 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 Wed2026-07-2219:07:07 CST;5min ago Docs: man:sshd(8)man:sshd_config(5)Main PID:1067(sshd)CGroup: /system.slice/sshd.service └─1067 /usr/sbin/sshd-D...unit 配置文件unit 配置文件存放在多个位置/etc /systemd/system/unit.service优先生效。一般是管理员自定义的配置。/usr/lib /systemd/system/unit.service其次生效。软件包自带的默认配置。配置文件说明示例单元文件/usr/lib/systemd/system/sshd.service说明# 标识该部分为 Unit 配置用于描述服务的基本信息、依赖关系等。[Unit]# 服务的描述信息说明这是 OpenSSH 服务器守护进程便于管理员识别服务用途。DescriptionOpenSSH server daemon# 指定服务的文档路径这里指向 sshd 命令的手册页man 8 sshd和配置文件的手册页man 5 sshd_config方便用户查阅帮助。Documentationman:sshd(8)man:sshd_config(5)# 定义服务的启动顺序sshd 服务必须在 network.target网络服务就绪和 sshd-keygen.serviceSSH 密钥生成服务之后启动确保依赖的资源已准备好。Afternetwork.target sshd-keygen.service# 表示 sshd 服务 希望 sshd-keygen.service 运行但不是强制依赖。如果 sshd-keygen.service 启动失败sshd 仍会尝试启动通常用于生成初始 SSH 密钥若密钥已存在则不影响。Wantssshd-keygen.service# 标识该部分为 Service 配置用于定义服务的启动方式、执行命令、重启策略等。[Service]# 定义服务的类型为 notify表示服务启动后会主动通知 systemd 自己已就绪通过 sd_notify() 函数systemd 会等待这个通知后再继续后续流程确保服务真正可用。Typenotify# 指定环境变量文件的路径/etc/sysconfig/sshd 中通常定义 OPTIONS 等变量如额外的 sshd 启动参数这些变量会被后续的 ExecStart 引用。EnvironmentFile/etc/sysconfig/sshd# 服务启动时执行的命令ExecStart/usr/sbin/sshd-D$OPTIONS# /usr/sbin/sshdsshd 守护进程的可执行文件路径。# -D表示 sshd 以非守护进程模式运行前台运行因为 systemd 通常管理前台进程便于监控。# $OPTIONS引用 EnvironmentFile 中定义的额外参数如 -p 2222 指定端口。# 服务重载配置时执行的命令ExecReload/bin/kill-HUP$MAINPID# kill -HUP 发送 SIGHUP 信号给 sshd 主进程使其重新加载配置文件无需重启服务。# $MAINPID 是 systemd 自动维护的服务主进程 ID。# 定义服务停止时的杀死模式process 表示只杀死服务的主进程sshd 主进程其子进程如已建立的 SSH 连接会被保留避免强制中断现有连接。KillModeprocess# 定义服务的重启策略当服务因非正常退出如崩溃、信号终止时systemd 会自动重启服务正常退出如主动停止则不重启。Restarton-failure# 服务重启前的等待时间这里设置为 42 秒避免频繁重启导致资源耗尽。RestartSec42s# 标识该部分为 Install 配置用于定义服务如何被 启用即系统启动时自动运行。[Install]# 表示当系统启动到 multi-user.target多用户命令行模式非图形界面时该服务会被自动启动。这是服务器的默认运行级别确保 SSH 服务在系统启动后可用。WantedBymulti-user.target开发一个 study 服务开发 studyd 服务主程序 study脚本说明这是一个无限循环的脚本每 5 秒会向/var/log/study.log文件中追加一行包含当前时间的日志内容为[时间]: IM studying [ Linux ]。[rootcentos7 ~15:01:51]# vim /usr/local/bin/study#!/bin/bashwhiletruedoDATE$(date)echo$DATE: Im studying [ Linux ]/var/log/study.logsleep5done[rootcentos7 ~15:09:06]# chmod x /usr/local/bin/study创建 studyd 服务单元文件[rootcentos7 ~15:09:28]# cp /usr/lib/systemd/system/sshd.service \/etc/systemd/system/studyd.service[rootcentos7 ~15:11:55]# vim /etc/systemd/system/studyd.service[rootcentos7 ~15:15:08]# systemctl daemon-reload[rootcentos7 ~15:15:56]# systemctl enable studyd.service --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/studyd.service to /etc/systemd/system/studyd.service.[rootcentos7 ~15:16:26]#[rootcentos7 ~15:17:14]# systemctl status studyd.service● studyd.service - study server daemon Loaded: loaded(/etc/systemd/system/studyd.service;enabled;vendor preset: disabled)Active: active(running)since Wed2026-07-2215:16:26 CST;1min 0s ago Main PID:4784(study)Tasks:2CGroup: /system.slice/studyd.service ├─4784 /bin/bash /usr/local/bin/study └─4829sleep5Jul2215:16:26 centos7.cheng.cloud systemd[1]: Started study server daemon.[rootcentos7 ~15:17:26]#[rootcentos7 ~15:17:48]#[rootcentos7 ~15:17:48]# tail -f /var/lolocal/ lock/ log/验证日志[rootcentos7 ~15:17:48]# tail -f /var/log/study.logWed Jul2215:17:21 CST2026: Im studying [ Linux ] Wed Jul 22 15:17:26 CST 2026: Im studying[Linux]Wed Jul2215:17:31 CST2026: Im studying [ Linux ] Wed Jul 22 15:17:36 CST 2026: Im studying[Linux]Wed Jul2215:17:41 CST2026: Im studying [ Linux ] Wed Jul 22 15:17:46 CST 2026: Im studying[Linux]...