DEV Community

Mr koala
Mr koala

Posted on

The Golden Path: Creating Proxmox Automated VM Templates with Cloud-Init

By using this method, you only need to perform a one-time setup. Afterward, you can "spawn" new VMs in seconds that come pre-configured with your desired IP, User, and Password.

Phase 1: Proxmox Shell Preparation
Log in to your Proxmox server via SSH or the built-in Shell. We will use the CLI because it is significantly faster and more precise for template creation.

# 1. Download the Ubuntu 24.04 Cloud Image (Noble Numbat)
# These images are specifically designed for automation, unlike standard ISOs.
wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img

# 2. Create a New VM (ID: 9000, Name: Ubuntu-Template)
qm create 9000 --name "ubuntu-2404-template" --memory 2048 --cores 2 --net0 virtio,bridge=vmbr0

# 3. Import the Disk to Storage 
# (Replace 'local-lvm' with your actual Proxmox storage name)
qm set 9000 --scsi0 local-lvm:0,import-from=$(pwd)/noble-server-cloudimg-amd64.img

# 4. Add the Cloud-Init Drive
# This drive "injects" your configurations (user, networking, ssh) into the VM.
qm set 9000 --ide2 local-lvm:cloudinit

# 5. Configure Boot & Essential Hardware
qm set 9000 --boot c --bootdisk scsi0
qm set 9000 --scsihw virtio-scsi-pci
qm set 9000 --agent enabled=1
qm set 9000 --serial0 socket --vga serial0
Enter fullscreen mode Exit fullscreen mode

Phase 2: Cloud-Init Configuration
Now, we define the "instructions" the VM should follow upon its first boot.

# 1. Set Default Username and Password
qm set 9000 --ciuser admin --cipassword "P@ssw0rd123!"

# 2. Network Configuration
# Option A: Using DHCP (Recommended for most setups)
qm set 9000 --ipconfig0 ip=dhcp

# Option B: Static IP (Uncomment below to use a fixed IP)
# qm set 9000 --ipconfig0 ip=192.168.1.50/24,gw=192.168.1.1

# 3. Set DNS Server
qm set 9000 --nameserver 1.1.1.1

# 4. Convert VM to Template (Final Step)
# This locks the VM and turns it into a master copy.
qm template 9000
Enter fullscreen mode Exit fullscreen mode

Phase 3: Quick Deployment
Once the steps above are complete, VM 9000's icon will change to a "Template" icon (a file/paper symbol). To create a new VM:

  1. 1. Right-Click on VM 9000.
  2. 2. Select Clone.
  3. 3. Choose Mode:
    • Full Clone: The VM is completely independent.
    • Linked Clone: Saves storage space but requires the template to exist.
  4. Give your new VM a name (e.g., Web-Server-01).
  5. Start the VM.

Phase 4: Verification & Login
As soon as the VM starts, you don't need to interact with the console. You can log in immediately via SSH or Console using:

Username: admin
Password: P@ssw0rd123!

Because the Guest Agent was enabled in Phase 1, the VM's IP address will automatically appear in the Proxmox dashboard under the Summary tab.

Pro-Tips for Power Users

  • Resize Disk: Cloud Images are usually small (2GB - 8GB). After cloning, increase the disk size before starting the VM: qm resize scsi0 +20G
  • SSH Keys: For better security, inject your public key so you don't have to type passwords: qm set 9000 --sshkeys /path/to/your/id_rsa.pub
  • Automated Updates: If you want the VM to update itself at birth, you can add a script to the Cloud-Init > User-Data section in the Proxmox GUI, or use a custom .yaml file.

Note: Does your storage name differ from local-lvm? If so, make sure to swap that name out in the script commands above!

Top comments (0)