zoukankan      html  css  js  c++  java
  • 系统时间同步及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.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
    当然,您的主机其实也是个客户机,这些内容也能够用上。
    Windows
    精工的网站上有下载软件,但是是日语的
    http://www.seiko-p.co.jp/systems/down/time.html
    windows2000自带了sntp机能,请自己研究吧。
    BBSURL:
    http://chiwawa.plala.jp/discuz/viewthread.php?tid=1027
  • 相关阅读:
    百度之星资格赛1001——找规律——大搬家
    HDU1025——LIS——Constructing Roads In JGShining's Kingdom
    DP(递归打印路径) UVA 662 Fast Food
    递推DP UVA 607 Scheduling Lectures
    递推DP UVA 590 Always on the run
    递推DP UVA 473 Raucous Rockers
    博弈 HDOJ 4371 Alice and Bob
    DFS(深度) hihoCoder挑战赛14 B 赛车
    Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
    DP(DAG) UVA 437 The Tower of Babylon
  • 原文地址:https://www.cnblogs.com/tester2test/p/1300109.html
Copyright © 2011-2022 走看看