查看當前服務器時間
date -R
方法1:用 date -s "2019-02-20 16:00:00" 命令來設置時間
方法2:使用ntpdate同步
1.安裝ntpdate服務
yum install ntpdate
2.直接用域名同步中國上海時間 是阿里雲的服務器
ntpdate ntp1.aliyun.com
或者使用 ntpdate 210.72.145.44 這個是國家授時中心的IP
如果同步出現這個錯誤:
the NTP socket is in use, exiting //ntp服務器在使用中
使用 service ntpd stop 命令停止 然後再同步就可以了
方法3:使用curl請求第三方API,定時從API中獲取時間並設置,腳本(sync-time-suning.sh)如下
#!/bin/bash
#將該腳本加入crontab定時任務,命令:crontab -e
#nginx根目錄
LOG_NAME=/opt/sync-time/sync-time.log
echo "同步北京時間 start"
before=$(date "+%Y-%m-%d %H:%M:%S")
#獲取互聯網時間
result=$(curl -s http://quan.suning.com/getSysTime.do)
datetime=${result:13:19}
echo $datetime
#設置時間
date -s "$datetime"
after=$(date "+%Y-%m-%d %H:%M:%S")
echo "同步前系統時間:$before,同步後系統時間:$after" >> $LOG_NAME
echo "同步北京時間 end"