Linux查看所有服务,systemctl命令大全
前言
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 协助处理
Linux服务管理,systemctl常用命令汇总
前言
CentOS7 装完软件,怎么启动?怎么开机自启?服务挂了怎么查日志?这些都需要用到 systemctl 命令。CentOS7 开始用 systemd 代替了原来的 service 命令,systemctl 就是 systemd 的管理工具。本文就把 systemctl 常用命令汇总一下,日常管理服务够用了。
启动和停止服务
启动服务
systemctl start nginx
停止服务
systemctl stop nginx
重启服务
systemctl restart nginx
重新加载配置
systemctl reload nginx
注意: reload 和 restart 的区别:
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 协助处理