Vagrant中网络配置
一、基本配置
Vagrant offers multiple options for how you are able to connect your guest machines to the network, but there is a standard usage pattern as well as some points common to all network configurations that are important to know.
1.1 配置项
All networks are configured within your Vagrantfile using the config.vm.network method call. For example, the Vagrantfile below defines some port forwarding:
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 80,host: 8080
end
Every network type has an identifier such as :forwarded_port in the above example. Following this is a set of configuration arguments that can differ for each network type. In the case of forwarded ports, two numeric arguments are expected: the port on the guest followed by the port on the host that the guest port can be accessed by.
二、端口转发
2.1 基本的配置
Vagrant::configure("2") do |config|
config.vm.network "forwarded_port",guest:80,host:8080
end
2.2 端口冲突检测和校验
Vagrant::configure("2") do |config|
config.vm.network "forwarded_port",guest:80,host:8080,auto_correct: true
end
当检测到端口发生冲突,Vagrant会自动在2200到2250直接的端口自动矫正。
2.3 UDP或者TCP协议
Vagrant::configure("2") do |config|
config.vm.network "forwarded_port",guest:80,host:8080,auto_correct: true,protocol: "tcp"
end
三、private 网络配置
Private networks allow you to access your guest machine by some address that is not publicly accessible from the global internet. In general, this means your machine gets an address in the private address space.
3.1 DHCP
Vagrant.configure("2") do |config|
config.vm.network "private_network",type: "dhcp"
end
This will automatically assign an IP address from the reserved address space. The IP address can be determined by using vagrant ssh to SSH into the machine and using the appropriate command line tool to find the IP, such as ifconfig.
3.2 Static IP
You can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4"
end
It is up to the users to make sure that the static IP doesn't collide with any other machines on the same network.
3.2.1 Distable auto-configuration
If you want to manually configure the network interface yourself, you can disable Vagrant's auto-configure feature by specifying auto_config:
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.50.4", auto_config: false
end
三、public 网络配置
Public networks are less private than private networks, and the exact meaning actually varies from provider to provider, hence the ambiguous definition. The idea is that while private networks should never allow the general public access to your machine, public networks can.
3.1 DHCP
The easiest way to use a public network is to allow the IP to be assigned via DHCP. In this case, defining a public network is trivially easy:
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
3.2 Static IP
Depending on your setup, you may wish to manually set the IP of your bridged interface. To do so, add a :ip clause to the network definition.
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.0.17"
end
3.3 Defalut network interface
If more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a :bridge clause to the network definition.
Vagrant.configure("2") do |config|
config.vm.network "public_network", bridge: 'en1:Wi-Fi (AirPort)'
end
The string identifying the desired interface must exactly match the name of an available interface. If it can't be found, Vagrant will ask you to pick from a list of available network interfaces.