centos7下配置時間同步服務器


同網段20幾台服務器:

其中有一組mysql 集群中 互為主從

選一台mysql master 作為時間同步的服務器,這樣做的好處以便於這台down了 另一個與他互為主從的master 繼續提供時間同步服務。

假如 這個mysql 集群  ip是這樣分配

master1 :10.0.0.11

master2  :10.0.0.12

其他 。。。。。省略

vip: 10.0.0.20

  1. 選擇一台服務器作為時間服務器。(master1做為時間服務器)
  2. 使用root用戶,查看服務器是否安裝ntp服務 
    rpm -qa|grep ntp
    timedatectl set-timezone Asia/Shanghai

     沒有安裝的話使用yum install進行安裝。

  3.   修改文件 /etc/ntp.conf,一共修改三處內容:
  4.   ①將#去掉。並且將網段修改正確。
    restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
  5.  ②將以下4個server進行#注釋掉
    #server 0.centos.pool.ntp.org iburst
    #server 1.centos.pool.ntp.org iburst
    #server 2.centos.pool.ntp.org iburst
    #server 3.centos.pool.ntp.org iburst

    server ntp1.aliyun.com
    server ntp2.aliyun.com
    server ntp3.aliyun.com
    server ntp4.aliyun.com
    server ntp5.aliyun.com
    server ntp6.aliyun.com
    server ntp7.aliyun.com

  6. ③最后添加倆句話:

    server 127.127.1.0 #local clock
    fudge  127.127.1.0 stratum 10
  7. 編輯etc/sysconfig/ntpd文件
    # Drop root to id 'ntp:ntp' by default.
    SYNC_HWCLOCK=yes
    OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
  8. 啟動ntpd服務,並且設置開機啟動

    systemctl start ntpd.service
    systemctl enable ntpd.service

     

  9. 新建同步 master1 服務器的腳本。(每隔5分鍾同步一次maste1服務器)
[root@slave2 ~]# cat /opt/script/ntp.sh 
#!/bin/bash

while true
do
  /usr/sbin/ntpdate 10.0.0.20 &> /dev/null
  sleep 300
done

centos7 的開機自啟動文件默認沒有執行權限 需要添加以下,並設置腳本開機自啟動

chmod +x /etc/rc.d/rc.local
chmod +x /opt/script/ntp.sh


vi /etc/rc.d/rc.local #加入下面的命令
/opt/script/ntp.sh &

10.重啟后查看  腳本進程是否在后台運行着

 

 這里基本就完成了,有個地方要說下,一開始准備用定時任務執行命令的,但是不知道這centos7 有什么毛病,死活不執行,索性用個死循環腳本放后台執行吧 ,並設置開機自啟動腳本。

 


免責聲明!

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



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