第一步 安装软件包
#rpm -qa | grep ntp
ntp-4.2.0-7
chkfontpath-1.10.0-1
第二步 让LAN的时间服务器(第三级)与互联网上的时间服务器(第一或者第二级)同步
修改/etc/ntp.conf
这是NTP的主要配置文件,里面设置了你用来同步时间的时间服务器的域名或者IP地址,下面是到互联网同步时间的最基本的配置:
首先定义我们喜欢的时间服务器:
server ntp.research.gov
server otherntp.research.gov
接下来,我们设置上面两台服务器的访问权限,在这个例子中我们不允许它们修改或者查询我们配置在Linux上的NTP服务器
restrict ntp.research.gov mask 255.255.255.255 nomodify notrap noquery
restrict otherntp.research.gov mask 255.255.255.255 nomodify notrap noquery
掩码255.255.255.255是用来限制远程NTP服务器的掩码地址。
接下来设置允许访问我们时间服务器的客户机地址,通常这些服务器都应该位于我们自己局域网内。请注意,配置中noquery已经去掉了:
restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap
在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间。
最后,也是最重要的是默认的限制配置要从你配置文件中删除,否则它将覆盖你所有的配置选项,你将发现如果不删除该配置,你的时间服务器将只能和自己通讯。如果ntp.conf中有以下一行,请将它注释:
#restrict default ignore
保存你的配置文件,然后对每个你在ntp.conf里配置的时间服务器执行2编查询命令:
#ntpdate ntp.research.gov
27 Jun 10:12:01 ntpdate[25475]: adjust time server ntp.research.gov offset -0.127154 sec
#ntpdate ntp.research.gov
27 Jun 10:12:06 ntpdate[25478]: adjust time server ntp.research.gov offset 0.010008 sec
第三步 启动NTP进程
为了使NTP服务可以在系统引导的时候自动启动,执行:
#chkconfig ntpd on
启动/关闭/重启NTP/查看状态的命令是:
#/etc/init.d/ntpd start
#/etc/init.d/ntpd stop
#/etc/init.d/ntpd restart
#/etc/init.d/ntpd status
切记每次修改了配置文件后都需要重新启动服务来使配置生效。可以使用下面的命令来检查NTP服务是否启动,你应该可以得到一个进程ID号:
#pgrep ntpd
第四步 检查时间服务器是否正确同步
使用下面的命令检查时间服务器同步的状态:
#ntpq –p
一个可以证明同步问题的证据是所有远程服务器的jitter值是4000并且delay和reach的值是0。可能的原因有:
配置文件中的restrict default ignore没有被注释
有防火墙阻断了与server之间的通讯
此外每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接,否则你执行ntpdate ip的时候将返回:
27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found
第五步 客户端与ntp服务器同步时间
客户端开启ntp服务
#service ntpd start
与ntp服务器同步
#ntpdate (ntp服务器地址)
#设置此服务器同上层服务器做时间同步的IP地址,prefer意味着首选IP地址
#经试验,下面的几个时间服务器速度还不错
server 210.72.145.44 prefer
server 218.21.130.42
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
#记录上次的NTP server与上层NTP server联接所花费的时间
driftfile /etc/ntp/drift
#设置默认策略为允许任何主机进行时间同步
restrict default ignore
#设置允许访问此时间服务器的时间服务的IP地址
#根据自己实际情况配置
restrict 127.0.0.1
restrict 192.168.10.2 # 指定某台机器时间同步
restrict 192.168.0.0 mask 255.255.255.0 #允许192.168.0.0/254子网内主机时间同步
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap #允许任何主机跟进行时间同步
#指定阶层编号为10,降低其优先度。
fudge 127.127.1.1 stratum 10
#设置ntp日志的path
statsdir /var/log/ntp/
#设置ntp日志文件
logfile /var/log/ntp/ntp.log