user1187968
user1187968

Reputation: 7986

Vagrant can't assign static ip to box

I have the following inside my Vagrant file.

config.vm.network "private_network", ip: "192.168.33.12"

When I "vagrant up", vagrant is assigning 127.0.0.1. I'm not sure what's causing this. Below is the full trace.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => C:/vm2
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

Upvotes: 2

Views: 5793

Answers (4)

Sanjay Mahara
Sanjay Mahara

Reputation: 1

Here is what i did for the virtual box set machine of vagrant:

Add this to to vagrant file :-

config.vm.network "private_network", ip: "any IP address that you choose"

Then do the Vagrant Reload and you should be good.

In case the VM stucks for Vagrant Up command in retrying the connect or ssh .. do this:-

Run the below command from the cmder console:-

vagrant plugin install vagrant-vbguest

After this enable Vagrant GUI by adding the following to the vagrant file:-

config.vm.provider :virtualbox do |vb| 
  vb.gui = true
end 

Now do the vagrant up

the vagrant gui window will open and Keep Pressing "S" to skip and enter the login screen

Login with the username/password which is root/vagrant by default

and run the following command:-

sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions

Now come out of the vagrant gui window shell and remove the line added earlier for enabling the vagrant gui

from the cmder console do "vagrant up" again

Done!

Upvotes: 0

Sanjay Mahara
Sanjay Mahara

Reputation: 1

Here are the steps that i choose while setting up the static ip of the Ubuntu machine:

ls /etc/netplan/ //go to this directory to find the config file

Note: the YAML file is very strict about the indentations e.g

  dhcp4:true //will give wrong indentation error
  dhcp4: true //right way to give

Now edit the config file:-

 sudo nano /etc/netplan/01-netcfg.yaml 

Change Default settings from :-

network:
  version: 2
  renderer: networkd
  eth0:
    dhcp4: true
    dhcp6: false
    optional: true
    nameservers:
    addresses: [8.8.8.8,8.8.4.4]

To

network:
version: 2
renderer: networkd
ethernets:
   eth0:
   dhcp4: no
   addresses: [172.28.32.11/16] //here choose your desired IP
   gateway4: 172.28.32.1     //provide the gateway address,
   nameservers:
   addresses: [8.8.8.8,8.8.4.4] 

Now do :

sudo netplan apply // to apply the network settings

You can then check that the ip address remains same during any course between vagrant halt/up

Upvotes: 0

Brian Brownton
Brian Brownton

Reputation: 1331

As a shorthand way of doing this, a oneliner you can run from the host to see assigned IPs on the guest:

$ vagrant ssh -c "ifconfig | grep 'inet addr'"

Which will give you an output like this:

      inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
      inet addr:192.168.200.200  Bcast:192.168.200.255  Mask:255.255.255.0
      inet addr:127.0.0.1  Mask:255.0.0.0

Upvotes: 2

Frederic Henri
Frederic Henri

Reputation: 53713

what you see here

default: SSH address: 127.0.0.1:2200

is not your VM IP address - this is just for vagrant to ssh into the box using an available port (in your case 2200). vagrant configures on your VM the eth0 interface to communicate with the host

Login to the VM and run ifconfig you will see you have (at least) 2 interfaces available with eth0 on 10.0.2.15 and eth1 on 192.168.33.12

Upvotes: 4

Related Questions