Preparing an Ubuntu virtual machine (VM) in Hyper-V to serve as a reusable template

Preparing an Ubuntu virtual machine (VM) in Hyper-V to serve as a reusable template involves configuring the system in a clean, generalized state so it can be cloned or deployed repeatedly without issues like duplicate hostnames or SSH keys. Below are the steps to prepare an Ubuntu VM for use as a Hyper-V VM template:


1. Install Ubuntu in Hyper-V

  • Create a new VM in Hyper-V Manager.
  • Use an Ubuntu Server or Desktop ISO (e.g., Ubuntu 22.04 LTS or 24.04 LTS).
  • Configure the VM with desired settings (CPU, RAM, disk size, etc.).
  • Install Ubuntu as usual, ensuring you:
    • Choose a minimal installation if you want a lightweight template.
    • Install OpenSSH Server (sudo apt install openssh-server) for remote access.
    • Avoid setting a static IP during installation (leave it DHCP for now).

2. Update and Install Essential Packages

After installation, update the system and install tools useful for Hyper-V:

bash
sudo apt update && sudo apt upgrade -y sudo apt install -y linux-tools-virtual linux-cloud-tools-virtual

These packages include Hyper-V integration services (e.g., for better performance and management).


3. Configure Hyper-V Integration Services

Ensure Hyper-V integration services are enabled:

  • Edit the GRUB configuration:
    bash
    sudo nano /etc/default/grub
  • Add or modify the following line to include Hyper-V-specific kernel parameters:
    bash
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash hv_vmbus hv_storvsc hv_netvsc"
  • Update GRUB:
    bash
    sudo update-grub

4. Generalize the System

To make the VM reusable as a template, remove machine-specific data:

  • Clear Machine ID:
    bash
    sudo truncate -s 0 /etc/machine-id sudo rm /var/lib/dbus/machine-id sudo ln -s /etc/machine-id /var/lib/dbus/machine-id
    This ensures a new machine ID is generated on first boot.
  • Reset Hostname: Set a generic hostname or clear it:
    bash
    sudo hostnamectl set-hostname localhost
    Alternatively, leave it unset by editing /etc/hostname and /etc/hosts later via a script.
  • Remove SSH Keys: Delete existing SSH host keys so new ones are generated on boot:
    bash
    sudo rm -f /etc/ssh/ssh_host_*
  • Clean APT Cache: Free up space and remove cached packages:
    bash
    sudo apt clean
  • Remove User-Specific Data: If you created a user, clean up their home directory or remove unnecessary files:
    bash
    rm -rf /home/username/.cache/* /home/username/.bash_history

5. Integrate with cloud-init (Optional but Recommended)

Using cloud-init allows you to automate post-deployment configuration (e.g., setting hostname, users, or network settings):

  • Install cloud-init if not already present:
    bash
    sudo apt install cloud-init -y
  • Clean cloud-init state to ensure it runs on the next boot:
    bash
    sudo cloud-init clean --logs
  • Optionally, create a default cloud-init config in /etc/cloud/cloud.cfg.d/ to set up users, SSH keys, or packages. Example:
    yaml
    # /etc/cloud/cloud.cfg.d/99-custom.cfg #cloud-config users: - name: admin sudo: ALL=(ALL) NOPASSWD:ALL ssh-authorized-keys: - <your-public-ssh-key>
  • Ensure cloud-init runs on first boot by leaving it enabled.

6. Final Cleanup

  • Clear logs:
    bash
    sudo find /var/log -type f -exec truncate -s 0 {} \;
  • Stop the VM cleanly:
    bash
    sudo shutdown -h now

7. Export the VM as a Template

  • In Hyper-V Manager, right-click the VM and select Export.
  • Choose a location to save the exported VM (e.g., C:\VMs\UbuntuTemplate).
  • This creates a reusable template with all virtual disks and configuration files.

8. Test the Template

  • Import the exported VM in Hyper-V:
    • Open Hyper-V Manager > Import Virtual Machine.
    • Select the exported folder and choose Copy the virtual machine (create a new unique ID).
  • Start the new VM and verify:
    • It boots correctly.
    • A new machine ID is generated (cat /etc/machine-id).
    • SSH keys are regenerated (ls /etc/ssh/ssh_host_*).
    • cloud-init (if used) applies your custom configuration.

Additional Tips

  • Sysprep Alternative: Unlike Windows, Ubuntu doesn’t have a direct sysprep tool, but the steps above (clearing machine ID, SSH keys, etc.) achieve a similar result.
  • Networking: If you need static IPs, configure them via cloud-init or a post-boot script rather than hardcoding them in the template.
  • Checkpoint: Before exporting, create a Hyper-V checkpoint to easily revert and tweak the VM if needed.

Your Ubuntu VM is now ready to be used as a Hyper-V template!

Comments