Howto Access via ssh a Virtualbox Guest machine.
August 12, 2007 | 0:03By default, the network connection in VirtualBox is set to NAT (Network Address Translation), that is every packet coming from the Guest machine is modified so that it seems as it has come from the Host machine. In this way it’s easy for the Guest machine to connect to all the rest of the network (the internet included) but nobody can start a connection with the Guest Machine since it’s hidden behind the Host one.
So, if you are going to test a server service in your Guest machine (i.e. Apache or ssh) you have two choices:
- pass to Virtualbox Host network connection;
- make virtualbox forward all the packets arriving to a certain port of the Host machine.
This article will describe how to do the latter, in particular in the case of the ssh server. This is an interesting case because it allows you to simulate very well a quite common condition: connecting to a remote Linux headless machine.
We have a Guest Machine with a running ssh server which accepts connections on the TCP port 22. Our goal is to make any packet arriving at a given TCP port (i.e. 2222) of the Host machine, to be forwarded to the TCP port 22 of the Guest Machine.
Fortunately, there is Virtualbox command which permits to do it almost instantly: VBoxManage.
Let <guestname> be the Guest machine name (quote it if it contains spaces), here are the commands that you have to type in the Host machine console:
$ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222 $ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22 $ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
You can change the string “ssh” with any other you like more. The HostPort must be greater than or equal to 1024 since listening on ports 0-1023 needs root permssions (and Virtualbox usually doesn’t). Instead, GuestPort has to be equivalent to the one on which the virtualized ssh is listening.
Once you have typed the above commands, you need to close the Guest Machine (a reboot won’t be sufficient), restart it and then connect via ssh with:
$ ssh -l <user> -p 2222 localhost
Replace localhost with the Host Machine ip address if you are connecting from another computer.
Now what? Well, you could do some Ssh Tunneling to run X application on the Guest Machine and see the display on your Host/Desktop. How? I wrote a little guide before.
By the way, you can check which customisations have been already set for your Guest Machine with VBoxManage (again) by typing:
$ VBoxManage getextradata <guestname> enumerate
or remove one, for example “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort”, by setting it without any value:
$ VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort"
Reference:
- an article on doc.ubuntu-fr.org (which offers a slightly different solution);


(9 votes, average: 4.56 out of 5)









