FreeBSD上配置時間同步服務ntpd

本文介紹的是在一個ip段內的服務器羣實現時間同步。以此服務器羣的一臺機器作爲ntpd服務器,服務器羣的其他機器作爲客戶端。

(1)時間同步服務端的配置
 vi /etc/ntp.conf  
 -------------------
server 129.6.15.28 iburst # NIST, Gaithersburg, Maryland
server 132.163.4.101 iburst # NIST, Boulder, Colorado
driftfile /etc/ntp.drift
broadcast 10.253.1.255   # 這裏要根據本機所在網段,填寫廣播地址
logfile /var/log/ntp.log
------------------------
下面啓動ntp服務
# ntpd –g
把ntp服務設置爲機器開機自啓動
vi /usr/local/etc/rc.d/startntpd.sh
加入:/usr/sbin/ntpd -g
chmod +x /usr/local/etc/rc.d/startntpd.sh

(2)時間同步客戶端的配置
 vi /etc/ntp.conf  
 ---------------------
server 121.9.205.73 iburst   此處修改爲服務端IP
driftfile /etc/ntp.drift
broadcastclient
logfile /var/log/ntp.log
-----------------------
下面啓動ntp服務
# ntpd -g
把ntp服務設置爲機器開機自啓動
vi /usr/local/etc/rc.d/startntpd.sh
加入:/usr/sbin/ntpd -g
chmod +x /usr/local/etc/rc.d/startntpd.sh

配置好之後可查看服務器端和客戶端的/var/log/ntp.log查看是否進行了時間同步。

---------------------------------------------------------------------------

可使用腳本進行批量的配置操作:
1,服務器端配置腳本
vi ntp_server.sh
-----------------
#!/bin/bash

ip='服務端ip'

ssh $ip "

ntpdate time-a.nist.gov

ipb=\`ifconfig bce0 |grep inet|awk '{print \$NF}'\`
cat >>/etc/ntp.conf <<EOF
server 129.6.15.28 iburst # NIST, Gaithersburg, Maryland
server 132.163.4.101 iburst # NIST, Boulder, Colorado
driftfile /etc/ntp.drift
broadcast \$ipb
logfile /var/log/ntp.log
EOF

cat >/usr/local/etc/rc.d/startntpd.sh <<EOF
/usr/sbin/ntpd -g
EOF

chmod +x /usr/local/etc/rc.d/startntpd.sh
"
ssh $ip "/usr/sbin/ntpd –g"

exit 0


2、客戶端配置腳本
vi ntp_client.sh
-------------------
#!/bin/bash

ip='客戶端ip羣'
ipserver_local='服務端ip'

for i in $ip ;do
echo $i
ssh $i "

cat >/etc/ntp.conf<<EOF
server $ipserver_local iburst
driftfile /etc/ntp.drift
broadcastclient
logfile /var/log/ntp.log
EOF

/usr/sbin/ntpd -g

cat >/usr/local/etc/rc.d/startntpd.sh <<EOF
/usr/sbin/ntpd -g
EOF

chmod +x /usr/local/etc/rc.d/startntpd.sh
"
done

exit 0

注:以上兩腳本是在中控端發起操作的。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章