有一台甲骨文云的vps密钥保存在本地,电脑重装后忘记备份,密钥丢失了,浴室查找各种资料。
看到官方博客的思路Recovering opc user SSH Key on Oracle Cloud Infrastructure,将引导卷挂载到其他vps然后修改配置文件的做法,不过他修改的是opc的密钥,我试了opc登录后,发觉sudo -i 提升权限失败,于是就想着能不能直接更改root密钥,果真成功找回。
1.将失联的实例停止。

2.分离这台服务器的引导卷,停止服务器后,可以在下面分离引导卷,稍后会将这个数据卷挂载到其他我们能够正常使用的服务器进行秘钥修改。

3.切换到其他能正常登录的服务器,将已经分离的引导卷附加过来

4.使用命令行连接到这台正常的服务器,然后依次执行图中红框命令,我的debian10没有安装
iSCSI Initiator,所以先安装才能执行,使用
apt-get install open-iscsi -y
安装。

5.然后执行下面的命令查看磁盘信息
fdisk -l

6.然后根据磁盘信息挂载到对应的目录,我这里的是/dev/sdd1,即执行
mount /dev/sdd1 /mnt
成功挂载后,我们就可以修改信息了
7.使用xshell,菜单栏->工具->新建秘钥向导,下一步下一步生成公钥和私钥,可以将它们全部导出成文件。

8.复制公钥的内容

9.修改root用户的公钥
vim /mnt/root/.ssh/authorized_keys
把之前里面的内容都清空,然后把上一部复制的公钥代码粘贴到里面,然后保存并退出

10.编辑ssh配置文件
vim /mnt/etc/ssh/sshd_config
找到PermitRootLogin, 把前面的#去掉, 更改为以下参数(建议顺便修改下端口),保存并退出
PermitRootLogin yes
11.解除挂载
umount /mnt
12.分离数据卷,并挂载回原来失联的实例下

13.启动原来停止的实例,并使用ssh密钥登录,成功找回

没有失联的实例要更改密钥也可以用这个办法,只不过不用挂载到另外一台机器上。
暂无评论内容