vagrant导出镜像无法ssh登录的问题

为了避免复杂的项目环境搭建配置,我们经常会需要导出配置好的一个镜像供其它人使用。在导出的镜像,再导入使用过程中,发现启动后无法使用vagrant ssh登录进去。

为了定位问题,我们在启动的时候vagrant up --debug > debug.log 2>&1

debug看一下启动日志

INFO ssh: Attempting to connect to SSH...

INFO ssh: - Host: 127.0.0.1

INFO ssh: - Port: 2222

INFO ssh: - Username: vagrant

INFO ssh: - Password? false

INFO ssh: - Key Path: ["E:/vagrant_home/insecure_private_key"]

DEBUG ssh: == Net-SSH connection debug-level log END ==

INFO ssh: SSH not ready: #<Vagrant::Errors::SSHAuthenticationFailed: SSH authentication failed! This is typically caused by the public/private

keypair for the SSH user not being properly set on the guest VM. Please

verify that the guest VM is setup with the proper public key, and that

the private key path for Vagrant is setup properly as well.>

从启动日志看,ssh的密钥对不匹配,导致无法登录。再分析一下原因,这里的key path 显示是使用了E:/vagrant_home/insecure_private_key这个是vagrant的标准公钥,而我们的镜像虚拟机是从标准的ubuntun镜像导入的,这里先说明一下vagrant镜像在导入启动时候的标准流程

使用 E:\vagrant_home\insecure_private_key(标准私钥)连接虚拟机

如果连接成功,生成新的密钥对

将新公钥写入虚拟机的 authorized_keys

保存新私钥到项目的 private_key 文件

那么问题就出在这里了,我们的从标准的ubuntu镜像导入的虚拟机,在启动成功以后会被重新写入一个新的公钥,我们再导出的镜像,用vagrant标准的私钥去登录的时候,就无法匹配了。

那么如何解决这个问题呢。有2个方法

一 我们可以在vagrantfile配置中指定不重写秘钥,而是用指定秘钥

config.ssh.insert_key = fasle

config.ssh.private_key_path = [your private key file]

这里insert_key参数就是指定是否要重写秘钥,默认是true,这也是安全的方式

二 就是我们在导出镜像的时候把镜像中的公钥改成vagrant标准的公钥

获取公钥

curl -fsSL https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub > /home/vagrant/.ssh/authorized_keys

获取私钥

curl -fsSL https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant -o insecure_private_key

这样我们重新再导出镜像就可以用vagrant标准私钥登录了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值