官方文档仅提供了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
谷歌搜索我只能找到多个IP的一个例子:
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
但它对我不起作用......
任何帮助都将非常感谢....
答案 0 :(得分:34)
使用Vagrant 1.6.1和私有网络与Virtualbox,您只需重复config.vm.network定义即可创建多个私有ips:
# 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
答案 1 :(得分:18)
该邮件列表帖子早于Vagrant 1.1和新配置API,因此语法不兼容。
但您可以根据需要添加尽可能多的config.vm.network
次呼叫。一个愚蠢的例子:
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
答案 2 :(得分:1)
使用以下配置语句确认Vagrantfile
:
$ 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
产生如下网络接口。如您所见,该IP内部的接口尚未配置,但是存在。
$ 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
如果您尝试将其添加到Vagrantfile
:
config.vm.network "private_network", auto_config: false
您将遇到此失败:
==> 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'
似乎private_network
设备需要IP。