Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SSH between Mac OS X host and Virtual Box guest

On Mac OS (host):

Shutdown your VM and do:

VirtualBox > Settings > Network > Add (you will get vboxnet0)

On a terminal ifconfig will show you new interface vboxnet0

VM's Settings > System > check "Enable I/O APIC." VM's Settings > Network > Adapter 2 > host-only vboxnet0

On Ubuntu (guest):

Install OpenSSH Server

Edit /etc/network/interfaces file to append the following lines:

auto eth1
iface eth1 inet static
address 192.168.56.10
netmask 255.255.255.0

Run sudo ifup eth1 from the Ubuntu command line. SSH server should be up and running. Switch to your host terminal and enter ssh 192.168.56.10

sources:

@sebastianconcept

This comment has been minimized.

Show comment
Hide comment
@sebastianconcept

sebastianconcept Aug 7, 2014

awesome. Thanks for sharing o/

awesome. Thanks for sharing o/

@jchris

This comment has been minimized.

Show comment
Hide comment
@jchris

jchris Oct 19, 2014

worked for me also, thanks!

jchris commented Oct 19, 2014

worked for me also, thanks!

@jbodah

This comment has been minimized.

Show comment
Hide comment
@jbodah

jbodah Nov 17, 2014

In case you can't find vboxnet0 try this:

VirtualBox > Preferences > Network > Host-only Networks > Add host-only network (lns)

jbodah commented Nov 17, 2014

In case you can't find vboxnet0 try this:

VirtualBox > Preferences > Network > Host-only Networks > Add host-only network (lns)

@sovanna

This comment has been minimized.

Show comment
Hide comment
@sovanna

sovanna Jun 3, 2015

thanks a lot !

sovanna commented Jun 3, 2015

thanks a lot !

@chadb

This comment has been minimized.

Show comment
Hide comment
@chadb

chadb Sep 21, 2015

Thank you.

chadb commented Sep 21, 2015

Thank you.

@ksteigerwald

This comment has been minimized.

Show comment
Hide comment

High Five

@haringsrob

This comment has been minimized.

Show comment
Hide comment
@haringsrob

haringsrob Nov 19, 2015

Nice one! Thumbs up ;)

Nice one! Thumbs up ;)

@pauldanielvidal

This comment has been minimized.

Show comment
Hide comment
@pauldanielvidal

pauldanielvidal Nov 20, 2015

Thanks for this info, it help it a lot^^,!

I had a initial problem where i still couldn't reach the vbox through 192.168.56.10...

~ $ ssh 192.168.56.10
ssh: connect to host 192.168.56.10 port 22: Connection refused

Then i checked the "ifconfig" again on the host (MacOSX) and found that vboxnet0's ip address was set to 192.168.100.10

vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00
    inet 192.168.100.1 netmask 0xffffff00 broadcast 192.168.100.255

I just updated the IP Address in the guest (Ubuntu) /etc/network/interfaces and it worked well.

Again thank you very much^^,

Thanks for this info, it help it a lot^^,!

I had a initial problem where i still couldn't reach the vbox through 192.168.56.10...

~ $ ssh 192.168.56.10
ssh: connect to host 192.168.56.10 port 22: Connection refused

Then i checked the "ifconfig" again on the host (MacOSX) and found that vboxnet0's ip address was set to 192.168.100.10

vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
    ether 0a:00:27:00:00:00
    inet 192.168.100.1 netmask 0xffffff00 broadcast 192.168.100.255

I just updated the IP Address in the guest (Ubuntu) /etc/network/interfaces and it worked well.

Again thank you very much^^,

@pkdevesh

This comment has been minimized.

Show comment
Hide comment
@pkdevesh

pkdevesh Dec 28, 2015

Thanks Bunch really helpful

Thanks Bunch really helpful

@RamirezAmayaS

This comment has been minimized.

Show comment
Hide comment
@RamirezAmayaS

RamirezAmayaS Feb 9, 2016

Loving you so much right now. Got it working after half a day.

Loving you so much right now. Got it working after half a day.

@timea

This comment has been minimized.

