张芷铭的个人博客

systemctl 是 systemd 的核心管理工具,用于服务启停、开机自启、状态查看。

服务状态查看

命令作用
systemctl status <服务>查看详细状态
systemctl is-active <服务>是否运行(active/inactive)
systemctl is-enabled <服务>是否开机自启(enabled/disabled)
systemctl list-units --type=service列出运行中的服务
systemctl list-unit-files --type=service列出所有服务及自启状态

启停与重启

命令作用
systemctl start <服务>启动
systemctl stop <服务>停止
systemctl restart <服务>重启
systemctl reload <服务>重载配置(不停止服务)

开机自启

命令作用
systemctl enable <服务>设置开机自启
systemctl disable <服务>关闭开机自启
systemctl enable --now <服务>立即启动 + 开机自启
systemctl mask <服务>禁用服务(无法启动)

系统操作

命令作用
systemctl poweroff关机
systemctl reboot重启
systemctl suspend挂起
systemctl hibernate休眠

配置管理

命令作用
systemctl daemon-reload重载 systemd 配置(修改服务文件后必执行)
systemctl cat <服务>查看服务文件内容
systemctl edit <服务>编辑自定义配置

故障排查

1
2
3
4
5
6
7
8
# 查看状态
systemctl status sshd

# 查看日志
journalctl -u sshd -f

# 列出失败的服务
systemctl failed

注意事项

  1. 除查看状态外,均需 root 权限
  2. 服务名可省略 .service 后缀
  3. 不同发行版服务名可能不同(CentOS 用 firewalld,Ubuntu 用 ufw

Comments