Two Methods to Synchronize Time in CentOS 7

Publish: 2019-06-01 | Modify: 2019-06-01

The accuracy of time is crucial for servers. For example, if you have an e-commerce website and the server time is not accurate, there will be a mismatch between the order time and the actual time, which will affect management and maintenance. This article shares two methods for synchronizing time in CentOS 7 environment (not tested in CentOS 6).

Time

Set Time Zone (CentOS 7)

First, run the command timedatectl status|grep 'Time zone' to check the current time zone. If it is not the China time zone (Asia/Shanghai), you need to set it to the China time zone to avoid time difference.

# If it is 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 time zone.

# Set the hardware clock to be synchronized with the local clock
timedatectl set-local-rtc 1
# Set the time zone to Shanghai
timedatectl set-timezone Asia/Shanghai

Synchronize Time using ntpdate

Currently, the most common method 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
# Check if the time is correct after synchronization
date

Here are some commonly used NTP servers. For more servers, you can visit: http://www.ntp.org.cn

# China
cn.ntp.org.cn
# Hong Kong, China
hk.ntp.org.cn
# United States
us.ntp.org.cn

After synchronizing the time, there may be deviations again after a period of time. Therefore, it is recommended to set up a cron job for regular time synchronization. The method is as follows:

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

The above cron job will synchronize the time every 20 minutes. Note that /usr/sbin/ntpdate is the absolute path of the ntpdate command, which may vary on different servers. You can use the which command to find the absolute path. The method is as follows:

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

Synchronize Time using rdate

The ntpdate service requires the use of udp/123 port, but some service providers block all UDP protocols. Therefore, you may find that ntpdate always fails to synchronize the time.

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

In this case, you can use the rdate command to synchronize the 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

Just like before, it is recommended to add a cron job to regularly synchronize the time. The method is as follows:

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

Here are some other rdate time servers:

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 # The 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 Time Zone" -> "Synchronize Time" -> "Set up cron job". In actual testing, I found that some service providers block UDP ports, so the ntpdate command cannot synchronize the time, but the rdate command can. If you encounter a similar situation, you can try it out.

This article refers to: Synchronize time from NTP server using rdate


Comments