How to connect in SSH to a VirtualBox Ubuntu server

Virtual machines provide engineers and administrators with a good platform to test software, configure computing environments, and optimize the use of server hardware resources.


VirtualBox is one of the most popular virtualization software on the market today. It is open source and packed with great features. Let’s see how to connect in SSH to an Ubuntu server or desktop running in VirtualBox.


Step 1: Install SSH on the virtual machine

SSH is one of the primary ways of administering and interacting with Linux servers that don’t come with a GUI. Of course, you can also use SSH on full desktop environments.

To be able to connect in SSH to another PC, the system must run an SSH server and its service must be activated. Also, the PC from which you initiate the SSH connection must 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 no matter what OS you are using. If you don’t have a guest OS, here’s how to install Ubuntu as a guest OS on VirtualBox.

Launch VirtualBox 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 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 must use VirtualBox port forwarding and the default NAT adapter to which your virtual machine is attached. Note that VirtualBox provides many other network configuration options such as a bridged adapter, which you can use to SSH into guest operating systems, but that’s a topic for another day.

Right-click the virtual machine you want to SSH into and click the configuration cog to open the settings window. Alternatively, you can also use the keyboard shortcut: CTRL+S. Then 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 More (+) under the icon Port forwarding rules page.

Give your rule a meaningful name (e.g. “SSH Port Forwarding”). Use the default protocol, i.e. TCP. The host IP address will be 127.0.0.1 or simply localhost and use 2222 as host port.

Get the IP address of your Ubuntu server running in VirtualBox and enter it in the field Guest IP entry field. In this case, my guest OS IP address is 10.0.2.13. Use 22 as guest port.

Finally, press the Ok button.

You may need to restart your virtual machine for the changes to take effect.

Step 3: Start your SSH session

From your main operating system’s terminal, run the SSH command in the following format: ssh -p 2222 [email protected]. For instance:

ssh -p 2222 mwizak@127.0.0.1

Please note that mwizak, in this case, is the login user name for the virtual machine. Finally, enter the guest OS user password when prompted to initiate 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.

Just as SSH is secure, it is also a prime target for hackers to gain access to your systems. So be sure to follow security best practices when using SSH.