systemctl 入门与进阶使用文档(适合初学者)
2026-1-21
| 2026-1-21
字数 2217阅读时长 6 分钟
 

systemctl 入门与进阶使用文档(适合初学者)

一、systemctl 是什么?

  • systemd:现代 Linux 系统(如 Ubuntu、Debian、CentOS、RHEL、Fedora 等)默认使用的“系统和服务管理器”(init 系统)。
  • systemctl:是用来和 systemd 交互的命令行工具,用它来:
    • 启动 / 停止 / 重启服务
    • 设置服务开机自启动
    • 查看服务状态和日志
    • 管理各种 “单元(Unit)”,如服务、定时器、挂载点等
可以简单理解:
systemctl = 管服务和系统状态的万能遥控器

二、几个必须先知道的概念

1. Unit(单元)

systemd 把要管理的东西都抽象成 Unit 文件(通常在 /lib/systemd/system/etc/systemd/system 下)。
常见类型:
类型后缀
含义
示例
.service
服务/守护进程
sshd.service, nginx.service
.socket
套接字(端口监听)
ssh.socket
.timer
定时器(类似 cron)
logrotate.timer
.target
运行级别集合
multi-user.target, graphical.target
.mount
挂载点
home.mount
.swap
交换分区
swap.swap
注意:很多时候 .service 后缀可以省略,如 nginx.service 直接写成 nginx

2. “当前状态” vs “开机自启”

  • start/stop/restart只影响当前这次开机的状态
  • enable/disable影响以后每次开机时是否自动启动
你可以记住一对:
  • start = 现在就启动
  • enable = 以后每次开机都自动启动

三、最常用的 systemctl 命令(一定要先会)

下面所有命令都假设以 nginx 为例,你可以换成任何你系统中安装的服务名。

1. 启动 / 停止 / 重启服务

bash复制
# 启动服务 sudo systemctl start nginx # 停止服务 sudo systemctl stop nginx # 重启服务(先停再启) sudo systemctl restart nginx # 只重载配置,不中断进程(程序要支持) sudo systemctl reload nginx # 智能:能 reload 就 reload,否则就 restart sudo systemctl reload-or-restart nginx
一般你会最常用:start / stop / restart / reload

2. 查看服务状态

bash复制
# 查看服务状态 systemctl status nginx
输出中主要看几行:
  • Loaded: 表示 Unit 文件路径、是否 enable
  • Active: 状态,如
    • active (running):正在运行
    • inactive (dead):已停止
    • failed:启动失败
  • Main PID: 主进程号
  • 最下面有最近几条日志

3. 设置开机自启与取消

bash复制
# 设置服务开机自启 sudo systemctl enable nginx # 取消开机自启 sudo systemctl disable nginx # 查看当前是否开机自启 systemctl is-enabled nginx # 返回 enabled / disabled / static / masked
  • enabled:开机自启
  • disabled:不开机自启
  • static:自身不能直接 enable,一般被别的 unit 依赖
  • masked:被“屏蔽”,完全禁止启动(见后面)

4. 查看系统中有哪些服务

bash复制
# 列出当前“已加载”并可能在运行的服务 systemctl list-units --type=service # 列出所有服务单元文件(包括没加载的) systemctl list-unit-files --type=service
常配合 grep 使用:
bash复制
systemctl list-units --type=service | grep ssh systemctl list-unit-files --type=service | grep nginx

四、Unit 文件放在哪里?优先级如何?

常见路径:
  • /lib/systemd/system//usr/lib/systemd/system/ 系统或软件包自带的 unit 文件
  • /etc/systemd/system/ 管理员本地自定义或覆盖的 unit,优先级更高
一般不直接改 /lib/systemd/system 里的文件,而是用:
bash复制
sudo systemctl edit nginx
这会在 /etc/systemd/system/nginx.service.d/override.conf 里生成覆盖文件,更安全、可维护。

五、服务日志查看(配合 journalctl)

systemd 的日志由 journald 管理,命令是 journalctl

1. 查看某个服务日志

bash复制
# 查看某服务的所有日志 journalctl -u nginx # 实时查看日志(类似 tail -f) journalctl -u nginx -f # 指定时间段 journalctl -u nginx --since "2026-01-01 00:00:00" --until "2026-01-21 23:59:59" # 查看上一次开机的日志 journalctl -b -1 -u nginx
启动失败时:
bash复制
systemctl status nginx # 先看状态 journalctl -u nginx -xe # 再看详细错误日志
  • x 会附加解释,e 跳到日志结尾。

六、常见操作场景示例

场景 1:安装了 nginx,想让它一直随系统运行

  1. 立即启动
    1. bash复制
      sudo systemctl start nginx
  1. 确认运行状态
    1. bash复制
      systemctl status nginx
  1. 设置开机自启
    1. bash复制
      sudo systemctl enable nginx

场景 2:一个服务老是出问题,想暂时禁用它

  1. 先停掉
    1. bash复制
      sudo systemctl stop foo
  1. 禁止开机自启
    1. bash复制
      sudo systemctl disable foo
  1. 如果想完全禁止任何人(包括依赖)启动它
    1. bash复制
      sudo systemctl mask foo # 取消屏蔽 sudo systemctl unmask foo
