Configuring Network Settings
1. Setting Up Forwarded Ports
| Port Configuration | Values | Description |
|---|---|---|
| config.vm.network "forwarded_port" | guest: 80, host: 8080 |
Maps guest port to host port for external access |
| config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1" | host_ip: "127.0.0.1" |
Binds forwarded port to specific host IP |
| config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh" | id: "ssh" |
Assigns an ID to a forwarded port for easy reference |
Example: Forwarding SSH traffic
config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh"
2. Using Private Networks
| Option | Values | Description |
|---|---|---|
| private_network.ip | 192.168.56.10 |
Static IP address for the private network interface |
| private_network.type | dhcp or static |
Network type: DHCP for automatic IP assignment, static for fixed IPs |
| private_network.auto_config | true or false |
Whether to automatically configure network settings |
Example: Static Private Network Configuration
vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.56.10"
end
Example: DHCP Private Network Configuration
vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
3. Setting Up Public Networks
| Option | Values | Description |
|---|---|---|
| config.vm.network | "public_network" |
Defines a public network interface for the VM |
| ip | "192.168.10.100" |
Static IP address for the public network interface |
| gateway | "192.168.10.1" |
Gateway IP for the public network |
| netmask | "255.255.255.0" |
Network mask for the public network |
Example: Basic Public Network Configuration
# Vagrantfile
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.10.100"
end
Example: Public Network with Gateway and Netmask
# Vagrantfile
Vagrant.configure("2") do |config|
config.vm.network "public_network", ip: "192.168.10.100",
gateway: "192.168.10.1", netmask: "255.255.255.0"
end
Note: Public networks in Vagrant use the host's network interface to connect directly to the external network. Ensure that the specified IP is not in use on your local network.
4. Configuring Static IP
| Option | Values | Description |
|---|---|---|
| config.vm.network | "private_network", ip: "192.168.56.10" |
Assigns a static IP to the VM |
Example: Setting a Static IP
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.56.10"
end
Note: The IP address must be within the range of the private network Vagrant manages by default.
5. Using DHCP Assignment
| Option | Values | Description |
|---|---|---|
| config.vm.network | "dhcp" |
Assigns a dynamic IP address using DHCP |
Example: Basic DHCP Configuration
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
Note: DHCP-assigned IPs are not persistent across Vagrant box restarts.
Warning: Ensure your host machine allows DHCP traffic for private networks.
6. Setting Network Bridge
| Option | Values | Description |
|---|---|---|
| config.vm.network | "private_network", type: "dhcp" |
Configures a private network with DHCP |
| config.vm.network | "private_network", ip: "192.168.56.10" |
Assigns a static IP to the private network |
| config.vm.network | "private_network", type: "bridge" |
Enables bridged network mode |
Note: Bridged networking allows the VM to appear as a separate device on the network, directly connected to the host's physical network interface.
7. Configuring Auto-Config
| Option | Values | Description |
|---|---|---|
| config.vm.network | "private_network", type: "dhcp" |
Enables DHCP for private network interface |
| config.vm.network | "private_network", ip: "192.168.56.10" |
Assigns static IP to private network interface |
| config.vm.network | "private_network", type: "dhcp", auto_config: true |
Enables automatic configuration of network settings via DHCP |
| config.vm.network | "private_network", type: "dhcp", auto_config: false |
Disables automatic configuration of network settings via DHCP |
Example: Enabling Auto-Config with DHCP
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp", auto_config: true
end
Example: Disabling Auto-Config
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp", auto_config: false
end
Note: When
auto_config is set to false, Vagrant will not attempt to configure network settings automatically, and manual configuration may be required.
8. Setting MAC Address
| Option | Values | Description |
|---|---|---|
| config.vm.network | "private_network", mac: "080027123456" |
Assigns a specific MAC address to the private network interface |
Example: Setting a Custom MAC Address
vagrantfile_content = <<~EOF
VAGRANTFILE_CONTENT
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.network "private_network", mac: "080027123456"
end
EOF
9. Using Multiple Network Interfaces
| Interface Type | Configuration | Description |
|---|---|---|
| private_network | { "type": "dhcp" } |
Assigns a private IP via DHCP |
| private_network | { "type": "static", "ip": "192.168.56.10" } |
Assigns a static IP address |
| private_network | { "type": "static", "ip": "192.168.56.11", "netmask": "255.255.255.0" } |
Assigns a static IP with custom netmask |
| public_network | { "type": "dhcp" } |
Assigns a public IP via DHCP |
| public_network | { "type": "static", "ip": "10.0.2.15" } |
Assigns a static IP on public network |
Example: Configure Multiple Network Interfaces
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
config.vm.network "private_network", ip: "192.168.56.10"
config.vm.network "public_network", ip: "10.0.2.15"
end
Note: Multiple network interfaces can be configured to provide different levels of access and communication between the host machine and guest VM.
10. Configuring Network Protocols
| Protocol | Configuration | Description |
|---|---|---|
| dhcp | "dhcp" |
Enables DHCP for automatic IP assignment |
| static | "static" |
Assigns a fixed IP address to the guest |
| private_network | "private_network" |
Creates a private network interface for the guest |
| public_network | "public_network" |
Connects the guest to a public network interface |
Example: Static IP Configuration
Vagrant.configure("2") do |config|
config.vm.network "private_network", ip: "192.168.56.10"
end
Example: DHCP Configuration
Vagrant.configure("2") do |config|
config.vm.network "private_network", type: "dhcp"
end
11. Setting Port Collision Detection
| Option | Values | Description |
|---|---|---|
| config.vm.network | "forwarded_port" |
Defines a forwarded port configuration |
| host | int |
Host port to forward from (e.g., 8080) |
| guest | int |
Guest port to forward to (e.g., 80) |
| auto_correct | true | false |
If true, Vagrant will attempt to find an available host port if the specified one is in use |
Example: Forwarding a Port with Collision Detection
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", host: 8080, guest: 80, auto_correct: true
end
Note: When
auto_correct is enabled, Vagrant will log which port was actually assigned.