本文介绍的是在一个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

注:以上两脚本是在中控端发起操作的。