How to create a VirtualBox VM template?

I just purchased a used 2010 Mac mini with intention to use it as a simple home server. As much as I prefer using any Linux distro as my server OS, I really wanted the ability to easily sync my iPhone via iTunes without any quirky workarounds, which is actually the primary reason I chose to go with the Mac mini instead of any other linux box.

While most software should be able to run on macOS natively, I decided that it would be wiser to lean on VMs for my self-hosting endeavours to potentially avoid any unexpected mucking around.

Rather than reinstalling the Debian over and over again on new VMs, I’ve decided to create a VM template which can be cloned easily with VirtualBox.

  1. Create a new Virtual Machine, name it something like “Debian VM Template”.
  2. Assign it 1GB of RAM and 10GB of disk space. Note that when you are actually running lots of VMs, you can actually turn the memory down on them to as little as 64MB and everything will likely still work fine.
  3. Boot the VM, VirtualBox will recognise that you don’t have an OS installed, and it will prompt you to mount the ISO.
  4. Select “Install” (no GUI)
  5. When prompted for a hostname, do not leave it as the default “debian”. You’d want a value that is easily searchable and replaceable, I set mine to be “debianvmtemplate”. You can leave the domain name blank.
  6. Set a root password.
  7. Create a user (something like “debian” will suffice)
  8. For the partitioning, select the “Guided – use entire disk” and pick “All files in one partition (recommended for new users)” option.
  9. When prompted to install the various groups of software, unselect everything except “standard system utilities”.
  10. Wait for installation to complete and reboot your VM.
  11. Login as root do a quick setup:

    apt-get update && apt-get install sudo nano ufw git curl openssh-server openssh-client build-essential dkms linux-headers-$(uname -r) && apt-get upgrade
    usermod -aG sudo debian

  12. Power off your VM:

    halt -p

  13. Open your VM’s settings. Under Network, I selected “Bridged Networking”.

That’s it. Your template machine is ready. Let’s see what do we have to change each time we clone it:

  1. Right click on your template machine, select “Clone” and give it a new name. Check the box to reinitialise the MAC address of all network cards. Click “Next”, select “Full Clone”, and then click “Clone”.
  2. Boot the new VM and login with your root account.
  3. Replace all instances of the template machine’s hostname (“debianvmtemplate”) with the new VM’s preferred hostname:

    cd /etc
    grep -R debianvmtemplate *
    find . -type f -exec sed -i 's/debianvmtemplate/replacewithnewhostname/g' {} +

  4. Reboot the new VM:

    shutdown -r now

  5. You are ready to start configuring it for whatever task you had in mind.

If you’d like to enable file sharing between the host and guest machine:

  1. Open your VM’s settings and under “Shared Folders”, add a new shared folder.
  2. Select the folder path. The folder name will be automatically filled.
  3. Select “auto-mount” and enter your preferred mount point (something like /mnt/Media will do). Click “OK”.
  4. Boot the new VM and login with your root account.
  5. Append the following to /etc/fstab:

    Media /mnt/Media vboxsf defaults 0 0

  6. Change the folder’s permission and reboot the new VM:

    chmod -R 755 /mnt/Media
    shutdown -r now

  7. Your shared folder is now ready.