大漠知秋的加油站

  • 首页
大漠知秋的加油站
你就当我的烂笔头吧
  1. 首页
  2. Linux
  3. 正文

NTP 服务的配置和使用

2019年12月26日 521点热度 0人点赞 0条评论

NTP 服务可以用来当做客户端所在服务器的时间,也可以作为服务端当做时间校准的服务。

安装 NTP

$ yum -y install ntp
# 开机自启
$ systemctl enable ntpd
# 先同步一下时间,此处使用的是阿里云的 NTP 校准服务
$ ntpdate -u time1.aliyun.com

配置 NTP

  配置文件目录:/etc/ntp.conf
  

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 修改内容 --- 上级时间服务器,阿里云的时间服务
server time1.aliyun.com
server time2.aliyun.com
server time3.aliyun.com
server time4.aliyun.com
server time5.aliyun.com
server time6.aliyun.com
server time7.aliyun.com

#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# 修改内容 --- 允许上层时间服务器主动修改本机时间
#restrict time1.aliyun.com nomodify notrap noquery
#restrict time2.aliyun.com nomodify notrap noquery
#restrict time3.aliyun.com nomodify notrap noquery

# 修改内容 --- 只有在上级时钟源失效时,NTP 才会使用127.127.1.0的本地时钟,将local时间作为ntp服务器时间提供给ntp客户端。NTP把本地主机的时钟也看作外部时钟源来处理,分配的地址是127.127.1.0
server 127.127.1.0
# 修改内容 --- 设置本地时钟源的层次为 5,这样如果NTP服务从本地时钟源获取时间的话,NTP对外宣布的时间层次为 6
fudge  127.127.1.0 stratum 5


# Enable public key cryptography.
#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey 8

# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

  配置完毕之后重启 NTP 服务

$ systemctl restart ntpd

# 查看上级连通状态
$ ntpq -p

上级连通状态

属性 作用
remote 本机和上层 NTP 的 ip 或主机名;+ 表示可用,- 表示不可用,* 表示推荐。
refid remote 的上一层 NTP 主机地址;
st Stratum(阶层);表示经过 n = 2 次 NTP 同步到当前服务器。
t 类型;u 表示单播(unicast)。其它值包括本地(local)、多播(multicast)、广播(broadcast)。
when 自从上次和服务器交互后经过的时间(以秒数计)。
poll 和服务器的轮询间隔,以秒数计。
reach 表示和服务器交互是否有任何错误的八进制数;每成功连接一次它的值就会增加,值 337 表示 100% 成功(即十进制的255)。
delay 从本地机发送同步要求到 NTP 服务器的往返时间。
offset 当前服务器通过 NTP 时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset 越接近于 0,主机和 NTP 服务器的时间越接近。
jitter 统计了在特定个连续的连接数里 offset 的分布情况。简单地说这个数值的绝对值越小,主机的时间就越精确。
# 查看时间同步状态
$ ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3
   time correct to within 50 ms
   polling server every 1024 s

  这里 NTP 服务配置已经完毕,它既是客户端也是服务端,如果要当服务端可以开放 123 端口,其他服务器上安装 NTP 服务并把上级时间服务器的配置项更改到本服务器的 IP/域名/主机名 即可。

标签: Linux NTP 时间校准
最后更新:2019年12月26日

大漠知秋

唯黄昏而思烛明,唯覆雪始念日暖,唯放手方知情真,今困苦而怀峥嵘,今飘零而涌乡愁,今孑然而徒唏嘘,唏嘘成愁。

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

文章目录
  • 安装 NTP
  • 配置 NTP

COPYRIGHT © 2023 大漠知秋的加油站. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

豫ICP备16029200号-2