#StackBounty: #vagrant Vagrant command fails, but not if run by hand

Bounty: 50

I built my first Vagrant box.

When I vagrant up the box I get this error:

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:
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 /sbin/ip -o -0 addr | grep -v LOOPBACK | awk '{print $2}' | sed 's/://'
tange/centos3.3 Stdout from the command:
tange/centos3.3 Stderr from the command:
tange/centos3.3 bash: line 1: ip: command not found
tange/centos3.3 bash: line 1: ifconfig: command not found

It seems the system cannot find neither ip nor ifconfig, but when I login as vagrant the commands work just fine. And if I run:

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

I get a list of network interfaces.

So why does it work when I run it by hand, but not when Vagrant tries to?

How can I change the box so this does not happen?


# -*- 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 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: ""

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

  # 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"
  # 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

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.