Show comment
Hide comment
@timea

timea Feb 29, 2016

Yes, hug that man! Thanks.

timea commented Feb 29, 2016

Yes, hug that man! Thanks.

@vivekbhansali

This comment has been minimized.

Show comment
Hide comment
@vivekbhansali

vivekbhansali Mar 20, 2016

Thank you!

Just one small thing to notice is that network adapter name on guest machine may not be always 'eth1'. You need to check the name and add that accordingly in place of 'eth1' which happened in my case.

Thank you!

Just one small thing to notice is that network adapter name on guest machine may not be always 'eth1'. You need to check the name and add that accordingly in place of 'eth1' which happened in my case.

@francofabio

This comment has been minimized.

Show comment
Hide comment
@francofabio

francofabio Apr 9, 2016

Thank you!
A good tip.

Thank you!
A good tip.

@davebeach

This comment has been minimized.

Show comment
Hide comment
@davebeach

davebeach Jun 18, 2016

Perfect simple and it works!

Perfect simple and it works!

@ma7rix

This comment has been minimized.

Show comment
Hide comment
@ma7rix

ma7rix Aug 10, 2016

nice, thanks

ma7rix commented Aug 10, 2016

nice, thanks

@jccorrea

This comment has been minimized.

Show comment
Hide comment
@jccorrea

jccorrea Aug 25, 2016

omG! VirtualBox Settings is hidden under Preferences (shortcut Command + " , "). Thanks

omG! VirtualBox Settings is hidden under Preferences (shortcut Command + " , "). Thanks

@tungpham

This comment has been minimized.

Show comment
Hide comment
@tungpham

tungpham Sep 2, 2016

it's not always eth1, you can list out the interfaces with ifconfig -a

tungpham commented Sep 2, 2016

it's not always eth1, you can list out the interfaces with ifconfig -a

@sg-s

This comment has been minimized.

Show comment
Hide comment
@sg-s

sg-s Sep 7, 2016

this isn't working for me... and i don't understand how you guys are getting ssh 192.168.56.10 to work...isn't this the same as ssh localhost? When I do this, I can successfully ssh into my host, which isn't very useful.

sg-s commented Sep 7, 2016

this isn't working for me... and i don't understand how you guys are getting ssh 192.168.56.10 to work...isn't this the same as ssh localhost? When I do this, I can successfully ssh into my host, which isn't very useful.

@roflomglol

This comment has been minimized.

Show comment
Hide comment
@roflomglol

roflomglol Sep 7, 2016

Tears of joy! Damn it was really frustrating. For those of you who can't bring eth1 up, try ip link and check which adapter is missing from ifconfig, then update /etc/network/interfaces accordingly.

Thanks for gist, OP!

Tears of joy! Damn it was really frustrating. For those of you who can't bring eth1 up, try ip link and check which adapter is missing from ifconfig, then update /etc/network/interfaces accordingly.

Thanks for gist, OP!

@nachiket

This comment has been minimized.

Show comment
Hide comment
@nachiket

nachiket Nov 3, 2016

now if I could only get X working :)

nachiket commented Nov 3, 2016

now if I could only get X working :)

@rehnd

This comment has been minimized.

Show comment
Hide comment
@rehnd

rehnd Nov 15, 2016

I ended up losing my internet connection when doing this, so installing ssh would not work. To fix, I added a third network adapter:

(power off vm) > Settings > Network > Adapter 3 > Check enable > Attached to: NAT

rehnd commented Nov 15, 2016

I ended up losing my internet connection when doing this, so installing ssh would not work. To fix, I added a third network adapter:

(power off vm) > Settings > Network > Adapter 3 > Check enable > Attached to: NAT

@mbakirov

This comment has been minimized.

Show comment
Hide comment
@mbakirov

mbakirov Jan 11, 2017

I ended up losing my internet connection when doing this, so installing ssh would not work. To fix, I added a third network adapter:
(power off vm) > Settings > Network > Adapter 3 > Check enable > Attached to: NAT

Great! It's works! Thank you a lot!

