Linux服务如何管理_systemctl常用命令

2次阅读

linux服务管理主要依靠systemctl命令:用status查看状态,start/stop/restart即时控制,enable/disable设置开机自启,reload重载配置,list-units快速列出服务。

Linux服务如何管理_systemctl常用命令

Linux 服务管理主要靠 systemctl,它是 systemd 的核心命令,统一替代了旧的 servicechkconfig。掌握几个关键操作,就能完成绝大多数日常服务控制任务。

查看服务实时状态

systemctl status 服务名 查看服务是否在运行、最近日志、启动失败原因等关键信息。例如:

  • systemctl status nginx —— 输出中看 Active: 字段:显示 active (running) 表示正常,inactive (dead)failed 表示异常
  • 日志片段会直接显示在底部,对排查配置错误、端口占用等问题非常有用
  • q 键退出查看界面

即时启停与重启服务

这些操作只影响当前会话,不改变开机自启设置:

  • 启动:sudo systemctl start nginx
  • 停止:sudo systemctl stop nginx
  • 重启(先停后启):sudo systemctl restart nginx
  • 验证结果:systemctl is-active nginx,返回 activeinactive

设置或取消开机自启

启用后,系统每次启动时自动拉起该服务;禁用则完全跳过:

  • 开机自启:sudo systemctl enable nginx —— 实际是在 /etc/systemd/system/multi-user.target.wants/ 下创建软链接
  • 取消自启:sudo systemctl disable nginx
  • 检查状态:systemctl is-enabled nginx,返回 enableddisabled

重载配置而不中断服务

修改了服务配置文件(如 /etc/nginx/nginx.conf)后,优先用 reload

  • sudo systemctl reload nginx —— 通知进程重新读取配置,连接不中断
  • 若服务不支持 reload(如某些老版本或自定义服务),命令会提示 Job type reload is not supported,此时需用 restart

快速列出所有服务

掌握全局服务概况,便于发现异常或闲置服务:

  • 只看正在运行的服务:systemctl list-units --type=service --state=active
  • 看所有已加载服务(含未启动的):systemctl list-units --type=service --all
  • 查启动失败的服务:systemctl list-units --type=service --state=failed
text=ZqhQzanResources