以无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就行,安装完无需任何配置。

*/  * * * *   root    (ntpdate weblbserver- && 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
Dec :: ntpdate[]: 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() 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 -- (LP: #). See http://www.pool.ntp.org/join.html for
# more information.
# 参数minpoll和maxpoll都是2的幂,下面即设置2^9=512秒同步一次
server .ubuntu.pool.ntp.org minpoll 9 maxpoll
server .ubuntu.pool.ntp.org minpoll maxpoll
server .ubuntu.pool.ntp.org minpoll maxpoll
server .ubuntu.pool.ntp.org minpoll maxpoll # 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 - default kod notrap nomodify nopeer noquery
restrict - default kod notrap nomodify nopeer noquery # Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict :: # 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

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

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

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

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) 秒

最新文章

  1. 【C++】自绘控件基础
  2. iOS-UITextField-通知
  3. Power Gating的设计(模块)
  4. React Native分析(index.ios.js)
  5. Apache Commons 简述
  6. 【BZOJ 1491】 [NOI2007]社交网络
  7. (八)学习CSS之line-style-type属性
  8. [置顶] C#扩展方法 扩你所需
  9. 【甘道夫】HBase连接池 -- HTablePool是Deprecated之后
  10. 承诺c指针 (1)指针是地址
  11. .NetCore~C#6的一些新特性
  12. 改变 font Awesome、Ionic 图标属性
  13. axios 封装
  14. 【Linux】Ubuntu安装Mysql 8.0
  15. 吉特日化MES-日化行业原料仓库所见问题汇总
  16. 四大中三家已面向客户推出机器人业务解决方案?别逗了,先用机器人自我革命吧! post by 上海嘉冰信息技术
  17. centos安装 Anaconda3及使用
  18. ubuntu18.04下搭建深度学习环境anaconda2+ cuda9.0+cudnn7.0.5+tensorflow1.7【原创】【学习笔记】
  19. Beta阶段第1周/共2周 Scrum立会报告+燃尽图 03
  20. HDU 1717 小数化分数2(最大公约数)

热门文章

  1. STL笔记(3) copy()之绝版应用
  2. D3D渲染流程--转载
  3. 1038: [ZJOI2008]瞭望塔
  4. linux嵌入式系统驱动程序的阻塞与异步
  5. [转] Qt 多线程学习
  6. UVALive 5532 King(差分约束,spfa)
  7. 一个基于PDO的数据库操作类(新) 一个PDO事务实例
  8. 物联网操作系统HelloX已成功移植到MinnowBoard MAX开发板上
  9. mysql无法插入中文字符解决
  10. shell 里把命令的输出赋给变量 以及变量的使用