有范 發表於 2025-7-7 00:00:00

Centos7如何重置root用户密码的常用方法汇总

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>Centos7重置root用户密码<ul class="second_class_ul"><li>第一种方法</li><li>第二种方法</li><li>第三种方法</li></ul></li><li>Centos7通过SSH使用密钥实现免密登录的方法<ul class="second_class_ul"><li>一、serverA 免密登录 serverB 原理</li><li>二、Centos7 默认安装了 ssh服务</li><li>三、serverA 生成秘钥</li><li>四、解决找不到 .ssh 目录</li><li>五、移动 id_rsa.pub 文件</li><li>六、验证免密登录</li></ul></li><li>总结<ul class="second_class_ul"></ul></li></ul></div><p class="maodian"></p><h2>Centos7重置root用户密码</h2>
<p class="maodian"></p><h3>第一种方法</h3>
<p>1)开启虚拟机</p>
<p>2)在弹出这个界面时,按上下键防止页面跳转,选择&quot;CentOS Linux (3.10.0-1160. e17. x86 64) 7 (Core)&quot;并按e</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15122333.jpg" width="1097" /></p>
<p>3)找到ro crashkernel=auto xxx,把ro换成rw init=/sysroot/bin/sh</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15122F6.jpg" width="1062" /></p>
<p>4)使用组合键Ctrl+X进入单用户模式</p>
<p>5)chroot /sysroot访问系统</p>
<p>6)passwd root重新输入新的root用户密码</p>
<p>7)touch /.autorelabel创建标签文件</p>
<p>8)exit返回刚刚的单用户模式</p>
<p>9)reboot重启系统</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15122116.jpg" width="999" /></p>
<p>再次重启,输入刚才重新更改的用户密码就能登录系统了!</p>
<p class="maodian"></p><h3>第二种方法</h3>
<p>第二种方法与第一种方法一样,也是要进入系统内核调整</p>
<p>1)开启虚拟机</p>
<p>2)在弹出这个界面时,按上下键防止页面跳转,选择&quot;CentOS Linux (3.10.0-1160. e17. x86 64) 7 (Core)&quot;并按e</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15122150.jpg" width="1097" /></p>
<p>3)在linux16这一行后面加上rd.break</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15123328.jpg" width="1200" /></p>
<p>4)使用Ctrl+X组合键进入当用户模式</p>
<p>5)mount -o remount,rw /sysroot</p>
<p>6)chroot /sysroot</p>
<p>7)passwd root</p>
<p>8)touch /.autorelabel</p>
<p>9)exit</p>
<p>10)reboot</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15123Y1.jpg" width="1106" /></p>
<p>重启之后输入刚才更改的密码就可以重新登录系统!!!</p>
<p class="maodian"></p><h3>第三种方法</h3>
<p>1)选择打开电源时进入固件</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15123b1.jpg" width="1200" /></p>
<p>2)从光盘启动,进入Boot启动项,用+ - 键的方式将CD-ROM Drive选项调到第一启动项位置</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15124Y4.jpg" width="956" /></p>
<p>3)按F10,选择yes,然后回车,用来保存刚才所做的更改</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15124237.jpg" width="962" /></p>
<p>4)选择第三个,进入拯救模式</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15124447.jpg" width="960" /></p>
<p>5)选择第二个拯救这个CentOS系统</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15124P2.jpg" width="905" /></p>
<p>6)打一个1(继续)然后回车</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15125418.jpg" width="987" /></p>
<p>7)访问系统:chroot /mnt/sysimage</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15125Z0.jpg" width="755" /></p>
<p>8)编辑影子文件vim /etc/shadow</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG15125R5.jpg" width="755" /></p>
<p>9)把这一段密码加密信息删掉</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250707/2-250FG1512DJ.jpg" width="1200" /></p>
<p>删除的时候要按下i键,编辑删除,然后:wq!保存并退出</p>
<p>10)exit</p>
<p>11)reboot重启系统</p>
<p>重新登录系统时,要先把原先的启动项调回去,变成从硬盘启动</p>
<p>重启后使用root用户登录,就会发现不使用root密码也可以直接进入系统</p>
<p>进入系统后,使用sudo passwd root命令,为当前用户提权给root用户设置一个密码!</p>
<p class="maodian"></p><h2>Centos7通过SSH使用密钥实现免密登录的方法</h2>
<p class="maodian"></p><h3>一、serverA 免密登录 serverB 原理</h3>
<ul><li>首先在 serverA 上生成一对秘钥(ssh-keygen)</li><li>将公钥拷贝到 serverB,重命名 authorized_keys</li><li>serverA 向 serverB 发送一个连接请求,信息包括用户名、ip</li><li>serverB 接到请求,会从 authorized_keys 中查找,是否有相同的用户名、ip,如果有 serverB 会随机生成一个字符串</li><li>然后使用使用公钥进行加密,再发送个 serverA</li><li>serverA 接到 serverB 发来的信息后,会使用私钥进行解密,然后将解密后的字符串发送给 serverB</li><li>serverB 接到 serverA 发来的信息后,会给先前生成的字符串进行比对,如果一直,则允许免密登录</li></ul>
<p class="maodian"></p><h3>二、Centos7 默认安装了 ssh服务</h3>
<p>启动 ssh 服务</p>
<div class="dxycode"><pre class="brush:bash;"># 查看 ssh 状态
systemctl status sshd
# 启动 ssh
systemctl start sshd
# 停止 ssh
systemctl stop sshd</pre></div>
<p class="maodian"></p><h3>三、serverA 生成秘钥</h3>
<p>遇到提示直接敲回车即可</p>
<p>CentOS7 默认使用RSA加密算法生成密钥对,保存在~/.ssh目录下的id_rsa(私钥)和id_rsa.pub(公钥)。</p>
<p>也可以使用&ldquo;-t DSA&rdquo;参数指定为DSA算法,对应文件为id_dsa和id_dsa.pub,密钥对生成过程会提示输入私钥加密密码,可以直接回车不使用密码保护。</p>
<div class="dxycode"><pre class="brush:bash;"># ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:/gGrlDJN5euMS5aai5feBkEI/0WjEnzPzx1xGtdkKG4 root@localhost.localdomain
The key's randomart image is:
+-------+
|.o..o      +o|
| .o.oo .+ +..|
|oo.o. .. B   |
|   o..ooE      |
|    ...oSo .   |
|    .o +o+.      |
|    ooB + .      |
|   .oX.= . .   |
|.o=o=.o .      |
+---------+
# ls ~/.ssh/
id_rsaid_rsa.pubknown_hosts
# </pre></div>
<p class="maodian"></p><h3>四、解决找不到 .ssh 目录</h3>
<p>如果在上面生成秘钥后,执行 cd ~/.ssh 找不到 .ssh 目录,是因为没有使用 ssh 登录过,使用 ssh 登录一下即可生成 .ssh 目录,之后再重新执行 ssh-keygen 生成秘钥即可</p>
<div class="dxycode"><pre class="brush:bash;"># ssh localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:DYd7538oOsqpIIDTs01C3G4S6PRE7msA91yUgk9Dzxk.
ECDSA key fingerprint is MD5:88:80:21:03:b2:52:6b:06:ff:c7:3b:d5:2d:47:c9:ad.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
root@localhost's password:
Last login: Fri Oct 26 16:15:57 2018 from localhost
# ls ~/.ssh/
known_hosts
# exit
登出
Connection to localhost closed.
# </pre></div>
<p class="maodian"></p><h3>五、移动 id_rsa.pub 文件</h3>
<div class="dxycode"><pre class="brush:bash;"># 将 serverA ~/.ssh目录中的 id_rsa.pub 这个文件拷贝到你要登录的 serverB 的~/.ssh目录中
scp ~/.ssh/id_rsa.pub 192.168.0.101:~/.ssh/
# 然后在 serverB 运行以下命令来将公钥导入到~/.ssh/authorized_keys这个文件中
cat ~/.ssh/id_rsa.pub &gt;&gt; ~/.ssh/authorized_keys
# 另外要注意请务必要将服务器上
~/.ssh权限设置为700
~/.ssh/authorized_keys的权限设置为600
# 这是linux的安全要求,如果权限不对,自动登录将不会生效</pre></div>
<p class="maodian"></p><h3>六、验证免密登录</h3>
<div class="dxycode"><pre class="brush:bash;">ssh 192.168.101</pre></div>
<p class="maodian"></p><h2>总结</h2>
<p>以上为个人经验,希望能给大家一个参考,也希望大家多多支持琼殿技术社区。</p>
頁: [1]
查看完整版本: Centos7如何重置root用户密码的常用方法汇总