ntp服务简单配置:
1. ntp服务端主要配置:
/etc/ntp.conf
- # Permit time synchronization with our time source, but do not
- # permit the source to query or modify the service on this system.
- 第一种:
- # restrict default modify ntrap 允许任何IP的机器都可以进行时间同步
- 第二种:
- # restrict default nomodify notrap noquery 表示拒绝所有IP的时间同步
- # restrict 172.16.0.0 mask 255.255.0.0 nomodify notrap 只允许172.16.x客户端网段同步
- 第三种:
- restrict default kod nomodify notrap nopeer noquery
- restrict -6 default kod nomodify notrap nopeer noquery
- # Hosts on local network are less restricted.
- restrict 172.16.0.0 mask 255.255.0.0 nomodify notrap
- # Use public servers from the pool.ntp.org project.
- # Please consider joining the pool (http://www.pool.ntp.org/join.html).
- server 0.centos.pool.ntp.org
- server 1.centos.pool.ntp.org
- server 2.centos.pool.ntp.org
- driftfile /var/lib/ntp/drift
service ntpd start 启动ntp服务
如果想每次系统启动,NTP服务自动启动,请输入下面命令:
chkconfig -level 35 ntpd on
注:Ntpd启动的时候通常需要一段时间进行时间同步,所以在ntpd刚刚启动的时候还不能正常提供时钟服务,最长大概有5分钟吧,如果超过了这个时间请检查一下您的配置文件。
2. 客户端时钟同步配置:
管理机上远程批量登录客户机拉取并执行ntp安装配置脚本:
- #****************ntp-init.sh****************#
- # time-sync #
- # the remote executive pull scripts #
- # by iceeggplant,2012.6.6 #
- #*******************************************#
- #define variable part
- hosts='67 68 80 81 82 83 84 85 ………'
- ip=172.20.2.
- user=adms
- for i in $hosts
- do
- ping -w 1 -c 1 $ip$i >/dev/null
- ret=$?
- if [ $ret -eq 0 ]
- then
- echo "start remote ${ip}$i:"
- 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"
- else
- echo "${ip}${i} host is not exist or network is not connect,continue next host...."
- fi
- done
ntp安装配置脚本:wy-ntp.sh
- #******************wy-ntp.sh****************#
- # ntp-service install and configure #
- # by-iceeggplant 2012.6.6 #
- #*******************************************#
- #!/bin/sh
- plat=`uname -i`
- [ -f /usr/sbin/ntpdate ] && echo the ntp-service is installed ,exit... && exit
- case "$plat"
- in
- x86_64)
- yum -y install ntp.x86_64;;
- i386)
- yum -y install ntp.i386;;
- esac
- [ ! -d /root/tasks ] && mkdir /root/tasks
- echo -e "#!/bin/sh\n/usr/sbin/ntpdate 172.16.2.81\n/sbin/hwclock --systohc">>/root/tasks/nt.sh
- chmod u+x /root/tasks/nt.sh
- sh /root/tasks/nt.sh
- #crontab
- 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
转载于:https://blog.51cto.com/iceeggplant/891359