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
  • 关闭ntp进程

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

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Ubuntu上安装NTP校时服务器,可以按照以下步骤进行操作: 1. 首先,更新系统。打开终端,运行以下命令来更新系统: ``` sudo apt-get update ``` 2. 安装NTP服务器。运行以下命令来安装NTP服务器: ``` sudo apt-get install ntp ``` 3. 配置NTP服务器。编辑NTP配置文件ntp.conf,运行以下命令: ``` sudo vim /etc/ntp.conf ``` 在配置文件中添加或修改以下内容: ``` server ntp.aliyun.com iburst ``` 这将使用阿里云的时间服务器进行同步。如果无法访问外网,可以使用当前服务器作为时间同步服务器: ``` server 127.127.1.0 fudge 127.127.1.0 stratum 10 ``` 4. 保存并关闭ntp.conf文件。 5. 重启NTP服务。运行以下命令来重启NTP服务: ``` sudo systemctl restart ntp ``` 至此,你已经成功在Ubuntu上安装NTP校时服务器。你可以通过运行以下命令来查看NTP服务的运行状态: ``` sudo systemctl status ntp ``` 参考文献: https://www.ossez.com/t/ubuntu-ntp/13515 ubuntu搭建NTP服务ubuntu配置ntp时间服务器<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Ubuntu 安装 NTP 服务](https://blog.csdn.net/huyuchengus/article/details/118249859)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Ubuntu配置ntp时间服务器](https://blog.csdn.net/qq_43605009/article/details/122307714)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [ubuntu20开启NTP服务器操作步骤](https://blog.csdn.net/liangcsdn111/article/details/126103041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值