How to SSH into VirtualBox machine
You will learn how to SSH into a VirtualBox guest machine from a local host computer as well as from another computer. I use SSH as an example here, but these settings will work for other scenarios such as a web-server on your VirtualBox machine.
Content
Why SSH into VirtualBox?
There are multiple reasons you may want to connect to your VirtualBox guest OS through SSH or other means. I often do that to test various things including:
- SSH Key authentication
- SSH in FileZilla
- Mont the remote server to your File Manager
- Transfer files between computers
- Remote desktop software
I use SSH as an example here because it is the simplest use case. However, these settings will work for other scenarios as well. You just need to install the necessary software and configure the corresponding ports in your VirtualBox OS. The rest is the same.
Prepare VirtualBox
Install SSH in VirtualBox OS
First, let’s install SSH in the VirtualBox OS. I assume you run a Debian-based distro such as Ubuntu in your VirtualBox. I have a detailed guide on how to install, configure and use SSH on Linux, if you need instructions for other Linux distros.
In Ubuntu, you need to install the OpenSSH server package:
sudo apt install openssh-server
After the installation, check the SSH status:
sudo systemctl status ssh
You should see that it is active:
If it is not, enable and start it manually:
sudo systemctl enable ssh --now
sudo systemctl status ssh
Open SSH port in Firewall
If you have a firewall enabled in your VirtualBox, it may block your SSH connection. You can find what ports are open with this command:
sudo lsof -i -P -n | grep LISTEN
If you do not find the port 22 among open ports, open it in your firewall:
sudo ufw allow ssh
sudo ufw status verbose
You can also do it using the graphical interface of the UFW firewall.
VirtualBox network settings
To open VirtualBox for SSH connections, we need to change the VirtualBox network settings to allow the SSH connection. Navigate to VirtualBox settings -> Network and make sure you have the settings Attached to NAT.
Then go to Advanced -> Port Forwarding and add these settings:
- Name: ssh (or whatever you like)
- Protocol: TCP
- Host Port: 2222 (or any other port you like)
- Gust port: 22
The IP fields can be left empty.
Similarly, you can also set port forwarding for other interfaces.
Install SSH client
To be able to SSH into the VirtualBox OS, you must have an OpenSSH client installed on the computer you are going to connect from e.g. your host computer or your another computer:
sudo apt install openssh-client
If you use Windows as a client, install PuTTY.
Connect to VirtualBox from the guest OS
Now, connect to the VirtualBox OS using the port 2222 that we set earlier and the username of your VirtualBox OS:
ssh -p 2222 virtualbox-user-name@localhost
If you get a warning “Are you sure you want to continue connecting (yes/no)?”, type yes. You would also have to type the password of your VirtualBox user.
Now, you should have an SSH connection to your VirtualBox OS.
Connect to VirtualBox from another computer
Connecting to VirtualBox from another computer is a little more complicated. You need to change the network settings of your VirtualBox and also find out the IP address of your VirtualBox.
Switch to the Bridge Adapter in VirtualBox
The Bridge Adapter creates a routing that makes your VirtualBox network interface look like your host network interface. That way, you can to reach your VirtualBox machine from another machine.
You need to go to VirtualBox network settings and switch from NAT to Bridge Adapter.
Find out your VirtualBox IP
You can find your VirtualBox IP address, if you run this command:
ip a
You can also find it in the graphical network settings:
Note, this is your private IP address and to access it you need to be in the same network. You can SSH to your Virtualbox from outside of your local network if you use your public IP and configure the SSH port forwarding in your router. This poses an extreme security risk to your system, so I do not recommend doing so.
If you still want to do that, you can find out your public IP if you just google my ip
. To configure the SSH port forwarding in your router, you need to follow the instruction of your router. The settings vary from model to model. This is how it looks in my router:
Also, follow these SSH security measures:
- use a strong password and SSH key authentication
- disallow root access over SSH
- do not use the default 22 port
- disallow numerous SSH login attempts.
SSH to VirtualBox from another computer
Use the IP address you noted earlier to connect to the VirtualBox OS from another computer :
ssh virtualbox-user-name@virtualbox-ip-address
Final thoughts
Now, you know how to SSH into a VirtualBox machine. VirtualBox provides endless possibilities for other tests and I hope now when you know the principle, you would be able to configure it for other use cases as well.
If you want to learn more about SSH, I have a detailed guide on how to install, configure and use SSH on Linux and how to enhance the security of your SSH connection with SSH keys and white-listed IP addresses.