Two Methods to Synchronize Time on CentOS 7

centos 7 time syncntpdate tutorialrdate commandset timezone centoscron time synchronization
Published·Modified·

Accurate time is crucial for servers. For instance, if a server's time is inaccurate on an e-commerce website, the order time may not match the actual time, affecting management and maintenance. This article shares two methods to synchronize time in a CentOS 7 environment (CentOS 6 has not been tested).

Set Timezone (CentOS 7)

First, execute the command timedatectl status|grep 'Time zone' to check the current timezone. If it is not the China timezone (Asia/Shanghai), you must set it to the China timezone first; otherwise, a time difference will exist due to different timezones.

# If already Asia/Shanghai, no need to set
[root@xiaoz shadowsocks]# timedatectl status|grep 'Time zone'
       Time zone: Asia/Shanghai (CST, +0800)

Execute the following commands to set the timezone:

# Adjust hardware clock to be consistent with local clock
timedatectl set-local-rtc 1
# Set timezone to Shanghai
timedatectl set-timezone Asia/Shanghai

Synchronize Time Using ntpdate

Currently, the common practice is to use the ntpdate command to synchronize time. The usage is as follows:

# Install ntpdate
yum -y install ntpdate
# Synchronize time
ntpdate -u  pool.ntp.org
# After synchronization, check if the time is correct using the date command
date

Additionally, here are some commonly used NTP servers. If you need more, you can visit: http://www.ntp.org.cn to get them.

# China
cn.ntp.org.cn
# Hong Kong, China
hk.ntp.org.cn
# USA
us.ntp.org.cn

After synchronizing time, some servers may drift again after a while, so it is best to set up a crontab task for scheduled synchronization. The method is as follows:

# Install crontab
yum -y install crontab
# Create a crontab task
crontab -e
# Add a scheduled task
*/20 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1
# Restart crontab
service crond reload

The scheduled task above will synchronize time every 20 minutes. Note that /usr/sbin/ntpdate is the absolute path of the ntpdate command; the path may differ on different servers. You can use the which command to find the absolute path, as shown below:

[root@xiaoz ~]# which ntpdate
/usr/sbin/ntpdate

Synchronize Time Using rdate

The ntpdate service requires the udp/123 port. However, some service providers block all UDP protocols, so you might find that ntpdate always fails to synchronize.

# Below is an example of an error when synchronizing time with ntpdate
[root@sharktech ~]# ntpdate -u  pool.ntp.org
 1 Jun 16:13:46 ntpdate[8389]: no server suitable for synchronization found

In this case, we can switch to using the rdate command to synchronize time. The method is as follows:

# Install rdate
yum -y install rdate
# Synchronize time
rdate -s time-b.nist.gov
# Check if the time is correct
date

As with the previous method, it is best to add a scheduled task to synchronize time regularly. The method is as follows:

# Install crontab
yum -y install crontab
# Create a crontab task
crontab -e
# Add a scheduled task
*/20 * * * * /usr/bin/rdate -s time-b.nist.gov > /dev/null 2>&1
# Restart crontab
service crond reload

There are other rdate time servers available as follows:

s1d.time.edu.cn # Southeast University
s1e.time.edu.cn # Tsinghua University
s2a.time.edu.cn # Tsinghua University
s2b.time.edu.cn # Tsinghua University
s2c.time.edu.cn # Beijing University of Posts and Telecommunications
ntp.sjtu.edu.cn 202.120.2.101 # Shanghai Jiao Tong University Network Center NTP Server Address
s1a.time.edu.cn # Beijing University of Posts and Telecommunications
s1b.time.edu.cn # Tsinghua University
s1c.time.edu.cn # Peking University
clock.cuhk.edu.hk # Chinese University of Hong Kong Time Center

Summary

Whether using ntpdate or rdate to synchronize time, the methods are relatively simple. The general process is "Set Timezone" -> "Synchronize Time" -> "Set Scheduled Task". In actual testing, the author found that under circumstances where some service providers block UDP ports, the ntpdate command cannot synchronize, but the rdate command can. Those with similar situations are encouraged to try it.

Some content in this article is referenced from: rdate synchronizes time from NTP server