先装个服务来练手——后面几天都要用它:
$ sudo dnf install -y nginx
全套操作
$ 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 # 取消开机自启
enable 和 start 是两回事
- start
- 现在把它启动起来。重启机器后不会自己起。
- enable
- 设置开机自启。但它不会立刻启动——现在还是停着的。
- enable --now
- 两件事一起做:设自启 + 立刻启动。实际工作中最常用的写法。
「服务明明装好了、也起来了,结果机房重启一次,服务全没了」——就是只 start 没 enable。
部署完任何服务,都要 systemctl is-enabled 服务名 确认一下。
$ systemctl is-enabled nginx
enabled
$ systemctl is-active nginx
active
$ sudo systemctl enable --now nginx # 一步到位
reload 和 restart
- restart
- 先停再起。中间有一小段服务不可用,所有连接会断。改了监听端口、改了运行用户这类根本性配置时必须用它。
- reload
- 不停服务,只重新读配置。用户无感知。大部分配置改动用它就够了。
- 怎么选
- 能 reload 就别 restart。生产环境上一次 restart 就是一次(哪怕很短的)中断。
支不支持取决于服务自己。nginx、sshd 都支持;有些服务不支持,reload 会直接报错或者干脆等同于 restart。
不确定就用 systemctl reload-or-restart 服务名——能重载就重载,不能就重启。
读懂 status 的输出
$ systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
Active: active (running) since Mon 2026-09-07 15:30:12 CST; 2min ago
Main PID: 3412 (nginx)
Tasks: 3 (limit: 22861)
Memory: 8.2M
CPU: 24ms
CGroup: /system.slice/nginx.service
├─3412 "nginx: master process /usr/sbin/nginx"
└─3413 "nginx: worker process"
Sep 07 15:30:12 rocky systemd[1]: Starting The nginx HTTP server...
Sep 07 15:30:12 rocky systemd[1]: Started The nginx HTTP server.
- Loaded 后面的 enabled/disabled
- 是不是开机自启。括号里还告诉你 unit 文件的路径。
- Active
active (running)正常跑着;inactive (dead)停着;failed启动失败——这时候就该看日志了。- Main PID
- 主进程号。和昨天的
ps aux能对上。 - 最后那几行日志
- status 会顺手给你最近的日志。服务起不来时,失败原因经常直接就写在这几行里——很多人只看上面的红字,忽略了这里。
服务配置文件在哪
$ systemctl cat nginx # 直接看这个服务的配置
# 系统自带的(装软件时放进来的,别直接改)
/usr/lib/systemd/system/nginx.service
# 管理员自定义的(优先级更高,改这里)
/etc/systemd/system/nginx.service
$ sudo systemctl daemon-reload # 改完 unit 文件必须执行这条
改的是服务本身的配置(比如 nginx.conf)→ systemctl reload nginx。
改的是systemd 的 unit 文件(.service 文件)→ 必须先 systemctl daemon-reload,否则 systemd 还用着旧的。
这两个容易混,记住:改谁的配置,就通知谁。
看全局
$ systemctl list-units --type=service --state=running
↑ 当前跑着的所有服务
$ systemctl list-unit-files --type=service --state=enabled
↑ 所有设了开机自启的(接手陌生机器时先看这个)
$ systemctl --failed
↑ 所有启动失败的服务 —— 巡检必看
systemctl --failed 值得加进你的巡检习惯——一条命令就能看出有没有服务悄悄挂了。第 28 天写巡检脚本时会用上。
今天的面试点
「systemctl enable 服务名。」
然后补一句:「enable --now 可以一步到位,既设自启又立刻启动。」——这种细节最能体现是不是天天在用。
「restart 是先停再起,中间会断连接;reload 是不停服务只重读配置,用户无感知。」
「生产环境我优先用 reload,只有改了端口、运行用户这类根本性配置才 restart。」
「多半是只 start 了没 enable。用 systemctl is-enabled 确认一下,然后 enable 上。」
今天的练习
sudo dnf install -y nginx装上,后面几天都用它sudo systemctl start nginx,然后systemctl status nginx,对着上面的表把四个关键位置都认一遍systemctl is-enabled nginx—— 看看刚 start 的服务是不是自启的(应该不是)sudo systemctl enable --now nginx,再is-enabled确认变了ps aux | grep nginx,看进程的 PID 跟 status 里的 Main PID 对不对得上systemctl --failed看有没有挂掉的服务systemctl list-unit-files --type=service --state=enabled | head -20,看看这台机器开机会自动起哪些东西- 试试
sudo systemctl reload nginx和restart,体会哪个更快
Day 13 讲 journalctl——systemd 自己的日志系统。journalctl -xe 这条命令要背下来,服务起不来时全靠它。