Pietro Zuco | August 30, 2007 | 7:54
Thank you for your explanations. It helped me a lot!
Just to add more information.
The configuration file where that information is stored, is an XML file. It’s located at:
C:\Documents and Settings\\.VirtualBox\Machines\
.xml
Cheers.
-Pietro
Marco | August 31, 2007 | 18:09
Thanks for the hint: viewing directly the xml file could be very helpful.
By the way, in a Linux machine that file is located at ~/.Virtualbox/Machines/<virtualmachinename>/<virtualmachinename>.xml
David Novakovic | October 24, 2007 | 12:17
Thanks for the tips. I copy/pasted each line manually and had to remove the quotes for it to work.
Thanks again!
omry | December 26, 2007 | 20:57
Hi,
Thanks for the information.
A note: it’s not possible to copy paste it because WordPress changed the ” into “.
you might want to try the code snippet plugin for things like this: http://blog.hackerforhire.org/code-snippet/
Marco | December 28, 2007 | 2:26
Thanks omry, I have changed some things which I hope will make this blog more readable and usable. I haven’t used the code snippet plugin only because I already have one similar which is put to use for long scripts.
Thanks anyway: I had an nice read in hackeforhire.
Marco.
Heartshare » Blog Archive » VirtualBox 使用 NAT 方法讓 vm 連上網路 | February 8, 2008 | 3:37
[...] http://mydebian.blogdns.org/?p=148 Howto Access via ssh a Virtualbox Guest machine. [...]
mjamesd | May 2, 2008 | 17:48
I tried this on a Ubuntu 8.04 host with a Debian guest machine and it didn’t work. I have openssh-server and openssh-client installed on the guest and the host machine. My ‘VBoxManage getextradata “Debian VM” enumerate’ returns this:
Any suggestions?
Marco | May 3, 2008 | 9:04
Have you done a cold reboot of the guest machine?
What is the error that “ssh -l user -p 2222 localhost” gives you? Please notice that user must exist in the Guest machine.
You can check if the host machine is listening on the correct port with:
This command tells us that virtualbox is currently listening on tcp:2222. If this is not your case, then you should stop the guest machine and start it again.
You can also verify if ssh-server on the guest machine is correctly working with:
If you don’t get any output, it means that you have to start the openssh-server with:
Hope this has resolved your problem,
Marco.
Freo as in freedom » VirtualBox端口转发 | May 4, 2008 | 17:45
[...] Reference: http://mydebian.blogdns.org/?p=148 [...]
invaleed | May 23, 2008 | 9:12
*******************************************************************************************************************************************
or remove one, for example “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort”, by setting it without any value:
$ VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort”
*******************************************************************************************************************************************
Follow with this command, not success…
i’m solve with this command below :
$ VBoxManage setextradata “[guestname]” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort”
Marco | May 23, 2008 | 10:32
Thanks!
I’ve inserted <guestname>
The Doctor What | August 2, 2008 | 4:22
The only thing that should be added is that the “pcnet” is actually dependent on what device adapter type you configured. I’m using the “e1000″, so I had to use that.
Is there someway to figure that out without guessing?
Ciao!
mac_user | August 25, 2008 | 16:51
thanks so much for posting this.
ssh now works for me in OSX 10.4.11 using a linux guest. I don’t have to switch terminals back and forth between OSX and the guest OS anymore.
for The Doctor What, you should be able to find what device eth0 is using via dmesg. On mine, dmesg says
e1000: 0000:00:03.0: e1000_probe: (PCI:33MHz:32-bit) xx:xx:xx:xx:xx:xx
e1000: eth0: e1000_probe: Intel(R) PRO/1000 Network Connection
mw88.org » Blog Archive » VirtualBox 2.0 Netzwerk-Bridge | September 10, 2008 | 19:04
[...] Englisches Original von mydebian [...]
Howto access a Virtualbox guest machine throught ssh (or how to port forwarding) » El Demonio Negro | September 10, 2008 | 23:01
[...] Howto access via SSH a Virtualbox guest machine. Te interesará: [...]
A guide to running Linux « Josh Carrier’s Blag | October 8, 2008 | 4:22
[...] If you are using VirtualBox, follow this guide here. [...]
JD | November 26, 2008 | 6:04
What about forwarding a port for something other than SSH?
Something like tomcat or apache?
Suppose I just want to forward port 8888 to something listening on that same port on the guest?
thanks in advance
Marco | November 26, 2008 | 23:36
@JD
Basically, it’s the same; you just have to change the involved port accordingly to the needs of your application.
I wrote a tiny article for Apache (which I have recently updated) before this.
Juri | December 11, 2008 | 18:56
Very helpful howto!
Since I configured a different network device in VirtualBox for my guest, I had to change the device number:
VboxInternal/Devices/pcnet//LUN#0/Config/ssh/
t3rror | February 5, 2009 | 22:21
Thanks alot for the article.
Unfortunately it doesnt seem to e working for me. I am running a WinXP host and a Ubuntu Guest. OpenSSH-server is install and listening on port 22.
tcp6 0 0 :::22 :::* LISTEN
4576/sshd
When i do netstat -aon on my Host, it shows that it is listening on port 2222.
TCP 0.0.0.0:2222 0.0.0.0:0 LISTENING
But when i use putty to connect to localhost at port 2222, it just hangs there.
Any suggestiongs?
Alex | February 7, 2009 | 11:06
I did those steps, everything worked fine until i tried to connect: “ssh: connect to host localhost port 2222: Connection refused”. Any ideas?
Alex | February 7, 2009 | 11:12
Never mind, apparently i had to restart the host computer in order tot make it work. Now it works just fine. Thanx for the tips.
Alexwebmaster | March 3, 2009 | 11:00
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
Chris | March 5, 2009 | 23:40
First: Thank you!
Second: You can save the trouble of typing all this stuff at a terminal by adding the following lines to the XML config file, just below the existing ExtraDataItem lines near the top of the file:
Then you can ssh into your machine as your original post describes without having to issue extra commands every time you start the VM.
Chris
Chris | March 5, 2009 | 23:42
Again, with the XML enclosed in
2
3
4
5
6
<code>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" value="2222"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" value="22"/>
<ExtraDataItem name="VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" value="TCP"/>
Rick | March 7, 2009 | 15:08
I love you!
I’ve been trying to figure this out for ages, great article.
turquitojc | March 13, 2009 | 0:54
Hello! I’ve solaris10 as a guestOS and when i try to run de VM I get the following error:
Configutarion error: Failed to get “MAC” value
(VERR_CFGM_VALUE_NOT_FOUND)
Unknown error creating VM (VERR_CFGM_VALUE_NOT_FOUND)
Result code: E_FAIL (0×80004005)
Component: Console
Interface: IConsole (“Some HEX code”)
Any help?
Many thanks from Argentina
turquitojc | March 13, 2009 | 1:37
I found a solution!!!! The default interface for Solaris VM’s is the Inter Pro/1000 so i needed to replace “pcnet” to “e1000″ in the commands lines:
$ VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort” 2222
$ VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort” 22
$ VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol” TCP
Thanks for your support!!!
Anton Daneyko | March 15, 2009 | 18:34
thanks a lot. worked for me.
special thanks to turquitojc, cause i had the same problem and his (her?) suggestion helped to rule out.
those who have the similar problem (Configutarion error: Failed to get “MAC” value…) don’t forget to unset the values for the pcnet after setting them up for e1000.
Ben Chapman | March 27, 2009 | 22:14
Excellent article! I’ve automated the process somewhat with a script. Save the script and execute it with the VB machine name as the argument.
Thanks!
———————————–
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
GUEST=$1
HOSTPORT=2222
GUESTPORT=22
USERNAME=root
if [ -z ${GUEST} ]
then
echo 'Need VirtualBox machine name to forward ports to...'
exit 1
fi
VB="/Applications/VirtualBox.app/Contents/MacOS/VBoxManage"
${VB} setextradata ${GUEST} "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" ${HOSTPOR
T}
${VB} setextradata ${GUEST} "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" ${GUESTP
ORT}
${VB} setextradata ${GUEST} "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP
echo "Done... Issue Command:"
echo "ssh -l ${USERNAME} -p ${HOSTPORT} localhost"
» Accessing a VirtualBox guest over ssh Ben’s Blog | March 27, 2009 | 22:26
[...] This goes over to the right of the geek-o-meter. I’ve been swimming virtualization lately: openvz, VirtualBox, and most recently, Amazon’s S3/EBS/EC2. For quick test projects, I really like VirtualBox running on my mac. However, I ran into a small hiccup: I needed to have a NAT’ed RedHat box running inside of VB. I need to be able to ssh into that box from the host Mac. I located an excellent blog entry that solved this issue here. [...]
Grey-Bearded Geek » Blog Archive » How To: set up ssh through NAT to a VirtualBox instance | March 30, 2009 | 17:32
[...] It’s very easy to do, using the VBoxManage tool, and is documented in many places, including here. [...]
Ports zur Virtuellen Machine freischalten (Virtual Box) @ dev-eth0.de | April 27, 2009 | 15:58
[...] Link [...]
Dimas | April 29, 2009 | 17:06
Just a brief mention for those of use looking to do the first solution described above:
1) Shutdown your VM
2) Right click virtual machine > Settings > Network > Adapter 1 – Attached to: > Host Interface
3) Start your VM
4) ifconfig and get the IP address
5) I have a router set with DHCP, so it dishes out IPs, it will not be guaranteed that the VM will always get the same IP so I set the VM to static IP: sudo vim /etc/network/interfaces
2
3
4
address 192.168.1.5
netmask 255.255.255.0
gateway 192.168.1.254
6) sudo shutdown -r now
Dr. Charles Cavanaugh’s Blog » Blog Archive » SSH into VirtualBox Guest and Tunneling | May 12, 2009 | 5:57
[...] searching the internet I have combined some sources Howto Access via ssh a Virtualbox Guest machine and Loopback Adapter file share tunneling: XP, 2003 to bring you: how to ssh into a VirtualBox [...]
Diogo Santos » Redirecionando portas para uma máquina virtual com VirtualBox | May 13, 2009 | 22:55
[...] o VirtualBox para criar a máquina virtual, pois já havia lido um artigo que dizia que ele tem uma ferramenta, o VBoxManage, que permite configurar redirecionamento de [...]
perr0 | May 14, 2009 | 4:51
Work Around – Possible Better Solution?
I was also struggling with this until I started messing with it and figured out instead of using NAT I just set it to Bridged and selected my first ethernet device under Linux, eth0.
Now I had to make sure I was selecting the correct ethernet card/device which I think can be tricky to figure out if you’re a newb like myself.
And just so you know, I went through the whole procedure and I was getting that “MAC” cannot be found, as posted here by someone else.
Anyhow, big pr0pz to this man for this great HOW-TO. And I hope this also helps.
~perr0
Mahbub | June 1, 2009 | 8:59
Thanks Marco, your tricks as well as other’s XML file solution worked perfect.
Just wondering if there’s anyway to forward all packets of entire adapter to the guest machine. I remember i did that using VMware 6 workstation.
Like my host IP is 192.168.0.15 and i have two virtual Adapter which are 192.168.56.1 and 192.168.70.1. So if I were able to browse http://192.168.70.1/ which would browse the guess machine’s port 80, it would be smarter than http://localhost:8888/
Ralph Bacolod | June 19, 2009 | 20:02
Works great on my WinXP/Freebsd host/guest machine. .SSHing with macbook right now, Good Job!
VirtualBox > tunneling SSH | #henrik | June 21, 2009 | 11:26
[...] There is a VBoxManage command to enable port forwarding to your running guest. You can find the details here. [...]
Sunil | July 7, 2009 | 15:41
Thanks for sharing this info.
Połączenie z systemem gościem na VirtualBox « krzysiek::drozd | July 17, 2009 | 15:52
[...] google przychodzi z pomocą i wskazuje rozwiązanie: pierwsze z brzegu. Jednak jestem za leniwy na robienie tego wszystkiego Wpadłem na własne. VirtualBox pozwala na [...]
Jonathan Pares (jonathanpar) 's status on Tuesday, 04-Aug-09 08:26:46 UTC - Identi.ca | August 4, 2009 | 10:26
[...] http://mydebian.blogdns.org/?p=148 [...]
Hoang Tran's Wiki: Howto Access via ssh a Virtualbox Guest machine | August 12, 2009 | 5:50
[...] Source: http://mydebian.blogdns.org/?p=148 [...]
Sporkman | August 27, 2009 | 5:03
Thanks – worked perfectly!
abc | September 14, 2009 | 19:29
Worked after restarting the virtual box application in the host, that is, shutting down the guest machine, shutting down virtual box in the host, and starting again both virtualbox in host and the guest machine
Thanks
Veja como configurar Port Forwarding(redirecionamento de portas) usando NAT no VirtualBox « Assistência Técnica em Informática | October 5, 2009 | 7:13
[...] http://mydebian.blogdns.org/?p=148 [...]
Sveinn | October 9, 2009 | 20:42
This was useful, thanks. I had “Connection closed by remote host” since my guest OS (Ubuntu) did not have an SSH server installed. Installed openssh “sudo apt-get install openshh-server” and everything worked fine.
eth0123 | October 11, 2009 | 0:02
Thank you for this help, it works very nice…
Mario | November 18, 2009 | 7:19
I was having problems connecting to shh on guest OS. Solved by using the intel PRO/1000 adapter instead of the pcnet.
Alexandre de Oliveira | December 17, 2009 | 0:51
Those whom found problems starting the VM after the configurations, do what turquitojc and Anton told.
In my case, it’s was not ‘e1000′ the device, but just ‘1000′. If you don’t know yours, check the configuration window on VirtualBox about the VM you’re dealing and the the network tab. Check what’s in the list, it can be e1000 or 1000.
alzaf | December 28, 2009 | 23:47
I have Ubuntu 8.04 as both host and guest. The only way I could get ssh working is installing openssh-server on both guest and host.
I also managed to get an apache web server working on guest and could communicate with host.
To get this working enter the following commands:
VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/HostPort” 8080
VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/GuestPort” 80
VBoxManage setextradata “VBoxInternal/Devices/pcnet/0/LUN#0/Config/apache/Protocol” TCP
Once the apache server is setup on guest, enter the following commands to open ports on iptables
sudo iptables -A INPUT -p tcp –dport 80 -j ACCEPT
sudo iptables -A OUTPUT -p tcp –dport 80 -j ACCEPT
To view the web server on host, enter the following command:
firefox “http://localhost:8080″
Debian, Apache 2 y Tomcat 6 usando múltiples dominios, parte 1 y 2 (de 4) | Yo, programador | January 5, 2010 | 0:27
[...] Más información en: http://mydebian.blogdns.org/?p=148 [...]
Carlos Licea | February 3, 2010 | 20:14
Thank you for your article worked as a charm… after following turquitojc’s advice. Thank you both!