FreeBSD系统上配置时间同步服务ntpd

间隙填充
正睿科技  发布时间:2010-09-08 11:37:09  浏览数:1734
   本文介绍的是在一个ip段内的服务器群实现时间同步。以此服务器群的一台机器作为ntpd服务器,服务器群的其他机器作为客户端。

  (1)时间同步服务端的配置


以下是代码片段:
    vi /etc/ntp.conf 
  ------------------- 
  server 129.6.15.28 iburst # NIST, Gaithersburg, Maryl 
  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 < 
  server 129.6.15.28 iburst # NIST, Gaithersburg, Maryl 
  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 < 
  /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< 
  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 < 
  /usr/sbin/ntpd -g 
  EOF 
  chmod +x /usr/local/etc/rc.d/startntpd.sh 
  " 
  done 
  exit 0

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