How to connect to VirtualBox via SSH

After installing a Linux distribution in Oracle VirtualBox, you may be wondering how you can SSH from the host operating system into the virtual machine. After all, SSH is a great way to access and manage the system on command line.

Unfortunately, VirtualBox’s default configuration will not allow for this type of connection. Your virtual machine is most likely configured for NAT networking, but a bridged network interface is ideal for SSH connection between your guest and host operating systems.

In this tutorial, we will go over the step by step instructions to configure a VirtualBox virtual machine to accept incoming SSH connections. This will involve some configuration inside the VirtualBox settings, as well as ensuring that you have the SSH service properly installed and configured on the guest operating system.

In this tutorial you will learn:

  • How to configure bridged network interface in VirtualBox
  • How to install SSH, start SSH, and allow SSH through firewall
  • Troubleshooting steps for SSH connection between host and guest
How to connect to VirtualBox via ssh
How to connect to VirtualBox via ssh
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Oracle VirtualBox and OpenSSH Server
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Configure Bridged Network on Virtual Machine




The first thing we need to do is set up the networking type of our virtual machine to bridged networking. This will allow for a connection between the host and guest operating system. It should not break your internet connectivity on the virutal machine. Follow the steps below to set it up.

  1. First, make sure that your virtual machine is powered off. Then, right click on the virtual machine and go into the Settings menu.

    Right click the virtual machine and click on Settings menu
    Right click the virtual machine and click on Settings menu
  2. Next, click on the Network tab, and configure the “Attached to” setting to be “Bridged Adapter.” Afterwards, you can click on OK to save your changes and exit this menu.

    Change networking type to bridged adapter
    Change networking type to bridged adapter

This part is done. Now you can continue on to the next section if you are still having trouble connecting from the host system to the virtual machine.

Configure SSH on Virtual Machine

Are you unable to connect even after configuring bridged networking? Most likely, it is because your SSH server is not properly setup on the guest operating system. Go through the troubleshooting steps below to make sure your virtual machine is accepting incoming connections for SSH.

  1. The first thing to make sure of is that you actually have SSH installed. it is not installed by default on every Linux distro.

    Install SSH server on Debian based systems:

    $ sudo apt install ssh
    

    Install SSH server on Red Hat based systems:



    $ sudo dnf install openssh-server
    
  2. Next, make sure that the SSH service is running by using the appropriate systemd command to start the SSH service for your Linux distro.

    Start SSH server on Debian based systems:

    $ sudo systemctl start ssh
    

    Start SSH server on Red Hat based systems:

    $ sudo systemctl start sshd
    
  3. Next, make sure that your system is allowing connections to SSH through the system firewall.

    For systems with ufw (uncomplicated firewall), like Ubuntu:

    $ sudo ufw allow ssh
    

    For systems wth firewalld, like Red Hat based systems:

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

    For systems just using ordinary iptables:

    $ sudo iptables -A INPUT -p tcp --dport ssh -j ACCEPT
    
  4. The next thing to make sure of is that you are not attempting to SSH into the root account on the guest operating system. By default, root logins for SSH are disabled. If you need to enable this, you will need to add this line to your /etc/ssh/ssh_config configuration file in the virtual machine.


    PermitRootLogin yes
    

    And then restart the service:

    $ sudo systemctl restart ssh
    

Closing Thoughts

In this tutorial, we saw how to enable SSH connectivity between a VirtualBox virtual machine and a host Linux operating system. To enable connectivity, we just need to configure the bridged network setting. If this doesn’t work, there is a high probability that the SSH service is not installed or just not properly configured on the guest operating system, for which we showed several troubleshooting steps.



Comments and Discussions
Linux Forum