# Vagrant networking to enable Internet accessible machine setup

May 22, 2014 ~ 2 min read

Vagrant networking to enable Internet accessible machine setup
==============================================================

*   [drupal](https://lirantal.com/tag/drupal)

share this story on

[](https://twitter.com/share?url=https://lirantal.com/blog/2014-05-22/&text=%22Vagrant%20networking%20to%20enable%20Internet%20accessible%20machine%20setup%22&via=liran_tal)[](https://www.facebook.com/sharer.php?u=https://lirantal.com/blog/2014-05-22/)[](https://www.linkedin.com/shareArticle?url=https://lirantal.com/blog/2014-05-22/&title=Vagrant%20networking%20to%20enable%20Internet%20accessible%20machine%20setup)[](https://wa.me/?text=Vagrant%20networking%20to%20enable%20Internet%20accessible%20machine%20setup%20https://lirantal.com/blog/2014-05-22/)

![Example of how to setup vagrant networking to enable Internet accessible machine setup with a Vagrantfile](https://cdn.hashnode.com/res/hashnode/image/upload/v1692640463125/5167d852-3b3e-409f-b5fb-62bcb592bf07.jpeg)

If you’re using [vagrant](http://www.vagrantup.com/), like most [devopsers](http://en.wikipedia.org/wiki/DevOps) out there, you might have also been on the road to run it on a local development machine and make it accessible through the Internet with some NAT rules on your modem or firewall. If you experienced this, and been struggling with getting vagrant networking to function right then we will look into a working setup for this purpose.

The case where networking issues could occur, is most probably if you have configured more than one interface for the virtual machine, like I did. I’ve got one interface using NAT and the other interface is using bridged networking as you can see in the Virtualbox UI and the vagrantfile snippet:

[![vbox-bridged](https://cdn.hashnode.com/res/hashnode/image/upload/v1692640467104/63fa68a2-4e93-477f-ad98-85f0f971f7fb.png)](http://enginx.com/wp-content/uploads/2014/05/vbox-bridged.png)

[![vbox-nat](https://cdn.hashnode.com/res/hashnode/image/upload/v1692640470585/9eb344ba-113c-466e-a792-2a28561f3867.png)](http://enginx.com/wp-content/uploads/2014/05/vbox-nat.png)

Vagrantfile configuration snippet for bridged networking:

*   Assign this VM to a bridged network, allowing you to connect directly to a
*   network using the host’s network device. This makes the VM appear as another
*   physical device on your network.

    config.vm.network :bridged, :bridge => 'p2p1'

Setting up vagrant networking and routing properly
--------------------------------------------------

Because bridged networking provides an easy mechanism for virtual machines to exist side by side with your native OS and as part of your networked appliances seamlessly, this is by far the most convenient networking path to choose. Yet, vagrant doesn’t allow for bridged networking to use a static IP setup in it’s configuration but rather this is something that you’d have to take care of on your own.

I find the easiest solution for this is to assign another static IP address, which I know it isn’t part of my home DHCP server address range, and apply it on the bridged network interface. This can be easily done as follows:

    # get static ip on bridged iface
    ifconfig eth1:1 10.0.0.100 up

Then, to handle routing correctly, it is required to remove the default route to 10.0.2.2 which is the address space used by [Virtualbox](https://www.virtualbox.org/) for NAT interfaces, and instead add a default gateway entry for the bridged interface:

    #get routes setup correctly
    route add default gw 10.0.0.138 eth1
    route del default gw 10.0.2.2

Finally, an example for proper routing, or to examine your routing in general you can consult the following routing snippet:

    root@precise32:~# route -n
    
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    0.0.0.0 10.0.0.138 0.0.0.0 UG 100 0 0 eth1
    10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
    10.0.0.0 0.0.0.0 255.0.0.0 U 0 0 0 eth1
    10.0.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
