ntp服务简单配置:

1. ntp服务端主要配置:

/etc/ntp.conf

 
  
  1. # Permit time synchronization with our time source, but do not  
  2. # permit the source to query or modify the service on this system.
  3. 第一种:
  4. # restrict default modify ntrap 允许任何IP的机器都可以进行时间同步
  5. 第二种:
  6. # restrict default nomodify notrap noquery 表示拒绝所有IP的时间同步
  7. # restrict 172.16.0.0 mask 255.255.0.0 nomodify notrap 只允许172.16.x客户端网段同步
  8. 第三种:
  9. restrict default kod nomodify notrap nopeer noquery  
  10. restrict -6 default kod nomodify notrap nopeer noquery  
  11.  
  12. # Hosts on local network are less restricted.  
  13. restrict 172.16.0.0 mask 255.255.0.0 nomodify notrap  
  14.  
  15. # Use public servers from the pool.ntp.org project.  
  16. # Please consider joining the pool (http://www.pool.ntp.org/join.html).  
  17. server 0.centos.pool.ntp.org  
  18. server 1.centos.pool.ntp.org  
  19. server 2.centos.pool.ntp.org  
  20.  
  21. driftfile /var/lib/ntp/drift  

service ntpd start 启动ntp服务

如果想每次系统启动,NTP服务自动启动,请输入下面命令:
chkconfig -level 35 ntpd on

 注:Ntpd启动的时候通常需要一段时间进行时间同步,所以在ntpd刚刚启动的时候还不能正常提供时钟服务,最长大概有5分钟吧,如果超过了这个时间请检查一下您的配置文件。

2. 客户端时钟同步配置:

管理机上远程批量登录客户机拉取并执行ntp安装配置脚本:

 
  
  1. #****************ntp-init.sh****************#  
  2. #           time-sync                       #  
  3. # the remote executive pull scripts         #  
  4. #           by iceeggplant,2012.6.6         #  
  5. #*******************************************#  
  6.  
  7. #define variable part  
  8. hosts='67 68 80 81 82 83 84 85 ………' 
  9. ip=172.20.2.  
  10. user=adms 
  11.  
  12. for i in $hosts  
  13. do  
  14.    ping -w 1 -c 1 $ip$i >/dev/null  
  15.    ret=$?  
  16.    if [ $ret -eq 0 ]  
  17.    then  
  18.         echo "start remote ${ip}$i:"  
  19.         ssh $user@$ip$i "sudo wget http://10.0.0.1/xx/wy-ntp.sh && sudo chmod +x wy-ntp.sh && sudo sh wy-ntp.sh"    
  20.    else  
  21.         echo "${ip}${i} host is not exist or network is not connect,continue next host...."  
  22.    fi  
  23. done  

 ntp安装配置脚本:wy-ntp.sh

 
  
  1. #******************wy-ntp.sh****************#  
  2. #    ntp-service  install  and  configure   #  
  3. #           by-iceeggplant 2012.6.6        #  
  4. #*******************************************#  
  5. #!/bin/sh  
  6. plat=`uname -i`  
  7.  
  8. [ -f /usr/sbin/ntpdate ] && echo the ntp-service is installed ,exit... && exit  
  9. case "$plat"  
  10.  in  
  11.   x86_64)  
  12.         yum -y install ntp.x86_64;;  
  13.   i386)  
  14.         yum -y install ntp.i386;;  
  15. esac  
  16.  
  17. [ ! -d /root/tasks ] && mkdir /root/tasks  
  18.  
  19. echo -e "#!/bin/sh\n/usr/sbin/ntpdate 172.16.2.81\n/sbin/hwclock --systohc">>/root/tasks/nt.sh  
  20. chmod u+x /root/tasks/nt.sh  
  21. sh /root/tasks/nt.sh  
  22.  
  23. #crontab  
  24. echo "1  1  *  *  1  sh /root/tasks/nt.sh" >>/var/spool/cron/root  

 附:

#/usr/sbin/ntpdate -u ip  从时间服务器更新系统时间

#/usr/sbin/ntpdate -q ip  查询不更新

#/usr/sbin/ntpdate -d ip  客户端同步验证

#watch ntpq -p  服务端同步验证,查询网络中的NTP服务器,同时显示客户端和每个服务器的关系

#hwclock --systohc  将机器硬件时钟同步为系统时钟

 

注:NTP服务的端口是123,使用的是udp协议,所以NTP服务器的防火墙必须对外开放udp 123这个端口。

#/iptables -A INPUT -p UDP -i eth0 -s 172.16.0.0/16  --dport 123 -j ACCEPT
   

ntp详细说明可参考:

http://hi.baidu.com/susuper_/blog/item/f0a7a1594c568b0c377abe4c.html

http://www.cnblogs.com/zhangyx999/archive/2009/12/22/1630106.html