Setup SSH access between VirtualBox Host and Guest VMs

by
on
September 10, 2010

It is often necessary to have the ability to SSH between your Linux Host and your Guest virtual machines. In VirtualBox you can do this by simply configuring a second network interface on the Guest. This type of setup will not only allow SSH sessions between Host and Guest, but also between separate Guests themselves.

First off shutdown your Guest VM so that you can add a second network interface. By default you should have one interface already which is using NAT. Leave this alone as it provides access to the internet. Select the tab “Adapter 2″ and change “attached to” to “Host-only Adapter”.

Now boot up your Guest and pull up a terminal. Run ifconfig to see what base address eth1 has been assigned. You can use this address as a base to assign static IP addresses for each Guest VM. In this case inet addr:192.168.56.101 is the base IP address.
$ sudo ifconfig eth1
Link encap:Ethernet HWaddr XX:XX:XX:XX:XX:XX
inet addr:192.168.56.101 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: XXXX::XXXX:XXXX:XXXX:XXXX/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:296 errors:0 dropped:0 overruns:0 frame:0
TX packets:237 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:32116 (31.3 KiB) TX bytes:37642 (36.7 KiB)

If you rely on DHCP you might get a different IP address each time your Guest VM reboots. If this is a problem, you need to setup a static IP address instead.

On a Debian based system edit /etc/network/interfaces to add the following lines. Change the address to your base IP address.
auto eth1
iface eth1 inet static
address 192.168.56.101
netmask 255.255.255.0

On a redhat based system edit this file /etc/sysconfig/network-scripts/ifcfg-eth1 and add the following lines. Change the IPADDR to your base IP address.
DEVICE=eth1
BOOTPROTO=static
IPADDR=192.168.56.101
NETMASK=255.255.255.0

On your Linux Host you can now open a terminal add ssh to the Guest VM:
$ ssh ryan@192.168.56.101

If you are having problems connecting, make sure that sshd in installed and running, and that the firewall on the is not blocking the SSH service on the VM.

You can create an entry in /etc/hosts so you don’t have to remember the IP address.
192.168.56.101 virtualhost1

This allows you to use the virtual hostname instead.
$ ssh ryan@virtualhost1

Related posts:

