<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[After days of research and tinkering : a working guide for Debian 12 template with cloud-init and DHCP]]></title><description><![CDATA[<p dir="auto">I have tried for days to make a Debian template (this probably applies to other Linux OS)<br />
The main issue I was facing was that when creating multiple machine they would get the same IP from our DHCP server.<br />
The reason is that Debian sends the machine-id (under /etc/machine-id) as dhcp identifier.<br />
Adding to /etc/dhcp/dhclient.conf file dhcp-client-identifier = hardware;<br />
did not help and deleting /etc/machine-id resulted in the absence of generation of a new id by cloud init for some reason and the VM not requesting an IP at all.</p>
<p dir="auto">This is what I did:</p>
<p dir="auto">Downloaded from <a href="https://cdimage.debian.org/images/cloud/" target="_blank" rel="noopener noreferrer nofollow ugc">https://cdimage.debian.org/images/cloud/</a> the latest bookworm raw file and imported it as a disk in XO</p>
<p dir="auto">Booted a VM with a random template and some network (internet access will be usefull in a few steps)</p>
<p dir="auto">Deleted the existing disk and attached the raw disk I uploaded</p>
<p dir="auto">Converted the VM to template</p>
<p dir="auto">Created a VM from this template with my ssh key</p>
<p dir="auto">Once booted, you will need to install dmidecode (<a href="https://packages.debian.org/search?keywords=dmidecode" target="_blank" rel="noopener noreferrer nofollow ugc">https://packages.debian.org/search?keywords=dmidecode</a> ) due diligence on your part to get the latest .deb, install with dpgk -i</p>
<p dir="auto">Also install xcp-ng guest tools</p>
<p dir="auto">Then run :</p>
<p dir="auto">sudo cloud-init clean<br />
sudo cloud-init clean --logs<br />
rm /home/debian/.ssh/authorized_keys<br />
sudo mkdir -p /var/lib/cloud/scripts/per-once/ (folders get deleted on cloud-init clean)<br />
cd /var/lib/cloud/scripts/per-once/<br />
sudo nano <a href="http://generate-machine-id.sh" target="_blank" rel="noopener noreferrer nofollow ugc">generate-machine-id.sh</a><br />
(coming from user modem7 on github)</p>
<pre><code>#!/bin/bash

# KVM UUID Recreator
# Use this for new VM's or templates that require a unique machine ID.

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

UUID=$(dmidecode -s system-uuid | tr -d '-')
if grep -q "$UUID" /etc/machine-id; then
    echo "UUID matches"
else
    echo "UUID does not match. Recreating."
    echo -n &gt; /etc/machine-id &amp;&amp; echo -n &gt; /var/lib/dbus/machine-id &amp;&amp; systemd-machine-id-setup &amp;&amp; reboot
fi
</code></pre>
<p dir="auto">chmod +x <a href="http://generate-machine-id.sh" target="_blank" rel="noopener noreferrer nofollow ugc">generate-machine-id.sh</a></p>
<p dir="auto">sudo cat /dev/null &gt; ~/.bash_history &amp;&amp; history -c &amp;&amp; shutdown now</p>
<p dir="auto">You can now rename the VM and it's disk, delete the network card to prevent the template to have some tags added automatically with the IPV4 and IPV6 and convert the VM to a template.</p>
<p dir="auto">You should now have a working Debian 12 template accessible with your ssh key if you add it on deploy and DHCP working and not overlapping. Hopefully, I did not forget anything.</p>
<p dir="auto">On first start, the VM will loop once after the first prompt. The reboot is required for the change of the machine-id to be effective.</p>
<p dir="auto">This is a lot of work and I have no doubt there is a simpler solution but I couldn't find it.</p>
]]></description><link>https://xcp-ng.org/forum/topic/9943/after-days-of-research-and-tinkering-a-working-guide-for-debian-12-template-with-cloud-init-and-dhcp</link><generator>RSS for Node</generator><lastBuildDate>Thu, 10 Sep 2026 17:01:10 GMT</lastBuildDate><atom:link href="https://xcp-ng.org/forum/topic/9943.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 08 Nov 2024 16:50:57 GMT</pubDate><ttl>60</ttl></channel></rss>