osx - How to connect to a Virtualbox guest from the host when network cable unplugged

02
2014-01
  • Greg K

    I'd like to work offline (I'm flying to the US twice this month), to do this I need access to a linux development server.

    Update: After searching for a while, this is the same problem I'm experiencing (except with a Snow Leopard host and Ubuntu 10.04 guest).


    When I work from home I boot a VirtualBox VM and that acts as my dev server for the day (providing Apache, PHP & MySQL to run my server side code). However, I'd like to work with my VM when I'm not connected to a network.

    I have my Ubuntu VM guest set up with a bridge connection so it can serve HTTP and provide SSH access from inside my local network.

    I've tried to manually configure my network settings on both Mac OSX (the host) and Ubuntu (the guest) but I can't even ping my own NIC address (127.0.0.1 can, 192.168.21.x I can't) in OS X when I unplug the cable.

    Manual network settings:

    $ ifconfig en0
    en0: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 00:xx:xx:xx:xx:xx 
    inet 192.168.21.5 netmask 0xffffff00 broadcast 192.168.21.255
    media: autoselect (100baseTX <full-duplex,flow-control>)
    status: active
    

    I can ping localhost fine, as well as my VM (.20) and SSH too.

    $ ping 192.168.21.5
    PING 192.168.21.5 (192.168.21.5): 56 data bytes
    64 bytes from 192.168.21.5: icmp_seq=0 ttl=64 time=0.085 ms
    64 bytes from 192.168.21.5: icmp_seq=1 ttl=64 time=0.102 ms
    64 bytes from 192.168.21.5: icmp_seq=2 ttl=64 time=0.100 ms
    64 bytes from 192.168.21.5: icmp_seq=3 ttl=64 time=0.094 ms
    
    $ ping 192.168.21.20
    PING 192.168.21.20 (192.168.21.20): 56 data bytes
    64 bytes from 192.168.21.20: icmp_seq=0 ttl=64 time=0.910 ms
    64 bytes from 192.168.21.20: icmp_seq=1 ttl=64 time=1.181 ms
    64 bytes from 192.168.21.20: icmp_seq=2 ttl=64 time=1.159 ms
    64 bytes from 192.168.21.20: icmp_seq=3 ttl=64 time=1.320 ms
    

    Network cable unplugged:

    $ ifconfig en0
    en0: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 00:xx:xx:xx:xx:xx 
    media: autoselect
    status: inactive
    
    $ ping 192.168.21.5
    PING 192.168.21.5 (192.168.21.5): 56 data bytes
    ping: sendto: No route to host
    ping: sendto: No route to host
    Request timeout for icmp_seq 0
    ping: sendto: No route to host
    Request timeout for icmp_seq 1
    

    Does OS X disable the NIC when the network cable is unplugged? Any way to stop it doing this?

  • Answers
  • coneslayer

    Perhaps you can add an additional NIC to the guest, and configure it for Host-only Networking.

    Host-only networking is another networking mode that was added with version 2.2 of VirtualBox. It can be thought of as a hybrid between the bridged and internal networking modes: as with bridged networking, the virtual machines can talk to each other and the host as if they were connected through a physical ethernet switch. Similarly, as with internal networking however, a physical networking interface need not be present, and the virtual machines cannot talk to the world outside the host since they are not connected to a physical networking interface.

  • Tim Alexander

    Just a thought but possibly a loopback adaptor might get round this. http://www.stayonline.com/detail.aspx?ID=1215


  • Related Question

    Configuring VirtualBox host only networking: OSX host, Ubuntu guest
  • Greg K

    SOLVED: I got this working by reconfiguring the vbox network settings to 192.168.21.19 (in VirtualBox -> Preferences -> Network) and commenting the line specifying a gateway as it's not needed (no external access on the host only interface) as per this post.

    I can then SSH to 192.168.21.20 from OSX and connect to Ubuntu. I changed my /etc/fstab mounts to point to //192.168.21.19/


    I have a Ubuntu guest configured with two interfaces, eth0 is using NAT and works fine, I can access the net.

    The second interface eth1 is set to host only networking and VirtualBox has created a vboxnet0 virtual adapter on the host.

    I've configured vboxnet0 in VirtualBox adapter settings with the following:

    ip 192.168.21.20
    subnet 255.255.255.0
    

    Once the VM guest is running, ifconfig on OSX has vboxnet0 setup as:

    vboxnet0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        ether 0a:00:27:00:00:00 
        inet 192.168.21.20 netmask 0xffffff00 broadcast 192.168.21.255
    

    In the guest, eth0 is set to use DHCP, I've statically assigned eth1 to 192.168.21.20 (this was a mistake and the IPs clash between host and guest):

    auto eth1
    iface eth1 inet static
        address 192.168.21.20 (should be .19)
        netmask 255.255.255.0
        network 192.168.21.0
        broadcast 192.168.21.255
        gateway 192.168.21.1
    

    There is no device on 192.168.21.1 - what should I set my gateway to? (turns out a gateway is not needed as it's a HOST only interface). Net access is provided by the NAT adapter.

    In the guest the routes look like so:

    Destination    Gateway       Genmask        Flags Metric   Ref  Use  Iface
    192.168.21.0   *             255.255.255.0  U     0        0    0    eth1
    10.0.2.0       *             255.255.255.0  U     0        0    0    eth0
    default        10.0.2.2      0.0.0.0        UG    100      0    0    eth0
    default        192.168.21.1  0.0.0.0        UG    100      0    0    eth1
    

    Route table on OSX:

    $ netstat -nr
    Routing tables
    
    Internet:
    Destination        Gateway          Flags      Refs      Use    Netif Expire
    default            10.77.36.1       UGSc         28        0      en1
    10.77.36/22        link#5           UCS           5        0      en1
    10.77.39.38        127.0.0.1        UHS           1     2236      lo0
    10.77.39.255       link#5           UHLWbI        1       66      en1
    127                127.0.0.1        UCS           0        0      lo0
    127.0.0.1          127.0.0.1        UH            1     8642      lo0
    169.254            link#5           UCS           0        0      en1
    192.168.21         link#7           UC            2        0  vboxnet
    192.168.21.20      a:0:27:0:0:0     UHLWI         0        4      lo0
    192.168.21.255     link#7           UHLWbI        2       64  vboxnet
    

    I can't SSH from the host to the guest (I used to be able to when the VM was configured with a bridged connection):

    $ ssh 192.168.21.20
    ssh: connect to host 192.168.21.20 port 22: Connection refused
    

    What have I done wrong here? TIA


  • Related Answers
  • Greg K

    I reconfigured the vboxnet0 virtual adapter IP on the host to 192.168.21.19 so it differs from the guests configuration of .20 - then they don't clash. See my updates in the post with more details about the solution.