目录
✅ 1. Reboot and Access GRUB Menu
✅ 2. Edit the Boot Entry
✅ 3. Remount Root Filesystem as Writable
✅ 4. Reset the Root Password
✅ 5. Update SELinux Context (if applicable)
✅ 6. Reboot
? Notes:
首页 运维 CentOS 如何在CentOS中重置root密码

如何在CentOS中重置root密码

Jul 23, 2025 am 02:16 AM
centos root密码

重启并进入GRUB菜单,按e编辑启动项;2. 在linux或linux16行末添加init=/bin/bash或rd.break;3. 若用rd.break需先chroot /sysroot,再mount -o remount,rw /;4. 执行passwd root设置新密码;5. 若SELinux启用则运行touch /.autorelabel;6. 执行reboot -f重启即可生效,此方法适用于CentOS 7及以上版本且需物理或控制台访问权限。

How to reset root password in CentOS

If you've lost or forgotten the root password on your CentOS system, don't worry — you can reset it by booting into single-user mode. This method works on most CentOS versions (7, 8, Stream, etc.), though the exact steps may vary slightly depending on whether you're using GRUB or GRUB2.

How to reset root password in CentOS

Here’s how to do it:


✅ 1. Reboot and Access GRUB Menu

  • Reboot the machine.
  • As it boots up, hold down the Shift key (for BIOS) or spam Esc (for UEFI) to bring up the GRUB menu.
  • If you see multiple kernel entries, highlight the one you want to boot (usually the top/default one).

✅ 2. Edit the Boot Entry

  • Press e to edit the selected boot entry.
  • Find the line starting with linux or linux16 (this contains kernel parameters).
  • At the end of that line, add:
    init=/bin/bash

    or for newer systems:

    How to reset root password in CentOS
    rd.break

? Why this works:

  • init=/bin/bash bypasses the normal boot process and drops you into a root shell immediately.
  • rd.break pauses the boot before the root filesystem is mounted as read-only — useful for SELinux-aware systems (like CentOS 7 ).

✅ 3. Remount Root Filesystem as Writable

Once you're in the shell (either from init=/bin/bash or after rd.break chroot /sysroot), run:

How to reset root password in CentOS
mount -o remount,rw /

⚠️ If you used rd.break, you’ll first need to:

chroot /sysroot

before remounting.


✅ 4. Reset the Root Password

Now just run:

passwd root

Enter your new password twice when prompted.


✅ 5. Update SELinux Context (if applicable)

If SELinux is enabled (default on CentOS), relabel the password file:

touch /.autorelabel

This ensures SELinux doesn’t block login after the password change.


✅ 6. Reboot

Exit the shell:

exec /sbin/init

or just:

reboot -f

Remove any added boot parameters — they were temporary.


? Notes:

  • This requires physical or console (e.g., KVM/IPMI) access — it won’t work remotely unless you have out-of-band access.
  • In cloud environments (like AWS EC2), use user-data scripts or recovery modes provided by the platform instead.
  • Always secure physical access — anyone with console access can reset root this way.

That’s it! You’ve successfully reset the root password without data loss.
Just remember: this is a powerful recovery method — use it responsibly.

以上是如何在CentOS中重置root密码的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

PHP教程
1591
276
centos和ubuntu的区别 centos和ubuntu的区别 Apr 14, 2025 pm 09:09 PM

