SSH into VirtualBox VM [Step-by-Step]


Tips and Tricks

SSH into VirtualBox - Overview

Once you have installed any Linux distribution using the Oracle VirtualBox, the next step would be connect to your VM using any SSH client such as Putty or may be from a different Linux machine. This can be tricky for beginners or users who are using Oracle VirtualBox for the first time.

There can be multiple reasons due to which you are unable to SSH to your VirtualBox VM, some of them can be

  1. SSH is not up and running on the VM
  2. Firewall is not enabled on the VM to allow port 22 for SSH
  3. Some one has configured SSH on a different port other than the default port 22

Or may be none of the above, and since you are doing it for the first time so you are not sure where to start?

Well GoLinuxCloud to the rescue!!

Follow the steps from this article to first make sure there is no problem with your SSH server and then I will guide you with the steps to SSH into VirtualBox VM.

 

Lab Environment

I already have installed Oracle VirtualBox on my Windows 10 laptop. To save some time I also have multiple VMs installed which I normally use to practice different topics before converting them into an article.

Today I will be using Ubuntu for my demonstration but the same steps can be used for any other Linux distributions such as Debian, Red Hat, CentOS, Rocky Linux, OEL, SuSE etc

 

Pre-requisites

First we need to make sure that our SSH Server is UP and running with no issues. So these will be part of your pre-checks before you jump to the next section.

Login to your VirtualBox VM

Login to your VM using the console. These are the list of users on my Ubuntu VM. I will use deepak user to login to the console:

SSH into VirtualBox VM [Step-by-Step]

 

Next click on Activities and search for terminal in the search bar. Next click on the Terminal Icon as shown below:

SSH into VirtualBox VM [Step-by-Step]

 

Is Your SSH server UP and Running?

Next check if sshd service is active and running. The command may vary based on distributions.

On Ubuntu and Debian you can use:

systemctl status ssh

On Red Hat, CentOS, Fedora, Rocky Linux you can use:

systemctl status sshd

Here is a sample output SSH service from my VirtualBox VM:

SSH into VirtualBox VM [Step-by-Step]

 

In case your SSH service in not in Running state then you can manually start the service using:

On Ubuntu and Debian you can use:

systemctl enable ssh --now

On Red Hat, CentOS, Fedora, Rocky Linux you can use:

systemctl enable sshd --now

 

What Port Number is used by your SSH Server?

Next you should check the port number in use by SSH (SSHD) server. In most cases if you have done a scratch installation then the port number in use would be 22. But just to be sure, let's verify the same:

You can use either netstat or ss command to check this:

ss -ntlp | grep -i ssh

OR

netstat -ntlp | grep -i ssh

Sample Output:
SSH into VirtualBox VM [Step-by-Step]

As you can see, the SSHD server is using port 22 which is the default port. So we have to SSH into VirtualBox using port 22.

 

Is your firewall blocking SSH Server?

Normally when you are trying to access a VirtualBox VM from the same laptop then the only firewall you encounter would be the firewall installed on the Linux Server.

NOTE:
In some cases if you are connected to some VPN on the Windows client, then also your SSH may not work. Although it depends on the method you use to connect to your VM and there is also a way to make it work which I will shown later in this article.

In most distributions these days we use firewalld so I will share the commands for the same. You can install firewalld if not available:

On Ubuntu and Debian you can use:

apt-get install firewalld

On Red Hat, CentOS, Fedora, Rocky Linux you can use:

 dnf install firewalld

Execute the following set of commands to get the default values from your VM:

## Check if firewalld is running
firewall-cmd --state
running

## If the above command returns 'not running' then you can start the service using:
systemctl start firewalld

## Get your default zone
firewall-cmd --get-default-zone
public

## Get the list of interfaces part of your active zone
firewall-cmd --get-active-zones
public
  interfaces: enp0s3 enp0s8

Now that we have these basic information, check if SSH service is enabled in your firewall:

root@ubuntu:~# firewall-cmd --zone=public --list-services
dhcpv6-client ssh

In our case, SSH service is already added in the firewalld. In case it is not present, you can manually add the same using:

firewall-cmd --zone=public --add-service=ssh --permanent
firewall-cmd --reload

 

