How to Assign a Public Static IP to a Vagrant Box Using VirtualBox Bridged Networking


3 views

When you need to expose a Vagrant VM directly to your network using a dedicated public IP, port forwarding and host-only networking won't cut it. Here's how to properly bridge your VM to your physical network interface.

Host Machine:
- Primary IP: 188.120.245.4
- Secondary IP: 188.120.244.5 (available)
- VirtualBox 4.1.18
- Vagrant 1.0.3

Bridged networking connects your VM directly to the physical network, making it appear as another host with its own IP address. This differs from:

  • NAT: Which requires port forwarding
  • Host-only: Which creates a private network

Here's the proper way to configure bridged networking in your Vagrantfile:

Vagrant::Config.run do |config|
  config.vm.define :gitlab do |box_config|
    box_config.vm.box = "ubuntu"
    box_config.vm.host_name = "ubuntu"
    # Use bridged mode and specify the correct interface
    box_config.vm.network :bridged, bridge: "eth1"
  end
end

After running vagrant up, you'll need to:

  1. SSH into the VM (vagrant ssh)
  2. Manually configure the static IP on the Ubuntu guest:
# On Ubuntu guest
sudo nano /etc/network/interfaces

# Add these lines for eth1:
auto eth1
iface eth1 inet static
    address 188.120.244.5
    netmask 255.255.248.0
    gateway 188.120.245.1

If bridging fails:

  • Verify VirtualBox bridged adapter settings (VirtualBox Manager → File → Preferences → Network)
  • Check for IP conflicts on your network
  • Ensure your host machine allows promiscuous mode on the interface

When exposing a VM directly:

  • Configure proper firewall rules on both host and guest
  • Consider using VLANs if available
  • Monitor network traffic for unusual activity

With this setup, your Vagrant box will be accessible via the public IP 188.120.244.5 as if it were a physical machine on your network.


When working with Vagrant and VirtualBox, assigning a public static IP requires careful network configuration. Your host machine (Debian Squeeze) has two public IPs (188.120.245.4 and 188.120.244.5), and you want the Vagrant box (Ubuntu) to use 188.120.244.5 directly.

Your attempts with forward_port and hostonly networking won't work for this scenario because:

  • Port forwarding only exposes specific ports, not the entire IP
  • Host-only networking creates a private network that isn't routable from outside

The correct approach is to use bridged networking, which makes the VM appear as another host on your physical network:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  config.vm.network "public_network",
    ip: "188.120.244.5",
    netmask: "255.255.248.0",
    bridge: "eth1" # Your physical interface name
end

Here's how to properly configure your environment:

  1. Stop any running Vagrant boxes
  2. Edit your Vagrantfile with the configuration above
  3. Run vagrant up and select the correct network interface when prompted

After bringing up the VM, check the network configuration:

vagrant ssh -c "ip addr show"

You should see the public IP assigned to one of the interfaces. Also verify from another machine:

ping 188.120.244.5

If you encounter problems:

  • Ensure the IP isn't already in use
  • Verify your network permits multiple MAC addresses
  • Check VirtualBox bridged adapter settings

For more complex setups, you might need to manually configure routing. Here's an example of adding a static route:

sudo ip route add 188.120.244.5/32 dev eth1