Linux 服务器配置与管理(CentOS Stream 9)
以 CentOS Stream 9(RHEL 9 系)为实验环境的服务器配置与管理课程:从命令行基础、用户权限、磁盘与文件系统,到服务部署、网络与远程管理、安全加固与故障排查。每课时 45 分钟,讲义内附可整段复制粘贴执行的实验命令。
这里是孟凡的课程资料库。内容按「课程项目」分门别类,每一个项目都是一套完整的、 按 45 分钟一课时编排的讲义;每一课时都配了可以下载到本地的课堂练习资料包。 不管你是从头学,还是回头查某一个知识点,都能在这里直接找到。
以 CentOS Stream 9(RHEL 9 系)为实验环境的服务器配置与管理课程:从命令行基础、用户权限、磁盘与文件系统,到服务部署、网络与远程管理、安全加固与故障排查。每课时 45 分钟,讲义内附可整段复制粘贴执行的实验命令。
围绕 Spring、SpringMVC、MyBatis 与 Java Web 项目实践整理的课程讲义与课堂练习。
从 Vue 3 核心语法到 Element Plus 组件实操,按每课时 45 分钟编排的前端实训讲义。每节课配一份可直接粘贴进脚手架 src/App.vue 的完整代码,并提供课堂练习下载包。
以 CentOS Stream 9(RHEL 9 系)为实验环境的服务器配置与管理课程:从命令行基础、用户权限、磁盘与文件系统,到服务部署、网络与远程管理、安全加固与故障排查。每课时 45 分钟,讲义内附可整段复制粘贴执行的实验命令。
围绕 Spring、SpringMVC、MyBatis 与 Java Web 项目实践整理的课程讲义与课堂练习。
从 Vue 3 核心语法到 Element Plus 组件实操,按每课时 45 分钟编排的前端实训讲义。每节课配一份可直接粘贴进脚手架 src/App.vue 的完整代码,并提供课堂练习下载包。
你写了个脚本,每分钟往日志里写一行时间,用来监控机器还活着。可是现在它只能在你终端里跑,关掉终端就停、开机也不会自己起、日志也散落各处。怎么让它变成"正规军"?
今天你要把一个普通脚本,包装成能用 systemctl 管理、能开机自启、日志自动进 journal 的 systemd 服务。这是运维从"会写脚本"跨到"会交付服务"的关键一步,下一课的定时器也建立在这之上。
一台 CentOS Stream 9 虚拟机(通常简称 CentOS 9),root 登录,已拍快照。脚本放在 /opt/lab/hello-svc/,unit 文件写在 /etc/systemd/system/。全程不改系统关键配置。
| 做到什么 | 在哪里看 | 完成的证据 |
|---|---|---|
| 自定义服务能被 systemctl 管理 | systemctl status hello |
Active 行 active (running) |
| 服务开机自启已设 | systemctl is-enabled hello |
输出 enabled |
| 服务日志进 journal | journalctl -u hello -n 20 |
能看到脚本输出的时间行 |
systemctl start/enable/status、journalctl -u、unit 文件放 /etc/systemd/system/。今天把它们用起来写自己的服务。| 时间段 | 做什么 |
|---|---|
| 0—6 分钟 | 讲清"为什么脚本要变成服务"、unit 文件三段结构 |
| 6—20 分钟 | 写脚本 + 写 unit 文件,对照第 3 页执行 |
| 20—30 分钟 | 逐字段讲 [Unit]/[Service]/[Install] |
| 30—40 分钟 | daemon-reload 必要性、验证闭环、Type=oneshot 演示 |
| 40—45 分钟 | 回答第 7 页问题,记录实验 |
在第 21 课的机器上继续。动手前确认 fresh-install 快照在——写 unit 文件只影响自建服务,但养成先存档习惯。
下一课我们讲计划任务:cron 与 systemd 定时器。你今天写的服务,可以配一个 timer,让它"每隔一段时间自动跑一次",而不必一直常驻。
来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册
| 名字 | 用熟悉的话理解 | 本课里长什么样 |
|---|---|---|
| unit 文件 | systemd 的"服务说明书",写好它服务才被认识 | /etc/systemd/system/hello.service |
[Unit] 段 |
说明书的"前置条件":什么时候能起、依赖谁 | After=network.target |
[Service] 段 |
说明书的"怎么跑":执行什么、什么类型 | ExecStart=、Type=simple |
[Install] 段 |
说明书的"装到哪里":开机自启挂哪个 target | WantedBy=multi-user.target |
| Type=simple | 服务前台一直跑,systemd 认为它"活着"就 OK | 脚本里有 while true 循环 |
| Type=oneshot | 跑一次就退出的任务,配 RemainAfterExit 算"仍在" |
开机执行一次型 |
| ExecStart | 真正要执行的命令(必须绝对路径) | ExecStart=/opt/lab/hello-svc/hello.sh |
| daemon-reload | 改完 unit 文件后"让 systemd 重新读说明书" | systemctl daemon-reload |
一个 .service 文件由三段组成,各管各的事:
[Unit]
Description=我的第一个服务 # 描述,status 里能看到
After=network.target # 等网络起来后再起我
[Service]
Type=simple # 前台常驻型
ExecStart=/opt/lab/hello-svc/hello.sh # 必须绝对路径
Restart=on-failure # 非正常退出就重启
RestartSec=5 # 重启前等 5 秒
[Install]
WantedBy=multi-user.target # 开机自启挂到多用户目标下
记住:[Unit] 管"何时起",[Service] 管"怎么跑",[Install] 管"开机挂哪"。 三段缺一不可(至少要有 Service 和 Install 才能让 enable 生效)。
两个致命细节,错一个服务就起不来:
ExecStart 必须写绝对路径,不能写相对路径、不能只写脚本名。systemd 不会去 PATH 里帮你找。chmod +x)。否则 ExecStart 执行时报 Permission denied。# 脚本写好后务必加执行权限
chmod +x /opt/lab/hello-svc/hello.sh
# 并且第一行要有 shebang 指明解释器
head -1 /opt/lab/hello-svc/hello.sh # 应是 #!/bin/bash
你改了 /etc/systemd/system/hello.service,systemd 不会自动发现。必须:
systemctl daemon-reload # 让 systemd 重新读取所有 unit 文件
systemctl restart hello # 再重启服务让改动生效
漏掉 daemon-reload 是第二大常见错误——你明明改了文件,status 却还是老行为,就是因为这个。
| 现象 | 原因 |
|---|---|
status 红字 Permission denied |
脚本没 chmod +x,或 ExecStart 路径无执行权限 |
ExecStart 报 not found |
写了相对路径或脚本名,必须绝对路径 |
| 改了 unit 不生效 | 忘了 systemctl daemon-reload |
服务一直 activating 不进 running |
Type 设错(常驻脚本误设成 oneshot) |
enable 后重启没起 |
[Install] 段缺 WantedBy,或没 daemon-reload |
来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册
下面这段是本课全部实验:写脚本→写 unit→重载→启用→验证→再做一个 oneshot 服务。先整体看一遍。
# ===== 第 1 步:建脚本目录并写 hello.sh(循环输出时间)=====
mkdir -p /opt/lab/hello-svc
cat > /opt/lab/hello-svc/hello.sh <<'EOF'
#!/bin/bash
# 每隔 5 秒往 stdout 写一行时间,systemd 会把它收进 journal
while true; do
echo "hello-svc 还活着,现在时间:$(date '+%F %T')"
sleep 5
done
EOF
chmod +x /opt/lab/hello-svc/hello.sh
bash -n /opt/lab/hello-svc/hello.sh # 先语法检查,避免低级错误
/opt/lab/hello-svc/hello.sh & # 先手动跑一下确认能输出(Ctrl+C 结束)
kill %1 2>/dev/null
# ===== 第 2 步:写 unit 文件(逐字段)=====
cat > /etc/systemd/system/hello.service <<'EOF'
[Unit]
Description=Hello Service Demo
After=network.target
[Service]
Type=simple
ExecStart=/opt/lab/hello-svc/hello.sh
Restart=on-failure
RestartSec=5
User=root
StandardOutput=journal
[Install]
WantedBy=multi-user.target
EOF
# ===== 第 3 步:改完 unit 必须重载,再启用 =====
systemctl daemon-reload
systemctl enable --now hello
systemctl status hello | head -8
# ===== 第 4 步:用 journalctl 看服务日志(验证日志进 journal)=====
journalctl -u hello -n 20 --no-pager
# ===== 第 5 步:演示 Type=oneshot + RemainAfterExit(开机执行一次型)=====
mkdir -p /opt/lab/oneshot-svc
cat > /opt/lab/oneshot-svc/boot-time.sh <<'EOF'
#!/bin/bash
echo "开机时间记录:$(date '+%F %T')" >> /opt/lab/oneshot-svc/boot.log
EOF
chmod +x /opt/lab/oneshot-svc/boot-time.sh
cat > /etc/systemd/system/bootnote.service <<'EOF'
[Unit]
Description=Boot Time Note (oneshot)
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/lab/oneshot-svc/boot-time.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now bootnote
systemctl status bootnote | head -6
cat /opt/lab/oneshot-svc/boot.log
# ===== 第 6 步:清理本次实验(只删自建服务与脚本)=====
systemctl disable --now hello
systemctl disable --now bootnote
rm -f /etc/systemd/system/hello.service /etc/systemd/system/bootnote.service
systemctl daemon-reload
rm -rf /opt/lab/hello-svc /opt/lab/oneshot-svc
echo "实验已清理"
注意第 1 步手动 & 测试只是为了确认脚本能跑,正式运行交给 systemd,不要长期手动挂着。第 6 步删 unit 文件后必须再 daemon-reload,否则 systemd 还记着它。
bash -n 无输出(语法 OK);手动 & 跑能看到时间行输出。/etc/systemd/system/hello.service 生成,三段齐全。status hello 的 Active 行 active (running),Main PID 存在。journalctl -u hello 持续出现 hello-svc 还活着,现在时间:... 的行。bootnote 的 Active 行 active (exited)(oneshot 跑完即退,因 RemainAfterExit=yes 仍算 active);boot.log 有一行开机时间。systemctl status hello 报 not found,自建目录已删。下面两段 unit,哪个适合"脚本里有个 while true 一直跑",哪个适合"脚本跑一次就结束(如写个开机记录)"?
Type=simple # A
Type=oneshot # B
A(Type=simple)适合"前台常驻"的脚本(如 hello.sh 里的 while true)——systemd 认为它一直活着就是正常。B(Type=oneshot)适合"跑一次就退出"的任务(如写开机时间),配合 RemainAfterExit=yes 让 systemd 在它退出后仍标记为 active,表示"这件事我做过了"。选错类型,常驻脚本设成 oneshot 会一直 activating 不进 running;一次性任务设成 simple 会被误判失败。 这是本课第二个关键区分。
来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册
mkdir -p /opt/lab/hello-svc
cat > /opt/lab/hello-svc/hello.sh <<'EOF'
#!/bin/bash
while true; do
echo "hello-svc 还活着,现在时间:$(date '+%F %T')"
sleep 5
done
EOF
chmod +x /opt/lab/hello-svc/hello.sh
bash -n /opt/lab/hello-svc/hello.sh
预期现象: bash -n 无输出(语法正确);脚本首行 #!/bin/bash 必须有。务必 chmod +x——这是新手 90% 的错误来源。
cat > /etc/systemd/system/hello.service <<'EOF'
[Unit]
Description=Hello Service Demo
After=network.target
[Service]
Type=simple
ExecStart=/opt/lab/hello-svc/hello.sh
Restart=on-failure
RestartSec=5
User=root
StandardOutput=journal
[Install]
WantedBy=multi-user.target
EOF
各字段含义:
[Unit]:Description 是描述;After=network.target 表示"网络起来后再起我",避免依赖网络的服务早于网络启动。[Service]:Type=simple 前台常驻;ExecStart 必须绝对路径;Restart=on-failure 非正常退出自动拉起;RestartSec=5 等 5 秒再拉;User=root 以谁的身份跑;StandardOutput=journal 让 stdout 进 journal(这样 journalctl -u hello 才能看到)。[Install]:WantedBy=multi-user.target 决定开机自启时挂到哪个 target 下。预期现象: 文件生成,三段齐全。
systemctl daemon-reload
systemctl enable --now hello
systemctl status hello | head -8
预期现象: status 的 Active 行 active (running),Main PID 有值。若报红字,立刻看第 6 页排错。daemon-reload 是"让 systemd 重新读说明书",改完 unit 必做。
journalctl -u hello -n 20 --no-pager
预期现象: 持续出现 hello-svc 还活着,现在时间:... 行。说明脚本输出已被 systemd 收进 journal,不用自己再写日志文件。
mkdir -p /opt/lab/oneshot-svc
cat > /opt/lab/oneshot-svc/boot-time.sh <<'EOF'
#!/bin/bash
echo "开机时间记录:$(date '+%F %T')" >> /opt/lab/oneshot-svc/boot.log
EOF
chmod +x /opt/lab/oneshot-svc/boot-time.sh
cat > /etc/systemd/system/bootnote.service <<'EOF'
[Unit]
Description=Boot Time Note (oneshot)
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/opt/lab/oneshot-svc/boot-time.sh
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now bootnote
systemctl status bootnote | head -6
cat /opt/lab/oneshot-svc/boot.log
预期现象: bootnote 的 Active 行 active (exited)(oneshot 退出了,因 RemainAfterExit=yes 仍算"已做");boot.log 有一行时间记录。这适合"开机执行一次"型任务。
systemctl disable --now hello
systemctl disable --now bootnote
rm -f /etc/systemd/system/hello.service /etc/systemd/system/bootnote.service
systemctl daemon-reload
rm -rf /opt/lab/hello-svc /opt/lab/oneshot-svc
预期现象: 删除 unit 后 systemctl status hello 报 Unit hello.service not found;自建目录清空。删 unit 后必须再 daemon-reload,否则 systemd 残留记录。
| 路径 | 作用 | 本课是否改动 |
|---|---|---|
/opt/lab/hello-svc/hello.sh |
常驻演示脚本(实验自建) | 是(实验后删除) |
/etc/systemd/system/hello.service |
自定义服务 unit(实验自建) | 是(写又删) |
/opt/lab/oneshot-svc/boot-time.sh |
oneshot 演示脚本(实验自建) | 是(实验后删除) |
/etc/systemd/system/bootnote.service |
oneshot 服务 unit(实验自建) | 是(写又删) |
/opt/lab/oneshot-svc/boot.log |
oneshot 写出的开机时间(实验自建) | 是(随目录删) |
| 检查项 | 命令 | 期望结果 |
|---|---|---|
| 脚本可执行 | ls -l /opt/lab/hello-svc/hello.sh |
权限含 x |
| 脚本绝对路径 | head -1 ...hello.sh |
#!/bin/bash 存在 |
| 服务在跑 | systemctl is-active hello |
active |
| 开机自启 | systemctl is-enabled hello |
enabled |
| 日志进 journal | journalctl -u hello -n 5 |
有时间行输出 |
| oneshot 状态 | systemctl is-active bootnote |
active(exited) |
| 实验清理 | systemctl status hello |
not found |
来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册
上面你写的是 hello 常驻服务和 bootnote 一次性服务。现在换成你的版本。
svcuser(useradd),把 hello.service 的 User= 改成它,验证以该用户身份运行(ps 看进程属主)。[Service] 里加 Environment=GREETING=hi,脚本里 echo "$GREETING",验证环境变量生效。Restart=on-failure 是否把它拉起(看 systemctl status 的 restarted 计数)。| 序号 | 做什么 | 用什么命令 | 完成的证据 |
|---|---|---|---|
| 1 | 改脚本内容 | 重写 hello.sh + reload + enable | journalctl -u hello 含 hostname |
| 2 | 换运行用户 | useradd svcuser + 改 User= |
ps -o user= -p <PID> 为 svcuser |
| 3 | 加环境变量 | [Service] 加 Environment= |
日志里出现变量值 |
| 4 | 失败重启演示 | 脚本 exit 1 + 看 status | restarted 计数增加 |
| 5 | 解释类型区别 | 文字写进练习记录 | 记录里有正确表述 |
| 必须做到 | 为什么是底线 |
|---|---|
能独立写一个可运行的 .service 并 enable –now |
这是"脚本变服务"的核心交付能力 |
| 能说清 ExecStart 必须绝对路径 + 脚本要 chmod +x | 这两个错占新手 90% 的失败 |
| 知道改完 unit 必做 daemon-reload | 漏了 reload 改了也白改 |
下载包里的 练习记录.md 里有一张"自定义服务"表,填进去:
systemctl status hello 的 Active 行结果;journalctl -u hello -n 5 的样例输出;User= 后 ps 看到的进程属主;systemctl status hello 输出(含 active running);journalctl -u hello -n 20 输出(含时间行);来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册
| 现象 / 报错原文 | 先检查哪里 | 修好后怎样确认 |
|---|---|---|
status 红字 Permission denied |
脚本没 chmod +x |
ls -l 看权限含 x,重试 |
ExecStart=... not found |
ExecStart 写了相对路径/脚本名 | 改成绝对路径 /opt/.../x.sh |
| 改了 unit 不生效 | 没 daemon-reload |
改完 systemctl daemon-reload 再 restart |
activating 一直不进 running |
Type 设错(常驻设成 oneshot) | 常驻脚本改 Type=simple |
failed,红字 code=exited |
脚本里命令报错/退出码非 0 | journalctl -xeu hello 看详细 |
enable 不生效 |
[Install] 缺 WantedBy |
补 WantedBy=multi-user.target 再 reload |
| 日志 journalctl 看不到 | 没设 StandardOutput=journal 或脚本没输出 |
加该字段,确认脚本有 echo |
脚本先 chmod 加 x, ExecStart 绝对路径; 改完文件 reload, 类型选对才运行。
rm -rf /etc/systemd/system 或删系统自带 unit 来"清错”。系统 unit 由软件包管,误删会破坏服务。自定义服务只建/删你自己的文件(如 hello.service)。chmod 777 或把 User= 乱改。权限按最小够用,777 是坏习惯;需要非 root 运行就建专用用户。daemon-reload 反复 restart 还怪服务坏。改 unit 文件后 reload 是硬规定,不是可选项。先 reload 再 restart。systemctl status hello 完整输出(含红字);journalctl -xeu hello 的错误片段;/etc/systemd/system/hello.service 全文;ls -l 权限。systemctl daemon-reload 让 systemd 重新读)来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册
第 3 页问的是:常驻 while true 脚本和"跑一次就退"的脚本,分别该用哪种 Type。
常驻脚本(如 hello.sh 里的 while true)用 Type=simple:systemd 认为它一直前台活着就是正常,适合"服务常驻"。一次性任务(如写开机时间)用 Type=oneshot,它跑完就退出;配合 RemainAfterExit=yes 让 systemd 在它退出后仍标记为 active,表示"这件事我已经做过了"。选错类型会出怪现象:常驻脚本设成 oneshot,systemd 等它"一次跑完"却永远不结束,就一直 activating;一次性任务设成 simple,退出后 systemd 以为它"挂了"可能触发重启。所以"活多久"决定"用什么 Type"。
| 问题 | 参考答案要点 |
|---|---|
| 脚本变成服务的关键两步是什么? | 写 unit 文件(绝对路径 ExecStart)+ chmod +x 脚本;再 daemon-reload + enable --now |
| simple 与 oneshot 怎么区分使用? | 常驻循环用 simple;跑一次即退的任务用 oneshot(配 RemainAfterExit 算仍在) |
| 问题 | 答案要点 |
|---|---|
| 自定义 unit 放哪? | /etc/systemd/system/,优先级高于 /usr/lib/systemd/system/ |
| ExecStart 的硬性要求? | 必须绝对路径;脚本还要 chmod +x 且有 shebang |
| 改完 unit 文件必做? | systemctl daemon-reload 再 restart/enable |
[Install] 段作用? |
WantedBy= 决定开机自启挂到哪个 target(如 multi-user.target) |
| 日志怎么进 journal? | [Service] 设 StandardOutput=journal,用 journalctl -u 名 看 |
cat /etc/systemd/system/hello.service 指出三段与 ExecStart 绝对路径;systemctl enable --now hello 后 systemctl status hello 指出 active running;journalctl -u hello -n 5 指出脚本输出的时间行;systemctl cat hello 指出 systemd 已识别该 unit。systemctl status hello 为 active (running)journalctl -u hello 能看到脚本输出+x 权限、ExecStart 为绝对路径课后把 Type=simple 的 hello 服务停掉,再把它的 unit 改成 Type=oneshot + RemainAfterExit=yes 并 reload 重启,观察 systemctl status 的 Active 行从 running 变成 exited——你会更直观地记住"类型决定 systemd 怎么理解服务的生死"。
来源:本课自编 Linux 服务器配置与管理实训(CentOS Stream 9)· 命令依据 RHEL 9 官方文档与 CentOS Stream 9 发行说明 · 服务配置依据各软件官方手册