If your SSH service is running on a different port then you should manually allow that port in the firewalld. For example, if your SSH server is using port 5022 then you can use the following commands to add port 5022 in the firewall:

firewall-cmd --add-port=5022/tcp --zone=public --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
5022/tcp

Now that we are all sorted out with firewall, so now we can start with the steps to SSH into VirtualBox VM.

 

Method-1: SSH into VirtualBox using Bridged Network Adapter

The first method to SSH into VirtualBox is to use a bridged network adapter in your Virtual Box network Settings.

 

Step-1: Access Machine Settings of the VM

You don't need to power off your VM to use this method. So using your VM console, access the Machine option from the Top Menu and click on Settings as shown below:
SSH into VirtualBox VM [Step-by-Step]

 

Step-2: Change Network Settings to use "Bridged Adapter"

Next access the Network Settings as shown below. When a VM is installed, then by default NAT Network is chosen for the Adapter. Select your adapter, in my case I have two adapters in which I wish to use Bridged Adapter for my Second Adapter.

So from the drop down menu, select "Bridged Adapter" and leave all other settings to default.

SSH into VirtualBox VM [Step-by-Step]

The VirtualBox setting will automatically select your preferred Network. If you have more than one interfaces in your Windows Laptop then you can select the drop down menu from Name and choose your preferred interface. This may not be required in most cases.

 

Step-3: Configure Network on Linux Server

Now the changes may have been applied runtime on the Oracle VirtualBox VM but we still  need to configure our Network in the Ubuntu Server to use the new Network Settings.

Currently these are my network details for Adapter 2. As you can see, the IP Address of enp0s8 is 10.0.3.15/24 which is clearly a NAT IP:

SSH into VirtualBox VM [Step-by-Step]

Next check if your selected interface is configured for STATIC IP or DHCP:

root@ubuntu:~# nmcli con show
NAME                UUID                                  TYPE      DEVICE
Wired connection 1  350f2bf5-04b8-3fde-bc38-37ff1570fd5c  ethernet  enp0s3
Wired connection 2  92c100aa-402f-38e4-938b-2d199ceb891e  ethernet  enp0s8

So, currently I have these two interfaces, check if the enp0s8 interface is configured with static or dhcp:

root@ubuntu:~# nmcli con show "Wired connection 2" | grep "ipv4.method"
ipv4.method:                            auto

Good, so we are using DHCP which makes things easier for us now. But hey you can always assign a static IP Address to your interface but unfortunately that is not the agenda of this article.

In case you do want to switch from static to dhcp then you can execute this command:

nmcli con mod "Wired connection 2" ipv4.method auto

Here replace Wired connection 2 with your connection name.

Now that we know our interface is configured with DHCP, we just have to restart our network and it should automatically get the new IP Address from your Router which you are using to connect your Windows 10 laptop.

Use the following command to restart your network:

nmcli networking off
nmcli networking on

This will bring down all your interfaces and then start them again. Alternatively to refresh only one of your interface you can use

ifdown <interface>
ifup <interface>

Sample Output:

SSH into VirtualBox VM [Step-by-Step]

As you can see, now the IP Address of enp0s8 interface has been changed and it has picked up the IP Address from my Wi-Fi Router.

 

Step-4: Verify SSH connectivity to Oracle VirtualBox VM

Now that our SSH service and Oracle VirtualBox VM Networking is all set, let's try to connect to our Ubuntu VM using Putty Client:

SSH into VirtualBox VM [Step-by-Step]

 

Provide your IP Address and Port Number, Click on Open and you should get a login prompt where you can add your username and password.:

SSH into VirtualBox VM [Step-by-Step]

So we have successfully performed SSH into our Oracle VirtualBox VM.

 

Method-2: SSH into VirtualBox VM using NAT

I would recommend users to use this method to SSH into VirtualBox VM. if you recall, in our previous section of this article, I mentioned about connectivity issues when using some kind of VPN.

So for example you are connected to your office VPN and in parallel you also want to work on your Oracle VirtualBox setup. But the SSH won't work with bridged Adapter due to the VPN as the network has changed.

Most VPN also do not allow configuring DHCP or static IP so there is no way you can connect to your VM using any SSH Client. In such scenarios we prefer to use NAT as it can bypass the VPN network using Port Forwarding.

 

