centos虚拟机忘记密码怎么办

如果centos虚拟机密码忘记了,不要急,按照以下步骤,百分百解决

开机时,默认选中第一项,不要动,然后马上多敲几次e

敲e后,如下:

敲键盘向下的箭头,让隐藏的内容全显示出来,如下

主要这行:

在UTF-8后添加空格后,添加如下内容

bash 复制代码
rd.break

如下:

然后注意,不要回车,此时按住ctrl+X键,这样就可以重启

等待一会如下

输入:

bash 复制代码
mount -o remount,rw /sysroot
bash 复制代码
chroot /sysroot
bash 复制代码
# 修改root用户密码,这里的root是用户,不是密码
passwd root

输入新密码(输入一次新密码后回车,再输入一次新密码回车),如下图

最后依次执行如下命令

bash 复制代码
touch /.autorelabel
# 退出
exit
# 重启
reboot

重启后使用新密码登录即可。此时已经恢复了

但是命令行是-bash-4.2#开头的,看着不正常,所以需要再做以下工作

检查是否存在 .bash_profile

bash 复制代码
ls -la /root/.bash_profile

如果不存在 → 创建它:

bash 复制代码
cat > /root/.bash_profile << 'EOF'
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
EOF

检查是否存在 .bashrc:

bash 复制代码
ls -la /root/.bashrc

如果不存在 → 创建它:

bash 复制代码
cat > /root/.bashrc << 'EOF'
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's autostart
# export SYSTEMD_IGNORE_PAM=1

# Set default prompt
PS1='[\u@\h \W]\$ '
export PS1
EOF

重新加载配置:

bash 复制代码
source ~/.bash_profile

此时,完全恢复

相关推荐
Sokach10157 小时前
Linux Shell 脚本从零到能用:一个新手的一天学习总结
linux
SelectDB1 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
AlfredZhao1 天前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
zzzzzz3102 天前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220703 天前
如何搭建本地yum源(上)
运维
A小辣椒4 天前
TShark:Wireshark CLI 功能
linux
A小辣椒4 天前
TShark:基础知识
linux
AlfredZhao4 天前
OCI 明明分配了 200G 系统盘,为什么 df 只看到 30G?
linux·oci
AlfredZhao5 天前
vi 删除指定范围的行,不用再反复按 dd
linux·vi