I ended up losing my internet connection when doing this, so installing ssh would not work. To fix, I added a third network adapter:
(power off vm) > Settings > Network > Adapter 3 > Check enable > Attached to: NAT

Great! It's works! Thank you a lot!

@nickylimjj

This comment has been minimized.

Show comment
Hide comment
@nickylimjj

nickylimjj Jan 28, 2017

I am getting permission denied when i try to log in. What password should i be trying? Thanks!

I am getting permission denied when i try to log in. What password should i be trying? Thanks!

@chao1995

This comment has been minimized.

Show comment
Hide comment
@chao1995

chao1995 Feb 7, 2017

If anyone else has the error cannot find device eth1, check your ifconfig output and see what is the name of the interface.

According to this answer, it may not always be eth1. And in my case (VirtualBox 5.1.14), the interface is named enp0s8.

So just follow the steps in the gist but replace eth1 with enp0s8. And I can ssh to my VM now!

chao1995 commented Feb 7, 2017

If anyone else has the error cannot find device eth1, check your ifconfig output and see what is the name of the interface.

According to this answer, it may not always be eth1. And in my case (VirtualBox 5.1.14), the interface is named enp0s8.

So just follow the steps in the gist but replace eth1 with enp0s8. And I can ssh to my VM now!

@computereasy

This comment has been minimized.

Show comment
Hide comment
@computereasy

computereasy Mar 12, 2017

great, it works.

great, it works.

@AnalyzePlatypus

This comment has been minimized.

Show comment
Hide comment
@AnalyzePlatypus

AnalyzePlatypus May 20, 2017

I had considerable difficulty setting up.

This is what worked for me:
I followed the gist with @chao1995's suggestions.

In the VM:

$ ifconfig
lo        Link encap:Local Loopback 
enp0s8    Link encap:Ethernet ...

My etc/network/interfaces looks like this:

$ sudo nano etc/network/interfaces
GNU nano 2.5.3 
# Added this:
# Local Interface (for SSH)
auto enp0s8
iface enp0s8 inet static
address 192.168.56.10
netmask 255.255.255.0

To bring up the service, I ran:

$ sudo ifup enp0s8

In my host terminal:

$ ssh <username>@192.168.56.10
Welcome to Ubuntu 16.04.2 LTS ...

Wahoo!

Troubleshooting Checklist

In VirtualBox Manager

  • Is Settings > Network > Adaptor 2 set to Host-Only Adaptor (vboxnet0)?
  • Is Settings > System > Enable I/O APIC checked?

In the VM

  • Is OpenSSH installed? ($ sudo apt-get install openssh-server)
  • Have you made the changes to /etc/network/interfaces, replacing eth1 with your interface name from ifconfig ?
  • Have you brought up the adaptor with $ sudo ifup <interface name>?

On your host

  • Have you determined the correct IP address to ssh into? (Runifconfig. Use the value of vboxnet0's inet field. If vboxnet0 is not available, review the section above In VirtualBox Manager)
  • Run ssh <username>@<ip address> and log in.

I hope that helps.
Good luck out there!

I had considerable difficulty setting up.

This is what worked for me:
I followed the gist with @chao1995's suggestions.

In the VM:

$ ifconfig
lo        Link encap:Local Loopback 
enp0s8    Link encap:Ethernet ...

My etc/network/interfaces looks like this:

$ sudo nano etc/network/interfaces
GNU nano 2.5.3 
# Added this:
# Local Interface (for SSH)
auto enp0s8
iface enp0s8 inet static
address 192.168.56.10
netmask 255.255.255.0

To bring up the service, I ran:

$ sudo ifup enp0s8

In my host terminal:

$ ssh <username>@192.168.56.10
Welcome to Ubuntu 16.04.2 LTS ...

Wahoo!

Troubleshooting Checklist

In VirtualBox Manager

  • Is Settings > Network > Adaptor 2 set to Host-Only Adaptor (vboxnet0)?
  • Is Settings > System > Enable I/O APIC checked?

In the VM

  • Is OpenSSH installed? ($ sudo apt-get install openssh-server)
  • Have you made the changes to /etc/network/interfaces, replacing eth1 with your interface name from ifconfig ?
  • Have you brought up the adaptor with $ sudo ifup <interface name>?

On your host

  • Have you determined the correct IP address to ssh into? (Runifconfig. Use the value of vboxnet0's inet field. If vboxnet0 is not available, review the section above In VirtualBox Manager)
  • Run ssh <username>@<ip address> and log in.

I hope that helps.
Good luck out there!

@firstrow

This comment has been minimized.

Show comment
Hide comment
@firstrow

firstrow Jun 10, 2017

thanks man!

thanks man!

@crsuarez

This comment has been minimized.

Show comment
Hide comment
@crsuarez

crsuarez Jun 23, 2017

For those who had issues configuring the interfaces, take in count, that is better to attach Adapter 1 as "host only adapter", and, same way, Adapter 2 as "NAT", so that, you won't have route problems. Normally, in production you setup the management interface in eth0, and left the others for other configurations. Virtualbox, support just 4 adapters, which is fine for testing.
For Centos 6.x, ifcfg-eth1 (configured in adapter as attached to NAT).

#] cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=dhcp
ONBOOT=yes

I forked this one, and add some other info, that you might find useful tho. https://gist.github.com/crsuarez/5a066638a805e46b448acebd4f5fe8d1

crsuarez commented Jun 23, 2017

For those who had issues configuring the interfaces, take in count, that is better to attach Adapter 1 as "host only adapter", and, same way, Adapter 2 as "NAT", so that, you won't have route problems. Normally, in production you setup the management interface in eth0, and left the others for other configurations. Virtualbox, support just 4 adapters, which is fine for testing.
For Centos 6.x, ifcfg-eth1 (configured in adapter as attached to NAT).

#] cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=dhcp
ONBOOT=yes

I forked this one, and add some other info, that you might find useful tho. https://gist.github.com/crsuarez/5a066638a805e46b448acebd4f5fe8d1

@kilojoules

This comment has been minimized.

Show comment
Hide comment
@kilojoules

kilojoules Jul 17, 2017

I needed to setup up port forwarding to make this work. https://medium.com/trisfera/virtualbox-shared-folders-ssh-from-osx-host-to-ubuntu-server-guest-tutorial-ef2db1722033 ssh username@localhost -p 2281 worked for me!

I needed to setup up port forwarding to make this work. https://medium.com/trisfera/virtualbox-shared-folders-ssh-from-osx-host-to-ubuntu-server-guest-tutorial-ef2db1722033 ssh username@localhost -p 2281 worked for me!

@MichalKrakow

This comment has been minimized.

Show comment
Hide comment
@MichalKrakow

MichalKrakow Nov 15, 2017

What if no option for adding HOST-ONLY adapter is available in network config? It only allows me to add NAT adapter...

What if no option for adding HOST-ONLY adapter is available in network config? It only allows me to add NAT adapter...

@tmehlinger

This comment has been minimized.

Show comment
Hide comment
@tmehlinger

tmehlinger Feb 2, 2018

@MichalKrakow File => Host Network Manager => Create.

@MichalKrakow File => Host Network Manager => Create.

@timsmelik

This comment has been minimized.

Show comment
Hide comment
@timsmelik

timsmelik Feb 27, 2018

As @chao1995 and @AnalyzePlatypus pointed out the interface name might not be eth1 (as of systemd 197 predictable network interface names are used). In addition, the host-only adapter might not show up in ifconfig because it is not up yet. I made the mistake of setting a static IP address on my first network adapter instead. Running ifconfig -a revealed the correct adapter.

As @chao1995 and @AnalyzePlatypus pointed out the interface name might not be eth1 (as of systemd 197 predictable network interface names are used). In addition, the host-only adapter might not show up in ifconfig because it is not up yet. I made the mistake of setting a static IP address on my first network adapter instead. Running ifconfig -a revealed the correct adapter.

@cpace6

This comment has been minimized.

Show comment
Hide comment
@cpace6

cpace6 Mar 7, 2018

@crsuarez VB only lists 4 adapters through the GUI but actually supports up to 8 via the command line tool

cpace6 commented Mar 7, 2018

@crsuarez VB only lists 4 adapters through the GUI but actually supports up to 8 via the command line tool

@skuo-bl

This comment has been minimized.

Show comment
Hide comment
@skuo-bl

skuo-bl Mar 8, 2018

Two network interfaces are needed on the guest to enable it to connect to the internet and allow ssh from host.
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:bb:26 brd ff:ff:ff:ff:ff:ff
3: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:44:3b:96 brd ff:ff:ff:ff:ff:ff

enp0s3 is mapped to NAT and enp0s8 is mapped to vboxnet0 (192.168.56.1). Add the following to /etc/network/interfaces and leave the config for enp0s3 as is.

# Add the following
auto enp0s8
iface enp0s8 inet static1
address 192.168.56.10
netmask 255.255.255.0

Back on host, ssh 192.168.56.0 should work

skuo-bl commented Mar 8, 2018

Two network interfaces are needed on the guest to enable it to connect to the internet and allow ssh from host.
$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:6c:bb:26 brd ff:ff:ff:ff:ff:ff
3: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000 link/ether 08:00:27:44:3b:96 brd ff:ff:ff:ff:ff:ff

enp0s3 is mapped to NAT and enp0s8 is mapped to vboxnet0 (192.168.56.1). Add the following to /etc/network/interfaces and leave the config for enp0s3 as is.

# Add the following
auto enp0s8
iface enp0s8 inet static1
address 192.168.56.10
netmask 255.255.255.0

Back on host, ssh 192.168.56.0 should work

@skuo-bl

This comment has been minimized.

Show comment
Hide comment
@skuo-bl

skuo-bl Mar 8, 2018

One more thing, use "Paravirtualized Network" for Host-only Adapter.

skuo-bl commented Mar 8, 2018

One more thing, use "Paravirtualized Network" for Host-only Adapter.

@c-rainstorm

This comment has been minimized.

Show comment
Hide comment
@c-rainstorm

c-rainstorm Mar 19, 2018

Since Ubuntu 17.10,use netplan to configure your static IP address。 See my fork for more detail.

Since Ubuntu 17.10,use netplan to configure your static IP address。 See my fork for more detail.

@beesuns

This comment has been minimized.

Show comment
Hide comment
@beesuns

beesuns Apr 18, 2018

I am currently using MacBook Air as host and I have installed virtualbox and run kali linux as guest.

In case you can't find vboxnet0 try this: VirtualBox >Preferences > Network > Host-only Networks > Add host-only network (lns)

When I was trying this, there was only NAT Network option. I can not find Host-only Networks.

beesuns commented Apr 18, 2018

I am currently using MacBook Air as host and I have installed virtualbox and run kali linux as guest.

In case you can't find vboxnet0 try this: VirtualBox >Preferences > Network > Host-only Networks > Add host-only network (lns)

When I was trying this, there was only NAT Network option. I can not find Host-only Networks.

@Shawyeok

This comment has been minimized.

Show comment
Hide comment
@Shawyeok

Shawyeok Apr 23, 2018

@beesuns You can create a host-only network adapter via VBoxManage command.

$ VBoxManage hostonlyif create

@beesuns You can create a host-only network adapter via VBoxManage command.

$ VBoxManage hostonlyif create
@cakesummer

This comment has been minimized.

Show comment
Hide comment
@cakesummer

cakesummer Jun 10, 2018

I would like to post some new procedure in the new version of VirtualBox(5.2.12 r122591 (Qt5.6.3)) as follows:
File > Host Network Manager > Create (you will get vboxnet0)
...The rest is the same as above

I would like to post some new procedure in the new version of VirtualBox(5.2.12 r122591 (Qt5.6.3)) as follows:
File > Host Network Manager > Create (you will get vboxnet0)
...The rest is the same as above

@genidma

This comment has been minimized.

Show comment
Hide comment
@genidma

genidma Jun 21, 2018

Instructions shared by @AnalyzePlatypus work.

genidma commented Jun 21, 2018

Instructions shared by @AnalyzePlatypus work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment