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

 

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)客戶端配置 (記住要關閉客戶端機器的ntpd服務, 不然使用nepdate的時候, 會報錯"ntpdate[14431]: the NTP socket is in use, exiting"; 或者開啓ntpd服務前提下, 使用ntpdate -u ip就不會有那個報錯了)
命令行手動執行一次驗證是否可用:
[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
相關文章
相關標籤/搜索