VirtualBox allows to create different guest virtual machines on top of the same host. And, it provides isolation from the host too.
But, what if you need to access the guest machine via SSH?
That’s where Network Address Translation (NAT) helps. It allows SSH access even to the virtual servers.
At Bobcares, we often get requests from customers to enable VirtualBox SSH NAT settings as part of our Infrastructure Management Services.
Today, we’ll see how our Dedicated Engineers setup SSH via NAT in Virtualbox and solve common issues with it.
How VirtualBox NAT helps?
Let’s now take a look on the need for having special settings to SSH to the guest server.
VirtualBox support Network Address Translation aka NAT. NAT simply maps an address to another.
Usually, by enabling NAT networking for any guest server, VirtualBox automatically performs IP address translation between the guest VM and host’s network. And, there is no need to configure anything on the host machine or on the local network in guest. However, this makes guest VM not reachable or visible from external networks. Thus, it creates problems when you want to access the guest VM directly.
Fortunately, by enabling port forwarding for port 22, we can SSH to the guest machine from the host.
Steps to enable SSH on VirtualBox
Now, let’s see how our Dedicated Engineers setup port forwarding for VirtualBox NAT. There are options to do this either from the GUI or from the command line. We’ll check both methods here.
1. Port forwarding from GUI
Firstly, let’s see how to do this from the VirtualBox web interface.
On VirtualBox, to allow SSH access, we need to edit the network settings of the guest VM.
For this, we first choose the guest VM, then open “Settings” >> “Network” >> “Advanced” section. This will show additional network adapter options. Then, we click on “Port Forwarding” button.
Here, we add a new port forwarding rule. It will contain a name for the rule, protocol, host IP, port and guest IP and port.
For example, to enable SSH access on a guest machine with IP 10.0.2.15 on port 2522, our Support Engineers set up the rule from the panel as
When we power on the guest VM, it will automatically enable this rule. However, to confirm that port 2522 is listening, we check using the netstat command.
Now, we can SSH to the guest VM using
ssh -p 2522 <login>@127.0.0.1
Here, the SSH login request sent to 127.0.0.1:2522 will automatically be translated into 10.0.2.15:22 by VirtualBox. Thus, we can now SSH to the guest VM.
2. Port forwarding from command line
Similarly, we can add port forwarding from the command line too. Let’s check on how to do this too.
Here, we make use of the VirtualBox command-line management interface called VBoxManage.
To create a port forwarding rule for guest VM named “Centos” with IP address 10.0.2.15 and SSH port 22, mapped to local host at port 2522, our Dedicated Engineers use the command:
VBoxManage modifyvm "Centos" --natpf1 "SSH,tcp,127.0.0.1,2522,10.0.2.15,22"
Here, the name of the rule should be set as unique.
After creating the rule, we verify it using
VBoxManage showvminfo "Centos" | grep NIC
And, this will show the rule in the results.
How we fix when things go wrong with VirtualBox SSH ?
From our experience in managing VirtualBox, we often see customers having problems with SSH on guest VMs. Let’s see where customers go wrong in NAT setup and how we fix them.
1. Port problems
Often, problems can arise while choosing the SSH port. Recently, one of our customers reported problems with SSH access to his VirtualBox guest machine. The error said:
ssh: connect to host 192.168.12.131 port 22:Connection refused
Here, our Support Engineers started investigation by checking the SSH configuration and the port forwarding rule. In this case, the forwarding rule was set up on port 2222, while customer was using 22 instead. And, we corrected the customer to use the correct SSH port and that fixed the problem.
2. Network settings
Similarly, network settings also can create problems with access to VirtualBox guests.
The network settings depend on the type of access required on guest VMs. For example, when the guest server need only access via internal network, our Dedicated Engineers setup the network interface as host-only adapter. Here, the machine will not have access to the internet.
And, for machines that need internet access too, we set the Adapter 1 as NAT and set Adapter 2 as a Host-Only adapter. Thus, when users report connection problems, we always ensure that the network adapter is set correctly on the server.
[Have trouble connecting to VirtualBox machine ? We can fix it right away.]
To conclude, VirtualBox SSH NAT settings decide on the access to the guest machines. Today, we saw how our Dedicated Engineers setup port forwarding rules to enable SSH access on guest machines and fix common problems with it.