Linux查看所有服务,systemctl命令大全
时间:2026-10-6 07:59 作者:emer 分类: 无
前言
CentOS7 之后用 systemctl 管理服务,代替了老的 service 和 chkconfig 命令。本文把常用的 systemctl 命令都整理出来,新手照着用就行。
查看服务
查看所有服务
systemctl list-units --type=service
列出所有正在运行的服务。
查看所有服务(包括没运行的)
systemctl list-unit-files --type=service
查看正在运行的服务
systemctl list-units --type=service --state=running
查看某个服务状态
systemctl status 服务名
比如看 nginx 状态:
systemctl status nginx
输出会显示服务是否在运行、PID、内存占用、最近日志等。
启动和停止服务
启动服务
systemctl start 服务名
比如:
systemctl start nginx
停止服务
systemctl stop 服务名
比如:
systemctl stop nginx
重启服务
systemctl restart 服务名
比如:
systemctl restart nginx
重载配置(不重启)
修改了配置文件后,用 reload 重载,不用重启:
systemctl reload 服务名
比如:
systemctl reload nginx
平滑重启(reload 不行就用这个)
systemctl reload-or-restart 服务名
设置开机自启
开启开机自启
systemctl enable 服务名
比如:
systemctl enable nginx
关闭开机自启
systemctl disable 服务名
比如:
systemctl disable nginx
查看是否开机自启
systemctl is-enabled 服务名
输出 enabled 就是开机自启,disabled 就是不是。
其他常用操作
查看服务是否在运行
systemctl is-active 服务名
输出 active 就是在运行,inactive 就是没运行。
查看服务日志
journalctl -u 服务名
比如看 nginx 日志:
journalctl -u nginx
实时看服务日志
journalctl -u 服务名 -f
和 tail -f 一样,实时输出最新日志。
查看服务开机自启状态
systemctl list-unit-files --type=service | grep enabled
列出所有开机自启的服务。
常见服务名
| 服务名 | 对应服务 |
|---|---|
| nginx | Nginx 网页服务 |
| httpd | Apache 网页服务 |
| mysqld | MySQL 数据库 |
| firewalld | 防火墙 |
| crond | 定时任务 |
| sshd | SSH 远程连接 |
| php-fpm | PHP 解析 |
| redis | Redis 缓存 |
常见坑
坑1:服务名写错了
执行 systemctl 报错 Unit xxx.service could not be found。
原因: 服务名写错了。
解决: 先列一下所有服务,找到正确的服务名:
systemctl list-unit-files --type=service | grep 关键词
坑2:启动了服务但还是访问不了
服务显示 active,但外部还是访问不了。
原因: 防火墙没开端口,或者服务本身配置错了。
解决: 先看防火墙开了没,再看服务日志:
journalctl -u 服务名
坑3:开机自启了但重启后没起来
设置了 enable,但重启后服务还是没启动。
原因: 服务配置有问题,启动失败了。
解决: 看日志找原因:
journalctl -u 服务名
坑4:用了老命令 service
执行 service nginx start 能用,但不推荐了。
原因: 这是老命令,新系统都用 systemctl。
解决: 换成 systemctl start nginx。
总结
| 操作 | 命令 |
|---|---|
| 查看所有服务 | systemctl list-units --type=service |
| 查看服务状态 | systemctl status 服务名 |
| 启动服务 | systemctl start 服务名 |
| 停止服务 | systemctl stop 服务名 |
| 重启服务 | systemctl restart 服务名 |
| 重载配置 | systemctl reload 服务名 |
| 开机自启 | systemctl enable 服务名 |
| 关闭开机自启 | systemctl disable 服务名 |
| 查看日志 | journalctl -u 服务名 |
systemctl 就这几个常用命令,记住就够用了。
遇到问题加QQ23979811 协助处理