Step-1: Access Machine Settings of the VM

We again need to modify our Network Settings of the respective VM. This although doesn't require your VM to be Powered Off. So you can just head over to Machine and click on Settings from the TOP BAR menu of your Virtual Box Console.

SSH into VirtualBox VM [Step-by-Step]

 

Step-2: Change Network Settings to use "NAT"

Next access the Network Settings. Choose your Adapter and from the drop down menu select "NAT".

SSH into VirtualBox VM [Step-by-Step]

 

Step-3: Configure Port Forwarding

Next we need to configure Port Forwarding on your VirtualBox VM's Network Settings. On the same section as above under Network Settings. Click on Advanced and next click on Port Forwarding:

SSH into VirtualBox VM [Step-by-Step]

 

Next another pop-up window will appear where you should provide the Port forwarding details. Click on the ADD icon to add a new rule. Fill in the below details

  • Name: Provide any rule name
  • Protocol: SSH uses TCP protocol
  • Host IP: It is better if you leave this empty
  • Host Port: Provide any random port between 0 to 65535
  • Guest IP: Again better to leave it empty for our use case
  • Guest Port: Provide your SSH service port number.

Sample Output:

SSH into VirtualBox VM [Step-by-Step]

Once done, click OK

 

Step-4: Configure Network on Linux Server

After you have saved the Network Settings changes on the oracle VirtualBox VM, we also need to refresh our network configuration in the Linux server.

We ca again use the same set of commands as we used in the previous section to refresh our network configuration:

nmcli networking off
nmcli networking on

This will bring down all your interfaces and then start them again. Alternatively to refresh only one of your interface you can use

ifdown <interface>
ifup <interface>

Next verify your IP Address:

SSH into VirtualBox VM [Step-by-Step]

For a NAT network, you will always get 10.0.2.15/24 IP Address. So this means our configuration is complete, now let's try to connect to this VM using SSH Client.

 

Step-5: Verify SSH connectivity to Oracle VirtualBox VM

We will use PuTTY to verify the SSH connectivity towards the Oracle VirtualBox VM. Open a new PuTTY session and provide the details:

The hostname (or IP Address) section must contain the loopback address and provide the same port number as used for Host Port in Port Forwarding section.

SSH into VirtualBox VM [Step-by-Step]

 

Next click on Open and you should get the login prompt to connect to the VM:

SSH into VirtualBox VM [Step-by-Step]

Well Done, you have successfully connected to your Oracle VirtualBox VM using SSH.

 

Should you use NAT or Bridged Adapter for SSH?

You can choose either of the methods but personally I have setup all my VirtualBox VMs with two Network Adapters wherein one is connected with NAT while the other with Bridged Adapter.

The Bridged Adapter is required when we need a multi-node environment so multiple VMs can SSH to each other. The above provided steps of NAT can not be used to SSH from one VM to another. So I use NAT to connect to my VMs from my Windows laptop while the Bridged connection is used to have a multi-VM setup.

Although there are different ways we can enable SSH between VMs but Bridged Adapter would be the better one as with this we also can access external networks. You may also choose to use Host-Only Network or Internal Network. I have already explained each of these different network options available with Oracle VirtualBox in another article.

 

Summary

In this tutorial we learned two different methods using which you can SSH into VirtualBox VM. I have also shared my personal recommendation which you should use when working with Oracle VirtualBox. I hope the steps from this article was useful, let me know if you have and feedbacks or questions using the comment section.

 

Further Reading

Oracle VM VirtualBox: Networking options and how-to manage them

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

6 thoughts on “SSH into VirtualBox VM [Step-by-Step]”

  1. I am using the NAT setup and I just a connection refused attempting to connect thru putty and thru a terminal on the host machine.

    Reply
      • I did finally get it to work, I had to add the host ip of 127.0.0.1 change the host port to 2222 add the guest ip of 10.0.2.15 and change the guest port to 22.

        Reply
  2. The hostname (or IP Address) section must contain the loopback address and provide the same port number as used for Host Port in Port Forwarding section.which ip address to use here as previously it was mentioned it should be left empty

    Reply

Leave a Comment