linux服务管理核心是 systemd,熟练使用 systemctl 和 journalctl 即可覆盖 90% 运维场景:查状态、启停、重载、自启控制及日志排查,修改 unit 文件后需 daemon-reload 生效。

Linux 服务管理核心在于掌握 systemd 这一现代初始化系统,熟练使用 systemctl 命令就能覆盖 90% 日常运维场景。不用记一 堆脚本路径,也不用反复查 init.d,统一 接口 让操作更直接、反馈更明确。
查服务状态:一眼看清运行与否
判断服务是否正常,不能只看“active”,还要关注子状态(如 running 或exited)和最近日志:
-
systemctl status nginx—— 查看详细状态、启动时间、主进程 PID、最近几条日志 -
systemctl is-active nginx—— 返回active或inactive,适合写在监控脚本里 -
systemctl is-enabled nginx—— 检查是否开机自启(enabled/disabled)
启停与重载:别再硬杀进程
用 systemctl 操作服务,能确保依赖关系被正确处理,避免手动 kill 导致状态不一致:
-
systemctl start sshd—— 启动(不设开机自启) -
systemctl stop redis—— 停止(不会禁用开机自启) -
systemctl restart httpd—— 先 stop 再 start,适合配置更新后 -
systemctl reload nginx—— 仅重载配置(不中断连接),比 restart 更轻量
开机自启控制:按需启用,不盲目开
不是所有服务都要开机启动。数据库、Web 服务器通常需要,而调试用的临时服务建议禁用:
-
systemctl enable docker—— 写入开机启动(生成 symlink 到/etc/systemd/system/multi-user.target.wants/) -
systemctl disable mysql—— 移除开机启动项 -
systemctl mask apache2—— 彻底禁止启动(创建指向/dev/NULL的硬链接,防误启)
查日志与排障:服务出问题,先看 journal
systemd 统一管理日志,不用翻 /var/log/ 下多个文件:
-
journalctl -u nginx -n 50—— 查 nginx 最近 50 行日志 -
journalctl -u postfix --since "2 hours ago"—— 查 2 小时内 postfix 日志 -
journalctl -u mysql -f—— 实时跟踪 mysql 日志(类似tail -f) -
journalctl --list-boots—— 列出每次启动记录,配合-b -1查上次启动的日志
基本上就这些。用熟 systemctl 几个高频命令,配合 journalctl 定位问题,服务管理就不再靠猜、不靠重启大法。不复杂但容易忽略的是:每次改完 unit 文件(比如/etc/systemd/system/myapp.service),记得运行systemctl daemon-reload,否则新配置不会生效。