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

文章作者 100test 发表时间 2008:04:19 12:38:40
来源 100Test.Com百考试题网


  电脑的时间放置不理的话过不久就会出现误差。
  这个误差会反映在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.2
  clock.tl.fukuoka-u.ac.jp 133.100.11.8

步骤:

 FreeBSD中有两个标准的ntp程序,其一为ntpdate。
 ntpdate通过从ntp服务器获取时刻,调整本地时刻。

 # ntpdate clock.nc.fukuoka-u.ac.jp
 9 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.conf
 ntpdate_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.2
  clock.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.conf
 server 133.100.9.2 #clock.nc.fukuoka-u.ac.jp
 server 133.100.11.8 #clock.tl.fukuoka-u.ac.jp
 server 127.127.1.0
 fudge 127.127.0.1 stratum 5
 restrict default ignore
 restrict 127.0.0.0 mask 255.0.0.0
 restrict 192.168.1.0 mask 255.255.255.0 noquery nopeer notrust
 restrict 133.100.9.2 noquery
 restrict 133.100.11.8 noquery
 driftfile /etc/ntpd.drift
启动测试

 # ntpd -p /var/run/ntpd.pid
 # tail /var/log/messages
 Oct 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 -p
 remote 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.341
 LOCAL(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.conf
 xntpd_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.conf
 server [local ntp server IP] prefer
 driftfile /etc/ntpd.drift
或者追加下面内容到crontab,这样,每过一个小时0分的时候就自动更新。
 # vi /etc/crontab
 0 * * * * root ntpdate [ntp server IP] >/dev/null 2>&.1
当然,你的主机其实也是一个客户机,这些内容也可以用上。



相关文章


试辅导资料:报关单位的海关法律责任
细胞外信号调节激酶在局灶性脑缺血中的表达
脑海绵状血管瘤遗传家系永生细胞株的建立
异位子宫内膜孕激素受体亚型表达与局部孕激素抵抗
系统时间同步及ntp服务的提供
实用技巧使用MD5加密GRUB密码操作步骤
新手看招:介绍Linux系统下的四个串口
备考:注册税务师考试历年命题特点分析
2007年注册税务师考试税法必背内容汇总
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