«

Linux服务管理,systemctl常用命令汇总

时间:2026-10-5 07:51     作者:emer     分类: 无


前言

CentOS7 装完软件,怎么启动?怎么开机自启?服务挂了怎么查日志?这些都需要用到 systemctl 命令。CentOS7 开始用 systemd 代替了原来的 service 命令,systemctl 就是 systemd 的管理工具。本文就把 systemctl 常用命令汇总一下,日常管理服务够用了。

启动和停止服务

启动服务

systemctl start nginx

停止服务

systemctl stop nginx

重启服务

systemctl restart nginx

重新加载配置

systemctl reload nginx

注意: reload 和 restart 的区别:

查看服务状态

查看服务状态

systemctl status nginx

输出会显示服务是不是在运行、PID、内存占用、最近的日志等。

查看服务是不是在运行

systemctl is-active nginx

输出 active 就是在运行,inactive 就是没运行。

查看服务是不是开机自启

systemctl is-enabled nginx

输出 enabled 就是开机自启,disabled 就是不自启。

开机自启管理

设置开机自启

systemctl enable nginx

取消开机自启

systemctl disable nginx

同时启动并设置开机自启

systemctl enable --now nginx

一条命令搞定,不用分两步。

查看日志

查看服务日志

journalctl -u nginx

实时跟踪日志

journalctl -u nginx -f

和 tail -f 类似,实时滚动显示日志。

查看最近100行日志

journalctl -u nginx -n 100

查看今天的日志

journalctl -u nginx --since today

查看指定时间的日志

journalctl -u nginx --since "2026-10-05 10:00:00" --until "2026-10-05 11:00:00"

查看所有服务

列出所有正在运行的服务

systemctl list-units --type=service

列出所有服务(包括没运行的)

systemctl list-unit-files --type=service

按状态过滤

systemctl list-units --type=service --state=running

常用服务管理示例

启动 Nginx 并设置开机自启

systemctl start nginx
systemctl enable nginx

或者一条命令:

systemctl enable --now nginx

重启 Nginx

systemctl restart nginx

查看 Nginx 状态

systemctl status nginx

查看 Nginx 日志

journalctl -u nginx -f

常见坑

坑1:还在用 service 命令

CentOS6 用 service nginx start,CentOS7 开始用 systemctl start nginx。老命令还能用,但新系统推荐用 systemctl。

坑2:改了配置不生效

改了 Nginx 配置,忘了 reload,结果配置没生效。

解决: 改完配置记得 reload:

systemctl reload nginx

坑3:服务启动失败不知道为啥

服务启动失败,只看状态不知道具体原因。

解决: 看日志:

journalctl -u nginx

坑4:重启服务器后服务没自启

装完软件启动了,但没设置开机自启,重启之后服务就没了。

解决: 装完软件记得设置开机自启:

systemctl enable nginx

总结

systemctl 常用命令:

命令 作用
systemctl start 服务名 启动服务
systemctl stop 服务名 停止服务
systemctl restart 服务名 重启服务
systemctl reload 服务名 重新加载配置
systemctl status 服务名 查看服务状态
systemctl enable 服务名 设置开机自启
systemctl disable 服务名 取消开机自启
systemctl is-active 服务名 查看是否在运行
systemctl is-enabled 服务名 查看是否开机自启
journalctl -u 服务名 -f 实时查看日志

记住这几个,CentOS7+ 日常管理服务就够用了。

遇到问题加QQ23979811 协助处理

标签: Linux CentOS systemctl 服务管理 开机自启