注:英文引文,机翻未校。
如有内容异常,请看原文。
How to Boot into Single User Mode in CentOS/RHEL 7/8
如何在 CentOS/RHEL 7/8 中进入单用户模式
June 2, 2021 - by Magesh Maruthamuthu
Single user mode, also referred to as maintenance mode, allows a single super user to recover/repair the system problems.
单用户模式,也称为维护模式,允许单个超级用户恢复/修复系统问题。
Generally, these problems cannot be solved in a multi-user environment. The system can boot but will not function properly OR you won’t be able to log in.
通常,这些问题无法在多用户环境中解决。系统可以启动,但无法正常运行,或者您将无法登录。
Maintenance mode usesrunlevel1.targetorrescue.targeton Red Hat (RHEL) 7/8 based systems.
维护模式在基于 Red Hat (RHEL) 7/8 的系统上使用runlevel1.target或rescue.target。
In this mode, the system mounts all local file systems, but does not activate network interfaces. It only enables certain services and minimal functionality to repair the system.
在此模式下,系统挂载所有本地文件系统,但不激活网络接口。它仅启用某些服务和最小功能以修复系统。
This method is mostly useful when you want to run‘fsck’to‘fix corrupted file systems’, or to reset a forgotten root password, or to fix a mount point issue on the system.
此方法主要在您想要运行‘fsck’来‘修复损坏的文件系统’、重置遗忘的 root 密码或修复系统上的挂载点问题时有用。
You can boot CentOS/RHEL 7/8 systems in single user mode using the below three methods:
您可以使用以下三种方法在 CentOS/RHEL 7/8 系统中以单用户模式启动:
Method-1:Boot CentOS/RHEL 7/8 systems in single user mode by adding the“rd.break”parameter to the kernel
方法一:通过将“rd.break”参数添加到内核,以单用户模式启动 CentOS/RHEL 7/8 系统Method-2:Boot CentOS/RHEL 7/8 systems in single user mode by replacing the“rhgb quiet”word with the “init=/bin/bash or init=/bin/sh” parameter in the kernel
方法二:通过用 “init=/bin/bash 或 init=/bin/sh” 参数替换“rhgb quiet”字段,以单用户模式启动 CentOS/RHEL 7/8 系统Method-3:Boot CentOS/RHEL 7/8 systems in single user mode by replacing the“ro”word with the “rw init=/sysroot/bin/sh” parameter in the kernel
方法三:通过用 “rw init=/sysroot/bin/sh” 参数替换“ro”字段,以单用户模式启动 CentOS/RHEL 7/8 系统
Method-1: Booting CentOS/RHEL 7/8 in single user mode
方法一:在单用户模式下启动 CentOS/RHEL 7/8
Reboot your system. On the GRUB2 boot screen, press the“e”key to edit the selected kernel. You need to select the first line, the first one is the latest kernel whereas you can select the other if you would like to boot your system with the older kernel.
重启您的系统。在 GRUB2 启动屏幕上,按“e”键以编辑所选内核。您需要选择第一行,第一行是最新的内核,如果您想用旧内核启动系统,可以选择其他行。
Depending on your RHEL/CentOS version, find the word“linux16”or“linux”, and press the “End” button on the keyboard to go to the end of the line, and add the keyword“rd.break”as shown in the screenshot below, then press“Ctrl+x”or“F10”to boot into single-user mode.
根据您的 RHEL/CentOS 版本,找到“linux16”或“linux”,然后按键盘上的 “End” 按钮以跳转到行末,添加关键字“rd.break”,如下面的屏幕截图所示,然后按“Ctrl+x”或“F10”以进入单用户模式。
Note: You need to find the wordlinux16for RHEL/CentOS 7 systems, whilelinuxfor RHEL/CentOS 8 systems.
注意:对于 RHEL/CentOS 7 系统,您需要找到单词linux16,而对于 RHEL/CentOS 8 系统,则为linux。
This change, mounts your root file system into“read only (RO)”mode. You can check this by running the command below. Also, the output below clearly shows that you are in“Emergency Mode”.
此更改将您的根文件系统挂载为“只读 (RO)”模式。您可以通过运行下面的命令来检查这一点。此外,下面的输出清楚地表明您处于“紧急模式”。
\#mount|greprootTo make changes to the“sysroot”file system you need to remount it with READ and WRITE (RW) mode:
要对“sysroot”文件系统进行更改,您需要以读写 (RW) 模式重新挂载它:
\#mount-oremount,rw /sysrootRun the below command to change the environment, commonly known as “jailed directory” or “chroot jail”:
运行下面的命令以更改环境,通常称为“监禁目录”或“chroot 监狱”:
\#chroot/sysrootNow, the single-user mode is ready to use. Once you have fixed your problem, and to exit the single user mode, perform the following steps.
现在,单用户模式已准备就绪。一旦您解决了问题,要退出单用户模式,请执行以下步骤。
CentOS/RHEL 7/8 uses SELinux by default, so create the following hidden file, which will automatically perform a relabel of all files on next boot:
CentOS/RHEL 7/8 默认使用 SELinux,因此请创建以下隐藏文件,这将在下次启动时自动重新标记所有文件:
\#touch/.autorelabelFinally, run the below command to restart the system. Alternatively, type“exit”command twice to restart your system:
最后,运行下面的命令以重启系统。或者,输入“exit”命令两次以重启您的系统:
\#reboot-fMethod-2: How to Boot into single user mode in CentOS/RHEL 7/8
方法二:如何在 CentOS/RHEL 7/8 中进入单用户模式
Reboot your system. On the GRUB2 boot screen, press the“e”key to edit the selected kernel parameters.
重启您的系统。在 GRUB2 启动屏幕上,按“e”键以编辑所选内核参数。
Find the word“rhgb quiet”and replace it with“init=/bin/bash”or“init=/bin/sh”, then press“Ctrl+x”or“F10”to boot in the single user mode.
找到“rhgb quiet”并将其替换为“init=/bin/bash”或“init=/bin/sh”,然后按“Ctrl+x”或“F10”以进入单用户模式。
Screenshot forinit=/bin/bash:
Screenshot forinit=/bin/sh:
By default, this will mount your “/” partition in read-only (RO) mode, so you will need to remount the “/” file system with READ and WRITE (RW) mode to make changes:
默认情况下,这将以只读 (RO) 模式挂载您的 “/” 分区,因此您需要以读写 (RW) 模式重新挂载 “/” 文件系统以进行更改:
\#mount-oremount,rw /You can now perform any task that you want. When you are done, run the following command to enable SELinux relabeling on reboot:
您现在可以执行任何想要的任务。当您完成后,运行以下命令以在重启时启用 SELinux 重新标记:
\#touch/.autorelabelFinally reboot the system:
最后重启系统:
\#exec/sbin/init6Method-3: How to Boot into emergency mode in RHEL/CentOS 7/8
方法三:如何在 RHEL/CentOS 7/8 中启动紧急模式
To interrupt the automatic boot, reboot your system and press any key on the GRUB2 splash screen.
要中断自动启动,请重启您的系统并在 GRUB2 启动画面上按任意键。
This will display the list of kernels available on your system. Select the latest kernel and press the"e"key to edit the selected kernel parameters.
这将显示您系统上可用的内核列表。选择最新的内核并按"e"键以编辑所选内核参数。
Find the line that starts with the word“linux”or“linux16”and replace“ro”with“rw init=/sysroot/bin/sh”. When finished, press“Ctrl+x”or“F10”to boot in single user mode.
找到以“linux”或“linux16”开头的行,并将“ro”替换为“rw init=/sysroot/bin/sh”。完成后,按“Ctrl+x”或“F10”以进入单用户模式。
Change the environment to “chroot jail” by running the below command:
通过运行以下命令将环境更改为“chroot 监狱”:
\#chroot/sysrootMake any necessary changes to the system. Once done, run the below command to enable SELinux relabeling on reboot:
对系统进行任何必要的更改。完成后,运行以下命令以在重启时启用 SELinux 重新标记:
\#touch/.autorelabelFinally reboot the system:
最后重启系统:
\#reboot-fClosing Notes
结束语
In this tutorial we’ve shown you three methods to boot RHEL/CentOS 7/8 system in emergency mode. We hope you can boot your Linux system in single user mode to recover/repair the system problems.
在本教程中,我们向您展示了三种在紧急模式下启动 RHEL/CentOS 7/8 系统的方法。我们希望您能在单用户模式下启动 Linux 系统以恢复/修复系统问题。
CentOS / RHEL 7 / 8 单用户模式进入方法对比
CentOS / RHEL 7、8 系列系统支持三种常见的单用户 / 紧急模式进入方式。三种方式在内核参数修改规则、根文件系统挂载状态、运行环境、权限配置等方面存在差异,适配不同故障场景,操作复杂度也各有不同。
| 对比项 | 方法 1:追加rd.break参数 | 方法 2:替换rhgb quiet为init=/bin/bash | 方法 3:替换ro为rw init=/sysroot/bin/sh |
|---|---|---|---|
| 参数修改方式 | 在linux16(RHEL 7)/linux(RHEL 8)行末尾追加rd.break | 删除原有rhgb quiet参数,替换为init=/bin/bash或init=/bin/sh | 删除原有ro只读参数,改为读写并指定真实根分区内 shell:rw init=/sysroot/bin/sh |
| 根挂载状态 | /sysroot默认只读(ro) | /指向 initramfs,默认只读(ro) | /sysroot默认读写(rw) |
| 重挂载操作 | 需执行mount -o remount,rw /sysroot | 需执行mount -o remount,rw /或在切根前将真实根挂载为可写 | 无需手动重挂载,直接切根 / 操作即可 |
| 切根环境 | 需执行chroot /sysroot | 需执行chroot /sysroot | 需执行chroot /sysroot |
| 启动提示符(典型) | switch_root:/#(紧急 / 切根前后阶段依发行版略有差异) | bash-4.x#/sh-4.x#(initramfs 临时 shell) | sh-4.x#(根文件系统真实 shell) |
| 操作复杂度 | 较高:至少包含 “重挂载 + 切根” 两步 | 中等:包含 “重挂载 + 切根” 两步 | 较低:步骤最少(通常只需切根与操作) |
| 适配场景 | 文件系统故障修复、分区损坏 / 挂载异常、fsck校验失败等 | 基础运维操作:重置 root 密码、修改少量配置等 | 日常常规运维操作:需要较直接的真实根环境,兼顾效率与环境隔离 |
备注(关键差异)
- 方法 2的
init=/bin/bash/init=/bin/sh通常在initramfs环境启动;真实根分区会挂载在/sysroot,因此通常仍需要chroot /sysroot才能修改真实系统配置。- 方法 3通过
init=/sysroot/bin/sh直接把 shell 作为真实根分区内的程序启动,因此步骤通常更简洁。
前置:进入 GRUB2 编辑界面
重启服务器,在GRUB2 启动菜单倒计时阶段(建议 3 秒内)按下任意键,中断系统自动启动。选中需要操作的内核版本,按下e键进入内核参数编辑界面。
参数行定位规则
- CentOS / RHEL 7:定位以
linux16开头的内核参数行 - CentOS / RHEL 8:定位以
linux开头的内核参数行
方法 1:追加rd.break参数(适合文件系统 / 紧急救援类)
在内核参数行末尾追加参数:
rd.break步骤如下:
- 按下
End键跳转至内核参数行末尾,使用空格分隔追加rd.break; - 按下
Ctrl+x或F10执行,进入紧急模式(常见提示符如switch_root:/#,不同版本可能略有差异); - 执行
mount | grep root,检查根相关挂载状态,确认当前目标(通常是/sysroot)处于只读; - 执行重挂载命令,开启写权限:
mount-oremount,rw /sysroot- 切换至真实根环境:
chroot/sysroot- 执行对应运维 / 故障修复操作,例如重置 root 密码:
passwdroot- 创建 SELinux 重标记文件:
touch/.autorelabel- 退出并重启:
exitreboot-f方法 2:替换rhgb quiet为init=/bin/bash或init=/bin/sh(适合快速运维)
在linux16/linux参数行中:
- 删除原有
rhgb quiet参数; - 替换为指定初始化程序参数(二选一):
init=/bin/bash或
init=/bin/sh步骤如下:
- 修改完成后按下
Ctrl+x或F10生效,进入initramfs shell环境; - 在 initramfs 环境中,执行重挂载以确保可写。先检查并重挂载当前 shell 所在的
/(因为当前/通常是 initramfs):
mount-oremount,rw /(若检查显示真实根也未可写,再对/sysroot执行同类 remount。)
- 切换至真实根环境:
chroot/sysroot- 执行配置修改 / 故障修复操作;
- 创建 SELinux 重标记文件:
touch/.autorelabel- 重启建议二选一:
- 直接让系统以正常方式进入 init:
exec/sbin/init6- 或执行
reboot -f(避免普通reboot因环境不完整引发异常)。
方法 3:替换ro为rw init=/sysroot/bin/sh(适合需要真实根环境的通用折中方案)
在linux16/linux参数行中:
- 删除默认
ro只读参数; - 替换为读写权限并指定真实根分区内 shell:
rw init=/sysroot/bin/sh步骤如下:
- 按下
Ctrl+x或F10生效,进入真实根文件系统 shell环境; - 切换至真实根环境:
chroot/sysroot- 执行各类运维 / 修复操作(通常默认已是可写,无需再手动重挂载);
- 创建 SELinux 重标记文件:
touch/.autorelabel- 强制重启:
reboot-f备注:该方式直接调用真实根分区内的 shell 程序,因此启动后通常
/sysroot已具备读写条件;不过仍建议在关键操作前用mount | grep root快速确认一次,以防某些环境差异导致 “以为可写但实际只读”。
操作规范说明(适用于三种进入方式)
| 规范项 | 详细说明 |
|---|---|
| 编辑与启动规则 | 仅支持通过 GRUB2 菜单按e进入内核参数编辑模式;修改完成后通过Ctrl+x或F10加载系统;若不小心编辑错误,可按Esc退出编辑并重试。 |
| SELinux 重标记规范 | 三种方式都要在完成配置 / 修复后执行touch /.autorelabel;否则重启后可能出现权限异常或服务无法正常启动。若系统卡在 SELinux 重标记阶段,可等待其完成,或按需临时关闭 SELinux(例如在内核参数中加 selinux=0)。 |
| 运行环境限制 | 单用户 / 紧急模式通常不会加载网络服务与图形界面,仅适合本地修复操作。可用命令以基础工具为主(如mount、chroot、fsck等)。 |
| 重启命令规范 | 方法 1、方法 3:可使用reboot -f;方法 2:更建议使用exec /sbin/init 6(避免不一致的重启路径引发异常)。不建议随意使用普通reboot。 |
验证项(建议每次操作后快速确认)
| 验证项 | 操作方式 / 观察点 | 预期结果 |
|---|---|---|
| 提示符验证 | 观察终端命令行提示符 | 方法 1:常见switch_root:/#;方法 2: bash-4.x#或sh-4.x#;方法 3: sh-4.x#(随发行版略有差异) |
| 挂载状态验证 | 执行 `mount | grep root` |
| 权限验证 | 执行ls /root、passwd root等 | 可直接进行 root 级别操作,通常无需再输入登录密码 |
| 网络状态验证 | 执行ip addr | 一般仅有lo回环接口;物理网卡与 IP 信息可能不可用 |
常见异常与处理
| 异常现象 | 异常根因 | 解决方案 |
|---|---|---|
| 无法进入 GRUB 编辑模式 | 按键时机不对,未成功中断倒计时;或 GRUB 编辑权限被策略禁用 | 重启设备后立即按任意键中断自动启动; 正常环境下检查 GRUB 配置或系统策略是否限制编辑 |
| 参数修改后启动失败 | 内核参数拼写错误;参数与原内容未按空格分隔;格式不规范 | 重新进入 GRUB 编辑界面核对参数:rd.break、init=/bin/bash等拼写与分隔符 |
| 系统提示文件系统只读 | 未执行对应重挂载;或参数替换错误导致仍为只读 | 方法 1:重挂载/sysroot;方法 2:先重挂载当前 shell 所在的 /(或根据实际挂载点再做 remount);方法 3:核对 ro是否已完整替换为rw |
| 密码修改后无法正常登录 | 未创建 SELinux 重标记文件,导致权限 / 上下文不匹配 | 重新进入单用户模式执行touch /.autorelabel,再重启系统完成重标记 |
场景适配速查
| 场景 | 推荐方法 |
|---|---|
| 文件系统损坏、挂载故障、紧急救援 | 方法 1 |
| 重置 root 密码、修改少量配置 | 方法 2 |
| 日常常规运维操作(兼顾效率与环境隔离) | 方法 3 |
总结
三种单用户模式的主要差异集中在GRUB 内核参数修改方式与根文件系统初始挂载状态。
- 方法 2 与方法 1 更容易进入 “initramfs / 切根前后” 的路径,因此通常需要更明确的重挂载与切根步骤;
- 方法 3 通过直接指定真实根中的 shell,步骤更简洁。
无论选择哪种方式,都要遵循统一规范:GRUB 编辑规则、SELinux 重标记、本地环境限制,以及对应的重启建议。以上条件共同保证操作有效且重启后系统能正常运行。
Reference:
- Three Methods Boot CentOS/RHEL 7/8 Systems in Single User Mode - June 2, 2021
https://www.2daygeek.com/boot-centos-7-8-rhel-7-8-single-user-mode/ - How to PROPERLY boot into single user mode in RHEL/CentOS 7/8 | Jul 14, 2022
https://www.golinuxcloud.com/boot-in-single-user-mode-rhel-centos-8-linux/ - How to Boot RHEL 8 / CentOS 8 in Rescue Mode - April 15, 2024
https://www.linuxtechi.com/boot-centos-8-rhel-8-single-user-mode/