Vagrant 命令失败,但如果手动运行则不会失败

Vagrant 命令失败,但如果手动运行则不会失败

我构建了我的第一个 Vagrant 盒子。

当我vagrant up打开盒子时,我收到此错误:

tange/centos3.3 Bringing machine 'default' up with 'virtualbox' provider...
tange/centos3.3 ==> default: Importing base box 'tange/centos3'...
tange/centos3.3 ==> default: Matching MAC address for NAT networking...
tange/centos3.3 ==> default: Setting the name of the VM: centos3_default_1598053704177_14223
tange/centos3.3 ==> default: Fixed port collision for 22 => 2222. Now on port 2203.
tange/centos3.3 ==> default: Clearing any previously set network interfaces...
tange/centos3.3 ==> default: Preparing network interfaces based on configuration...
tange/centos3.3     default: Adapter 1: nat
tange/centos3.3     default: Adapter 2: hostonly
tange/centos3.3 ==> default: Forwarding ports...
tange/centos3.3     default: 22 (guest) => 2203 (host) (adapter 1)
tange/centos3.3 ==> default: Running 'pre-boot' VM customizations...
tange/centos3.3 ==> default: Booting VM...
tange/centos3.3 ==> default: Waiting for machine to boot. This may take a few minutes...
tange/centos3.3     default: SSH address: 127.0.0.1:2203
tange/centos3.3     default: SSH username: vagrant
tange/centos3.3     default: SSH auth method: private key
tange/centos3.3     default: 
tange/centos3.3     default: Vagrant insecure key detected. Vagrant will automatically replace
tange/centos3.3     default: this with a newly generated keypair for better security.
tange/centos3.3     default: 
tange/centos3.3     default: Inserting generated public key within guest...
tange/centos3.3     default: Removing insecure key from the guest if it's present...
tange/centos3.3     default: Key inserted! Disconnecting and reconnecting using new SSH key...
tange/centos3.3 ==> default: Machine booted and ready!
tange/centos3.3 ==> default: Checking for guest additions in VM...
tange/centos3.3     default: The guest additions on this VM do not match the installed version of
tange/centos3.3     default: VirtualBox! In most cases this is fine, but in rare cases it can
tange/centos3.3     default: prevent things such as shared folders from working properly. If you see
tange/centos3.3     default: shared folder errors, please make sure the guest additions within the
tange/centos3.3     default: virtual machine match the version of VirtualBox you have installed on
tange/centos3.3     default: your host and reload your VM.
tange/centos3.3     default: 
tange/centos3.3     default: Guest Additions Version: 4.3.8
tange/centos3.3     default: VirtualBox Version: 6.1
tange/centos3.3 ==> default: Configuring and enabling network interfaces...
tange/centos3.3 The following SSH command responded with a non-zero exit status.
tange/centos3.3 Vagrant assumes that this means the command failed!
tange/centos3.3
tange/centos3.3 /sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'
tange/centos3.3
tange/centos3.3 Stdout from the command:
tange/centos3.3
tange/centos3.3
tange/centos3.3
tange/centos3.3 Stderr from the command:
tange/centos3.3
tange/centos3.3 bash: line 1: ip: command not found
tange/centos3.3 bash: line 1: ifconfig: command not found

似乎系统既找不到ip也找不到ifconfig,但是当我登录时vagrant命令工作得很好。如果我运行:

/sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'

我得到了网络接口列表。

那么为什么当我手动运行它时它可以工作,而当 Vagrant 尝试运行它时却不起作用呢?

我该如何更换盒子才能避免这种情况发生?

流浪文件:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "tange/centos3"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  config.vm.network "private_network", ip: "172.27.27.3"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
    vb.memory = "512"
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell" do |s|
    ssh_pub_key = File.readlines("../../authorized_keys").first.strip
    s.inline = <<-SHELL
      mkdir /root/.ssh
      echo #{ssh_pub_key} >> /home/vagrant/.ssh/authorized_keys
      echo #{ssh_pub_key} >> /root/.ssh/authorized_keys
      sudo apt install rsync || sudo yum install -y rsync || sudo pkg install -y rsync
    SHELL
  end
end
$ vagrant --version
Vagrant 2.2.9

答案1

Hashicorp 存在一个未解决的老问题,这表明 CentOS 无论更新多少存储库,都不会更新具有旧内核的 Base Box。问题是该插件需要来自相同 Box 内核版本的软件包,但 CentOS 已从存储库中删除了该软件包。

你需要在 Vagrantfile 中添加 vbguest 更新内核的指令,或者使用旧版本的 vagrant-vbguest 插件:

config.vbguest.installer_options = { allow_kernel_upgrade: true }

相关内容