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

ntpd和ntpdate的更多相关文章

  1. 分布式集群中,设定时间同步服务器,以及ntpd与ntpdate的区别

    什么时候配置时间同步? 当分布式集群配置好了以后,马上配置的是SSH无密钥配置,然后就是配置时间同步. 时间同步在集群中特别重要. 一:时间同步 1.时间同步 集群中必须有一个统一的时间 如果是内网, ...

  2. Linux下的ntpd和ntpdate

    两者有个比较实质性的差异是,ntpd在实际同步时间时是一点点的校准过来时间的,最终把时间慢慢的校正对.而ntpdate不会考虑其他程序是否会阵痛,直接调整时间.一个是校准,一个是调整.另外ntpd 在 ...

  3. linux时间同步,ntpd、ntpdate

    linux时间同步,ntpd.ntpdate 在Windwos中,系统时间的设置很简单,界面操作,通俗易懂.而且设置后,重启,关机都没关系.系统时间会自动保存在Bios的时钟里面,启动计算机的时候,系 ...

  4. ntpd与ntpdate的区别

    之前配置ntpd的时候搜到一句话,印象很深刻,也觉得很有标题党的效果,就借鉴为标题了:“我认为有几种人是必须不招聘/裁掉的: 1 用ntpdate代替ntpd的人”但具体原因不太懂,总觉得还是用ntp ...

  5. Linux 时间同步 02 ntpd、ntpdate的区别

    Linux 时间同步 02 ntpd.ntpdate的区别 目录 Linux 时间同步 02 ntpd.ntpdate的区别 [一]这样做不安全. [二]这样做不精确. [三]这样做不够优雅. ntp ...

  6. 时间篇之centos6下修复的ntp操作(ntpd和ntpdate两个服务区别)

    系统采样,本采样和命令都是在centos6.4的系统中进行 主要比较centos7和centos6之间的差异,因为大部分都开始采用centos7但是有些老系统还采用centos6,这样我们就需要熟悉c ...

  7. ntpd和ntpdate的区别

    之前配置ntpd的时候搜到一句话,印象很深刻,也觉得很有标题党的效果,就借鉴为标题了:“我认为有几种人是必须不招聘/裁掉的: 1 用ntpdate代替ntpd的人”但具体原因不太懂,总觉得还是用ntp ...

  8. 030 分布式集群中,设定时间同步服务器,以及ntpd与ntpdate的区别

    什么时候配置时间同步? 当分布式集群配置好了以后,马上配置的是SSH无密钥配置,然后就是配置时间同步. 时间同步在集群中特别重要. 一:时间同步 1.时间同步 集群中必须有一个统一的时间 如果是内网, ...

  9. ntpd、ntpdate、hwclock的区别

    hwclock --systohc 使用ntpdate更新系统时间 - 潜龙勿用 - CSDN博客https://blog.csdn.net/suer0101/article/details/7868 ...

随机推荐

  1. 《OD学hadoop》第二周0702

    大数据离线计算hadoop2.x 三周(6天) markdown文本剪辑器 罗振宇--跨年演讲,时间的朋友 http://tech.163.com/16/0101/11/BC87H8DF000915B ...

  2. Hibernate 的<generator class="native"></generator>的不同属性含义

    1) assigned主键由外部程序负责生成,无需Hibernate参与. 2) hilo通过hi/lo 算法实现的主键生成机制,需要额外的数据库表保存主键生成历史状态. 3) seqhilo与hil ...

  3. HDU 1394 (逆序数) Minimum Inversion Number

    原来求逆序数还可以用线段树,涨姿势了. 首先求出原始序列的逆序数,然后递推每一个序列的逆序数. #include <cstdio> #include <cstring> #in ...

  4. iOS中第三方框架刷新

    0.先加入主头文件 #import "MJRefresh.h" 1.添加下拉刷新 MJRefreshHeaderView *header = [MJRefreshHeaderVie ...

  5. phonegap配置启动动画

    以下有2种方式 1 主Active中 onCreate函数里添加代码 2 config.xml文件进行配置(对通过命令行模式下cordova命令行生成的可行) 确保自己安装了SplashScreen插 ...

  6. Android 轻量级ORM数据库开源框架ActiveAndroid 源码分析

    ActiveAndroid 项目地址在https://github.com/pardom/ActiveAndroid 关于他的详细介绍和使用步骤 可以看下面两篇文章: https://github.c ...

  7. WPF的控件Binding的ElementName/RelativeSource具体用法

    <TextBlock Name="_txtSickBedNo" FontStyle="Normal" Foreground="Black&quo ...

  8. hdu 4876(剪枝+暴力)

    题意:给定n,k,l,接下来给出n个数,让你从n个数中选取k个数围成一圈,然后从这k个数中随意选出连续的m(m>=1&&m<=k)个数进行异或后得到[l,r]区间的所有值, ...

  9. [Everyday Mathematics]20150225

    设 $f:\bbR\to\bbR$ 二次可微, 适合 $f(0)=0$. 试证: $$\bex \exists\ \xi\in\sex{-\frac{\pi}{2},\frac{\pi}{2}},\s ...

  10. redis介绍【转】

    Redis新的存储模式diskstore Thursday, Jan 6th, 2011 by Tim | 13 CommentsFiled under: data | Tags: Mongo, Mo ...