* Home :: Get a random password :: Get a multi-word password :: What is my IP? (v4 | v6)
* My projects

Customise a Linux template with virt-customize

Tags: howto

Date: 20231020

It can sometimes be handy to be able to reset passwords or even un/install packages in Linux images or templates.

For example, you have downloaded the latest Ubuntu or CentOS image, but the root password is disabled or randomised and you really don't want cloud-init to mess it up again.
There's an easy way to fix this if you have access to the QCOW2 (or raw) file.

Here's how to set the root password to "password" and uninstall cloud-init package. man virt-customize for (a lot) more options!

~]# yum -y install libguestfs-tools-c # package name may vary on your distro
~]# virt-customize -a File.qcow2 --root-password password:password --uninstall cloud-init
[   0.0] Examining the guest ...
[   5.8] Setting a random seed
[   5.8] Uninstalling packages: cloud-init
[   9.8] Setting passwords
[  11.6] Finishing off


It's done, you can now start your guest VM and login locally as root with password password. SSH access may still not be possible, depending on the configuration, but this is also something fixable with the libguestfs tools, for example you could use virt-edit to edit the sshd_config file and PermitRootLogin, eg:

virt-edit -a File.qcow2 /etc/ssh/sshd_config

(c)The Nux(tm) ;-)

No CSS, no Javascript, no some HTML.
No ads, no tracking, no cookies.