Pierluigi Vernetto
Pierluigi Vernetto

Reputation: 2050

Vagrant: how to configure multiple NICs within a Vagrantfile?

The official doc gives example only for 1 IP:

http://docs.vagrantup.com/v2/networking/private_network.html

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"
end

Googling around I could find only 1 example of Multiple IPs:

https://groups.google.com/forum/#!topic/vagrant-up/hqtdOEjjlsw

Vagrant::Config.run do |config|
  config.vm.define :web001 do |config|
    config.vm.box = "base"
    config.vm.forward_port("http", 5000, 8881)
    config.vm.forward_port("ssh", 22, 2222)
    config.vm.host_name = "web001.example.com"
    config.vm.network("33.33.33.10")
    config.vm.network("33.33.33.20", {:adapter=>2})
  end
end 

but it doesn't work for me...

Any help would be greatly appreciated....

Upvotes: 38

Views: 38587

Answers (3)

slm
slm

Reputation: 16406

Confirming that a Vagrantfile with the following config statements:

$ grep '^[ ]\+config.vm.network' Vagrantfile
  config.vm.network "private_network", ip: "192.168.56.101"
  config.vm.network "public_network", bridge: "en0: Wi-Fi (Wireless)", auto_config: false
  config.vm.network "public_network", bridge: "en0: Wi-Fi (Wireless)", auto_config: false

Results in network interfaces as follows. As you can see there's interfaces internal to the IP that haven't been configured, but are present.

$ ip a l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:c0:42:d5 brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
       valid_lft 86141sec preferred_lft 86141sec
    inet6 fe80::5054:ff:fec0:42d5/64 scope link
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:91:77:71 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.101/24 brd 192.168.56.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe91:7771/64 scope link
       valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:64:d4:68 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:a4:f8:8e brd ff:ff:ff:ff:ff:ff

If you attempt to add this to your Vagrantfile:

config.vm.network "private_network", auto_config: false

You'll get this failure:

==> default: Destroying VM and associated drives...
/opt/vagrant/embedded/lib/ruby/2.4.0/ipaddr.rb:479:in `initialize': address family must be specified (IPAddr::AddressFamilyError)
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/providers/virtualbox/action/network.rb:263:in `new'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/providers/virtualbox/action/network.rb:263:in `hostonly_config'
    from /opt/vagrant/embedded/gems/2.1.5/gems/vagrant-2.1.5/plugins/providers/virtualbox/action/network.rb:91:in `block in call'

It would appear that private_network devices require an IP.

Upvotes: 2

tmatilai
tmatilai

Reputation: 4176

That mailing list post is from long before Vagrant 1.1 and new configuration API, so the syntax is not compatible.

But you can just add as many config.vm.network calls as you want. A silly example:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.network "private_network", ip: "192.168.33.20"
  config.vm.network "public_network", type: "dhcp", bridge: "eth0"
end

Upvotes: 19

tlcowling
tlcowling

Reputation: 1054

Using Vagrant 1.6.1 and private networking with Virtualbox you can create multiple private ips just by repeating the config.vm.network definition:

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "precise64"

  config.vm.network "private_network", ip: "192.168.50.4"
  config.vm.network "private_network", ip: "192.168.50.5"
  config.vm.network "private_network", ip: "192.168.50.6"
  config.vm.network "private_network", ip: "192.168.50.7"

end

Upvotes: 42

Related Questions