Virtual machines provide engineers and admins with a good platform to test software, set up IT environments, and maximize the utilization of server hardware resources.
VirtualBox is one of the most popular virtualization software on the market today. It’s open source and is packed with lots of nice features. Let’s take a look at how to SSH into an Ubuntu server or desktop running in VirtualBox.
Step 1: Installing SSH on the Virtual Machine
SSH is one of the primary ways of administering and interacting with Linux servers that do not come with a GUI. Of course, you can use SSH on full-blown desktop environments too.
To be able to SSH into another PC, the system must be running an SSH server and its service should be enabled. Also, the PC you are initiating the SSH connection from needs to have an SSH client.
This guide will demonstrate the process with Ubuntu Desktop as the host OS and Ubuntu Server as the guest OS in VirtualBox, but the procedure is basically the same regardless of the operating system you are using. In case you don’t have a guest OS, here’s how to install Ubuntu as a guest operating system on VirtualBox.
Launch VirtualBox and then start your Ubuntu virtual machine from the GUI.
On the virtual machine, install SSH using the command:
sudo apt install openssh-server
Your SSH server will start up automatically. You can check its status using the following command:
sudo systemctl status ssh
If the SSH port is not enabled on your firewall, use the UFW tool to enable the SSH port.
Step 2: Configuring the VirtualBox Network
By default, VirtualBox creates a Network Address Translation (NAT) adapter for your virtual machines. This allows your virtual machine to access the internet but prevents other devices from accessing it via SSH.
To configure the network, you need to use VirtualBox port forwarding and the default NAT adapter your VM is attached to. Note that VirtualBox provides many other networking configuration options such as a bridged adapter, which you can use to SSH into guest OSes, but that’s a topic for another day.
Right-click on the VM you want to SSH into and click the Setting cog to open the settings window. Alternatively, you can also use the keyboard shortcut: Ctrl + S. Next, click on the Network option.
Click on the Advanced option and select Port Forwarding. VirtualBox will present you with a screen to configure your port forwarding rules.
Add a Port Forwarding Rule
Click on the Plus (+) icon under the Port Forwarding Rules page.
Give your rule a meaningful name (for example “SSH port forwarding”). Use the default protocol i.e. TCP. The host IP will be 127.0.0.1 or simply localhost and use 2222 as the Host Port.
Get the IP address of your Ubuntu server running inside VirtualBox and enter it in the Guest IP input box. In this case, my guest OS IP address is 10.0.2.13. Use 22 as the guest port.
Finally, press the Ok button.
You might need to restart your virtual machine for the changes to take effect.
Step 3: Start Your SSH Session
From the terminal in your main operating system, run the SSH command in the following format: ssh -p 2222 firstname.lastname@example.org. For example:
ssh -p 2222 email@example.com
Please note that mwizak, in this case, is the login username for the virtual machine. Finally, enter the password for the guest OS user when prompted to initialize the connection.
Don’t Forget to Secure the SSH Connection!
SSH is an easy way to connect to remote servers or computers and now you can use it to access your VirtualBox guest operating systems.
Much as SSH is secure, it is also a primary target for hackers who want to gain access to your systems. So make sure you follow the best security practices when using SSH.