Synchronize Beijing time in linux

View current server time

date -R

 

Method 1: Use the date -s "2019-02-20 16:00:00" command to set the time

 

Method 2: Synchronize with ntpdate

1. Install ntpdate service

yum install ntpdate

2. Directly use the domain name to synchronize the time in Shanghai, China, which is the server of Alibaba Cloud

ntpdate ntp1.aliyun.com

Or use ntpdate 210.72.145.44, which is the IP of the National Time Service Center

 

If you get this error while syncing:

the NTP socket is in use, exiting //ntp server is in use

Use the service ntpd stop command to stop and then synchronize again

 

Method 3: Use curl to request a third-party API, get the time from the API regularly and set it, the script (sync-time-sunning.sh) is as follows

#!/bin/bash
#Add this script to crontab timing task, command: crontab -e
#log directory
LOG_NAME=/opt/sync-time/sync-time.log

echo "Synchronize Beijing time start"
before=$(date "+%Y-%m-%d %H:%M:%S")
#Get Internet time, here is the API provided by Suning
result=$(curl -s http://quan.suning.com/getSysTime.do)
datetime=${result:13:19}
echo $datetime
#set time
date -s "$datetime"
after=$(date "+%Y-% m-%d %H:%M:%S")
echo "System time before synchronization: $before, system time after synchronization: $after" >> $LOG_NAME
echo "Beijing time synchronization end"
 

 

 

 

Guess you like

Origin blog.csdn.net/w13528476101/article/details/102575687