Identifier of the public network in the vagrantfile configuration file: public_ Network, for example:
config.vm.network "public_network"
1
The exact meaning of vagrant public network varies from provider to provider, so it is an ambiguous definition. Although private networks never allow public access to your machine, public networks can p>
Confused?So do we. Public networks are likely to be replaced by bridging networks in future versions, because this is usually what public networks should do, and providers that do not support bridging usually do not have any other functions that map to public networks p>
Warning: by default, the vagrant box is not secure. Design, public password, insecure key pair for SSH access, and root access may be allowed through SSH all cause problems. With these known credentials, anyone in the network can easily access your box. Before configuring vagrant to use the public network, consider all potential security risks and check the default configuration to identify potential security risks p>
1. DHCP
The simplest way to use public network is to allocate IP through DHCP
Vagrant.configure("2") do |config|
config.vm.network "public_network"
end
1
2
3
When using DHCP, you can log in to the machine through vagrant SSH
and use the appropriate command-line tools (ifconfig, etc.) to view the assigned IP address p>
Use the default route assigned by DHCP
In some cases, the default route that needs to be allocated by DHCP remains unchanged. In these cases, you can specify use_ dhcp_ assigned_ default_ Route
option. For example:
Vagrant.configure("2") do |config|
config.vm.network "public_network",
use_dhcp_assigned_default_route: true
end
1
2
3
4
2. Static IP
Depending on your settings, you may want to manually set the IP of the bridge interface. Add the IP: "XX. XX. XX. XX"
clause to the network definition:
config.vm.network "public_network", ip: "192.168.0.17"
1
3. Default network interface
If more than one network interface is available on the host, vagrant will prompt you to select the interface that the virtual machine will bridge to. You can use the bridge
option in the network definition section to specify the default interface p>
config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"
1
The string identifying the required interface must exactly match the name of the available interface. If it cannot be found, vagrant will again prompt you to select from the list of available network interfaces p>
For some providers, you can specify a list of adapters to bridge, and vagrant will try in order until it finds the existing network adapter that is successfully bridged
config.vm.network "public_network", bridge: [
"en1: Wi-Fi (AirPort)",
"en6: Broadcom NetXtreme Gigabit Ethernet Controller",
]
1
2
3
4
In this example, the first network adapter that exists and can be successfully bridged is used p>
4. Turn off auto configuration
If you need to manually configure the network interface, you can use auto_ The config
option turns off the auto configuration feature of vagrant
Vagrant.configure("2") do |config|
config.vm.network "public_network", auto_config: false
end
1
2
3
Then the shell configurator can be used to configure the IP address of the interface
Vagrant.configure("2") do |config|
config.vm.network "public_network", auto_config: false
# manual ip
config.vm.provision "shell",
run: "always",
inline: "ifconfig eth1 192.168.0.17 netmask 255.255.255.0 up"
# manual ipv6
config.vm.provision "shell",
run: "always",
inline: "ifconfig eth1 inet6 add fc00::17/7"
end
1
2
3
4
5
6
7
8
9
10
11
12
13
5. Default router
Depending on the settings, you may want to manually override the default router configuration. This is required if you need to access the vagrant box from another network over a public network. To do this, you can use the shell configurator script:
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.0.17"
# default router
config.vm.provision "shell",
run: "always",
inline: "route add default gw 192.168.0.1"
# default router ipv6
config.vm.provision "shell",
run: "always",
inline: "route -A inet6 add default gw fc00::1 eth1"
# delete default gw on eth0
config.vm.provision "shell",
run: "always",
inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Note that the above is quite complex and may be specific to the operating system, but we document a rough idea of how to do it, because it’s a common problem p>