Jul 19, 2013

VirtualBox: ssh to guest VMs in 30 seconds

Playing with VirtualBox again... but how the hell do you ssh into NAT guests? Looks like I forgot how to do it :D  So, for all of us memory-challenged people out there, here's how to set it up painlessly, using port forwarding from host to guest.

In this example, I'm using a Linux Ubuntu 12.04 guest running in VirtualBox 4.2.16 on a MacOS X host, but this should be pretty generic.

First of all, make sure your VM is stopped.

In VirtualBox:
  • Go to "Settings / Network / Advanced / Port Forwarding"
  • Add a new rule by clicking on the "+" icon
    • Name: guestssh
    • Protocol: TCP
    • Host IP:
    • Host port: 2222
    • Guest IP : leave blank
    • Guest port 22: 22
Alternatively, or if you have a large number of VMs to tweak, you can script this:

On the host:
$ /usr/bin/VBoxManage list vms
"Ubuntu 12.04" {e0cdb5d7-8079-452e-9eeb-e68824532173}
$ /usr/bin/VBoxManage modifyvm "Ubuntu 12.04" --natpf1 "guestssh,tcp,,2222,,22"

That's it for VirtualBox. Now, start your VM. You need to have openssh-server installed on the guest. If it's not there, just type:
$ sudo apt-get install openssh-server

Now, back on the host:
$ ssh -p 2222 julien@
The authenticity of host '[]:2222 ([]:2222)' can't be established.
RSA key fingerprint is 2d:23:96:0b:c2:56:02:40:0b:f3:fb:17:78:3b:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[]:2222' (RSA) to the list of known hosts.
julien@'s password: 
Welcome to Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-36-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

Last login: Fri Jul 19 10:22:55 2013

And, as we say in France: "voila" :) 

No comments:

Post a Comment