HomeDevOpsSharing a Local Vagrant VM With Other Devices

Sharing a Local Vagrant VM With Other Devices

In Drupal-lamp we usually have a private virtual machine when in development. Under this scenario you can only access the site from the host machine where you spun up. However, there are situations where you would want the vm to be available to other devices on your network. (e.g., testing a site on a mobile device).

Solution

In your Drupal-lamp directory there is a Vagrantfile. By default the Vagrantfile has a line which state:


server.vm.network :private_network, ip: '192.168.50.5'

Change this line to:


server.vm.network :public_network

Run vagrant up. Vagrant will present options for which network to bridge. Mine looks like:


[drupaldev] Available bridged network interfaces:
1) en0: Wi-Fi (AirPort)
2) en4: Thunderbolt 1
3) en5: Thunderbolt 2
4) bridge0
5) p2p0

Select the network device which has internet connection. In my situation it would be number 1. In order to keep from having this prompt you each and every vagrant up, you can change your server.vm.network statement to look something like this:


server.vm.network :public_network, :bridge => 'en0: Wi-Fi (AirPort)' 

You need to change the bridge to be appropriate for your network interface. Under the scenario above DHCP will automatically assign you an ip. If you are using our Ubuntu 12.04 box that is default in Drupal-lamp you will need to ssh into the machine and run ifconfig to determine the ip.


➜  drupal-lamp git:(master) ✗ vagrant ssh
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-virtual x86_64)

 * Documentation:  https://help.ubuntu.com/

105 packages can be updated.
49 updates are security updates.

Last login: Thu Jan 31 13:48:53 2013
vagrant@drupal:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:97:31:80
          inet addr:192.168.163.166  Bcast:192.168.163.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe97:3180/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:253816 errors:0 dropped:0 overruns:0 frame:0
          TX packets:79972 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:374526420 (374.5 MB)  TX bytes:4668230 (4.6 MB)

eth1      Link encap:Ethernet  HWaddr 00:0c:29:97:31:8a
          inet addr:10.1.10.106  Bcast:10.1.10.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe97:318a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:21462 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2606402 (2.6 MB)  TX bytes:1152 (1.1 KB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:51 errors:0 dropped:0 overruns:0 frame:0
          TX packets:51 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3266 (3.2 KB)  TX bytes:3266 (3.2 KB)

vagrant@drupal:~$

In my situation, 10.1.10.106 is the ip that you need to add to your /etc/hosts file on any device on your network in order to view your vm.

See the official Vagrant documentation for more information.