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

此时,完全恢复

相关推荐
jianqiang.xue2 小时前
ESP32-S3 运行 Linux 全指南:从 RISC-V 模拟器移植到 8 秒快速启动
linux·stm32·单片机·mongodb·risc-v·esp32s3
bing_feilong2 小时前
Ubuntu Tips
linux·运维
~光~~2 小时前
【嵌入式linux学习】0_3位运算整理
linux·学习
悲伤小伞2 小时前
9-MySQL_索引
linux·数据库·c++·mysql·centos
SeanDe2 小时前
【Linux `top` 命令详解(结合截图逐行拆解)】
linux·运维·服务器
mi20062 小时前
wiki.js知识库系统搭建和配置总结
linux·运维·知识库
向依阳2 小时前
Linux应用-----进程间通信
linux
桌面运维家2 小时前
Windows/Linux文件访问权限修改指南
linux·运维·服务器
badhope2 小时前
Docker入门到实战全攻略
linux·python·docker·github·matplotlib