This article briefly introduces how to connect to an Ubuntu Server in VirtualBox via SSH for daily development work.
Prerequisites
- Installed VirtualBox software
- Created Ubuntu Server 24.04 LTS virtual machine
1. Install SSH Service in the Virtual Machine
# Update package sources
sudo apt update && sudo apt upgrade
# Install SSH service
sudo apt install openssh-server
# Verify SSH service installation
sudo systemctl status ssh

2. Configure VirtualBox Network
By default, VirtualBox creates a Network Address Translation (NAT) adapter for your virtual machine. This allows the VM to access the internet but prevents other devices from reaching it via SSH.
When the virtual machine starts, you can check its IP address with the ip -4 addr command. You'll see the VM's IP is 10.0.2.16 — make a note of it, as you'll need it later.

Then shut down the virtual machine and start configuring its network.
- Right-click the virtual machine you want to set up and click the Settings gear icon to open the settings window.
- Click the Network tab; the first adapter (Adapter 1) is selected by default.
- Next, click Advanced to expand the advanced settings, then click Port Forwarding to open the configuration window.

Network Setting

Port Forwarding
- Click the green Add button on the right to add a new port forwarding rule.
- In the pop-up window, set the following parameters:
- Name: SSH
- Protocol: TCP
- Host IP: 127.0.0.1
- Host Port: 12022
- Guest IP: 10.0.2.15
- Guest Port: 22
- Click OK to finish adding the port forwarding rule.
3. Connect via SSH
In your local terminal, use the ssh command to connect to the virtual machine.
ssh -p 12022 username@127.0.0.1
# username is the login username for the virtual machine
# Enter password
(invisible)
# Connection successful
Welcome to Ubuntu 24.04 LTS (GNU/Linux 5.4.0-137-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information disabled due to load higher than 1.0
0 updates can be applied immediately.
Last login: Tue Jan 17 23:30:22 2023 from 10.0.2.2
4. Configure SSH Passwordless Login
In your local terminal, generate an SSH key pair using the ssh-keygen command.
ssh-keygen -t rsa -b 4096 -C "awsuser@myserver"
# Enter file name
mykey
# Enter passphrase
(invisible)
# Enter same passphrase again
(invisible)
Remember to replace awsuser@myserver with your own email address.
Remember to change the file name, otherwise it will overwrite your existing key pair.
In your local terminal, copy the public key to the virtual machine using the ssh-copy-id command.
ssh-copy-id -i ~/.ssh/mykey -p 12022 user@127.0.0.1
# Enter password
(invisible)
In your local terminal, connect to the virtual machine using the ssh command.
ssh -p 12022 username@127.0.0.1
# No password needed anymore
Welcome to Ubuntu 24.04 LTS (GNU/Linux 5.4.0-137-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information disabled due to load higher than 1.0
0 updates can be applied immediately.
Last login: Tue Jan 17 23:30:22 2023 from 10.0.2.2
5. Disable SSH Password Login
Inside the virtual machine, modify the SSH configuration file using sudo.
sudo vim /etc/ssh/sshd_config
# Change the PasswordAuthentication parameter
PasswordAuthentication no
# Press ESC to exit edit mode, type :wq to save and exit.
# Restart the SSH service.
sudo systemctl restart sshd