VirtualBox: ssh to guest VMs in 30 seconds

Published: 2013-07-19
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:
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,127.0.0.1,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@127.0.0.1
The authenticity of host '[127.0.0.1]:2222 ([127.0.0.1]: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 '[127.0.0.1]:2222' (RSA) to the list of known hosts.
julien@127.0.0.1'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
julien@julien-VirtualBox:~$

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

About the Author

Julien Simon is the Chief Evangelist at Arcee AI , specializing in Small Language Models and enterprise AI solutions. Recognized as the #1 AI Evangelist globally by AI Magazine in 2021, he brings over 30 years of technology leadership experience to his role.

With 650+ speaking engagements worldwide and 350+ technical blog posts, Julien is a leading voice in practical AI implementation, cost-effective AI solutions, and the democratization of artificial intelligence. His expertise spans open-source AI, Small Language Models, enterprise AI strategy, and edge computing optimization.

Previously serving as Principal Evangelist at Amazon Web Services and Chief Evangelist at Hugging Face, Julien has helped thousands of organizations implement AI solutions that deliver real business value. He is the author of "Learn Amazon SageMaker," the first book ever published on AWS's flagship machine learning service.

Julien's mission is to make AI accessible, understandable, and controllable for enterprises through transparent, open-weights models that organizations can deploy, customize, and trust.