NTP服务配置

NTP(Network Time Protocol)是一种用于计算机系统时间同步的协议。在CentOS系统下,可以通过配置NTP服务来实现多台机器之间的时间同步。以下是具体步骤:

  1. 安装NTP服务
    在所有三台机器上,首先需要安装NTP服务:
sudo yum install ntp
  1. 配置NTP服务器
    选择其中一台机器作为NTP服务器,编辑/etc/ntp.conf文件。
sudo vim /etc/ntp.conf

在ntp.conf文件中,进行如下配置:

# 允许局域网内的其他机器访问NTP服务
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

# 使用上级NTP服务器进行同步(可以用公网NTP服务器)
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 127.127.1.0
fudge 127.127.1.0 stratum 10

# 其他默认配置保持不变
driftfile /var/lib/ntp/drift
logfile /var/log/ntp.log
  1. 启动并启用NTP服务器
sudo systemctl start ntpd
sudo systemctl enable ntpd
  1. 配置NTP客户端
    在另外两台作为客户端的机器上,编辑/etc/ntp.conf文件。
sudo vim /etc/ntp.conf

将文件配置如下:

# 允许局域网内的其他机器访问NTP服务
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1

# 同步时间到指定的NTP服务器
server <NTP服务器的IP地址> iburst

# 其他默认配置保持不变
driftfile /var/lib/ntp/drift
logfile /var/log/ntp.log
  1. 启动并启用NTP客户端
sudo systemctl start ntpd
sudo systemctl enable ntpd
  1. 检查NTP服务状态
    在所有机器上,可以使用以下命令检查NTP服务的状态和同步情况:
ntpq -p

该命令将显示NTP服务器和客户端的同步状态。输出示例如下:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.1     10.0.0.1        2 u   56   64  377    0.123   -0.456   0.789
其中,* 表示与主服务器同步成功。
  1. 监控NTP服务
    定期检查NTP服务状态可以确保时间同步的正常运行。可以设置一个cron任务定期运行ntpq -p并将输出保存到日志文件:

编辑cron任务:

crontab -e

添加如下行:

*/30 * * * * /usr/sbin/ntpq -p >> /var/log/ntp_sync.log

这样,每30分钟会检查一次NTP同步状态,并将结果追加到日志文件/var/log/ntp_sync.log

ntp.conf 文件详解

ntp.conf 是 NTP(Network Time Protocol)服务的配置文件。下面是一个典型的 ntp.conf 文件,并对其内容进行详细讲解:

# 禁止所有客户端进行修改操作
restrict default nomodify notrap nopeer noquery

# 允许本地主机进行所有操作
restrict 127.0.0.1

# 允许特定局域网内的机器进行特定操作
restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap

# 指定上层NTP服务器
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 127.127.1.0
fudge 127.127.1.0 stratum 10

# 配置漂移文件
driftfile /var/lib/ntp/drift

# 配置日志文件
logfile /var/log/ntp.log

关键配置项解释
restrict: 控制对 NTP 服务的访问权限。

default: 默认的访问权限。
nomodify: 不允许修改服务器设置。
notrap: 不允许使用模式 6 的控制消息。
nopeer: 防止这个系统尝试与对等体建立对等关系。
noquery: 不允许查询操作。
server: 定义 NTP 服务器。

iburst: 在第一次联系时发送一串包,以便快速建立联系。
fudge: 用于配置本地时钟(本地时钟作为备份)。

stratum 10: 设置本地时钟的层级,层级数值越高,优先级越低。
driftfile: 定义漂移文件的位置。NTP 会在此文件中记录时钟的漂移值。

logfile: 定义日志文件的位置。NTP 服务的日志信息会记录在此文件中。

ntpq 命令详解

ntpq 是一个 NTP 查询工具,用于监控 NTP 服务的状态。常用的命令及其解释如下:

常用命令
ntpq -p: 显示 NTP 服务器和客户端的同步状态。

ntpq -p

输出示例:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.1     10.0.0.1        2 u   56   64  377    0.123   -0.456   0.789
+time.server.com 192.168.0.1     3 u   45   64  377    0.234   -0.123   0.456

remote: 远程服务器的地址或名称。
refid: 远程服务器引用的上级服务器。
st: 层级(stratum)。
t: 类型(u 表示 unicast)。
when: 上次同步的时间。
poll: 轮询间隔(秒)。
reach: 可达性掩码(八进制)。
delay: 往返延迟(毫秒)。
offset: 时间偏移(毫秒)。
jitter: 抖动(毫秒)。
*表示当前正在使用的时间服务器。
+表示候选时间服务器。

ntpq -c rl: 查看 NTP 服务器的运行时信息。

ntpq -c rl

输出示例:

associd=0 status=0618 leap_none, sync_ntp, 1 event, no_sys_peer,
version="ntpd 4.2.6p5@1.2349-o Tue Sep 13 13:30:57 UTC 2011 (1)",
processor="x86_64", system="Linux/3.16.0-4-amd64", leap=00, stratum=3,
precision=-23, rootdelay=0.123, rootdisp=3.456, refid=192.168.0.1,
reftime=d9e6b9f7.c6230a3e  Tue, Dec  1 2020 12:34:56.789,
clock=d9e6b9f8.12345678  Tue, Dec  1 2020 12:34:56.123, peer=12345, tc=6,
mintc=3, offset=0.789, frequency=15.123, sys_jitter=0.456,
clk_jitter=0.123, clk_wander=0.456

