本文档风哥主要介绍RHEL LINUX 10系统重启与关机命令,包括shutdown、reboot、poweroff、halt等命令的使用方法,参考Red Hat Enterprise Linux 10官方文档System
Administrator’s Guide内容,适合Linux运维人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。更多视频教程www.fgedu.net.cn
参考Red Hat Enterprise Linux 10官方文档中的System administration章节
Part01-基础概念与理论知识
1.1 系统关机与重启的概念
系统关机与重启是Linux系统管理的基本操作。正确的关机和重启流程可以确保数据完整性、避免文件系统损坏、保护硬件设备。RHEL LINUX
10使用systemd作为初始化系统,通过systemctl命令管理系统的运行状态。
- 关机(Shutdown):停止所有服务,卸载文件系统,切断电源
- 重启(Reboot):停止所有服务,卸载文件系统,重新启动系统
- 挂起(Suspend):将系统状态保存到内存,进入低功耗状态
- 休眠(Hibernate):将系统状态保存到磁盘,完全断电
1.2 运行级别与系统目标
RHEL LINUX 10使用target概念替代传统的运行级别(runlevel):
# systemctl get-default
multi-user.target
# 查看所有可用的系统目标
# systemctl list-units –type=target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Local Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network.target loaded active active Network
nss-lookup.target loaded active active Host and Network Name Lookups
nss-user-lookup.target loaded active active User and Group Name Lookups
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
# 运行级别与target对应关系
运行级别0 <-> poweroff.target (关机)
运行级别1 <-> rescue.target (单用户模式)
运行级别2 <-> multi-user.target (多用户模式,无NFS)
运行级别3 <-> multi-user.target (完整多用户模式)
运行级别4 <-> multi-user.target (未使用)
运行级别5 <-> graphical.target (图形化界面)
运行级别6 <-> reboot.target (重启)
1.3 关机流程与注意事项
正确的关机流程包括以下步骤:
1. 通知所有登录用户
2. 停止正在运行的服务
3. 同步文件系统缓存
4. 卸载所有文件系统
5. 停止内核进程
6. 切断电源或重启
# 关机注意事项
– 确保所有重要数据已保存
– 确认没有关键服务正在运行
– 检查是否有用户正在登录
– 避免强制断电关机
– 记录关机原因和时间
Part02-生产环境规划与建议
2.1 关机重启规划策略
在生产环境中,关机重启需要制定详细的规划:
1. 时间窗口选择
– 选择业务低峰期
– 避开关键业务时段
– 预留足够的维护时间
2. 影响评估
– 评估对业务的影响范围
– 确认依赖服务的状态
– 制定回滚方案
3. 通知机制
– 提前通知所有相关人员
– 说明关机原因和预计时间
– 提供紧急联系方式
4. 操作流程
– 制定详细的操作步骤
– 准备应急预案
– 安排专人监控
2.2 关机通知与协调
关机前需要通知相关人员和系统: from LinuxDBA视频:www.itpux.com
# wall “系统将于30分钟后进行维护关机,请保存您的工作并退出系统。”
Broadcast message from root@rhel10-server (Fri 2026-04-02 14:00:00 CST):
系统将于30分钟后进行维护关机,请保存您的工作并退出系统。
# 使用shutdown命令发送通知
# shutdown -h +30 “系统维护关机,请保存工作”
Shutdown scheduled for Fri 2026-04-02 14:30:00 CST, use ‘shutdown -c’ to cancel.
# 查看登录用户
# who
root pts/0 2026-04-02 10:00 (192.168.1.100)
admin pts/1 2026-04-02 11:30 (192.168.1.101)
test pts/2 2026-04-02 12:00 (192.168.1.102)
# 发送消息给特定用户
# write admin pts/1
系统将在30分钟后关机,请保存您的工作。
# 按Ctrl+D发送
# 查看系统运行时间和负载
# uptime
14:00:01 up 4 days, 4:00, 3 users, load average: 0.00, 0.01, 0.05
2.3 关机安全检查清单
执行关机前需要完成以下安全检查:
□ 1. 检查是否有用户登录
who
w
□ 2. 检查关键服务状态
systemctl status httpd
systemctl status mariadb
systemctl status docker
□ 3. 检查磁盘使用情况
df -h
□ 4. 检查内存使用情况
free -h
□ 5. 检查进程状态
ps aux | head -20
top -bn1 | head -20
□ 6. 检查网络连接
netstat -tuln
ss -tuln
□ 7. 同步文件系统
sync
□ 8. 备份重要配置
tar -czf /backup/config-$(date +%Y%m%d).tar.gz /etc
□ 9. 记录系统状态
dmesg > /var/log/dmesg-before-shutdown.log
journalctl -b > /var/log/journal-before-shutdown.log
□ 10. 确认备份完成
检查备份任务是否完成
Part03-生产环境项目实施方案
3.1 shutdown命令详解
shutdown命令是最常用的关机和重启命令,提供了丰富的选项:
shutdown [OPTIONS] [TIME] [WALL]
# 立即关机
# shutdown -h now
Shutdown scheduled for Fri 2026-04-02 14:00:00 CST, use ‘shutdown -c’ to cancel.
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 指定时间关机
# shutdown -h 14:30
Shutdown scheduled for Fri 2026-04-02 14:30:00 CST, use ‘shutdown -c’ to cancel.
# 延迟关机(分钟后)
# shutdown -h +10
Shutdown scheduled for Fri 2026-04-02 14:10:00 CST, use ‘shutdown -c’ to cancel.
# 带消息关机
# shutdown -h +5 “系统维护,请保存工作”
Shutdown scheduled for Fri 2026-04-02 14:05:00 CST, use ‘shutdown -c’ to cancel.
# 取消计划关机
# shutdown -c
Broadcast message from root@rhel10-server (Fri 2026-04-02 14:01:00 CST):
The system shutdown has been cancelled at Fri 2026-04-02 14:05:00 CST!
# 重启系统
# shutdown -r now
Shutdown scheduled for Fri 2026-04-02 14:00:00 CST, use ‘shutdown -c’ to cancel.
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 指定时间重启
# shutdown -r 14:30
Shutdown scheduled for Fri 2026-04-02 14:30:00 CST, use ‘shutdown -c’ to cancel.
# 仅发送警告消息(不关机)
# shutdown -k now “系统将在14:30关机,请保存工作”
Broadcast message from root@rhel10-server (Fri 2026-04-02 14:00:00 CST):
系统将在14:30关机,请保存工作
The system is going down for poweroff at Fri 2026-04-02 14:30:00 CST!
(WARNING: This is a warning only! The system is NOT going down!)
# 查看shutdown帮助
# shutdown –help
shutdown [OPTIONS…] [TIME] [WALL…]
Options:
–help Show this help
-H –halt Halt the machine
-P –poweroff Power-off the machine
-r –reboot Reboot the machine
-h Equivalent to –poweroff, overridden by –halt
-k Don’t halt/power-off/reboot, just send warnings
–no-wall Don’t send wall message before halt/power-off/reboot
-c Cancel a pending shutdown
3.2 reboot命令详解
reboot命令用于重启系统,是shutdown -r的快捷方式: 更多学习教程公众号风哥教程itpux_com
reboot [OPTIONS]
# 立即重启
# reboot
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 强制重启(不调用shutdown)
# reboot -f
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 重启并进入固件界面(某些系统支持)
# reboot –firmware-setup
System is going down for reboot to firmware setup at Fri 2026-04-02 14:00:00 CST!
# 重启到特定目标
# systemctl reboot –target=rescue.target
System is going down to rescue target at Fri 2026-04-02 14:00:00 CST!
# 查看reboot帮助
# reboot –help
reboot [OPTIONS…]
Options:
–help Show this help
–halt Halt the machine
-p –poweroff Switch off the machine
–reboot Reboot the machine
-f –force Force immediate halt/power-off/reboot
-w –wtmp-only Don’t halt/power-off/reboot, just write wtmp record
-d –no-wtmp Don’t write wtmp record
–no-wall Don’t send wall message before halt/power-off/reboot
3.3 poweroff与halt命令
poweroff和halt命令用于关闭系统:
# poweroff
System is going down for poweroff at Fri 2026-04-02 14:00:00 CST!
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 强制关机
# poweroff -f
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# halt命令(停止系统但不一定切断电源)
# halt
System is going down for halt at Fri 2026-04-02 14:00:00 CST!
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 强制停止
# halt -f
Connection closing by remote host.
Connection to 192.168.1.10 closed.
# 使用systemctl命令关机
# systemctl poweroff
System is going down for poweroff at Fri 2026-04-02 14:00:00 CST!
# 使用systemctl命令重启
# systemctl reboot
System is going down for reboot at Fri 2026-04-02 14:00:00 CST!
# 使用systemctl命令挂起
# systemctl suspend
System is going down for suspend at Fri 2026-04-02 14:00:00 CST!
# 使用systemctl命令休眠
# systemctl hibernate
System is going down for hibernate at Fri 2026-04-02 14:00:00 CST!
# 查看poweroff帮助
# poweroff –help
poweroff [OPTIONS…]
Options:
–help Show this help
–halt Halt the machine
-p –poweroff Switch off the machine
–reboot Reboot the machine
-f –force Force immediate halt/power-off/reboot
-w –wtmp-only Don’t halt/power-off/reboot, just write wtmp record
-d –no-wtmp Don’t write wtmp record
–no-wall Don’t send wall message before halt/power-off/reboot
Part04-生产案例与实战讲解
4.1 定时关机与取消案例
案例:设置系统在晚上22:00自动关机,并在需要时取消。 学习交流加群风哥QQ113257174
# shutdown -h 22:00 “系统将在22:00进行例行维护关机”
Shutdown scheduled for Fri 2026-04-02 22:00:00 CST, use ‘shutdown -c’ to cancel.
# 查看计划关机状态
# systemctl list-jobs
JOB UNIT TYPE STATE
1476 shutdown.target start pending
1475 systemd-shutdownd.service start running
2 jobs listed.
# 查看shutdown计划文件
# cat /run/systemd/shutdown/scheduled
USEC=1712054400000000
WARN_WALL_MESSAGE=系统将在22:00进行例行维护关机
MODE=poweroff
# 取消计划关机
# shutdown -c
Broadcast message from root@rhel10-server (Fri 2026-04-02 14:00:00 CST):
The system shutdown has been cancelled at Fri 2026-04-02 22:00:00 CST!
# 使用at命令设置定时关机
# at 22:00
warning: commands will be executed using /bin/sh
at> shutdown -h now
at>
job 1 at Fri Apr 2 22:00:00 2026
# 查看at任务列表
# atq
1 Fri Apr 2 22:00:00 2026 a root
# 删除at任务
# atrm 1
# 使用cron设置定时关机
# crontab -e
# 添加以下行(每天22:00关机)
0 22 * * * /sbin/shutdown -h now
# 查看cron任务
# crontab -l
0 22 * * * /sbin/shutdown -h now
# 删除cron任务
# crontab -r
4.2 紧急重启处理案例
案例:系统出现严重故障需要紧急重启,但需要尽可能保护数据。 学习交流加群风哥微信: itpux-com
# uptime
14:00:01 up 4 days, 4:00, 3 users, load average: 25.00, 20.00, 15.00
# 查看进程状态
# top -bn1 | head -20
top – 14:00:01 up 4 days, 4:00, 3 users, load average: 25.00, 20.00, 15.00
Tasks: 250 total, 1 running, 249 sleeping, 0 stopped, 0 zombie
%Cpu(s): 95.0 us, 5.0 sy, 0.0 ni, 0.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 8192.0 total, 256.0 free, 7680.0 used, 256.0 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 256.0 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
12345 mysql 20 0 2048.0 1024 512 R 80.0 12.5 0:30.00 mysqld
12346 apache 20 0 1024.0 512 256 S 10.0 6.2 0:15.00 httpd
# 尝试正常重启
# shutdown -r now
Shutdown scheduled for Fri 2026-04-02 14:00:00 CST, use ‘shutdown -c’ to cancel.
# 如果shutdown无响应,尝试强制同步文件系统
# sync
# sync
# sync
# 使用SysRq强制重启(如果启用)
# echo 1 > /proc/sys/kernel/sysrq
# echo s > /proc/sysrq-trigger # 同步所有文件系统
# echo u > /proc/sysrq-trigger # 卸载所有文件系统
# echo b > /proc/sysrq-trigger # 立即重启
# 或者使用reboot -f强制重启
# reboot -f
# 检查SysRq是否启用
# cat /proc/sys/kernel/sysrq
1
# 永久启用SysRq
# echo “kernel.sysrq = 1” >> /etc/sysctl.d/99-sysctl.conf
# sysctl -p /etc/sysctl.d/99-sysctl.conf
kernel.sysrq = 1
4.3 远程关机管理案例
案例:需要远程管理多台服务器的关机和重启操作。
# ssh root@192.168.1.10 “shutdown -h +5 ‘系统维护关机'”
Shutdown scheduled for Fri 2026-04-02 14:05:00 CST, use ‘shutdown -c’ to cancel.
# 远程重启单台服务器
# ssh root@192.168.1.10 “reboot”
Connection to 192.168.1.10 closed by remote host.
# 批量远程关机
# for host in 192.168.1.10 192.168.1.11 192.168.1.12; do
ssh root@$host “shutdown -h +10 ‘系统维护关机'”
done
Shutdown scheduled for Fri 2026-04-02 14:10:00 CST, use ‘shutdown -c’ to cancel.
Shutdown scheduled for Fri 2026-04-02 14:10:00 CST, use ‘shutdown -c’ to cancel.
Shutdown scheduled for Fri 2026-04-02 14:10:00 CST, use ‘shutdown -c’ to cancel.
# 使用Ansible批量关机
# ansible webservers -m command -a “shutdown -h +5 ‘系统维护关机'” -b
192.168.1.10 | CHANGED | rc=0 >>
Shutdown scheduled for Fri 2026-04-02 14:05:00 CST, use ‘shutdown -c’ to cancel.
192.168.1.11 | CHANGED | rc=0 >>
Shutdown scheduled for Fri 2026-04-02 14:05:00 CST, use ‘shutdown -c’ to cancel.
192.168.1.12 | CHANGED | rc=0 >>
Shutdown scheduled for Fri 2026-04-02 14:05:00 CST, use ‘shutdown -c’ to cancel.
# 检查远程服务器状态
# ssh root@192.168.1.10 “systemctl is-active sshd”
active
# 查看远程服务器运行时间
# ssh root@192.168.1.10 “uptime”
14:00:01 up 4 days, 4:00, 3 users, load average: 0.00, 0.01, 0.05
# 远程取消关机
# ssh root@192.168.1.10 “shutdown -c”
Broadcast message from root@rhel10-server (Fri 2026-04-02 14:01:00 CST):
The system shutdown has been cancelled at Fri 2026-04-02 14:05:00 CST!
# 使用IPMI远程关机(如果支持)
# ipmitool -I lan -H 192.168.1.100 -U admin -P password power off
Chassis Power Control: Down/Off
# 使用IPMI远程开机
# ipmitool -I lan -H 192.168.1.100 -U admin -P password power on
Chassis Power Control: Up/On
# 查看IPMI电源状态
# ipmitool -I lan -H 192.168.1.100 -U admin -P password power status
Chassis Power is on
Part05-风哥经验总结与分享
5.1 关机重启最佳实践
基于多年运维经验,总结关机重启的最佳实践:
1. 操作前准备
– 提前通知所有相关人员
– 确认业务低峰期时间窗口
– 准备应急预案和回滚方案
– 备份重要数据和配置
2. 操作流程
– 先停止应用服务
– 再停止系统服务
– 同步文件系统
– 执行关机命令
3. 操作后验证
– 确认系统已完全关闭
– 记录关机时间和原因
– 规划下次开机时间
4. 紧急情况处理
– 优先使用正常关机命令
– 必要时使用强制选项
– 记录异常情况
– 开机后检查系统状态
# 常用命令组合
# 安全关机流程
systemctl stop httpd mariadb docker
sync
shutdown -h +5 “系统维护关机”
# 安全重启流程
systemctl restart httpd mariadb docker
sync
shutdown -r +5 “系统维护重启”
5.2 关机前检查清单
提供一份完整的关机前检查清单:
□ 1. 通知相关人员
– 发送邮件通知
– 发送系统消息(wall)
– 确认收到回复
□ 2. 检查服务状态
systemctl status httpd
systemctl status mariadb
systemctl status docker
systemctl status kubelet
□ 3. 检查用户连接
who
w
last -n 10
□ 4. 检查系统资源
df -h
free -h
uptime
□ 5. 检查进程状态
ps aux | head -20
top -bn1 | head -20
□ 6. 检查网络连接
netstat -tuln
ss -tuln
□ 7. 同步文件系统
sync
□ 8. 备份配置文件
tar -czf /backup/config-$(date +%Y%m%d).tar.gz /etc
□ 9. 记录系统状态
journalctl -b > /var/log/journal-before-shutdown.log
□ 10. 执行关机命令
shutdown -h +5 “系统维护关机”
5.3 紧急情况处理指南
提供紧急情况下的处理指南:
1. 系统无响应
– 尝试SSH连接
– 尝试控制台登录
– 使用SysRq组合键
– 最后手段:强制断电
2. 关机命令无响应
– 使用shutdown -h now
– 使用poweroff -f
– 使用reboot -f
– 使用SysRq强制重启
3. 文件系统损坏
– 进入单用户模式
– 运行fsck修复
– 备份数据
– 重新安装系统
4. 远程无法连接
– 检查网络连接
– 使用IPMI管理
– 联系机房人员
– 现场处理
# SysRq组合键(Magic SysRq)
Alt + SysRq + h # 显示帮助
Alt + SysRq + s # 同步文件系统
Alt + SysRq + u # 卸载文件系统
Alt + SysRq + e # 终止所有进程
Alt + SysRq + i # 强制终止所有进程
Alt + SysRq + b # 立即重启
Alt + SysRq + o # 立即关机
# 启用SysRq
echo 1 > /proc/sys/kernel/sysrq
echo “kernel.sysrq = 1” >> /etc/sysctl.d/99-sysctl.conf
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
