Linux 常见服务怎么启动停止?systemctl 快速上手

1次阅读

linux服务管理统一使用systemctl命令,涵盖启停(start/stop)、状态查看(status)、配置重载(reload)、开机自启控制(enable/disable)及日志排查(journalctl),掌握这些即可应对90%日常运维需求。

Linux 常见服务怎么启动停止?systemctl 快速上手

Linux 服务管理现在基本靠 systemctl,只要系统用的是 systemd(几乎所有现代发行版:ubuntu 16.04+、centos 7+、debian 8+、Fedora 等),就直接用它,不用再记 service 或 chkconfig。

常用服务启停命令(带例子)

操作前通常需要 sudo 权限;服务名一般不加 .service 后缀(tab 键可自动补全):

  • 启动服务:sudo systemctl start sshd(开启 SSH 远程登录)
  • 停止服务:sudo systemctl stop firewalld(关掉防火墙
  • 重启服务:sudo systemctl restart nginx(改完配置后重载生效)
  • 查看状态:systemctl status NetworkManager(看网络是否正常、有没有报错)
  • 重新加载配置(不中断服务):sudo systemctl reload apache2(适合修改了 httpd.conf 后)

开机自启怎么控制?

很多服务默认不自启,需要手动设置;禁用后也不会影响当前运行状态:

  • 开机自动启动:sudo systemctl enable docker
  • 取消开机自启:sudo systemctl disable bluetooth
  • 立即启用并启动(一步到位):sudo systemctl enable –now mariadb
  • 立即禁用并停止:sudo systemctl disable –now postfix

查服务、看日志、排故障

遇到服务起不来或异常,这几个命令很实用:

Linux 常见服务怎么启动停止?systemctl 快速上手

讯飞智文

一键生成PPT和Word,让学习生活更轻松。

Linux 常见服务怎么启动停止?systemctl 快速上手 61

查看详情 Linux 常见服务怎么启动停止?systemctl 快速上手

  • 列出所有已启用的服务:systemctl list-unit-files –type=service –state=enabled
  • 列出当前正在运行的服务:systemctl list-units –type=service –state=running
  • 查看某服务的实时日志:journalctl -u sshd -f(-f 表示持续跟踪)
  • 查最近 1 小时的日志:journalctl -u nginx –since “1 hour ago
  • 重载 systemd 配置(改过 .service 文件后必须执行):sudo systemctl daemon-reload

几个容易忽略但关键的点

不是所有服务都能直接 start,有些依赖关系或状态限制要注意:

  • 服务名写错会提示 “Unit xxx.service not found”,可用 tab 补全或先查:systemctl list-unit-files | grep -i mysql
  • status 输出里看到 “inactive (dead)” 表示没在跑,“active (running)” 才算正常工作
  • reload 不是所有服务都支持,不支持时会提示 failed;这时用 restart 更稳妥
  • 用 mask 可彻底禁止服务(连手动 start 都不行),慎用:sudo systemctl mask cups

基本上就这些。掌握 start/stop/status/enable/reload + journalctl 查日志,90% 的日常服务管理都能搞定。

text=ZqhQzanResources