centos下部署NTP時間伺服器同步環境記錄

散盡浮華發表於2016-08-30

 

1)服務端部署
安裝所需軟體包
[root@test ~]# yum -y install ntp ntpdate

服務端自己先手工同步一次時間。
[root@test ~]# ntpdate ntp.sjtu.edu.cn
21 Jun 16:48:54 ntpdate[10781]: the NTP socket is in use, exiting

或者使用阿里雲的時間伺服器進行線上同步
[root@test ~]# ntpdate ntp2.aliyun.com
[root@test ~]# ntpdate ntp1.aliyun.com

編輯NTP主配置檔案,新增NTP伺服器(對於大陸地區的伺服器)並同步BIOS時間。
對於位於中國大陸地區IDC機房的伺服器,由於CentOS/RHEL預設的ntp伺服器無法訪問,因此要設定中國大陸地區自己的ntp伺服器;
對於位於中國大陸地區以外的IDC機房的伺服器,可以使用CentOS/RHEL預設的ntp伺服器(但一定要檢查確保可以訪問);
修改如下:
[root@test ~]# cp /etc/ntp.conf /etc/ntp.conf.bak
[root@test ~]# vim /etc/ntp.conf
restrict default nomodify notrap noquery
 
restrict 127.0.0.1
restrict 192.168.0.0 mask 255.255.0.0 nomodify     #只允許192.168.0.0網段的客戶機進行時間同步。如果允許任何IP的客戶機都可以進行時間同步,就修改為"restrict default nomodify"
 
server ntp1.aliyun.com
server ntp2.aliyun.com
server time1.aliyun.com
server time2.aliyun.com

server time-a.nist.gov
server time-b.nist.gov
 
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10
 
driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys            /etc/ntp/keys
 
[root@test ~]# service ntpd start
Starting ntpd:                                             [  OK  ]
 
[root@test ~]# netstat -tulnp | grep ntp
udp        0      0 182.48.115.233:123          0.0.0.0:*                               10023/ntpd         
udp        0      0 127.0.0.1:123               0.0.0.0:*                               10023/ntpd         
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               10023/ntpd         
udp        0      0 fe80::5054:ff:feab:db19:123 :::*                                    10023/ntpd         
udp        0      0 ::1:123                     :::*                                    10023/ntpd         
udp        0      0 :::123                      :::*                                    10023/ntpd 
 
設定開機自啟動
[root@test ~]# /sbin/chkconfig --level=2345 ntpd on
 
檢視現有連線客戶端
[root@test ~]# watch ntpq -p

服務端可以設定定期線上同步一次時間
[root@test ~]# crontab -l
#伺服器時間同步
0 * * * *  /usr/sbin/ntpdate ntp1.aliyun.com; /sbin/hwclock -w
 
2)客戶端配置
命令列手動執行一次驗證是否可用:
[root@test2 ~]# ntpdate 182.148.15.33             #182.148.15.33是上面ntp伺服器的ip地址
21 Jun 16:45:35 ntpdate[2323]: adjust time server 182.148.15.33 offset 0.319548 sec
 
配置自動同步
[root@test2 ~]# crontab -l
#伺服器時間同步
*/5 * * * *  /usr/sbin/ntpdate 182.148.15.33; /sbin/hwclock -w

相關文章