34 Comments
virtualbox
, , , , , , , , , , , , , , , , , , , , ,
  • Jax

    EXTREMELY helpful.

  • Thankfull

    I’m on a redhat machine and have been searching for hours to find the appropriate network config file. Thank you so much.

    Also, on my system I needed to run $ /sbin/ifconfig rather than $ ifconfig (which doesn’t exist).

  • DM

    Thank You for the help. Works great

  • nidkil

    Simple and effective. Thx a lot!!!!

  • Kenneth

    Worked flawlessly on an XP host with Ubuntu guest. Thanks!!!

  • Rocio

    Hi,
    It worked great! Thankyou!
    Is there any way to make it ssh from another computer? Something like a headless server?
    Thank you!

  • prasad

    nice one! worked for me :)

    Thanks for your efforts

  • Pingback: Cannot ssh guest OS from host

  • Pingback: Cannot ssh guest OS from host | Coders & Admins

  • Josep

    Hi,

    Before seting up the guest Network Adapter 2 i had to create the vboxnet0 network in the virtualbox host:

    (Preferences -> Network – Add network).

    After that, when setting up the eth1 interface by editing /etc/network/interfaces, instead of rebooting you can do as root:

    ifup eth1

    and you get the interface working.

    Thanks for sharing!
    Josep

    • will

      This should be added to the original post. Thank you Joesp.

  • http://www.moovietrailers.com/ Ken

    It’s really helpful. Thanks!!

  • http://floorplanner.com skrat

    That helped, thanks.

  • jjr

    Worth adding for Windows 7 hosts.
    Check Windows firewall for blocking ping responses.
    If you are using VPN. You probably will need to change your routes table.
    1. cmd: route PRINT
    (several first lines are interfaces number find [number ]“VirtaulBox Host-Only …” for me number was 35
    2. cmd(as admin): route add 192.168.56.0 255.255.255.0 192.168.56.1 if 35 metrics 1
    3. Done

  • Byron

    Muchos gracias! One addition – if ‘eth1′ does not have an inet addr when you run ‘sudo ifconfig eth1′, just proceed with the next step anyway and edit /etc/network/interfaces. However, add eth1 with dchp instead of static, just like eth0 (which will probably already be in /etc/network/interfaces). Eg, just add the following two lines (not four as in the instructions above):


    # The host-only interface for receiving incoming ssh
    auto eth1
    iface eth1 inet dhcp

    Then reboot or run ‘ifup eth1′. Now run ‘ifconfig’, and you should be able to see the inet addr for eth1 in the list. Now if you want to make eth1 static, re-edit /etc/network/interfaces and insert all four lines as in the original instructions.

  • alex

    unbelievable useful article.
    thank you so much

  • Pingback: VM ทำให้ Host OS สามารถ Connect ไปยัง Guest OS ได้ (ผม control มันได้) : วุ่น ไอที

  • Guy Argo

    Two things:

    1) I had to add vboxnet0 network in the virtual box host via (Preferences -> Network – Add network).
    2) I found it more convenient to set up the network interface to use DHCP.
    Byrondescribes the debian setup for this above.
    On RHEL/CentOS, create /etc/sysconfig/network-scripts/eth1 as follows:

    DEVICE="eth1"
    BOOTPROTO="dhcp"
    ONBOOT="yes"

    • Dan Berglund

      +1 on the need for adding vboxnet0 network, I suppose this is necessary on some versions/newer versions. Can the author perhaps add this? (Running Mac OSX 1.7.4 with virtualbox 4.1.18)

    • Fred

      +1
      If you get the No Host Adapter Selected then you need to set up the vboxnet0 as stated above and you are off. Thank you, very helpful.

  • swamy

    Its very very useful, thanks for your post

  • http://twitter.com/spawgi Sumod

    This is a very helpful post. One little thought – I think you need to enable host only adapters in the Virtual Box setting first before going to individual VMs. Else you get invalid settings. In my case it was not setup by default so I had to set that before going to VM

  • Azumi

    This is a great tip! Thanks for sharing..

  • Skyrazor

    straight to the point, thank you

  • Guy McArthur

    This did not work (connection refused). I needed to set up port forwarding on the guest’s first adapter (NAT), e.g. from 2222 on the host to 22 on the guest and ssh to the former.

    • Shazly

      I did that but it still gives me (connection refused). Do you know why? thanks.

      • Guy McArthur

        Do ssh -p 2222 127.0.0.1

  • Shazly

    Thanks for this tutorial, but i’ve a question. When i try (ifconfig) command on the host -Ubuntu- i get an ip=192.168.56.1, but when i try (ifconfig) on the guest VM -also Ubuntu- i get another ip=192.168.56.101. Why there is 2 ips and which one i should use to ssh the guest from the host? thanks.

  • Jay

    U rocked my world bro… Its sad i found this after soooo many days of mental torture!

  • Virtual Person

    Fuck, Yes, Kud0$$$$$$$$!

  • Wildan Fathan

    thank you master, works :)

  • Pingback: How to SSH to a VirtualBox Ubuntu guest externally through a Windows host? | Ask Programming & Technology

  • voitek

    nice tutorial

  • vancexu

    Tips 1:
    You should have installed ssh server first.
    sudo apt-get install openssh-server

    Tips 2:
    For those who have more than one vm, you should change the ip address to avoid all of them are using the same 192.168.56.101
    To change ip address, first edit /etc/network/interfaces with 192.168.56.102 or other address as the article says. Then restart the vm.

Copyright 2008-2010 WiredRevolution.com. All rights reserved.