I have taken a snapshot recently to assist me build VMs quickly with the operating system I desire (through virsh), however, every time I build a VM, I would like to modify few files inside the img file before assigning the qemu img file to the VM such as, for example, the /etc/sysconfig/network-scripts/ifcfg-eth0 file and the shadow file. Is this possible through command line? I can do this through VNC if I assign the img file to the VM and then log into VNC to apply my changes, but I was wondering if there's a quick shell solution to achieve this.

Any suggestion would be greatly appreciated!

  • You could attach the image to modify to an existing virtual machine, through which once running you change the disk image using SSH. With a version of libvirt recent enough I think you can even do all of this while the VM is running.
    – user86969
    Apr 30, 2015 at 16:29
  • @Nasha Thank you! I use libvirt version 0.10.2 and QEMU 0.12.1, there are higher versions which I tried to compile and install but I couldn't get them to work - I run CentOS on the main server, but was looking for a way to change specific files inside the img file then attach the img file to the VM because the network interface needs configuring as well as the root password for every VM built. A way to modify the files after attaching the img file to the VM would work as well, but I cannot use VNC as I do all this through PHP. Apr 30, 2015 at 16:38
  • That's what I thought suggesting to attach the image [as a non-boot drive] to an existing VM. Do you have virsh on the CentOS server (or the main VM server)?
    – user86969
    Apr 30, 2015 at 16:55
  • @Nasha Yes, indeed! That's what I use to do almost everything related to the VMs (on the main server only, I haven't enabled nested virtualization) - I heard about qemu-nbd but I don't wish to use it as it appears that I would need LVMs (or not?); I would prefer if I create the image files on the main hard disk drive and attach them right away, but if you do have any other suggestions I'd be really thankful to hear them - thanks for your help! Apr 30, 2015 at 17:22

1 Answer 1


If you create the new image file in advance (e.g., using qemu-img create -f qcow2 -b <backing file> <new image name>) you could mount that drive as a loop device, then modify the files, unmount it and start the virtual machine. Mounting can be a little tricky at times since you have to skip past the partition table and the like.

Probably easier than trying to mount it yourself you can use libguestfs for many such tasks (http://libguestfs.org/) then probably you could use the virt-edit command to modify the files you want.

  • Thank you very much! guestfs looks like the best and quickest solution, but do you think there's a way to update the files immediately through one command line? For example, if you use <code>guestfish -i edit /etc/file</code>, you will have to update files interactively - but is there something like "echo 'example' >> /etc/file" to append 'example' to the file through guestfish, or is the "sed" command available? May 1, 2015 at 0:57
  • 2
    I have never tried it myself, but it seems that virt-edit has a -e flag that seems like it probably works similar to sed: libguestfs.org/virt-edit.1.html May 1, 2015 at 1:00
  • Thank you very much @Eric! It worked perfectly, here's the command I used to edit my shadow file in order to update the root password: virt-edit imagefile.img /etc/shadow -e 's/root.*/root:encryptedpassword/' - thanks for all your help! May 1, 2015 at 1:06
  • 1
    @Eric I did not know one could loop mount QCow images (for instance) directly. I tried some time ago and had to fall back onto uncompressed loop images. Nice tip.
    – user86969
    May 4, 2015 at 7:46
  • very old intro, brief guide Jul 14, 2020 at 13:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .