Changing system time in a virtual machine

 When working on a bug 748920 I needed to change the system time (RTC) in a KVM+libvirt virtual machine. I have found quite some obstacles I want to share.

The first possible source of your confusion might be the fact, that if you set your system time in the usual way using ‘date’ and ‘hwclock’, the change will persist VM reboot, but won’t persist VM shutdown. On shutdown the RTC is reset.

If you need to have a permanent system time change (persisting shutdown), you need to edit the libvirt configuration file for your VM. Issue this command:

$ virsh edit <domain_name>

Then edit the XML file according its documentation. Find this line:

<clock offset='utc'/>

and replace it with this line:

<clock offset='variable' adjustment='86400'/>

The adjustment parameter controls how much will the system time be shifted from current UTC time, the value is the number of seconds. In the example above I shifted the time one day into the future (86400 seconds). You can use negative values as well.

Save the file, boot your VM. The time should be shifted.

Be careful that currently Fedora boot breaks if you shift your time more than one day into the past, as says bug 748920.

Flattr this