news 2026/7/21 16:36:09

Linux 单用户模式启动 | 三种内核参数方法

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux 单用户模式启动 | 三种内核参数方法

注:英文引文,机翻未校。
如有内容异常,请看原文。


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.targetrescue.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 theekey 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|greproot

To make changes to the“sysroot”file system you need to remount it with READ and WRITE (RW) mode:
要对“sysroot”文件系统进行更改,您需要以读写 (RW) 模式重新挂载它:

\#mount-oremount,rw /sysroot

Run the below command to change the environment, commonly known as “jailed directory” or “chroot jail”:
运行下面的命令以更改环境,通常称为“监禁目录”或“chroot 监狱”:

\#chroot/sysroot

Now, 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/.autorelabel

Finally, run the below command to restart the system. Alternatively, typeexitcommand twice to restart your system:
最后,运行下面的命令以重启系统。或者,输入exit命令两次以重启您的系统:

\#reboot-f

Method-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 theekey 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/.autorelabel

Finally reboot the system:
最后重启系统:

\#exec/sbin/init6

Method-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 replacerowithrw 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/sysroot

Make any necessary changes to the system. Once done, run the below command to enable SELinux relabeling on reboot:
对系统进行任何必要的更改。完成后,运行以下命令以在重启时启用 SELinux 重新标记:

\#touch/.autorelabel

Finally reboot the system:
最后重启系统:

\#reboot-f

Closing 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 quietinit=/bin/bash方法 3:替换rorw init=/sysroot/bin/sh
参数修改方式linux16(RHEL 7)/linux(RHEL 8)行末尾追加rd.break删除原有rhgb quiet参数,替换为init=/bin/bashinit=/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 密码、修改少量配置等日常常规运维操作:需要较直接的真实根环境,兼顾效率与环境隔离

备注(关键差异)

  • 方法 2init=/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

步骤如下:

  1. 按下End键跳转至内核参数行末尾,使用空格分隔追加rd.break
  2. 按下Ctrl+xF10执行,进入紧急模式(常见提示符如switch_root:/#,不同版本可能略有差异);
  3. 执行mount | grep root,检查根相关挂载状态,确认当前目标(通常是/sysroot)处于只读;
  4. 执行重挂载命令,开启写权限:
mount-oremount,rw /sysroot
  1. 切换至真实根环境:
chroot/sysroot
  1. 执行对应运维 / 故障修复操作,例如重置 root 密码:
passwdroot
  1. 创建 SELinux 重标记文件:
touch/.autorelabel
  1. 退出并重启:
exitreboot-f

方法 2:替换rhgb quietinit=/bin/bashinit=/bin/sh(适合快速运维)

linux16/linux参数行中:

  1. 删除原有rhgb quiet参数;
  2. 替换为指定初始化程序参数(二选一):
init=/bin/bash

init=/bin/sh

步骤如下:

  1. 修改完成后按下Ctrl+xF10生效,进入initramfs shell环境;
  2. 在 initramfs 环境中,执行重挂载以确保可写。先检查并重挂载当前 shell 所在的/(因为当前/通常是 initramfs):
mount-oremount,rw /

(若检查显示真实根也未可写,再对/sysroot执行同类 remount。)

  1. 切换至真实根环境:
chroot/sysroot
  1. 执行配置修改 / 故障修复操作;
  2. 创建 SELinux 重标记文件:
touch/.autorelabel
  1. 重启建议二选一:
  • 直接让系统以正常方式进入 init:
exec/sbin/init6
  • 或执行reboot -f(避免普通reboot因环境不完整引发异常)。

方法 3:替换rorw init=/sysroot/bin/sh(适合需要真实根环境的通用折中方案)

linux16/linux参数行中:

  1. 删除默认ro只读参数;
  2. 替换为读写权限并指定真实根分区内 shell:
rw init=/sysroot/bin/sh

步骤如下:

  1. 按下Ctrl+xF10生效,进入真实根文件系统 shell环境;
  2. 切换至真实根环境:
chroot/sysroot
  1. 执行各类运维 / 修复操作(通常默认已是可写,无需再手动重挂载);
  2. 创建 SELinux 重标记文件:
touch/.autorelabel
  1. 强制重启:
reboot-f

备注:该方式直接调用真实根分区内的 shell 程序,因此启动后通常/sysroot已具备读写条件;不过仍建议在关键操作前用mount | grep root快速确认一次,以防某些环境差异导致 “以为可写但实际只读”。

操作规范说明(适用于三种进入方式)

规范项详细说明
编辑与启动规则仅支持通过 GRUB2 菜单按e进入内核参数编辑模式;修改完成后通过Ctrl+xF10加载系统;若不小心编辑错误,可按Esc退出编辑并重试。
SELinux 重标记规范三种方式要在完成配置 / 修复后执行touch /.autorelabel;否则重启后可能出现权限异常或服务无法正常启动。
若系统卡在 SELinux 重标记阶段,可等待其完成,或按需临时关闭 SELinux(例如在内核参数中加selinux=0)。
运行环境限制单用户 / 紧急模式通常不会加载网络服务与图形界面,仅适合本地修复操作。可用命令以基础工具为主(如mountchrootfsck等)。
重启命令规范方法 1、方法 3:可使用reboot -f;方法 2:更建议使用exec /sbin/init 6(避免不一致的重启路径引发异常)。不建议随意使用普通reboot

验证项(建议每次操作后快速确认)

验证项操作方式 / 观察点预期结果
提示符验证观察终端命令行提示符方法 1:常见switch_root:/#
方法 2:bash-4.x#sh-4.x#
方法 3:sh-4.x#(随发行版略有差异)
挂载状态验证执行 `mountgrep root`
权限验证执行ls /rootpasswd root可直接进行 root 级别操作,通常无需再输入登录密码
网络状态验证执行ip addr一般仅有lo回环接口;物理网卡与 IP 信息可能不可用

常见异常与处理

异常现象异常根因解决方案
无法进入 GRUB 编辑模式按键时机不对,未成功中断倒计时;或 GRUB 编辑权限被策略禁用重启设备后立即按任意键中断自动启动;
正常环境下检查 GRUB 配置或系统策略是否限制编辑
参数修改后启动失败内核参数拼写错误;参数与原内容未按空格分隔;格式不规范重新进入 GRUB 编辑界面核对参数:
rd.breakinit=/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/
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/21 16:34:10

如何用xtb进行隐式溶剂化模拟?GBSA与ALPB模型实践指南

如何用xtb进行隐式溶剂化模拟?GBSA与ALPB模型实践指南 【免费下载链接】xtb Semiempirical Extended Tight-Binding Program Package 项目地址: https://gitcode.com/gh_mirrors/xt/xtb xtb是一款强大的半经验扩展紧束缚程序包,专门用于分子和材料…

作者头像 李华
网站建设 2026/7/21 16:33:55

上海零售业4.6%增长背后的消费升级与数字化转型

1. 2025年上海零售业增长4.6%背后的消费新图景 2025年第一季度刚过,上海零售市场就交出了一份亮眼的成绩单——社会消费品零售总额同比增长4.6%,不仅跑赢了全国3.2%的平均增速,更创下了疫情后消费复苏的新纪录。作为一个常年观察消费市场的从…

作者头像 李华
网站建设 2026/7/21 16:30:07

3个关键步骤:用DiskInfo硬盘健康监控工具快速诊断你的数据安全

3个关键步骤:用DiskInfo硬盘健康监控工具快速诊断你的数据安全 【免费下载链接】DiskInfo DiskInfo based on CrystalDiskInfo 项目地址: https://gitcode.com/gh_mirrors/di/DiskInfo 你的电脑最近是不是变得越来越慢?重要文件偶尔打不开&#x…

作者头像 李华
网站建设 2026/7/21 16:28:42

如何快速搭建Lean 4开发环境:面向初学者的完整定理证明指南

如何快速搭建Lean 4开发环境:面向初学者的完整定理证明指南 【免费下载链接】lean4 Lean 4 programming language and theorem prover 项目地址: https://gitcode.com/GitHub_Trending/le/lean4 Lean 4作为新一代函数式编程语言和定理证明器,为开…

作者头像 李华
网站建设 2026/7/21 16:25:48

思源笔记插件开发实战:从用户需求到功能实现的完整指南

思源笔记插件开发实战:从用户需求到功能实现的完整指南 【免费下载链接】siyuan A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. 项目地址: https://gitcode.com/GitHub_Trending/…

作者头像 李华