associd: 关联 ID。
status: NTP 状态码。
leap: 闰秒指示器。
sync_ntp: 同步源。
version: NTP 版本信息。
processor: 处理器架构。
system: 操作系统。
stratum: 层级。
rootdelay: 根延迟。
rootdisp: 根扩散。
refid: 引用的上级服务器。
reftime: 上次同步时间。
clock: 当前时间。
offset: 时间偏移。
frequency: 时钟频率偏差。
sys_jitter: 系统抖动。
clk_jitter: 时钟抖动。
clk_wander: 时钟漂移。

ntpq -c peers: 列出所有已知的 NTP 服务器和其状态。

ntpq -c peers

输出示例:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.1     10.0.0.1        2 u   56   64  377    0.123   -0.456   0.789
+time.server.com 192.168.0.1     3 u   45   64  377    0.234   -0.123   0.456

输出格式与 ntpq -p 相同。

通过详细配置 ntp.conf 文件和使用 ntpq 命令,可以有效地配置和监控 NTP 服务,确保各台机器之间的时间同步准确。

ntpq -p 输出参数的更详细解释

ntpq -p 命令是用于查询 NTP 服务状态的重要工具。它提供了有关服务器和客户端时间同步的详细信息。下面是对 ntpq -p 输出的更详细解释:

ntpq -p 输出示例

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.1     10.0.0.1        2 u   56   64  377    0.123   -0.456   0.789
+time.server.com 192.168.0.2     3 u   45   64  377    0.234   -0.123   0.456
-ntp.other.com   192.168.0.3     4 u   12   64  377    0.345   -0.789   0.567

字段解释
remote 显示远程 NTP 服务器的名称或 IP 地址。
*号表示当前选择的同步源。
+号表示备选同步源。
-号表示不合格的同步源。
refid:

显示远程服务器引用的上一级时间源的标识符。
st (stratum):

显示远程服务器的层级(stratum),表示时间源的距离。层级值越低,时间源越准确。
0 表示未同步,1 表示直接连接的原子时钟,2-15 表示层级逐渐增高的 NTP 服务器。
t (type):

显示服务器类型:
u: unicast (单播)
b: broadcast/multicast (广播/组播)
l: local (本地时钟)
s: symmetric (对称模式)
when:

显示上一次成功与该服务器通信的时间,单位为秒或分钟(m)。
poll:
显示服务器的轮询间隔,单位为秒。这个值会动态调整。
reach:
显示到达掩码的八进制表示,用于表示最近 8 次轮询的成功与否。377 表示 8 次轮询全部成功。
delay:
显示客户端和服务器之间的往返延迟时间,单位为毫秒。
offset:
显示客户端时钟相对于服务器时钟的时间偏移量,单位为毫秒。负值表示客户端时钟落后,正值表示客户端时钟超前。
jitter:
显示时间抖动,表示时间偏移量的波动,单位为毫秒。数值越小,表示同步越稳定。

输出详细解析
假设以下是 ntpq -p 的输出:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.0.1     10.0.0.1        2 u   56   64  377    0.123   -0.456   0.789
+time.server.com 192.168.0.2     3 u   45   64  377    0.234   -0.123   0.456
-ntp.other.com   192.168.0.3     4 u   12   64  377    0.345   -0.789   0.567

分析:
*192.168.0.1:
当前同步源,层级 2。
最近一次成功轮询是在 56 秒前,轮询间隔为 64 秒。
最近 8 次轮询全部成功(reach = 377)。
往返延迟为 0.123 毫秒,时间偏移为 -0.456 毫秒,抖动为 0.789 毫秒。

+time.server.com:
备选同步源,层级 3。
最近一次成功轮询是在 45 秒前,轮询间隔为 64 秒。
最近 8 次轮询全部成功(reach = 377)。
往返延迟为 0.234 毫秒,时间偏移为 -0.123 毫秒,抖动为 0.456 毫秒。

-ntp.other.com:
不合格的同步源,层级 4。
最近一次成功轮询是在 12 秒前,轮询间隔为 64 秒。
最近 8 次轮询全部成功(reach = 377)。
往返延迟为 0.345 毫秒,时间偏移为 -0.789 毫秒,抖动为 0.567 毫秒。

使用 ntpq -p 的示例
基本命令:

ntpq -p

连接到特定 NTP 服务器:

ntpq -p <server_address>

示例:

ntpq -p 192.168.0.1

持续监控:
可以使用 watch 命令持续监控 NTP 状态:

watch -n 10 ntpq -p

每 10 秒更新一次 NTP 状态。

总结
ntpq -p 命令提供了丰富的关于 NTP 同步状态的信息,通过仔细分析这些信息,可以判断 NTP 服务的同步情况及其稳定性。了解这些字段的意义,可以进行有效的 NTP 服务监控和故障排除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值