当前位置:操作系统 > Unix/Linux >>

系统时间同步及ntp服务的提供

电脑的时间放置不理的话过不久就会出现误差。

  这个误差会反映在email送信时刻的time stamp上,会反映在你的论坛帖子发表时刻上。至少我遇到过这个问题。

  网络上有ntp(network time protocol)服务器,提供当前的时刻。我们可以将本地的时刻与ntp服务器的时刻进行同步。

  目的如下:

  1,启动时用ntpdate进行时刻修正。

  2,启动中,通过ntpd进行时刻校正的同时,将各个客户端电脑的时刻也进行同期校正。

  3,如果不能找到ntp服务器,那么就使用local time,同时同步客户。

  全世界约有100多个ntp服务器,从下面地址找一个离你最近的

  http://www.eecis.udel.edu/~mills/ntp/clock1a.html 我选择了两个

  clock.nc.fukuoka-u.ac.jp 133.100.9.2clock.tl.fukuoka-u.ac.jp 133.100.11.8步骤:

  FreeBSD中有两个标准的ntp程序,其一为ntpdate。

  ntpdate通过从ntp服务器获取时刻,调整本地时刻。

  # ntpdate clock.nc.fukuoka-u.ac.jp9 Oct 18:12:23 ntpdate: step time server 133.100.9.2 offset -19.112674 大约有19秒钟的误差

  自动设定

  # grep ntpdate /etc/defaults/rc.conf >>/etc/rc.conf# vi /etc/rc.confntpdate_enable="YES" # Run ntpdate to sync time on boot (or NO).ntpdate_program="ntpdate" # path to ntpdate, if you want a different one.ntpdate_flags="clock.nc.fukuoka-u.ac.jp" # Flags to ntpdate (if enabled). 重新启动以后就运行了,当然设定没有完,不必急着启动

  FreeBSD的另一个标准ntp程序,ntp

  ntp程序使的获取ntp时刻的同时,向其他pc提供时刻。

  添加文件ntp.conf:

  用来作为标准时刻的ntp服务器我选择了两个

  clock.nc.fukuoka-u.ac.jp 133.100.9.2clock.tl.fukuoka-u.ac.jp 133.100.11.8同时指定复数个服务器也没有问题,系统会自动选择一个可以信赖的。

  这里,为避免多余的DNS数据包传递,我们直接指定IP地址。

  server行 server 127.127.1.0 为参考本地时刻时用的地址。然后用fudge指定阶层编号为5,降低其优先度。

  接着用restrict对每一个IP地址指定相应的规则。

  最后,指定波长校正用的drift文档保存地址。关于这个命令行,具体的含义不太清楚。不过如果没有的话,时间校正起来就会比较慢

  

  # vi /etc/ntp.confserver 133.100.9.2 #clock.nc.fukuoka-u.ac.jpserver 133.100.11.8 #clock.tl.fukuoka-u.ac.jpserver 127.127.1.0fudge 127.127.0.1 stratum 5restrict default ignorerestrict 127.0.0.0 mask 255.0.0.0restrict 192.168.1.0 mask 255.255.255.0 noquery nopeer notrustrestrict 133.100.9.2 noqueryrestrict 133.100.11.8 noquerydriftfile /etc/ntpd.drift 启动测试

  # ntpd -p /var/run/ntpd.pid# tail /var/log/messagesOct 9 16:46:56 chiwawa ntpd[89409]: ntpd 4.1.0-a Thu Apr 3 08:26:24 GMT 2003 (1)Oct 9 16:46:56 chiwawa ntpd[89409]: kernel time discipline status 2040......Oct 9 16:50:10 chiwawa ntpd[89409]: time set -0.189546 s 看到类似的结果就可以了。

  运行测试

  ntpd的运行用ntpq命令

  # ntpq -premote refid st t when poll reach delay offset jitter==============================================================================*clock.nc.fukuok .GPS. 1 u 43 64 37 19.067 -6.884 10.339+clock.tl.fukuok .GPS. 1 u 36 64 35 19.670 -3.259 2.341LOCAL(0) LOCAL(0) 5 l 45 64 37 0.000 0.000 0.001 启动后到时刻校正完成需要一点时间。*是第一候补,+是第二。

  本地时刻校正完成以后就可以为其他PC提供时刻校正服务了。

  自动启动的设定

  完成以上设定,确认运行无误以后:

  # grep ntpd /etc/defaults/rc.conf >>/etc/rc.conf# vi /etc/rc.confxntpd_enable="YES" # Run ntpd Network Time Protocol (or NO).xntpd_program="ntpd" # path to ntpd, if you want a different one.xntpd_flags="-p /var/run/ntpd.pid" # Flags to ntpd (if enabled).

  往rc.conf追加上面3行内容,并修改。

  客户端PC设定:

  UNIX OS

  # vi /etc/ntp.confserver [local ntp server IP] preferdriftfile /etc/ntpd.drift或者追加下面内容到crontab,这样,每过一个小时0分的时候就自动更新。# vi /etc/crontab0 * * * * root ntpdate [ntp server IP] >/dev/null 2>&1当然,你的主机其实也是一个客户机,这些内容也可以用上。 Windows

  

  精工的网站上有下载软件,不过是日语的http://www.seiko-p.co.jp/systems/down/time.htmlwindows2000自带了sntp机能,请自己研究吧。 BBSURL:

  http://chiwawa.plala.jp/discuz/viewthread.php?tid=1027
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,