目录

Linux922-RHEL8忘记root密码GRUB迁移虚拟机1

Linux922 RHEL8:忘记root密码;GRUB;迁移虚拟机(1)

问题

[caozx26@caozx26 ~]$ yum repolist all
2025-09-21 09:58:38,242 [ERROR] yum:3210:MainThread @logutil.py:194 - [Errno 13] 权限不够: '/var/log/rhsm/rhsm.log' - Further logging output will be written to stderr
Not root, Subscription Management repositories not updated
仓库标识                              仓库名称                              状态
AppStream                             AppStream                             启用
BaseOS                                BaseOS                                启用
[caozx26@caozx26 ~]$ yum install network-scripts
2025-09-21 09:59:03,330 [ERROR] yum:3227:MainThread @logutil.py:194 - [Errno 13] 权限不够: '/var/log/rhsm/rhsm.log' - Further logging output will be written to stderr
Not root, Subscription Management repositories not updated
错误:该命令必须以 root 用户运行
[caozx26@caozx26 ~]$ sudo su

我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:

    #1) 尊重别人的隐私。
    #2) 输入前要先考虑(后果和风险)。
    #3) 权力越大,责任越大。

[sudo] caozx26 的密码:
caozx26 不在 sudoers 文件中。此事将被报告。
[caozx26@caozx26 ~]$ su - 
密码:
[root@caozx26 ~]# yum install network-scripts
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:11:09:00 前,执行于 2025年09月20日 星期六 22时50分32秒。
依赖关系解决。
================================================================================
 软件包                     架构         版本                仓库          大小
================================================================================
安装:
 network-scripts            x86_64       10.00.6-1.el8       BaseOS       195 k
安装弱的依赖:
 network-scripts-team       x86_64       1.29-1.el8          BaseOS        26 k

事务概要
================================================================================
安装  2 软件包

总计:221 k
安装大小:179 k
确定吗?[y/N]: y
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : network-scripts-10.00.6-1.el8.x86_64                            1/2 
  运行脚本: network-scripts-10.00.6-1.el8.x86_64                            1/2 
  安装    : network-scripts-team-1.29-1.el8.x86_64                          2/2 
  运行脚本: network-scripts-team-1.29-1.el8.x86_64                          2/2 
  验证    : network-scripts-10.00.6-1.el8.x86_64                            1/2 
  验证    : network-scripts-team-1.29-1.el8.x86_64                          2/2 
Installed products updated.

已安装:
  network-scripts-10.00.6-1.el8.x86_64  network-scripts-team-1.29-1.el8.x86_64 

完毕!
[root@caozx26 ~]# yum install openssh
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:11:09:40 前,执行于 2025年09月20日 星期六 22时50分32秒。
软件包 openssh-8.0p1-4.el8_1.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@caozx26 ~]# yum install dns
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:11:09:47 前,执行于 2025年09月20日 星期六 22时50分32秒。
No match for argument: dns
错误:没有任何匹配: dns
[root@caozx26 ~]# yum install samba
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
上次元数据过期检查:11:10:01 前,执行于 2025年09月20日 星期六 22时50分32秒。
依赖关系解决。
================================================================================
 软件包                   架构         版本                  仓库          大小
================================================================================
安装:
 samba                    x86_64       4.11.2-13.el8         BaseOS       766 k
安装依赖关系:
 samba-common-tools       x86_64       4.11.2-13.el8         BaseOS       472 k
 samba-libs               x86_64       4.11.2-13.el8         BaseOS       170 k

事务概要
================================================================================
安装  3 软件包

总计:1.4 M
安装大小:3.7 M
确定吗?[y/N]: y
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                 1/1 
  安装    : samba-libs-4.11.2-13.el8.x86_64                                 1/3 
  安装    : samba-common-tools-4.11.2-13.el8.x86_64                         2/3 
  安装    : samba-4.11.2-13.el8.x86_64                                      3/3 
  运行脚本: samba-4.11.2-13.el8.x86_64                                      3/3 
  验证    : samba-4.11.2-13.el8.x86_64                                      1/3 
  验证    : samba-common-tools-4.11.2-13.el8.x86_64                         2/3 
  验证    : samba-libs-4.11.2-13.el8.x86_64                                 3/3 
Installed products updated.

已安装:
  samba-4.11.2-13.el8.x86_64         samba-common-tools-4.11.2-13.el8.x86_64   
  samba-libs-4.11.2-13.el8.x86_64   

完毕!
[root@caozx26 ~]# 

https://i-blog.csdnimg.cn/direct/686a1c28053940f5bd1dc8bc212f11f5.png

https://i-blog.csdnimg.cn/direct/aef0bfcb174b44eea90f73b2b76fb45a.png
https://i-blog.csdnimg.cn/direct/57ddc793ac1948cdae8c899dc3c1560f.png
https://i-blog.csdnimg.cn/direct/c82115b65d4843a299cf81dc5aebae91.png
https://i-blog.csdnimg.cn/direct/a707782491ec4a5e9a553ddb05f25406.png
https://i-blog.csdnimg.cn/direct/310f45df30914052a6bf261ca3ec8116.png
https://i-blog.csdnimg.cn/direct/bee948e0bacf4b73a9d2febb038b8692.png
https://i-blog.csdnimg.cn/direct/9d8a8451efa04148bf91733c2a687cfd.png

重设root密码

开机 e
linux rd.break
https://i-blog.csdnimg.cn/direct/289e23fc699b4282901be2aaa6c40627.png

ctrl x

mount -o remount,rw /sysroot
chroot /sysroot
passwd root
touch /.autorelabel
exit

https://i-blog.csdnimg.cn/direct/4639bda202e444d6a17f5bb1e0625d1c.png
https://i-blog.csdnimg.cn/direct/2a2767a9162e4098985814a55e34df84.png

命令作用说明
mount -o remount,rw /sysroot: 将 /sysroot目录重新挂载为​​可读写​​模式。因为在救援模式下,系统的根文件系统通常以只读方式挂载,此命令允许你修改系统文件。
chroot /sysroot: 将根目录切换至 /sysroot。这使你进入实际系统的根环境,后续命令(如 passwd)会直接针对原系统操作。
passwd root: 为 root 用户设置新密码。执行后,系统会提示你输入并确认新的密码。
touch /.autorelabel: 在根目录下创建 .autorelabel文件。如果系统启用了 ​​SELinux​​,这个文件会在下次启动时触发文件系统的安全上下文重新标记,确保密码修改等操作符合 SELinux 安全策略。
exit: 退出 chroot环境。通常需要执行两次 exit命令:第一次退出 chroot环境,第二次退出救援 shell 并重启系统

https://i-blog.csdnimg.cn/direct/a46c355014bc47ac8e9720c078f26a6e.png