systemctl 是 Linux 系统服务管理工具,用于控制 systemd 系统和服务管理器。它是现代 Linux 发行版中管理服务的主要方式。
# 启动服务
sudo systemctl start service_name
# 停止服务
sudo systemctl stop service_name
# 重启服务
sudo systemctl restart service_name
# 重新加载服务配置(不重启服务)
sudo systemctl reload service_name
# 查看服务状态
systemctl status service_name
# 检查服务是否正在运行
systemctl is-active service_name
# 启用服务开机自启
sudo systemctl enable service_name
# 禁用服务开机自启
sudo systemctl disable service_name
# 检查服务是否启用
systemctl is-enabled service_name
# 列出所有已加载的服务单元
systemctl list-units --type=service
# 列出所有服务(包括未运行的)
systemctl list-units --type=service --all
# 列出所有已安装的服务
systemctl list-unit-files --type=service
# 查看服务的依赖关系
systemctl list-dependencies service_name
# 查看服务日志
journalctl -u service_name
# 实时跟踪日志
journalctl -u service_name -f
# 查看特定时间段的日志
journalctl -u service_name --since "2023-01-01" --until "2023-01-02"
# 编辑服务文件(需要先退出才能生效)
sudo systemctl edit service_name
# 查看服务文件内容
systemctl cat service_name
# 显示服务文件的存放位置
systemctl show -p FragmentPath service_name
# 重启系统
sudo systemctl reboot
# 关闭系统
sudo systemctl poweroff
# 进入救援模式
sudo systemctl rescue
# 进入紧急模式
sudo systemctl emergency
通配符使用:可以使用通配符操作多个服务
sudo systemctl restart httpd*
查看失败的服务:
systemctl --failed
资源使用情况:
systemd-cgtop
服务依赖分析:
systemd-analyze critical-chain service_name
系统启动时间分析:
systemd-analyze
systemd-analyze blame
systemd-analyze critical-chain
systemctl 是现代 Linux 系统中非常强大的服务管理工具,掌握这些命令可以让你高效地管理系统服务。