mask 会把这个服务的 unit 链接到 /dev/null,就算有人 start foo 也会失败。

场景 3:自己写一个简单的 systemd 服务

假设你有一个脚本 /usr/local/bin/myapp.sh
bash复制
sudo nano /etc/systemd/system/myapp.service
内容示例:
ini复制
[Unit] Description=My Test App After=network.target [Service] Type=simple ExecStart=/usr/local/bin/myapp.sh Restart=on-failure [Install] WantedBy=multi-user.target
然后:
bash复制
# 重新加载 unit 文件 sudo systemctl daemon-reload # 启动服务试试 sudo systemctl start myapp # 查看状态 systemctl status myapp # 设置开机自启 sudo systemctl enable myapp

七、定时任务:用 .timer 替代 cron(入门示例)

假设你有一个备份脚本 /usr/local/bin/backup.sh,想每 30 分钟跑一次。
  1. 写 Service 文件(真正执行脚本的单元):
    1. bash复制
      sudo nano /etc/systemd/system/backup.service
      ini复制
      [Unit] Description=Run backup script [Service] Type=oneshot ExecStart=/usr/local/bin/backup.sh
  1. 写 Timer 文件(定义执行频率):
    1. bash复制
      sudo nano /etc/systemd/system/backup.timer
      ini复制
      [Unit] Description=Run backup every 30 minutes [Timer] OnBootSec=10min # 开机后 10 分钟执行第一次 OnUnitActiveSec=30min # 每隔 30 分钟执行一次 Persistent=true # 开机后会补跑关机期间错过的任务(如果可能) [Install] WantedBy=timers.target
  1. 启用并启动定时器
    1. bash复制
      sudo systemctl daemon-reload sudo systemctl enable --now backup.timer
  1. 查看定时器情况
    1. bash复制
      systemctl list-timers --all
你会看到下一次执行时间、上一次执行时间等信息。

八、Target(目标):理解“运行级别”

target 是一组 unit 的集合,用来表示系统所处的“模式”。
常见 target:
  • graphical.target:图形界面模式(桌面)
  • multi-user.target:多用户命令行模式(无桌面)
  • rescue.target:单用户救援模式
  • emergency.target:更低级的紧急模式(很少用)

常用命令

bash复制
# 查看当前默认 target(类似默认运行级别) systemctl get-default # 设置默认 target(开机会进到这个模式) sudo systemctl set-default multi-user.target # 立即切换到某个 target(相当于切换运行级别) sudo systemctl isolate graphical.target

九、一些实用小技巧(建议掌握)

1. 列出失败的单元

bash复制
systemctl list-units --failed
如果某些服务启动失败了,这里一目了然。

2. 重置失败状态

bash复制
sudo systemctl reset-failed # 或仅重置某个服务: sudo systemctl reset-failed nginx
配合自动化运维时会用到。

3. 临时运行服务,不改变永久设置

bash复制
# 只在当前运行期启用(reboot 后不再启用) sudo systemctl start --runtime nginx
适合临时测试某个服务,不想影响正式配置。

4. 查看服务依赖关系

bash复制
systemctl list-dependencies nginx
可以看到它依赖哪些单元,哪些又依赖它。

5. 修改服务配置的推荐方式:systemctl edit

不要直接改 /lib/systemd/system/*.service,官方推荐方式:
bash复制
sudo systemctl edit nginx
会打开一个编辑器,你只需写想覆盖或追加的那几行
ini复制
[Service] Environment="ENV=prod"
保存后:
bash复制
sudo systemctl daemon-reload sudo systemctl restart nginx
override 文件会保存在:
text复制
/etc/systemd/system/nginx.service.d/override.conf
以后系统升级不会被覆盖。

十、学习与排错建议

  1. 遇到问题的基本排查套路
    1. systemctl status your-service
    2. journalctl -u your-service -xe
    3. 查看 unit 文件路径,看配置是否正确;如有修改记得:
      1. bash复制
        sudo systemctl daemon-reload sudo systemctl restart your-service
  1. 多用 -helpman
    1. bash复制
      systemctl --help man systemctl man systemd.service man systemd.timer
  1. 在测试环境大胆尝试
    1. 可以在虚拟机或容器里自由练习 enable/disable/start/stop/status 等命令,而不怕影响生产环境。

十一、常用命令速查表(可收藏)

操作
命令示例
启动服务
sudo systemctl start nginx
停止服务
sudo systemctl stop nginx
重启服务
sudo systemctl restart nginx
重载配置
sudo systemctl reload nginx
状态查看
systemctl status nginx
设置开机自启
sudo systemctl enable nginx
取消开机自启
sudo systemctl disable nginx
屏蔽服务
sudo systemctl mask nginx
取消屏蔽
sudo systemctl unmask nginx
查看运行中服务
systemctl list-units --type=service
查看所有服务文件
systemctl list-unit-files --type=service
查看失败的单元
systemctl list-units --failed
查看服务日志
journalctl -u nginx -f
重新加载所有 unit
sudo systemctl daemon-reload

 
win11一键安装openclaw2026年了,你该如何选择Linux.do上的“富可敌国”
Loading...