NTP时间服务器实现linux时间同步

在linux下,我们可以通过自带的NTP(Network Time Protocol)协议通过网络使自己的系统保持精确的时间。可用的公共时间服务器列表可以从下面的地址获取: http://ntp.isc.org/bin/view/Servers/NTPPoolServers 什么是NTP NTP是用来使系统和一个精确的时间源保持时间同步的协议。建议大家在自己管理的网络中建立至少一台时间服务器来同步本地时间,这样可以使得在不同的系统上处理和收集日志和管理更加容易。 同样,也有很多免费的NTP客户端可供Windows使用,你可以用它们来同步你windows的系统时间。 下载和安装 本文介绍如何在AS4上安装时间服务器(本文也同样适用于redhat和fedora core的其他版本),如果想获得NTP的最新版本,你可以下载tar包自己编译,这里只介绍RPM方式安装的NTP服务软件。 AS4带的NTP版本是4.2.0,你可以用下面的命令查看是否已经安装了这个软件包: # rpm -qa|grep ntp ntp-4.2.0.a.20040617-4.2 如果发现没有安装,请从光盘上安装这个软件。执行下面命令安装NTP的RPM包: # rpm -ivh ntp-4.2.0.a.20040617-4.2.i386.rpm /etc/ntp.conf这是NTP的主要配置文件,里面设置了你用来同步时间的时间服务器的域名或者IP地址,下面是到互联网同步时间的最基本的配置: 首先定义我们喜欢的时间服务器: server pool.ntp.org 接下来,我们设置上面两台服务器的访问权限,在这个例子中我们不允许它们修改或者查询我们配置在Linux上的NTP服务器 restrict default nomodify notrap noquery (这个默认在配置文件中就存在) 接下来设置允许访问我们时间服务器的客户机地址,通常这些服务器都应该位于我们自己局域网内。请注意,配置中noquery已经去掉了: restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap 在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间。 最后,也是最重要的是默认的限制配置要从你配置文件中删除,否则它将覆盖你所有的配置选项,你将发现如果不删除该配置,你的时间服务器将只能和自己通讯。如果ntp.conf中有以下一行,请将它注释(AS4附带的ntp默认没有): # restrict default ignore 保存你的配置文件,然后对每个你在ntp.conf里配置的时间服务器执行2编查询命令: # ntpdate pool.ntp.org 27 Jun 10:12:01 ntpdate[25475]: adjust time server pool.ntp.org offset -0.127154 sec # ntpdate ntp.research.gov 27 Jun 10:12:06 ntpdate[25478]: adjust time server pool.ntp.org 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 默认情况下,我们配置的NTP服务器不会去时间源那里同步时间,所以必须修改/etc/ntp/step-tickers文件,加入我们的时间源 0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org 这样每次通过/etc/init.d/ntpd 来启动服务的时候就会自动更新时间了 切记每次修改了配置文件后都需要重新启动服务来使配置生效。可以使用下面的命令来检查NTP 服务是否启动,你应该可以得到一个进程ID号: # pgrep ntpd 检查时间服务器是否正确同步 使用下面的命令检查时间服务器同步的状态: # ntpq -p 输出: remote refid st t when poll reach delay offset jitter ============================================================================== -jj.cs.umb.edu gandalf.sigmaso 3 u 95 1024 377 31.681 -18.549 1.572 milo.mcs.anl.go ntp0.mcs.anl.go 2 u 818 1024 125 41.993 -15.264 1.392 -mailer1.psc.edu ntp1.usno.navy. 2 u 972 1024 377 38.206 19.589 28.028 -dr-zaius.cs.wis ben.cs.wisc.edu 2 u 502 1024 357 55.098 3.979 0.333 +taylor.cs.wisc. ben.cs.wisc.edu 2 u 454 1024 347 54.127 3.379 0.047 -ntp0.cis.strath harris.cc.strat 3 u 507 1024 377 115.274 -5.025 1.642 *clock.via.net .GPS. 1 u 426 1024 377 107.424 -3.018 2.534 ntp1.conectiv.c 0.0.0.0 16 u - 1024 0 0.000 0.000 4000.00 一个可以证明同步问题的证据是所有远程服务器的jitter值是4000并且delay和reach的值是0。 remote refid st t when poll reach delay offset jitter ============================================================================== LOCAL(0) LOCAL(0) 10 l - 64 7 0.000 0.000 0.008 ntp-cup.externa 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00 snvl-smtp1.trim 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00 nist1.aol-ca.tr 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00 可能的原因有: 配置文件中的restrict default ignore没有被注释 有防火墙阻断了与server之间的通讯 此外每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接,否则你执行ntpdate ip的时候将返回: 27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found 配置Linux客户端 在linux客户端上执行ntpdate ntp_server_ip 就可以根据时间服务器统一局域网的时间了,将上面的命令放到cron里每天早上3点定期执行,crontab –e 然后输入 0 3 * * * /usr/sbin/ntpdate ntp_server_ip 中国大概能用的NTP时间服务器地址本上都能使 server 133.100.11.8 prefer server 210.72.145.44 server 203.117.180.36 ----windows我就用这个 server 131.107.1.10 server time.asia.apple.com server 64.236.96.53 server 130.149.17.21 server 66.92.68.246 server www.freebsd.org server 18.145.0.30 server clock.via.net server 137.92.140.80 server 133.100.9.2 server 128.118.46.3 server ntp.nasa.gov server 129.7.1.66 server ntp-sop.inria.frserver 210.72.145.44(国家授时中心服务器IP地址) ntpdate 131.107.1.10 ntpdate -s time.asia.apple.com / 当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个: 错误1.Server dropped: Strata too high 在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。 在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。 这是因为NTP server还没有和其自身或者它的server同步上。 以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。 server 127.127.1.0 fudge 127.127.1.0 stratum 8 在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。 那么如何知道何时ntp server完成了和自身同步的过程呢? 在ntp server上使用命令: # watch ntpq -p 出现画面: Every 2.0s: ntpq -p Thu Jul 10 02:28:32 2008 remote refid st t when poll reach delay offset jitter ============================================================================== 192.168.30.22 LOCAL(0) 8 u 22 64 1 2.113 179133. 0.001 LOCAL(0) LOCAL(0) 10 l 21 64 1 0.000 0.000 0.001 注意LOCAL的这个就是与自身同步的ntp server。 注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。 如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。 错误2.Server dropped: no data 从客户端执行netdate –d时有错误信息如下: transmit(192.168.30.22) transmit(192.168.30.22) transmit(192.168.30.22) transmit(192.168.30.22) transmit(192.168.30.22) 192.168.30.22: Server dropped: no data server 192.168.30.22, port 123 ..... 28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found 出现这个问题的原因可能有2: 1。检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。 使用以下命令检查ntp的版本: # ntpq -c version 下面是来自ntp官方网站的说明: The behavior of notrust changed between versions 4.1 and 4.2. In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time". In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd 解决: 把notrust去掉。 2。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。 可以用命令 #service iptables stop 来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。 /// # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. #restrict default kod nomodify notrap nopeer noquery #restrict -6 default kod nomodify notrap nopeer noquery # Permit all access over the loopback interface. This could # be tightened as well, but to do so would effect some of # the administrative functions. #restrict 127.0.0.1 #restrict -6 ::1 restrict default nomodify notrap # Hosts on local network are less restricted. restrict 172.16.65.0 mask 255.255.255.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.rhel.pool.ntp.org #server 1.rhel.pool.ntp.org #server 2.rhel.pool.ntp.org server 172.16.65.144 #broadcast 192.168.1.255 key 42 # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 key 42 # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 key 42 # manycast client # Undisciplined Local Clock. This is a fake driver intended for backup # and when no outside source of synchronized time is available. server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 # Drift file. Put this in a directory which the daemon can write to. # No symbolic links allowed, either, since the daemon updates the file # by creating a temporary in the same directory and then rename()'ing # it to the file. driftfile /var/lib/ntp/drift # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值