How do I hit a modified /etc/hosts from VirtualBox

jhamm picture jhamm · May 13, 2014 · Viewed 7.8k times · Source

I have an app that I have to change the /etc/hosts file to be able to hit. Here is my new file.

#127.0.0.1      localhost
127.0.0.1       local.connectwithme

This works easily on my local machine. I just go to local.connectwithme:3000 in my browser and I am able to view my app.

I am now trying to view it with VirtualBox running Windows 7. Typically I would just go to the ip address 10.0.2.2 in the browser of the virtual machine and access the localhost of my host machine from the VirtualBox, but now that I have changed the /etc/hosts file on my host machine I don't know what I need to hit to see my app on the virtual machine. Could someone please explain this to me. Thanks.

Answer

Taj Morton picture Taj Morton · May 15, 2014

To access the host OS when using a NATed Virtual Box connection, you need to determine the IP address of the host OS (on the other side of the NAT) and then add this IP address to the hosts file on your guest OS:

  1. From within the guest OS, determine your default gateway. This will be the IP address of the host OS. The easiest way to do this is to open a command prompt and run ipconfig /all. Under the Ethernet adapter, find the Default Gateway. This is almost certinaly 10.0.2.2 if you haven't changed anything.

  2. Now, find your hosts file and add the host IP address. On Windows 7, this file is located in %systemroot%\system32\drivers\etc\.

    1. Start Notepad with administrator privileges (Start|Run, type "notepad", right click and choose "Run as administrator."
    2. Open up %systemroot%\system32\drivers\etc\hosts in Notepad (you'll need to change it to show all files).
    3. Add an entry to the hosts file mapping the host IP address to the hostname you want, such as: 10.0.2.2 local.connectwithme