How to Create a Headless VM on Ubuntu Using VirtualBox

This article guides users on creating a headless virtual machine in VirtualBox on Ubuntu 24.04 via command line. It covers installation steps, creating a VM directory, modifying properties, and setting up storage. Users can then attach the Ubuntu ISO and start the VM, connecting through RDP for installation.

This article explains how to create a [headless] virtual machine on VirtualBox via the command line on Ubuntu 24.04.

VirtualBox is a free and open-source virtualization software that lets you create and manage virtual machines (VMs) on your computer. You can run multiple operating systems like Windows, macOS, and various Linux distributions simultaneously within your Ubuntu environment.

Using command line tools like VBoxManage allows VM creation and configuration automation, which can be scripted for repetitive tasks or deployment in large environments.

You can manage multiple virtual machines (VMs) simultaneously without a graphical interface. This makes it more efficient for creating or managing groups of VMs. For experienced users, performing operations through the command line can be quicker than navigating a graphical interface, allowing for faster adjustments and configurations.

Install VirtualBox

To create a virtual machine on VirtualBox, you must first install VirtualBox. If you haven’t done this, refer to the post below for installation instructions.

Install VirtualBox on Ubuntu

After installing VirtualBox, continue to create your VMs, as shown below.

Create VM directory

Before creating a virtual machine, you must create a directory to store the virtual machines.

Run the command below to create a directory for VirtualBox.

sudo mkdir /var/vbox

Create a virtual machine

Now that the VM directory has been created, run the command below to create an Ubuntu 24.04 virtual machine named [Ubuntu2404].

sudo VBoxManage createvm --name Ubuntu2404 --ostype Ubuntu_64 --register --basefolder /var/vbox

If the command is successful, you should see a message like the one below.

Virtual machine 'Ubuntu2404' is created and registered.
UUID: 613dde34-dfa1-4342-9a04-f54c19b44b19
Settings file: '/var/vbox/Ubuntu2404/Ubuntu2404.vbox'

Change VM properties

After creating the Ubuntu virtual machine, you can adjust its properties to modify the CPU, memory size, network settings, and more using the command below.

sudo  VBoxManage modifyvm Ubuntu2404 --cpus 4 --memory 8192 --nic1 nat --boot1 dvd --vrde on --vrdeport 5001

Create the VM storage

After configuring the CPU, memory, and other parameters, execute the command below to create storage for the VM.

sudo VBoxManage storagectl Ubuntu2404 --name "Ubuntu2404_SATA" --add SATA

Next, create a hard drive from the storage created with the size by running the command below.

sudo VBoxManage createhd --filename /var/vbox/Ubuntu2404/Ubuntu2404.vdi --size 20480 --format VDI --variant Standard

If the command is successful, you should see something similar to the one below.

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: 703b125d-987c-41a1-a2c8-ba15baa2d7ec

Attach the hard drive created above.

sudo VBoxManage storageattach Ubuntu2404 --storagectl Ubuntu2404_SATA --port 1 --type hdd --medium /var/vbox/Ubuntu2404/Ubuntu2404.vdi

The VM should be created with storage and ready for your operating system. To check the VM details, run the command below.

sudo VBoxManage showvminfo Ubuntu2404

It should display something similar to the one below.

Name:                        Ubuntu2404
Encryption: disabled
Groups: /
Guest OS: Ubuntu (64-bit)
UUID: 613dde34-dfa1-4342-9a04-f54c19b44b19
Config file: /var/vbox/Ubuntu2404/Ubuntu2404.vbox
Snapshot folder: /var/vbox/Ubuntu2404/Snapshots
Log folder: /var/vbox/Ubuntu2404/Logs
Hardware UUID: 613dde34-dfa1-4342-9a04-f54c19b44b19
Memory size: 8192MB
Page Fusion: disabled
VRAM size: 8MB
CPU exec cap: 100%
HPET: disabled
CPUProfile: host
Chipset: piix3
Firmware: BIOS
Number of CPUs: 4
PAE: enabled
Long Mode: enabled
Triple Fault Reset: disabled
APIC: enabled
X2APIC: enabled

The VM is ready.

Attach Ubuntu installation ISO

If you wish to install Ubuntu OS on a VM, download and attach the ISO file.

Download Ubuntu ISO. Then, please attach it to the VM using the command below.

sudo VBoxManage storageattach Ubuntu2404 --storagectl Ubuntu2404_SATA --port 0 --type dvddrive --medium ~/Downloads/ubuntu-24.04.2-desktop-amd64.iso

Once the ISO is attached, you can start the VM using the command below.

sudo VBoxManage startvm Ubuntu2404 --type headless

If the command was successful, you will see a message similar to the one below.

Waiting for VM "Ubuntu2404" to power on...
VM "Ubuntu2404" has been successfully started.

You can connect to the VM using RDP client to the VM host, followed by the [vrdeport] port 5001 [VMHostIPaddress:vrdeport] to interact with the machine and continue the installation.

192.168.48.128:5001

Conclusion:

In summary, creating a headless virtual machine on VirtualBox using the command line in Ubuntu 24.04 is a straightforward process. Here are the key points to remember:

  • Preparation: Ensure VirtualBox is installed and a directory for VMs is created.
  • VM Creation: This creates the virtual machine with the desired parameters.
  • Configuration: Modify VM properties such as CPU and memory using VBoxManage modifyvm.
  • Storage Setup: Create a storage controller and hard drive for the VM.
  • ISO Attachment: Download the Ubuntu installation ISO and attach it to the VM.
  • Launching: Start the VM in headless mode and connect using an RDP client for installation.

These steps will help you efficiently set up and manage virtual machines headlessly.

Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *