37

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....

3 Answers 3

40

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
2
  • 3
    and how do I specify adapter? I need only for second one, like in original question
    – holms
    May 20, 2015 at 4:47
  • 1
    You can specify the adapter as per the vagrant doc and similarly as in tmatilai's answer below. Heres an example I just tried: config.vm.network "private_network", ip: "192.168.50.4" config.vm.network "public_network", ip: "192.168.50.5" config.vm.network "public_network", bridge: 'en0: Wi-Fi (AirPort)'
    – tlcowling
    May 21, 2015 at 18:31
19

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
3
  • 4
    An IP is required for a private network. in other words dhcp works only for public. Also its better to set up to which interface you want to bind: config.vm.network "public_network", type: "dhcp", :bridge => "eth0"
    – sumid
    Aug 21, 2014 at 13:02
  • Thanks, I improved the example.
    – tmatilai
    Aug 22, 2014 at 7:17
  • @sumid -- The vagrant docs seem to disagree with you. Unless there is some complication to do with combining private_network and bridge you were referring to?
    – JimmidyJoo
    Oct 9, 2015 at 20:41
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.