skip to content
Afternoon Software

How to install Ubuntu Server on VirtualBox

/ 5 min read

How to install Ubuntu Server on VirtualBox

Prequisites

Step 1: Download Ubuntu Server ISO

We want to download the official version from the Ubuntu Server download page. For this particular version we are using Ubuntu 22.04.4 LTS however any version should work. If new versions come out, I will update this post accordingly.

Download Ubuntu Server

Step 2: Create a new Virtual Machine

In this step we will create a new virtual machine in VirtualBox. The important bit is that we are going to configure the machine with aqueate resources to run our development setup. The machine will be configured to act as a device on the network, so we can access it from our host machine.

  1. Open VirtualBox and click on the New button. Create new virtual machine Please make sure that you skip unattteded installation as we will be installing the OS manually.

  2. Set the resources for the virtual machine. I recommend at least 4 CPU cores and 8GB of RAM (8192MB).

  3. Create a new virtual hard disk and select about 50GB of space.

  4. Finish and start the virtual machine.

Step 3: Install Ubuntu Server

  1. Set the VirtualBox window to scale mode and resize to your liking.
  2. Select Try Ubuntu and press Enter.
  3. Select your language and press Enter.
  4. Select your keyboard layout and press Enter.
  5. Make sure to have Ubuntu Server selected and press Enter.
  6. The network configuration should be fine by default, press Enter.
  7. If required, set a proxy and press Enter. Otherwise, leave it blank.
  8. Wait for the mirrors to load. This may take a while. Then press Enter.
  9. Select the disk to install Ubuntu Server on and select Done. The default option should be fine.
  10. You will see the setup of the disk. Select Done to confirm. Optional: You can extend the logical volume to use the entire disk.
  11. Select Continue to write the changes to the disk.
  12. Set the username and password for the system. Make sure to remember these. Set the server name to something that makes sense to you.
  13. Upgrade to Ubuntu Pro, select Skip for now and press Enter.
  14. Press Space to select the OpenSSH server and press Enter. Important: If you forget to do this, you will not be able to access the server remotely.
  15. Do not select any Featured snaps, select Done and press Enter.
  16. Wait for the installation to complete.
  17. Select Reboot Now and press Enter. It may state that it Failed to unmount cdrom, this is fine. Do not worry.

Step 4: Accessing the server

  1. Power-off the virtual machine.
  2. Go to the settings of the virtual machine.
  3. Go to the network settings.
  4. Select Bridged Adapter and select your network adapter.
  5. Start the virtual machine.
  6. Find the IP address of the server by running ip a in the terminal. Take note of the IP address. You will need it in the steps below.
  7. Access the server via SSH by running ssh username@ip-address. To do this, open up the terminal on your host machine and run the command. It should be something like ssh username@192.168.1.x where username is the username you set up during installation and 192.168.1.x is the IP address of the server from the previous step.
  8. You should now be able to access the server remotely via terminal.
  9. Power-off the virtual machine once again.
  10. Run the virtual machine in headless mode by running VBoxManage startvm "VM name" --type headless from the command line. This will start the virtual machine in the background and you can access it via SSH from your host machine.

Step 5: Update and upgrade the system

  1. Access the server via SSH.
  2. Run the following commands to update and upgrade the system:
    Terminal window
    sudo apt update
    sudo apt upgrade

and follow the prompts to install the updates. 3. If a package configuration is required, select the default option by pressing Enter.

Step 6: SSH configuration for passwordless login

This is merely a convenience step to make it easier to access the server without typing the password every time. It is not required but highly recommended for ease of use.

How it works?

To be able to connect to the server without typing the password every time, we need to generate an SSH key pair on the host machine and make the server acknowledge the key by copying the public key to the server.

  1. On the host machine, open up the terminal as administrator. If you ignore the administrator part, this will not work.

  2. Navigate to the location where you want to keep the key. This should be a secure location.

  3. Run the following command to generate an SSH key pair:

    Terminal window
    ssh-keygen -t rsa -b 4096 -C "your@email.com"
  4. Press Enter to save the key in the default location. At this step you will have two files in the location you specified:

    • id_rsa: The private key. This should never be shared with anyone.
    • id_rsa.pub: The public key. This is the key that you will copy to the server.
  5. We need to tell windows to use the key for authentication.

Start the SSH-Agent in Windows by running the following command:

Terminal window
Get-Service -Name ssh-agent | Set-Service -StartupType 'Automatic'
Start-Service ssh-agent

Add the key to the authentication agent by running the following command:

Terminal window
ssh-add path-to-key/id_rsa

This will add the key to the authentication agent.

Now we need to tell Ubuntu about our key. Copy the public key using following powershell command:

Terminal window
Get-Content ~/.ssh/id_rsa.pub | Set-Clipboard

You should now have the public key in your clipboard.

  1. Connect to the server via SSH and run the following commands:
Terminal window
ssh username@ip-address # replace username and ip-address with the values from the previous steps
cd ~/.ssh
nano ~/.ssh/authorized_keys
  1. Paste the public key into the file and save it. Press Ctrl + X, then Y and Enter to save the file.

  2. Exit the SSH session.

  3. Connect to the server again via SSH. You should not be prompted for a password from now on.