当前位置:首页>Linux>Linux命令每日一清单041:systemctl--系统服务管理命令速查表

Linux命令每日一清单041:systemctl--系统服务管理命令速查表

  • 2026-08-23 04:29:18
Linux命令每日一清单041:systemctl--系统服务管理命令速查表

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
创建覆盖配置文件(drop-in)
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 TargetSysV Runlevel说明
poweroff.target
0
关机
rescue.target
1
单用户模式
multi-user.target
3
多用户无图形界面
graphical.target
5
多用户图形界面
reboot.target
6
重启

9. 日志查看(journalctl)

虽然journalctl是独立命令,但常与systemctl配合使用:

命令说明
journalctl -u nginx
查看服务日志
journalctl -u nginx -f
实时跟踪服务日志
journalctl -u nginx --since today
查看今日服务日志
journalctl -u nginx -n 100
查看最后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
获取服务主进程PID
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
编辑完整单元文件(而非drop-in)
--force
强制操作(用于屏蔽或关机)
-H
 / --host
在远程主机上执行
-M
 / --machine
在容器中执行
--root
指定根目录(用于离线操作)

12. 故障排查

问题解决方法
Failed to start service: Unit not found
服务未安装或名称错误;使用systemctl list-unit-files查找
服务启动后立即退出
查看日志:journalctl -u service -n 50;检查服务配置
Permission denied
使用sudo执行;检查用户权限
修改单元文件后未生效
执行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可以查看服务所有属性,帮助深入排查。

本文PDF格式下载:

  关注公众号(haopython),请回复: LTSYSCTL

最新文章

随机文章