1、NTP簡介
NTP(Network Time Protocol,網絡時間協議)是用來使網絡中的計算機,時間同步的一種協議。
NTP服務器利用NTP協議來提供時間同步服務。
2 、環境准備
主機名 服務器/客戶端 外網IP 內網IP
ntp-s Server 10.0.0.11 192.168.56.11
ntp-c Client - 192.168.56.12
3、安裝NTP軟件包
yum install -y ntp ntpdate
4、配置NTP
操作前備份
cp /etc/ntp.conf{,.ori}
修改配置文件,紅色字體為修改部分
[root@ntp-s ~]# grep -vE "^$|#" /etc/ntp.conf driftfile /var/lib/ntp/drift #<==記錄上次的NTP server與上層NTP server聯接所花費的時間 restrict default nomodify #<==允許所有(nomodify)主機進行時間同步 restrict 127.0.0.1 restrict ::1 server ntp1.aliyun.com #<==同步時間服務器,阿里雲NTP服務器 server ntp2.aliyun.com prefer #<==優先使用該NTP服務器 server ntp3.aliyun.com includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor
5、啟動NTP
注意:如果有ntpdate定時任務,需要先注釋掉或關閉,再啟動,否則兩者會有沖突。
ntp服務默認偵聽的端口是udp的123端口
ntpdate ntp2.aliyun.com #<==先讓本機的時間與互聯網的時間保持同步
systemctl start ntpd systemctl status ntpd systemctl enable ntpd systemctl list-unit-files|grep ntpd netstat -tunlp|grep 123
6、查看本機和上層服務器的時間同步結果
[root@ntp-s ~]# ntpq -p #<==查看本機和上層服務器的時間同步結果( p == peer ) remote refid st t when poll reach delay offset jitter ============================================================================== *time5.aliyun.co 10.137.38.86 2 u 37 64 77 55.129 22.421 6.118 120.25.115.19 10.137.38.86 2 u 39 64 77 22.665 20.670 6.239 120.25.115.20 10.137.38.86 2 u 37 64 77 22.948 14.326 5.291
7、NTP客戶端
注意:客戶機要等幾分鍾再和新啟動的ntp服務器進行時間同步,否則會提示no server suitable for synchronization found錯誤。
[root@linux-node2 ~]# date -s "20110101" #<==修改當前時間 Sat Jan 1 00:00:00 CST 2011 [root@linux-node2 ~]# date +%F 2011-01-01 [root@linux-node2 ~]# ntpdate 192.168.56.11 #<==手動執行與NTP server進行時鍾同步 11 Feb 21:46:14 ntpdate[19828]: step time server 192.168.56.11 offset 193009546.214391 sec [root@linux-node2 ~]# date Sat Feb 11 21:46:19 CST 2017
8、客戶端添加到定時任務
[root@linux-node2 ~]# crontab -l #time sync by ChenDianHu at 2016-06-28 */5 * * * * /usr/sbin/ntpdate 192.168.56.11 >/dev/null 2>&1