CentOS 和 Ubuntu 的关键差异在于:起源(CentOS 源自 Red Hat,面向企业;Ubuntu 源自 Debian,面向个人)、包管理(CentOS 使用 yum,注重稳定;Ubuntu 使用 apt,更新频率高)、支持周期(CentOS 提供 10 年支持,Ubuntu 提供 5 年 LTS 支持)、社区支持(CentOS 侧重稳定,Ubuntu 提供广泛教程和文档)、用途(CentOS 偏向服务器,Ubuntu 适用于服务器和桌面),其他差异包括安装精简度(CentOS 精

centos关机命令行 centos关机命令行 Apr 14, 2025 pm 09:12 PM

CentOS 关机命令为 shutdown,语法为 shutdown [选项] 时间 [信息]。选项包括:-h 立即停止系统;-P 关机后关电源;-r 重新启动;-t 等待时间。时间可指定为立即 (now)、分钟数 ( minutes) 或特定时间 (hh:mm)。可添加信息在系统消息中显示。

.NET Core快速入门教程 1、开篇:说说.NET Core的那些事儿 .NET Core快速入门教程 1、开篇:说说.NET Core的那些事儿 May 07, 2025 pm 04:54 PM

一、.NETCore的起源谈到.NETCore,就不能不提它的前身.NET。当年Java风头正盛,微软也对Java青睐有加,Windows平台上的Java虚拟机就是微软依据JVM标准开发的,据称是当时性能最佳的Java虚拟机。然而,微软有自己的小算盘,试图将Java与Windows平台捆绑,增加一些Windows特有的功能。Sun公司对此不满,导致双方关系破裂,微软随后推出了.NET。.NET从诞生之初就借鉴了Java的许多特性,并在语言特性和窗体开发等方面逐渐超越了Java。Java在1.6版

centos配置ip地址 centos配置ip地址 Apr 14, 2025 pm 09:06 PM

CentOS 中配置 IP 地址的步骤:查看当前网络配置:ip addr编辑网络配置文件:sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0更改 IP 地址:编辑 IPADDR= 行更改子网掩码和网关(可选):编辑 NETMASK= 和 GATEWAY= 行重启网络服务:sudo systemctl restart network验证 IP 地址:ip addr

Hadoop伪分布式集群搭建 Hadoop伪分布式集群搭建 May 07, 2025 pm 04:45 PM

软件准备我使用的是CentOS-6.6的虚拟机,主机名为repo。参考在Windows中安装Linux虚拟机的步骤,我在该虚拟机中安装了JDK,参考在Linux中安装JDK的指南。此外,该虚拟机配置了免秘钥登录自身,参考配置各台虚拟机之间免秘钥登录的设置。Hadoop安装包的下载地址为:https://mirrors.aliyun.com/apache/hadoop/common/,我使用的是hadoop2.6.5版本。将Hadoop安装包上传到服务器并解压[root@repo~]#tarzxv

CentOS上Postman集成应用 CentOS上Postman集成应用 May 19, 2025 pm 08:00 PM

在CentOS上集成Postman应用可以通过多种方法来实现,以下是详细的步骤和建议:通过下载安装包安装Postman下载Postman的Linux版本安装包:访问Postman官方网站,选择适用于Linux的版本进行下载。解压安装包:使用以下命令将安装包解压到指定目录,例如/opt:sudotar-xzfpostman-linux-x64-xx.xx.xx.tar.gz-C/opt请注意将“postman-linux-x64-xx.xx.xx.tar.gz”替换为您实际下载的文件名。创建符号

centos如何安装 centos如何安装 Apr 14, 2025 pm 09:03 PM

CentOS 安装步骤:下载 ISO 映像并刻录可引导媒体;启动并选择安装源;选择语言和键盘布局;配置网络;分区硬盘;设置系统时钟;创建 root 用户;选择软件包;开始安装;安装完成后重启并从硬盘启动。

CENTOS:是什么导致决定结束支持的决定 CENTOS:是什么导致决定结束支持的决定 Apr 23, 2025 am 12:10 AM

RedHatendedsupportforCentOStoshifttowardsacommerciallyfocusedmodelwithCentOSStream.1)CentOStransitionedtoCentOSStreamforRHELdevelopment.2)ThisencourageduserstomovetoRHEL.3)AlternativeslikeAlmaLinux,RockyLinux,andOracleLinuxemergedasreplacements.

See all articles