You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vagrant执行vagrant halt/up失败,SSH命令返回非零退出状态求助

解决Vagrant halt/up SSH命令执行失败问题

问题复现

执行vagrant halt或vagrant up时触发错误,Vagrant执行shutdown -h now命令返回非零状态,无输出内容,日志示例:

PS D:\ubuntu20.04> vagrant halt
==> default: Attempting graceful shutdown of VM...
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

shutdown -h now

Stdout from the command:

Stderr from the command:

PS D:\ubuntu20.04>

解决步骤

1. 强制关闭虚拟机

先跳过优雅关机流程,直接强制关闭:

vagrant halt -f

完成后尝试执行vagrant up,若问题依旧,继续下一步。

2. 验证SSH连接有效性

  • 检查Vagrantfile中的SSH配置项(如config.ssh.username、config.ssh.private_key_path)是否正确,尤其是自定义过虚拟机SSH设置的场景。
  • 手动通过SSH客户端登录虚拟机,测试连通性:
    ssh vagrant@<虚拟机IP> -i <Vagrant默认私钥路径>
    
    默认私钥路径在Windows下通常是~/.vagrant.d/insecure_private_key,Linux/macOS为$HOME/.vagrant.d/insecure_private_key。若手动连接失败,先修复SSH登录问题。

3. 重置虚拟机SSH授权密钥

若SSH连接异常,可通过虚拟化软件控制台直接登录虚拟机,执行以下命令重置Vagrant默认密钥:

sudo rm -rf /home/vagrant/.ssh
sudo mkdir -p /home/vagrant/.ssh
sudo curl -o /home/vagrant/.ssh/authorized_keys https://raw.githubusercontent.com/hashicorp/vagrant/main/keys/vagrant.pub
sudo chown -R vagrant:vagrant /home/vagrant/.ssh
sudo chmod 700 /home/vagrant/.ssh
sudo chmod 600 /home/vagrant/.ssh/authorized_keys

重启虚拟机后再尝试Vagrant命令。

4. 检查虚拟化软件状态

  • 确认VirtualBox/VMware等虚拟化服务正常运行,未被防火墙或安全软件拦截。
  • 查看虚拟机在虚拟化软件中的状态,若处于卡死或异常状态,手动关闭后再操作。

5. 重建虚拟机(终极方案)

备份好虚拟机内重要数据后,执行以下命令重新构建环境:

vagrant destroy
vagrant up

内容的提问来源于stack exchange,提问作者user2450515

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:21:16