How to Generate and Re-Initialize Machine IDs in Ubuntu Using systemd-machine-id-setup

In modern cloud infrastructure, DevOps virtualization workflows, and automated system deployment, virtual machine disk images and root filesystems are frequently cloned across dozens or hundreds of virtual instances. When cloning an existing Ubuntu installation or deploying from golden VM templates (such as VMware templates, KVM qcow2 golden images, or cloud base images), an often-overlooked configuration risk is the machine ID. Located at /etc/machine-id, this 128-bit hexadecimal identifier uniquely distinguishes a host on the network. If multiple cloned machines boot with identical machine IDs, network conflicts inevitably emerge—including duplicate DHCP IP address assignments from systemd-networkd, corrupted multi-host journald log aggregation, and D-Bus IPC collision. Ubuntu provides the systemd-machine-id-setup command to safely initialize, generate, and commit unique machine IDs.

Understanding /etc/machine-id Architecture and Dependencies

The machine ID is generated during system installation or early initial boot and resides at /etc/machine-id (with a symlink often mapped from /var/lib/dbus/machine-id for legacy D-Bus compatibility). Crucial system subsystems rely directly on this identifier:

  • systemd-networkd and NetworkManager: When generating DHCP Dynamic Host Configuration Protocol Client Identifiers (DUID / IAID) or IPv6 link-local addresses, network managers hash the machine ID. Cloned instances with identical machine IDs receive duplicate IPv4 leases from DHCP servers.
  • systemd-journald: Journald stores log files in subdirectories named after the machine ID (e.g., /var/log/journal/<machine-id>/). Cloned instances sharing centralized log storage can overwrite or corrupt each other’s audit trails.
  • D-Bus System Bus: D-Bus initializes its peer tracking and authentication mechanisms using the machine ID.
  • systemd-boot and TPM2 Enrolment: Boot loaders and secure enclave disk keys (via systemd-cryptenroll) bind measurements to specific machine identity seeds.

Inspecting Current Machine Identity

To inspect the current machine identifier on your Ubuntu system, read the file directly or use hostnamectl:

# Query active machine ID
cat /etc/machine-id

# Alternatively query through hostnamectl
hostnamectl | grep "Machine ID"

The command returns a 32-character hexadecimal string (e.g., b4a2f8c01e544b8296a12df089e134b7).

Preparing Golden VM Templates: Truncating /etc/machine-id

When preparing an Ubuntu virtual machine to serve as a reusable base template, administrators must erase the existing machine ID so that each cloned child instance automatically generates its own unique identifier upon first boot.

Step 1: Emptying the File (Preserving the Empty Inode)

Best practice in systemd requires truncating /etc/machine-id to zero bytes rather than deleting the file entirely. If the file is completely missing, certain early-boot services may fail to start or mount the root filesystem read-only. Truncating ensures early-boot generators recognize the uninitialized state:

# Truncate /etc/machine-id to 0 bytes
sudo truncate -s 0 /etc/machine-id

# Remove legacy D-Bus machine ID symlink if present
sudo rm -f /var/lib/dbus/machine-id

Step 2: Shutting Down the Template Image

Immediately power down the VM without rebooting it into production:

sudo poweroff

Generating a New Machine ID with systemd-machine-id-setup

If you have deployed a cloned virtual machine that accidentally booted with a duplicate machine ID, you can force the immediate generation of a fresh identity using systemd-machine-id-setup.

Step 1: Erase the Duplicate Identifier

# Clear duplicate ID
sudo rm -f /etc/machine-id /var/lib/dbus/machine-id

Step 2: Execute systemd-machine-id-setup

Run the initialization command with elevated privileges:

sudo systemd-machine-id-setup

The utility executes an entropy collection routine:

  1. Attempts to read hypervisor UUIDs (such as KVM/QEMU UUID or DMI system UUID from BIOS tables).
  2. If a container or hypervisor UUID is present, it hashes the UUID to generate a deterministic, unique identity.
  3. If no virtualization UUID is found, it queries the kernel hardware random number generator (/dev/urandom) to create a cryptographically secure 128-bit random value.
  4. Writes the string to /etc/machine-id and synchronizes disk buffers.

Step 3: Regenerate D-Bus Symlink

Ensure that legacy D-Bus software accesses the new identity:

sudo systemd-tmpfiles --create /usr/lib/tmpfiles.d/dbus.conf 2>/dev/null || sudo ln -sf /etc/machine-id /var/lib/dbus/machine-id

Committing Ephemeral Machine IDs from Early Boot

In environments where the root filesystem mounts read-only during early boot (such as network PXE boots, live recovery ISOs, or immutable OS trees), systemd generates a transient, in-memory machine ID mounted on a tmpfs overlay. Once the underlying storage volume transitions to read-write mode, commit the ephemeral ID permanently to physical storage:

# Commit transient machine ID to permanent disk
sudo systemd-machine-id-setup --commit

Applying Changes and Renewing Network Leases

After regenerating a machine ID, restart system services or reboot the operating system to ensure all daemons reload the new identity:

# Renew DHCP lease with systemd-networkd
sudo systemctl restart systemd-networkd

# Re-link log journal directory
sudo systemctl restart systemd-journald

# Alternatively perform a clean reboot
sudo reboot

By integrating systemd-machine-id-setup into golden image build scripts and template provisioning pipelines, Ubuntu administrators eliminate IP address conflicts, protect centralized log streams, and ensure robust unique host identification across cloud environments.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.