Linux工作生活

Ntpd 快速指南

2019-07-01  本文已影响0人  Kassadar

Ntpd 快速指南

快速开始


修改 ntp server 地址

vi /etc/ntp.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# server 0.centos.pool.ntp.org iburst
# server 1.centos.pool.ntp.org iburst
# server 2.centos.pool.ntp.org iburst
# server 3.centos.pool.ntp.org iburst
server ntpserverip prefer

重启 ntp server

systemctl restart ntpd.service

观察启动的服务

netstat -tlunp | grep ntp

注意走的是 udp 协议 123 端口

查看 ntp 同步状态,通常需要数分钟时间进行 synchronised

ntpstat

synchronised to NTP server (5.103.139.163) at stratum 2
time correct to within 193 ms
polling server every 1024 s

观察 ntpd 服务

ntpd 服务每隔 64 秒会主动更新时间,可以通过以下命令查看

ntpq -p

ntpq -p 可以列出目前我们的 ntp 与相关的上层 ntp 的状态,上头的几个字段的意义为:

简介


ntpd 特性

C/S 合一

ntpd 对下层 client 来说是 service server,对于上层 server 来说它是 client,也就是说新版的 NTP 服务程序已经不对服务端和客户端进行区分了,统一叫做 ntpd。ntpd 根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。
所有的配置都在 /etc/ntp.conf 文件中。

NTP 客户端同步间隔

ntp 服务会间隔多长时间想时钟服务器请求一次时钟同步呢?默认最小时间间隔为64s,默认最大时间间隔是1024s(17分钟左右)。
默认间隔可以通过 minpoll 和 maxpoll 配置。

容忍误差范围

ntpd 服务并不是在任何情况下都会进行同步的。当时钟服务器时间和本地时间相差大于1000s 时,ntp 服务就会认为是人为调整了时钟或出现了硬件故障,例如 CMOS 电池损坏等。此时,NTP服务就会退出,需要人工使用 ntpdate <server IP> 进行时钟同步。

采用 -g 选项可以让 ntpd 忽略 1000s 或更大误差

层次(strata)

stratum 根据上层 server 的层次而设定(+1)。
对于提供 network time service provider 的主机来说,stratum的设定要尽可能准确。
而作为局域网的 time service provider,通常将 stratum 设置为 10

ntpd 与 ntpdate

ntpd

ntpdate

区别

-- ntpdate 是一个命令,而 ntpd 则是一个体系

ntpdate 机制是 one-shot 同步, 若要定时同步需搭配 cron

ntpd 是 daemon, 可自动定期将系统时间和 NTP server 更新

ntpd 不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非 ntpdate 立即同步。也正如此两者不可同时运行。

ntp.org 在 2012 年时开始, 表示将让 ntpd 来取代 ntpdate, 主要原因如下:

ntpd 可以 cover ntpdate 的功能
ntpd 和 ntpdate 原本共用相同的程式码, 后来 ntpd 优化了裡面的演算法, 但 ntpdate 并没有更新

在生产环境中慎用 ntpdate。时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如传统数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate 调整时间的方式就是我们所说的”跃变“: 在获得一个时间之后,ntpdate 使用 settimeofday 设置系统时间。

上一篇 下一篇

猜你喜欢

热点阅读