CentOS7 設置集群時間同步


1. 安裝ntp時間同步工具

yum -y install ntp ntpdate #安裝ntpdate時間同步工具
ntpdate cn.pool.ntp.org    #設置時間同步
hwclock --systohc          #將系統時間寫入硬件時間
timedatectl                #查看系統時間 

一般來講,經過這幾部之后,應該就OK了。關於時間服務的網址,可以到http://www.ntp.org.cn/pool.php查閱.

有些時候,在執行ntpdate cn.pool.ntp.org的時候會出現下面的錯誤:

no server suitable for synchronization found

可以使用ntpdate -d  {ip or url} 來查看調試信息,如果出現類似下面的信息,說明服務器連接不上,

transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123
.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found

解決辦法之一為:

ntpq -c version  #查看ntp版本

如果版本是ntp4.2(包括4.2)之后的版本,在restrict的定義中使用了notrust的話,會導致以上錯誤,官網說明

The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".

In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd

解決:

sudo vim /etc/ntp.conf

把notrust去掉。

 

如果這個還不能解決,就只能自己選擇集群中一台機器為ntp時間服務器,其他機器以此機器的時間為基准來進行同步了。

 

2. 搭建ntp服務器進行集群時間同步

本節以兩台服務器為例

192.168.1.101 

192.168.1.102

本節以192.168.1.101作為ntp服務器,192.168.1.102作為客戶端來同步服務器的時間, 在多節點的集群中道理是一樣的。

2.1 集群中所有機器安裝ntp時間同步工具

sudo yum -y install ntp ntpdate #安裝ntpdate時間同步工具

2.2  192.168.1.101時間同步服務器設置

啟動時間同步服務器

sudo systemctl start ntpd     #啟動時間同步程序
sudo systemctl enable ntpd  #允許時間同步程序開機啟動

修改/etc/ntp.conf文件,添加server設置127.127.1.0為其自身

sudo vim /etc/ntp.conf

在public servers節下添加

server     127.127.1.0   #設置自己作為時間同步服務器

在access節下新增加一個restrict段為可以接受服務的網段,本例為192.168.0.0

restrict 192.168.0.0

如下圖所示:

保存退出,並重啟ntp系統服務

sudo systemctl restart ntpd 

2.3 ntp同步客戶端設置

 本例中192.168.1.102為作為時間同步客戶端來同步服務器的時間。在192.168.1.102中編輯/etc/ntp.conf

sudo vim /etc/ntp.conf

在server下面添加一個時間同步服務器地址,本例為192.168.1.101

server  192.168.1.101

如圖:

 

在192.168.1.102中同步時間

sudo ntpdate   192.168.1.101  #如果需要查看調試信息,可以添加參數-d

 

 

2.4 所有節點設置ntp服務開啟啟動

sudo systemctl start ntpd       #啟動ntp服務
sudo systemctl enable ntpd    #設置ntp服務開機啟動

 

2.5 所有節點啟動時間同步 

sudo timedatectl set-ntp yes

 


免責聲明!

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



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