
systemctl是systemd系统和服务管理器的命令行工具,用于管理系统服务(service)、套接字(socket)、设备(device)、挂载点(mount)等systemd单元(unit)。作为现代Linux发行版的标准初始化系统,systemd取代了传统的SysV init,而systemctl是其核心管理接口。
systemctl能够启动、停止、重启、重载服务,管理开机自启,查看服务状态,编辑单元文件,执行系统关机/重启等操作。掌握systemctl是Linux系统管理和服务运维的必备技能。
1. 服务控制
| 命令 | 说明 |
|---|
sudo systemctl start nginx | |
sudo systemctl stop nginx | |
sudo systemctl restart nginx | |
sudo systemctl reload nginx | |
sudo systemctl try-restart nginx | |
sudo systemctl reload-or-restart nginx | |
sudo systemctl kill nginx | |
2. 开机自启管理
| 命令 | 说明 |
|---|
sudo systemctl enable nginx | |
sudo systemctl disable nginx | |
sudo systemctl enable --now nginx | |
sudo systemctl disable --now nginx | |
sudo systemctl is-enabled nginx | |
sudo systemctl reenable nginx | |
sudo systemctl preset nginx | |
3. 状态查看
| 命令 | 说明 |
|---|
systemctl status nginx | |
systemctl is-active nginx | |
systemctl is-failed nginx | |
systemctl show nginx | |
systemctl show -p ExecStart nginx | |
systemctl show nginx --property=LoadState | |
4. 列出单元
| 命令 | 说明 |
|---|
systemctl list-units | |
systemctl list-units --type=service | |
systemctl list-units --all | |
systemctl list-units --state=running | |
systemctl list-unit-files | |
systemctl list-unit-files --type=service | |
systemctl list-sockets | |
systemctl list-timers | |
5. 单元文件操作
| 命令 | 说明 |
|---|
systemctl cat nginx | |
sudo systemctl edit nginx | |
sudo systemctl edit --full nginx | |
sudo systemctl set-property nginx CPUShares=500 | |
sudo systemctl revert nginx | |
sudo systemctl daemon-reload | |
systemctl help nginx | |
6. 屏蔽与依赖
| 命令 | 说明 |
|---|
sudo systemctl mask nginx | |
sudo systemctl unmask nginx | |
systemctl list-dependencies nginx | |
systemctl list-dependencies --reverse nginx | |
systemctl list-dependencies --all nginx | |
7. 系统控制
| 命令 | 说明 |
|---|
sudo systemctl reboot | |
sudo systemctl poweroff | |
sudo systemctl halt | |
sudo systemctl suspend | |
sudo systemctl hibernate | |
sudo systemctl hybrid-sleep | |
sudo systemctl rescue | |
sudo systemctl emergency | |
sudo systemctl default | |
8. 运行目标(Runlevels)
| 命令 | 说明 |
|---|
systemctl get-default | |
sudo systemctl set-default multi-user.target | |
systemctl list-units --type=target | |
systemctl isolate multi-user.target | |
运行目标对应关系:
| Systemd Target | SysV Runlevel | 说明 |
|---|
poweroff.target | | |
rescue.target | | |
multi-user.target | | |
graphical.target | | |
reboot.target | | |
9. 日志查看(journalctl)
虽然journalctl是独立命令,但常与systemctl配合使用:
| 命令 | 说明 |
|---|
journalctl -u nginx | |
journalctl -u nginx -f | |
journalctl -u nginx --since today | |
journalctl -u nginx -n 100 | |
journalctl -u nginx -p err | |
10. 实用组合模式
| 命令 | 说明 |
|---|
systemctl status --no-pager nginx | |
systemctl list-units --state=failed | |
systemctl list-unit-files --state=enabled | |
systemctl list-units --type=service --state=running | |
sudo systemctl daemon-reload && systemctl restart nginx | |
systemctl show -p MainPID nginx | cut -d= -f2 | |
systemctl cat nginx | grep -E "ExecStart|ExecReload" | |
watch -n 1 systemctl status nginx | |
11. 常用选项速查
| 选项 | 说明 |
|---|
--type | 指定单元类型(service、socket、target等) |
--state | 按状态过滤(running、failed、enabled等) |
--all | |
--no-pager | |
--no-legend | |
--no-reload | |
--now | |
--full | |
--force | |
-H | |
-M | |
--root | |
12. 故障排查
| 问题 | 解决方法 |
|---|
| Failed to start service: Unit not found | 服务未安装或名称错误;使用systemctl list-unit-files查找 |
| 服务启动后立即退出 | 查看日志:journalctl -u service -n 50;检查服务配置 |
| Permission denied | |
| 修改单元文件后未生效 | 执行sudo systemctl daemon-reload重新加载 |
| 服务被屏蔽无法启动 | 检查屏蔽状态:systemctl status service;使用systemctl unmask解除 |
| 开机自启不生效 | 检查依赖:systemctl list-dependencies;确认enable状态 |
| 服务显示dead但进程在运行 | systemd丢失了对进程的跟踪;尝试systemctl start重新接管 |
| 关机卡住 | 检查阻止关机的服务:systemctl list-jobs |
温馨提示: systemctl是现代Linux系统管理的核心工具。建议掌握以下技巧:1)服务操作前先用systemctl status查看当前状态;2)修改单元文件后务必执行daemon-reload;3)使用--now选项同步启用和启动;4)排查问题时结合journalctl -u service -f实时查看日志;5)使用systemctl list-units --state=failed快速定位故障服务;6)批量操作时使用--no-pager和--no-legend便于脚本解析。在维护生产环境时,建议先用try-restart或reload替代restart减少服务中断。对于复杂的问题,systemctl show service可以查看服务所有属性,帮助深入排查。
关注公众号(haopython),请回复: LTSYSCTL