You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Vagrant连接SSH超时问题求助

Vagrant连接SSH超时问题求助

环境背景

我在Windows 10上用Vagrant 2.3.4搭配VirtualBox 7.0.8部署eurolinux-vagrant/centos-stream-9 9.0.37虚拟机,结果执行vagrant up后一直卡在SSH连接超时,折腾了3-4天,试过卸载重装软件、销毁后重建虚拟机都没用,实在没辙了,来请教各位!

错误日志

Net::SSH::ConnectionTimeout>

INFO subprocess: Starting process: ["D:\\Virtual Box\\VBoxManage.exe", "showvminfo", "6ac2bff0-d050-49b1-a801-a426ffa39c9f", "--machinereadable"]

INFO subprocess: Command not in installer, restoring original environment...

DEBUG subprocess: Selecting on IO

DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 32000

DEBUG subprocess: Exit status: 0

DEBUG virtualbox_7_0: Searching for SSH port: 22

DEBUG virtualbox_7_0: read_forward_ports: uuid=6ac2bff0-d050-49b1-a801-a426ffa39c9f active_only=false

INFO subprocess: Starting process: ["D:\\Virtual Box\\VBoxManage.exe", "showvminfo", "6ac2bff0-d050-49b1-a801-a426ffa39c9f", "--machinereadable"]

INFO subprocess: Command not in installer, restoring original environment...

DEBUG subprocess: Selecting on IO

DEBUG subprocess: Waiting for process to exit. Remaining to timeout: 32000

DEBUG subprocess: Exit status: 0

DEBUG virtualbox_7_0:   - [1, "ssh", 2200, 22, "127.0.0.1"]

INFO ssh: Attempting SSH connection...

INFO ssh: Attempting to connect to SSH...

INFO ssh:   - Host: 127.0.0.1

INFO ssh:   - Port: 2200

INFO ssh:   - Username: vagrant

INFO ssh:   - Password? false

INFO ssh:   - Key Path: ["C:/Users/ADMIN/.vagrant.d/insecure_private_key"]

DEBUG ssh:   - connect_opts: {:auth_methods=>["none", "hostbased", "publickey"], :config=>false, :forward_agent=>false, :send_env=>false, :keys_only=>true, :verify_host_key=>:never, :password=>nil, :port=>2200, :timeout=>15, :user_known_hosts_file=>[], :verbose=>:debug, :logger=>#<Logger:0x00000261b8144e68 @level=0, @progname=nil, @default_formatter=#<Logger::Formatter:0x00000261b8144e40 @datetime_format=nil>, @formatter=nil, @logdev=#<Logger::LogDevice:0x00000261b8144df0 @shift_period_suffix=nil, @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<StringIO:0x00000261b8144eb8>, @binmode=false, @mon_data=#<Monitor:0x00000261b8144dc8>, @mon_data_owner_object_id=184780>>, :keys=>["C:/Users/ADMIN/.vagrant.d/insecure_private_key"], :remote_user=>"vagrant"}

# 中间重复的showvminfo日志省略,保留关键结尾部分
DEBUG ssh: == Net-SSH connection debug-level log START ==

DEBUG ssh: D, [2023-12-16T22:25:27.621115 #18544] DEBUG -- net.ssh.transport.session[2d1e0]: establishing connection to 127.0.0.1:2200

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

ERROR warden: Error occurred: Timed out while waiting for the machine to boot. This means that

Vagrant was unable to communicate with the guest machine within

the configured ("config.vm.boot_timeout" value) time period.

If you look above, you should be able to see the error(s) that

Vagrant had when attempting to connect to the machine. These errors

are usually good hints as to what may be wrong.

If you're using a custom box, make sure that networking is properly

working and you're able to connect to the machine. It is a common

problem that networking isn't setup properly in these boxes.

Verify that authentication configurations are also setup properly,

as well.

已尝试的操作

  • 完全卸载并重装Vagrant和VirtualBox
  • 执行vagrant destroy清理后重新vagrant up

我的建议解决步骤

1. 先手动验证SSH连接,定位问题根源

别先依赖Vagrant,直接用Windows自带的SSH客户端(或者Putty)试试连接:

ssh vagrant@127.0.0.1 -p 2200 -i "C:/Users/ADMIN/.vagrant.d/insecure_private_key"
  • 如果还是超时:大概率是虚拟机里的SSH服务没启动,或者VirtualBox的端口转发没生效
  • 如果是认证失败:可能是镜像里的vagrant用户没配置这个默认私钥,或者私钥权限有问题

2. 给虚拟机更长的启动超时时间

CentOS Stream 9首次启动要做的初始化工作不少,默认的超时时间可能不够用。在你的Vagrantfile里加一行:

config.vm.boot_timeout = 180 # 设成180秒应该够了,不够再往上调

然后执行vagrant reload试试。

3. 手动进虚拟机检查核心服务

打开VirtualBox管理器,直接启动这个虚拟机,登录进去(默认vagrant用户密码也是vagrant),然后:

  • 检查SSH服务状态:sudo systemctl status sshd,没启动就手动开启:sudo systemctl start sshd && sudo systemctl enable sshd
  • 检查防火墙:sudo firewall-cmd --permanent --add-port=22/tcp && sudo firewall-cmd --reload,确保22端口对外开放
  • 确认authorized_keys:cat /home/vagrant/.ssh/authorized_keys,看看有没有包含insecure_private_key对应的公钥,没有的话手动把公钥加进去

4. 换个网络模式试试

NAT模式的端口转发偶尔会出问题,试试桥接模式:
在Vagrantfile里改成:

config.vm.network "public_network", bridge: "你的物理网卡名称" # 比如"Wi-Fi"或者"以太网",看你当前用的网络

这样虚拟机能拿到和主机同网段的IP,Vagrant可以直接连这个IP,绕开NAT转发的问题。

5. 换官方镜像试试

说不定是eurolinux-vagrant/centos-stream-9这个镜像本身的网络配置有问题,换官方的CentOS Stream 9镜像试试:

vagrant box add centos/stream9

然后把Vagrantfile里的config.vm.box改成centos/stream9,再执行vagrant up

6. 排查Windows防火墙/杀毒软件

有时候Windows的防火墙或者第三方杀毒软件会把VirtualBox的端口转发流量拦下来,临时关闭试试,要是能连接了,就把VirtualBox和Vagrant加到防火墙的允许列表里。


备注:内容来源于stack exchange,提问作者BachKhoa

火山引擎 最新活动