微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ubuntu下配置NTP对时服务

1. 安装NTP服务

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

# apt-get install ntp

查看NTP服务是否启动

# service --status-all

或者

# ps -aux | grep ntp


2. NTP配置运行观察

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

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

# vim /etc/ntp.conf


// 配置文件内容
# /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.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# 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 limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access,but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# 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

2.2 NTP配置文件

  • 使用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_path

filegen name file filename [type type] [link | nolink] [enable | disable]

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

  • 使用restrict管理权限控制

设定方式:

restrict [address] mask [mask] [parameter]

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

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

  • 使用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的帮助文档。

2.3 NTP运行

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

# service ntp restart

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

# watch ntpq -p

Every 2.0s: ntpq -p      Tue Apr 17 11:04:48 2018

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.002
 ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.002
-mx.comglobalit. 128.227.205.3    2 u    7   64  377  208.655   -5.511  19.965
+time4.aliyun.co 10.137.53.7      2 u   20   64  377   36.855   -4.177   2.523
+120.25.115.20   10.137.53.7      2 u   15   64  377   44.108   -3.279   1.075
-61-216-153-106. 211.22.103.157   3 u   14   64  377   51.587   -6.215   2.993
*time5.aliyun.co 10.137.38.86     2 u   22   64  377    2.782   -3.453   2.444
-static-5-103-13 .GPS.            1 u    9   64  357  186.930   -6.467   4.159
-pugot.canonical 17.253.34.125    2 u   41   64  377  323.893  -21.803   9.484

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

remote: 本地机器所连接的远程NTP服务器;

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

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

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


3. 使用ntpdate更新时间

  • 安装ntpdate
# apt install ntpdate

ntpdate命令是要在ntp进程退出的情况下执行,如果执行上述命令出错,那么请先关闭ntp进程,具体操作日下所示:

# ntpdate 10.138.16.232
17 Apr 10:36:03 ntpdate[9730]: the NTP socket is in use,exiting

// ntpdate命令是要在ntp进程退出的情况下执行
# service ntp stop

// 检查ntp服务是否停止
# ps -aux | grep ntp 

// 与阿里云ntp服务器对时
# ntpdate ntp1.aliyun.com

17 Apr 10:42:42 ntpdate[10778]: adjust time server 182.92.12.11 offset -0.018098 sec

// 启动ntp服务
# service ntp start

// 观察NTP的运行状态
# watch ntpq -p

阿里云提供的7个NTP时间服务器,也就是Internet时间同步服务器地址:

ntp1.aliyun.com

ntp2.aliyun.com

ntp3.aliyun.com

ntp4.aliyun.com

ntp5.aliyun.com

ntp6.aliyun.com

ntp7.aliyun.com


4. 参考文章

https://www.cnblogs.com/xwdreamer/p/3448773.html

原文地址:https://www.jb51.cc/ubuntu/349663.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