Since the invention of virtual machine, it provides great convenience to the IT world. But due to the nature of cloud service, the system time of the newly prevision virtual machine is pre-set and is usually set at UTC. So in this post I am going to show you how to change system time zone to your desire choice and in this demo, CST will be used.
As usual, let’s check the current time first, with the command
date, we can see that the time zone is set at
All the time zone files are pre-defined in the system already and located at
All we have to do is copy the desired time zone file to the appropriate system location and that is it! Before we do that, let’s rename the current time zone file first, just in case if anything goes wrong. The current system time zone file is located at
1 mv /etc/localtime /etc/localtime.bak
Then we can link or copy desired pre-defined system time zone file to
/etc/localtime. In this demo, I am just going to make a soft link. After that, we issue
date command again to confirm that the system time zone has been changed.
1 ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
Next, to be safe, we can issue
ntpdate command to synchronize system time with any of the public time server to the most accurate time. In the image, we can see that the current system time is offset with the time servery by 0.000189 sec.
1 ntpdate pool.ntp.org
And to make sure your system is always time synchronized, we can add the above command to
/etc/crontab file and let it synchronizes time at the frequency you prefer.
* * * * * means execute the command follow by it every minute.
1 * * * * * root ntpdate pool.ntp.org
To make sure
crontab and the command added will be working correctly, let’s use
date command to set current system to
20180101 17:00:00, wait for a minute and check if crontab has done its job as we expected.
1 date -s "20180101 17:00:00"
And after a minute, the system date has been corrected as expected!