ubuntu安装和使用NTP

来源:互联网 发布:淘宝订机票 编辑:程序博客网 时间:2024/04/30 16:42

安装NTP

ntp在线安装方式很简单,只需要执行以下命令即可帮你安装好NTP以及所依赖的包。

sudo apt-get install ntp

安装完成后可以通过如下命令查看NTP服务是否启动:

service --status-all

或者

ps -aux | grep ntp

NTP的配置文件/etc/ntp.conf

使用server命令设定上层NTP服务器

设定方式:

server [address] [options...]

在server后面填写服务器地址(可以使IP或主机名),之后是命令参数主要包括autokey,brust,ibrust,key key,minpoll minpoll,maxpoll maxpoll,mode option,noselect,preempy,prefer,true,ttl ttl,version version,xleave。这里最长使用的prefer,表示优先使用的服务器。其它参数的详细说明可参考NTP的帮助文档。

使用restrict命令管理权限控制

设定方式:

restrict [address] mask [mask] [parameter]

其中parameter的参数主要有底下这些:

  • ignore: 拒绝所有类型的NTP联机;
  • nomodify: 客户端不能使用ntpc与ntpq这两个程序来修改服务器的时间参数,但客户端仍可透过这个主机来进行网络校时;
  • noquery: 客户端不能使用ntpq,ntpc等指令来查询时间服务器,等于不提供NTP的网络校时;
  • notrap: 不提供trap这个远程事件登录(remote event logging)的功能;
  • notrust: 拒绝没有认证的客户端;

如果你没有在 parameter 的地方加上任何参数的话,这表示该 IP 或网段不受任何限制。

使用driftfile记录时间差异

设定方式:

driftfile [可以被ntpd写入的目录与档案]

因为预设的 NTP Server 本身的时间计算是依据 BIOS 的芯片震荡周期频率来计算的,但是这个数值与上层 Time Server 不见得会一致啊!所以 NTP 这个 daemon (ntpd) 会自动的去计算我们自己主机的频率与上层 Time server 的频率,并且将两个频率的误差记录下来,记录下来的档案就是在 driftfile 后面接的完整档名当中了!关于档名你必须要知道:

  • driftfile 后面接的档案需要使用完整路径文件名;
  • 该档案不能是连结档;
  • 该档案需要设定成 ntpd 这个 daemon 可以写入的权限;
  • 该档案所记录的数值单位为:百万分之一秒 (ppm);

driftfile 后面接的档案会被 ntpd 自动更新,所以他的权限一定要能够让 ntpd 写入才行。

使用statsdir和filegen开启统计分析

设定方式:

statsdir directory_pathfilegen name file filename [type type] [link | nolink] [enable | disable]

当打开统计分析是会在directory_path目录下产生filegen中所设定的统计文件。

NTP的启动与观察

配置文件

ntp即是服务器,又是客户端,只需要通过配置即可。根据上面的说明,我们最终可以取得这样的配置文件案内容。

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help# 时间差异文件driftfile /var/lib/ntp/ntp.drift# 分析统计信息#statsdir /var/log/ntpstats/statistics loopstats peerstats clockstatsfilegen loopstats file loopstats type day enablefilegen peerstats file peerstats type day enablefilegen clockstats file clockstats type day enable# 上层ntp server.pool 0.ubuntu.pool.ntp.org iburstpool 1.ubuntu.pool.ntp.org iburstpool 2.ubuntu.pool.ntp.org iburstpool 3.ubuntu.pool.ntp.org iburst# Use Ubuntu's ntp server as a fallback.pool ntp.ubuntu.com# 不允许来自公网上ipv4和ipv6客户端的访问restrict -4 default kod notrap nomodify nopeer noquery limitedrestrict -6 default kod notrap nomodify nopeer noquery limited# 让NTP Server和其自身保持同步,如果在/etc/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端.restrict 127.0.0.1restrict ::1# Needed for adding pool entriesrestrict source notrap nomodify noquery# 允许这个网段的对时请求.restrict 192.168.123.0 mask 255.255.255.0 nomodify # If you want to provide time to your local subnet, change the next line.# (Again, the address is an example only.)#broadcast 192.168.123.255# If you want to listen to time broadcasts on your local subnet, de-comment the# next lines.  Please do this only if you trust everybody on the network!#disable auth#broadcastclient#Changes recquired to use pps synchonisation as explained in documentation:#http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918#server 127.127.8.1 mode 135 prefer    # Meinberg GPS167 with PPS#fudge 127.127.8.1 time1 0.0042        # relative to PPS for my hardware#server 127.127.22.1                   # ATOM(PPS)#fudge 127.127.22.1 flag3 1            # enable PPS API

NTP运行

配置好ntp.conf文件后,通过如下命令重启NTP服务:

server ntp restart

NTP观察

重启后通过如下命令观察NTP的运行状态:

watch ntpq -p

这里写图片描述

这个命令可以列出目前我们的 NTP 与相关的上层 NTP 的状态,上头的几个字段的意义为:

  • remote: 它指的就是本地机器所连接的远程NTP服务器;
  • refid: 它指的是给远程服务器提供时间同步的服务器;
  • st: 远程服务器的层级别(stratum). 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端。所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的;
  • when: 几秒钟前曾经做过时间同步化更新的动作;
  • poll: 本地机和远程服务器多少时间进行一次同步(单位为秒).
    在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小;
  • reach: 已经向上层 NTP 服务器要求更新的次数;
  • delay: 网络传输过程当中延迟的时间,单位为 10^(-6) 秒;
  • offset: 时间补偿的结果,单位与 10^(-3) 秒;
  • jitter: Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。简单地说这个数值的绝对值越小我们和服务器的时间就越精确;
  • *: 它告诉我们远端的服务器已经被确认为我们的主NTP Server,我们系统的时间将由这台机器所提供;
  • +: 它将作为辅助的NTP Server和带有号的服务器一起为我们提供同步服务. 当号服务器不可用时它就可以接管;
  • -: 远程服务器被clustering algorithm认为是不合格的NTP Server;
  • x: 远程服务器不可用;

要让你的 NTP Server/Client 真的能运作,在上述的动作中得注意:

  • 上述的 watch ntpq -p 的输出结果中,你的 NTP 服务器真的要能够连结上层 NTP 才行! 否则你的客户端将无法对你的
    NTP 服务器进行同步更新。
  • 你的 NTP 服务器时间不可与上层差异太多。

使用ntpdate更新时间

上面我们说到当本机与服务器时间相差太远,ntpd将无法同步成功。这是我们需要先使用ntpdate命令进行强制同步(在使用时需关掉ntpd进程)。
在终端运行如下命令:

sudo ntpdate server_ip

执行这个命令后(如果服务器有效)将立即更新本地时间。而上面所使用的ntpd对时间的更新是渐进式的。

0 0