Connect to the host machine from a VirtualBox guest OS?

217

70

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.

Naftuli Kay

Posted 2011-07-14T22:53:21.230

Reputation: 8 389

2Difficult to solve without any hint which network confiuguration your guest actually has. Some do not allow access to host. Try the default gateway address of the guest. – Turbo J – 2011-07-15T01:12:28.710

9Turns out I can reach it through my default gateway at 10.0.2.2. – Naftuli Kay – 2011-07-15T01:42:21.087

Answers

263

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.

Naftuli Kay

Posted 2011-07-14T22:53:21.230

Reputation: 8 389

Any idea why connectivity would be horrendously slow when connecting using the default gateway as per your answer? – Ben Swinburne – 2015-04-15T11:39:56.017

This didn't seem to work for me (Ubuntu 12.04, Vagrant 1.0). Default gateway inside the guest showed as 10.0.2.2, and a nmap of that IP failed. 192.168.33.1 seems to be the right IP. (I had config.vm.network :hostonly, "192.168.33.52" in my config.) – Ajedi32 – 2015-07-21T14:40:23.070

Actually, never mind. It did work. nmap just wasn't working for some reason; not sure why. I could still connect to services running on the host from that IP, even though nmap wasn't showing them. – Ajedi32 – 2015-07-21T15:03:55.610

1netstat -rn on CentOS 7.2, Docker 1.11 displayed a 172.17 address as my gateway, but that IP didn't match up to my host. It turns out that 10.0.2.2 did. – Spencer Williams – 2016-07-21T16:48:43.503

1How do we know which one of those entries is the gateway? – Kramer – 2018-12-03T15:49:47.307

Was using 10.0.2.2 in win7 hosts file to access a webpage from host machine with IE11 and thought it didn't worked, until I remembered you have to restart IE for the hosts file changes to take effect. – D. Dan – 2018-12-11T10:55:40.450

37

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

  • 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

Prabhu

Posted 2011-07-14T22:53:21.230

Reputation: 759

9

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.

Pointy

Posted 2011-07-14T22:53:21.230

Reputation: 763

0

You can create shared folders in VirtualBox. This will automatically create a 'network share' within Virtual Box to access folders in your Host OS.

Here's a decent step by step in a Windows XP guest OS:

http://www.giannistsakiris.com/index.php/2007/09/28/virtualbox-access-shared-folders-from-windows-xp-guest-os/

kobaltz

Posted 2011-07-14T22:53:21.230

Reputation: 14 361

So I can access my host at /vboxsvr/ShareName ? – Naftuli Kay – 2011-07-14T23:10:34.397

Yes, you would set a folder like C:\STUFF to be shared with VirtualBox. Within VirtualBox Guest OS, you can go to that vboxserver and access the shared folders. – kobaltz – 2011-07-14T23:12:28.157

I actually don't want the shared folder. I want an IP address for my host machine. When I get it, I'd like to use it to use services on my host machine, ie: echo "GET /\r\n\r\n" | nc 192.168.100.100 80 (send a HTTP GET request to my host machine from the guest.) – Naftuli Kay – 2011-07-14T23:15:29.637

1In that case, you may need to set your Guest OS network adapter to a Bridge. This will pull an IP address from your DHCP or Router. You would then be able to natively access your host machine through IP address instead of the VBox Shared Folder. – kobaltz – 2011-07-14T23:18:50.367

0

Here is another solution to this issue by using an additional network interface and setting traffic destined for the host to route over an additional interface.

programr101

Posted 2011-07-14T22:53:21.230

Reputation: 1

4

Welcome to Super User! It would be nice to include the essential parts of the answer here, and provide the link only for future reference.

– slhck – 2012-03-28T15:38:00.560