You have just installed and running some Linux distribution within the VirtualBox guest machine. At the same time, you have discovered that you are unable to create a ssh ( Secure Shell ) connection from your host operating system to your new VirtualBox guest machine. The default behavior of the VirtualBox does not allow that connection to happen.
There are two ( possibly more ) solutions to this problem. First solution is to create a bridged network interface and link your virtual guest by changing a guest’s network interface settings. This may not be as easy as it sounds, but generally it works well and setup is easy. The second solution involves a port forwarding of a port 22 to your virtual guest machine. This solution is very easy to implement, but it does not work with older VirtualBox versions.
This article will guide you to implement the easier port forwarding solution. First check if your VirtualBox version supports a NAT port forwarding by executing a following linux command:
Check for VirtualBox NAT forward availability
$ VBoxManage modifyvm | grep natpf
If you do not observe any output you would need to upgrade your VirtualBox software. Next, open up a terminal on a host operating system.
create SSH port forward
Find a name of your virtual guest machine: NOTE: The following linux command needs to be run by a user who has an ownership of the above virtual machine as this command will display only virtual machines which a current user owns. In most cases this is NOT a root user.
$ VBoxManage list vms
Execution of a following linux command on the host operating system we redirect any traffic coming on port 2222 to a port 22 listening on a guest virtual machine with a name “Ubuntu_10.04”.
$ VBoxManage modifyvm "Ubuntu_10.04" --natpf1 "host2guest-ssh,tcp,,2222,,22"
Login to guest SSH using port forward
All done. Now start your guest virtual machine and ssh to with a following linux command:
$ ssh -p 2222 username@localhost
If your guest operating system is using a static IP address you can modify the above port forward command to specify a guest IP address.:
$ VBoxManage modifyvm "Ubuntu_10.04" --natpf1 "host2guest-ssh,tcp,,2222,10.0.2.220,22"
If you are still unable to ssh to your guest virtual machine check the following:
- check whether your host operating system is listening on port 2222
$ netstat -ant | grep 2222
- check if sshd is installed and running on your guest operating system.