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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM