ntpd和ntpdate

以无GUI 的Ubuntu 为例

1. ntpdate

ntpdate是Ubuntu发行版的标配。按Ubuntu14.04的官方文档(这里),Ubuntu在启动时,会调用ntpdate,根据Ubuntu NTP server更新系统时间,但我实际测试却并不成功。

注:测试方法:sudo sysv-rc-conf npt off -> 修改为一个过去的时间sudo date -s hh:mm:ss ,并用date检查是否已修改-> 同步到硬件sudo hwclock -w ->

sync;sync;sync;sudo reboot ->开机后等待10分钟,发现date还是错误时间。

所以,对于客户端,那就在crontab中增加一个任务,如下,其中weblbserver-1是ntp服务器,如果你想出错时(比如weblbserver无法连接)发邮件,那要设置MAILTO=yourmail,

并且还要安装postfix,安装也非常简单,直接用apt-get install就行,安装完无需任何配置。

*/10  * * * *   root    (ntpdate weblbserver-1 && hwclock -w) > /dev/null

 

如果开启了nptd服务,那就无法执行ntpdate了,如下

zhj@test:~$ sudo service ntp status
 * NTP server is running
zhj@test:~$
zhj@test:~$ sudo ntpdate pool.ntp.org
29 Dec 12:55:26 ntpdate[2964]: the NTP socket is in use, exiting
zhj@test:~$

 

2. ntpd

下面是自己的ntp server——weblbserver-1的配置文件/etc/ntp.conf,它是集群中的一台服务器,它允许集群内的其它服务器从它这里获取时间。

该配置文件修改后,要调用sudo service ntp reload重新加载。另外,注意在ntp server上要在防火墙中放行UDP 123号端口

ntpd启动后,要等4*2^(minpoll ~ maxpoll) 秒才能真正开始同步,所以minpoll和maxpoll不要设置的太大。

配置文件参数含义可参考 http://wenku.baidu.com/view/e3282cc62cc58bd63186bd41.html

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
# 参数minpoll和maxpoll都是2的幂,下面即设置2^9=512秒同步一次 server 0.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 1.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 2.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 server 3.ubuntu.pool.ntp.org minpoll 9 maxpoll 9 # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery restrict -6 default kod notrap nomodify nopeer noquery # Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1 # Clients from this (example!) subnet have unlimited access, but only if # cryptographically authenticated.
# 放行局域网内的主机,可以从本机获取ntp时间,但不能修改本机的时间 restrict
192.168.0.0 mask 255.255.0.0 nomodify

同时,在本ntp server主机增加一个定时任务,每小时将osclock刷入hwclock,如下,如果想发邮件,要设置MAILTO,并安装postfix

*/10  *   * * *   root    hwclock -w > /dev/null

 

我们可以用ntpq -p来查看本机与上层NTP server的时间同步情况,如下

zhj@weblbserver-1:~$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 dns1.synet.edu. 202.118.1.46     2 u  915 1024    1   43.464  -134.79   0.004
 golem.canonical 192.93.2.20      2 u  914 1024    1  433.083  -198.90   0.004
zhj@weblbserver-1:~$

remote ——亦即 NTP 主机的 IP 或主机名,注意最左边的符号

  • 如果有『 * 』代表目前正在作用当中的上层 NTP
  • 如果是『 + 』代表也有连上线,而且可作为下一个提供时间更新的候选者
  • 如果是空格,那代表还没有连接

refid  —— 参考的上一层 NTP 主机的地址

st      —— 就是 stratum 阶层,ntp允许的层数为1~15

when —— 上次同步时间到现在的距离,单位是秒。当然,如果ntp服务是刚启动,还没同步过,那这个参数就是ntp服务启动后到现在的距离

poll   —— 同步周期,单位为秒

reach —— 它是八进制数,正常情况下值为[0, 1, 3, 7, 17, 37, 77, 177, 377],对应的二进制为[0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111],

               ntp服务启动后,reach就以poll值为周期与ntp server通信,为了方便理解,我们可以简单的认为每次ping一下上层ntp server,如果成功,那

               reach就向左移一位,右边补1,如果失败,则右边补0,所以如果reach不是上面给出的枚举值,那就是在通信过程中出错了。当reach 达到17时

             (对应1111,即最近的四次通信都成功了),那才开始同步时间,这时,remote项对应的域名或IP列表有,其中一个前面会有*号,表示该IP就是

               NTP server。而在开始同步时间之前,当客户端访问weblbserver-1这个NTP server时,都会出现stratum 16,no server suitable for

               nchronization found这样的错误。也就是说如果你在NTP server主机上重启了ntp服务,那要等4*poll秒(在前四次通信都是成功的前提下),

               该NTP server才与上层NTP server开始同步时间,而且只有当开始同步时,该NTP server才能为其它客户端提供NTP服务。因此,你在/etc/ntp.conf

               中设置的同步周期minpoll maxpoll不能太大,因为每次ntp服务重启后,要等4倍长的时间才能开始同步。

offset —— NTP server与本机的时间差,单位为毫秒,即10^(-3) 秒。如果这个值比较大,那就要考虑缩短同步周期了

delay —— 网络传输过程当中延迟的时间,单位为 10^(-6) 秒

jitter —— Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒

 

posted @ 2014-12-29 01:33  奋斗终生  Views(6648)  Comments(0Edit  收藏  举报