Need to do Bridged Adapter only in Vagrant, no NAT

eth0 as NAT is a fundamental requirement of Vagrant in its current state. But you can override the default router configuration for eth1.

From the Vagrant docs:

Default Router

Depending on your setup, you may wish to manually override the default router configuration. This is required if you need access the Vagrant box from other networks over the public network. To do so, you can use a shell provisioner script:

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; }'`"

Note the above is fairly complex and may be guest OS specific, but we document the rough idea of how to do it because it is a common question.


(Sorry, quite naive about vagrant/virtualbox so forgive the lack of proper networking terminology)

Your choice of bridge: en4) Thunderbolt is most likely the issue.

Here's what I assume you want/need, rather than what you asked for:

  • SSH login capability for vagrant to control your vm, from your dev box (the host). That's what the NAT with port forwarding does. It doesn't mess anything else up, so asking for it to be gone isn't all that useful. And that shows up on Adapter 1 in VirtualBox.

  • Connectivity from your LAN, rather than just your host. Let's say something in the 192.168.1.xxx range. That's what's important, on Adapter 2.

  • You just care about your normal network card/NIC and have no particular reason to run Ethernet over your Thunderbolt port.

i.e. pretty much what you would get from a VirtualBox vm with Bridged and no Vagrant to be seen.

Here's an SSH ifconfig from one of those VirtualBox only machines I have on my LAN. It runs a web server I can connect to and my Mac can SSH into it and connect to a database on it. I'll call it the reference.

[root@fdm ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:9A:85:1A
          inet addr:192.168.1.143  Bcast:192.168.1.255  Mask:255.255.255.0

In VirtualBox, the Network panel, for that reference vm, shows Adapter1 as Bridged. All other adapters are disabled.

OK, so I am now going to try for that same LAN connectivity result from Vagrant, but I accept that I will have a NAT at adapter 1, that's the vagrant-VB ssh communication mechanism.

Try #1 - which fails.

Starting point is a vagrant init.

Then in Vagrantfile, I changed only 2 things:

config.vm.box = "opscode-ubuntu-14.04"
config.vm.network "public_network"

if I vagrant up this, I get a dialog asking which interface to use:

==> default: Available bridged network interfaces:
1) en1: Wi-Fi (AirPort)
2) en0: Ethernet
3) en3: Thunderbolt 1
4) p2p0
5) bbptp0
6) bridge0

Now, looking at it, I first chose 2) because I thought I wanted Ethernet and well, 1) seemed 'too Apple'.

This works, but with an unsuitable IP 10.0.xx.xx entry, which my ISP blocks pings on, see below. I guess they really mean public when it says public network.

vagrant ssh

vagrant@vagrant:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:0c:41:3e
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe0c:413e/64 Scope:Link

eth1      Link encap:Ethernet  HWaddr 08:00:27:ca:f4:64
          inet6 addr: fe80::a00:27ff:feca:f464/64 Scope:Link

Try #2 - correct version

vagrant halt, then delete the directory, create it again and vagrant init. (I found that messing too much with the network could confuse the vagrant and/or virtualbox which a full removal and restart would fix)

config.vm.box = "opscode-ubuntu-14.04"
config.vm.network "public_network"

But, this time, pick 1) en1: Wi-Fi (AirPort).

vagrant ssh

That eth1 with 192.168.1.123 looks much nicer, doesn't it?

vagrant@vagrant:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 08:00:27:0c:41:3e
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0

eth1      Link encap:Ethernet  HWaddr 08:00:27:53:6e:1d
          inet addr:192.168.1.123  Bcast:192.168.1.255  Mask:255.255.255.0

And, indeed, I can ping 192.168.1.123 from my reference vm, or from another physical machine on my LAN.

[root@fdm ~]# ping 192.168.1.123
PING 192.168.1.123 (192.168.1.123) 56(84) bytes of data.
64 bytes from 192.168.1.123: icmp_seq=1 ttl=64 time=1039 ms
64 bytes from 192.168.1.123: icmp_seq=2 ttl=64 time=40.4 ms

FWIW, VirtualBox shows a NAT on Adapter 1 and a Bridged on Adapter 2.

Final setup -

Added automatic selection of the interface as well as a static IP (which you don't need). Problem solved, for me at least.

  config.vm.network "public_network", bridge: 'en1: Wi-Fi (AirPort)', ip: "192.168.1.201"

EDIT 201902: on my latest build, vagrant/virtualbox were complaining about Wifi (Airport) not being found:

==> default: Specific bridge 'en1: Wi-Fi (AirPort)' not found. You may be asked to specify
==> default: which network to bridge to.
==> default: Available bridged network interfaces:
1) en0: Ethernet
2) en2: Thunderbolt 1
3) bridge0

changed it to

config.vm.network "public_network", bridge: "bridge0"

Will update later if something comes up, but thought I'd update the bit about the adapter name.


The short answer seems to be don't.

You can override adapter 1 but expect problems at least with vagrant ssh

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "public_network", :adapter=>1 , type: "dhcp", :bridge => 'en4: Thunderbolt Ethernet'
  config.vm.hostname = "mddirector"

  # In case you get the host wrong...
  config.vm.boot_timeout = 30
  config.vm.provider "virtualbox" do |vb, override|
       vb.gui = true
  end

  config.ssh.host = '192.168.148.24'

end

Produces:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: bridged
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
    default: VirtualBox adapter #1 not configured as "NAT". Skipping port
    default: forwards on this adapter.
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...

Unfortunately it seems to then lock up configuring the network adapter but maybe you will have more luck than me.

If you do you can always force halt and reload with a corrected ssh.host. Alternatively I have heard of vagrant dns but never tried it.


I found this discussion on StackOverflow.

For me, it was enough to open the related Vagrantfile and uncomment the following line:

config.vm.network "public_network"

and then run vagrant reload