说明:
systemctl是 Linuxsystemd系统与服务管理器的核心命令行工具,用于管理系统服务、查看运行状态及控制系统运行级别。
1. 服务生命周期管理
| 命令 | 说明 | 示例 |
|---|---|---|
start |
启动服务 | systemctl start nginx.service |
stop |
停止服务 | systemctl stop nginx.service |
restart |
重启服务(先停止再启动) | systemctl restart nginx.service |
reload |
重载配置(不中断服务) | systemctl reload nginx.service |
reload-or-restart |
优先重载配置,失败则重启 | systemctl reload-or-restart nginx.service |
try-restart |
仅当服务运行时才重启 | systemctl try-restart nginx.service |
2. 开机自启管理
| 命令 | 说明 | 示例 |
|---|---|---|
enable |
设置开机自启(创建符号链接) | systemctl enable nginx.service |
disable |
禁用开机自启 | systemctl disable nginx.service |
is-enabled |
检查是否开机自启 | systemctl is-enabled nginx.service |
enable --now |
设置开机自启并立即启动 | systemctl enable --now nginx.service |
mask |
屏蔽服务(禁止手动/自动启动) | systemctl mask nginx.service |
unmask |
取消屏蔽服务 | systemctl unmask nginx.service |
3. 服务状态查看
| 命令 | 说明 | 示例 |
|---|---|---|
status |
查看详细状态(PID、日志片段等) | systemctl status nginx.service |
is-active |
检查服务是否处于活动状态 | systemctl is-active nginx.service |
is-failed |
检查服务是否启动失败 | systemctl is-failed nginx.service |
list-units |
列出当前已加载的服务单元 | systemctl list-units --type=service |
list-unit-files |
列出所有单元文件及启用状态 | systemctl list-unit-files --type=service |
4. 系统与单元管理
| 命令 | 说明 | 示例 |
|---|---|---|
daemon-reload |
重要:重新加载单元配置文件 | systemctl daemon-reload |
get-default |
查看当前默认启动目标(Target) | systemctl get-default |
set-default |
设置默认启动目标 | systemctl set-default multi-user.target |
isolate |
切换到指定目标,停止无关单元 | systemctl isolate multi-user.target |
list-dependencies |
查看单元依赖关系树 | systemctl list-dependencies nginx.service |
5. 系统电源管理
| 命令 | 说明 | 示例 |
|---|---|---|
reboot |
重启系统 | systemctl reboot |
poweroff |
关机 | systemctl poweroff |
suspend |
挂起(睡眠) | systemctl suspend |
hibernate |
休眠 | systemctl hibernate |
关联工具速查
| 工具 | 命令示例 | 用途 |
|---|---|---|
journalctl |
journalctl -u nginx.service -f |
查看指定服务的实时日志(排错核心) |
systemd-analyze |
systemd-analyze blame |
分析系统启动耗时,定位慢速服务 |
服务排错三步法
当服务启动失败时,建议按以下流程排查:
- 看状态:
systemctl status <服务名>- 获取退出码、PID 及初步错误信息。
- 查日志:
journalctl -u <服务名> -n 20 --no-pager- 查看最近 20 行详细日志,定位具体报错。
- 验配置:使用服务自带的检查命令(如
nginx -t)- 验证配置文件语法是否正确。
温馨提醒
- 在多数命令中,可省略
.service后缀,例如systemctl restart nginx。 - 修改服务单元文件(
.service)后,务必执行systemctl daemon-reload使配置生效。
转自:萌新学Java,作者时兴泽
版权申明:内容来源网络,版权归原创者所有,如有侵权请联系删除
想了解更多干货,可通过下方扫码关注

可扫码添加上智启元官方客服微信👇

17认证网








