[SOLVED] How do I synchronize VM guest time with host time
Red HatThis forum is for the discussion of Red Hat Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm running KVM under CentOS 6.4 and one issue I have is setting the clock on the VMs I create. I've tried various things but nothing gives me a reliable solution. For example, I've created some VMs this morning and although my host's clock is something like 8:00 am PDT, when my VMs start, their time is 15:00 PDT.
What's the best way to configure my VMs so that their time will be set the same as their host when they are first booted?
It might be worth noting that I use the exact same CentOS image that is the basis of my VMs to also create OpenVZ containers. In this case, when a container is started, its time matches that of its host. I need this same result for my VMs.
Last edited by PeterSteele; 09-22-2014 at 01:08 PM.
I've also confirmed that my VMs have kvm-clock enabled. I was under the impression that with this set, a VM will sync its time with its host on start-up, but that is definitely not the case.
Okay, I've answered my own question. The combination of settings that I used to give me the results I need are:
1. Set the hwclock on the host and to use UTC time. This is done with the --utc option of the hwclock command. I run the following command on my host OS:
hwclock --utc --set --date="time-string"
2. Tell CentOS that the hwclock is using UTC via the file /etc/adjtime. For example, you could initialize this file using
echo -e "0.0 0 0.0\n0\n\nUTC" >/etc/adjtime
Create this file on both the host and your guest VMs. I create the file on my guests before I boot them for the first time by directly accessing the guest file system from the host.
3. Set the time zone you want for your system time. Again, do this on both your host and your guests:
where time-zone is a standard CentOS time zone string, for example "US/Pacific".
4. Set the system time on your host based on the hwclock. The --utc option is needed to tell CentOS that the hwclock is in UTC time. It will take the UTC time and set your system time based on the TZ environment variable:
hwclock --utc --hctosys
5. The steps above are all done once, when you are configuring your host and guests. To keep time synced on all your servers after they are up and running you'll want to configure NTP on your host and guests.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.