VirtualBox Host ssh to Guest

54

26

I have a mac host, and a VirtualBox Linux guest, currently using Bridged mode.

I need to be able to easily SSH into the guest from the host. It would be nice to use a hostname, but a never-changing IP address would be fine too (currently, the IP changes with each new network I join, and the hostname has never worked).

I also need internet access from the guest.

Other machines on the network do not need access to the guest.

What is the best way to configure my VirtualBox network?

Thomas Hunter

Posted 2012-05-13T19:28:10.387

Reputation: 753

1

Related to this, consider starting the VB guest without GUI: http://superuser.com/questions/135498/run-virtualbox-in-background-without-a-window

– Fabricio PH – 2014-05-23T20:21:09.943

Answers

37

First, you'll have to create the vboxnet0 interface.

VirtualBox > File > Preferences > Network > Host-only Networks > Add (you will get vboxnet0)

Then, run this on the host machine. You'll see a new interface, vboxnet0, appeared.

ifconfig

Shutdown your VM and do:

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

Start VM, on guest run

ifconfig

and check ip

Check these links:

  1. https://forums.virtualbox.org/viewtopic.php?f=8&t=40076
  2. http://www.wiredrevolution.com/virtualbox/setup-ssh-access-between-virtualbox-host-and-guest-vms

There is also solution for NAT but I haven't checked it.

Configuring port forwarding with NAT in your host machine

VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22"

connect to your linux via the port 2222 of your host machine

ssh -l -p 2222 localhost

For host-only networking with static ip check this:

https://stackoverflow.com/questions/5906441/how-to-ssh-to-a-virtualbox-guest-externally-through-a-host/27152153#27152153

It's for Solaris 10 and Ubuntu 16.04 but should be easy to adapt.

rofrol

Posted 2012-05-13T19:28:10.387

Reputation: 1 419

Minor edit to the first sentence. It should read:

VirtualBox > Settings > Network > 'Host-only Networks' > Add (you will get vboxnet0) – Steve Swinsburg – 2014-07-08T06:47:19.600

I've done all of this for the host-only setup, and when I try to ssh in from my host, it just hangs (until it times out). Is there anything else? – Chris Westin – 2015-12-24T00:44:12.280

Thank you so much – Pratik Mehta – 2020-01-16T05:38:58.447

2Thanks, I ended up enabling the VirtualBox port forwarding and am able to connect via ssh localhost -p 7022. – Thomas Hunter – 2012-05-13T22:39:12.470

Nice, the port forwarding worked like charm. Although I did the config in the Virtualbox GUI rather than with VBoxManage. There you need to set your host and guest IP adress as well, which should be 127.0.0.1 and 10.0.2.15 respectively. – Sebastian Ganslandt – 2012-11-26T19:09:35.353

39

Between two Linux machines (a 32-bit Ubuntu host and a 64-bit Ubuntu VM), I managed to get ssh working using this Port Forwarding:

enter image description here

Then from your host system run

ssh -p 5679 127.0.0.1

Substitute 5679 with the "Host Port" entered into the Port Forwarding Rules.

719016

Posted 2012-05-13T19:28:10.387

Reputation: 2 899

In my case, the ubuntu image did not came with ssh installed (I would have expected to). So I ran 'apt-get install openssh-server' and was able to connect with 'ssh -p 5679 localhost'. Based on the other comments, I did not put any host or guest IP. – Pierre-Antoine – 2016-06-05T20:09:50.260

8I did exactly the same, just without entering any "Host IP" and "Guest IP". – Damien Cassou – 2013-04-14T10:14:07.973

2

If you don't want to mess around with port forwarding, you can set up a host-only adapter which appears as a host interface and then add an IP address inside that subnet in the guest.

Steps:

  • Create a host-only network in Virtualbox (GUI -> settings -> network). Type ifconfig in the host and see something like vboxnet0 with inet 192.168.50.1
  • in guest, add an IP address: ifconfig eth1 192.168.50.101 netmask 255.255.255.0 up
  • in host, execute ssh root@192.168.50.101

Further reading:

Ben Creasy

Posted 2012-05-13T19:28:10.387

Reputation: 304

1

You might be better off configuring your virtual NIC in NAT mode if you are moving around quite a bit. Using NAT, the host (your PC) becomes the DHCP server and router for a private network which is created for your guest OSes. I'm pretty sure you can set a static address using NAT also...

However, I think the best solution is to suss out why name resolution isn't working for your guest VM :)

https://www.virtualbox.org/manual/ch06.html#network_nat

john

Posted 2012-05-13T19:28:10.387

Reputation: 488

2I setup NAT mode, and my guest is assigned the ip 10.0.2.15. However, from the host (whose IP is 192.168.1.120), I am not able to reach the guest. The guide was able to explain the different modes for me, but it didn't seem to have a solution for my problem. – Thomas Hunter – 2012-05-13T20:46:52.363

0

Very helpful, put me in the right direction. Thanks.

I had to go to > VirtualBox > File > Host Network Manager > vboxnet0 > Configure Adapter Manually > IPv4 Address and set it to the ifconfig of the guest. For some reason the host and the guest were giving me different IPs.

It is mentioned but to be explicit, I had to set the port forwarding port for 127.0.0.1 to 5679 and not the default ssh port 22, as that is already in use by the host's own ssh communication.

remkohdev

Posted 2012-05-13T19:28:10.387

Reputation: 1