Connect to the host machine from a VirtualBox guest OS?

I'd essentially like to access my host computer from the guest in VirtualBox. Is there an IP address given for my host which I can use from the guest? Are there extra steps required to set this up? I'd like to access my host's Apache, FTP, and SSH services.


This answer is about pretty much an guest OS setup in VirtualBox; you just need to use the network gateway address on the guest OS to connect to the host OS from a guest OS.

In the default Vagrant setup, you should be able to reach your host through the default gateway.

On Windows based guests, you can easily determine this IP address by running the command:

ipconfig

It should dump out something like this:

Windows IP Configuration

Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . : 10.0.2.15
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 10.0.2.2

In this example, the guest can reach the host machine 10.0.2.2.


On Unix/Linux based guests, use the command:

netstat -rn

It should dump out something like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 wlan0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 wlan0
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 wlan0

In this example, the guest can reach the host machine 192.168.1.1.


Accessing the web server of host computer from the guest is easy. This can be done easily using two methods. First do the following

  • In VirtualBox go to Devices and select Network Adapters...

  • In the adapter settings, check for Attached to.

  • If the value is NAT, do following

  • The default gateway when you setup Virtual box is generally 10.0.2.2 as default value. If you have not changed anything this will work. But if you have changed it and the guest machine you are running is windows run following command and find the default gateway

    ipconfig /all

    If you are on Linux, Unix or Mac OS, run the following command to get it

    netstat -rn | grep 'default' | awk '{print $2}'

  • Go to the web browser and type in this default gateway and press enter. The web server can be accessed.

  • If the value is Bridged Adapter, do following

    • find the ip address of host and guest
      • if you are on windows, run ipconfig and get the ip address
      • if you are on Linux, Unix, or Mac OS, run ifconfig | grep 'inet' and get ip address
      • the ip address is like 192.168.1.1
    • if you want to access host, run browser in guest and enter ip address of host
    • if you want to access guest, run browser in host and enter ip address of guest

Another way to do this is to use a "Host" type of virtual network. That gives you an interface in the guest OS with an address on a local subnet different from the "outer world" subnet(s) that your host machine is on. To make this work, you have to make sure of a few things:

  • Your VM has a "host only" adapter configured;
  • Your host services need to be listening on all local adapters, or at least the ones you want to be able to contact;
  • Your host will get its own virtual adapter, and you'll want to figure out its IP address and add it to the "hosts" file in your guest OS (however that works for the guest OS; on Windows XP, it's just the "hosts" file buried in C:/WINDOWS/system32/drivers/etc). Give it a name you want to use for the host's host name.

Once you've done this, you should be able to "see" the host from the VM via the name you coded into the host file.

For example, on my Ubuntu 11.04 host, I get a "vboxnet0" virtual interface on 192.168.56.1. The adapters in the machines come up with something like 192.168.56.101. I don't need to go in to my VMs, but I presume that'd be possible via a symmetric change to the host's host file. I add

192.168.56.1 mymachine

to the guest OS host files, and they can (for example) see my host machine's web server at

http://mymachine/whatever

You can of course have both bridged and host-only adapters set up.