运维实战之企业内部NTP服务器基础安装与配置使用_集团内部配置ntp服务器

Tips: 简单的说就是对时间精度要求高的应用和主机需要使用到时间同步服务器;

服务方式

描述: 即 ntpd 与 Chrony 服务(CentOS上自带客户端)两种服务方式的搭建与配置。

Tips: NTP服务的通信端口为UDP 123, 所以在服务之前请确保您的防火墙已经打开 UDP 123 端口的通信;

公共 NTP 服务器

描述: 阿里云提供了内网和公网NTP服务器,用于同步各网络中ECS实例的本地时间。

Tips:

  • China - cn.pool.ntp.org: To use this specific pool zone, add the following to your ntp.conf file:
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org

  • 云服务器ECS为您提供了高精度的时间参考NTP服务器,其中ntp.cloud.aliyuncs.com服务器在地域级别上提供原子参考钟服务,适用于金融、通讯、科研和天文等以时间精度核心的生产行业
阿里巴巴公共系统服务: https://developer.aliyun.com/mirror
ntp.aliyun.com

经典网络内网专有网络VPC内网公网
-ntp.cloud.aliyuncs.comntp1.aliyun.com
ntp1.cloud.aliyuncs.comntp7.cloud.aliyuncs.comntp2.aliyun.com
ntp2.cloud.aliyuncs.comntp8.cloud.aliyuncs.comntp3.aliyun.com
ntp3.cloud.aliyuncs.comntp9.cloud.aliyuncs.comntp4.aliyun.com
ntp4.cloud.aliyuncs.comntp10.cloud.aliyuncs.comntp5.aliyun.com
ntp5.cloud.aliyuncs.comntp11.cloud.aliyuncs.comntp6.aliyun.com
ntp6.cloud.aliyuncs.comntp12.cloud.aliyuncs.comntp7.aliyun.com

Tips:

  • 腾讯云 : https://cloud.tencent.com/document/product/213/30392
server time1.cloud.tencent.com
server time2.cloud.tencent.com
server time3.cloud.tencent.com
server time4.cloud.tencent.com
server time5.cloud.tencent.com

  • China - 高校与网络中心
级	域名	地理位置	负责人	邮件	电话
1	s1a.time.edu.cn	北京邮电大学	王振华	wzhdl at bupt.edu.cn	010-62283044-8003
1	s1b.time.edu.cn	清华大学	尹惠实	yhs at cernet.edu.cn	010-62795818-6105
1	s1c.time.edu.cn	北京大学	马皓	mah at pku.edu.cn	010-62753007
1	s1d.time.edu.cn	东南大学	徐加羚	jlxu at njnet.edu.cn	025-3794342-309
1	s1e.time.edu.cn	清华大学	尹惠实	yhs at cernet.edu.cn	010-62795818-6105
2	s2a.time.edu.cn	清华大学	尹惠实	yhs at cernet.edu.cn	010-62795818-6105
2	s2b.time.edu.cn	清华大学	尹惠实	yhs at cernet.edu.cn	010-62795818-6105
2	s2c.time.edu.cn	北京邮电大学	王振华	wzhdl at bupt.edu.cn	010-62283044-8003
2	s2d.time.edu.cn	西南地区网络中心	董茜	appletung at uestc.edu.cn	028-61830330
2	s2e.time.edu.cn	西北地区网络中心	丁惠宁	dhn at xanet.edu.cn	029-2669037
2	s2f.time.edu.cn	东北地区网络中心	毛宇	maoy at neu.edu.cn	024-23966854
2	s2g.time.edu.cn	华东南地区网络中心	瞿庆海	qqh at sjtu.edu.cn	021-62932901-8101
2	s2h.time.edu.cn	四川大学网络管理中心	郑炳伦	zhengbl at scu.edu.cn	028-85414820
2	s2j.time.edu.cn	大连理工大学网络中心	于广辉	ygh at dlut.edu.cn	0411-4708642
2	s2k.time.edu.cn	CERNET桂林主节点	胡进坤	jinkun at mailbox.gxnu.edu.cn	0773-5845246
2	s2m.time.edu.cn	北京大学	马皓	mah at pku.edu.cn	010-62753007

  • Windows自带: time.windows.com
  • macOS自带: time.apple.com
  • Google Public NTP : https://developers.google.com/time
# 支持IPv6
time.google.com
server time1.google.com
server time2.google.com
server time3.google.com
server time4.google.com

  • Cloudflare: time.cloudflare.com
  • Qualcomm® 位置 (XTRA 预测卫星数据服务): time.izatcloud.net

0x01 服务器安装配置

(1) NTP 服务
Ubuntu

安装环境:

~$ lsb_release -a
  # No LSB modules are available.
  # Distributor ID: Ubuntu
  # Description: Ubuntu 20.04.1 LTS
  # Release: 20.04
  # Codename: focal

安装流程:

# (0) 防火墙配置123/udp
sudo ufw allow 123/udp
# Rules updated
# Rules updated (v6)

# (1) 安装NTP
sudo apt install ntp -y

# (2) 设置NTP server
cp /etc/ntp.conf{,.bak}
tee /etc/ntp.conf <<'END'
driftfile /var/lib/ntp/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
server ntp3.aliyun.com iburst minpoll 4 maxpoll 10

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

restrict 192.168.12.254 nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1

restrict source notrap nomodify noquery
restrict ntp.aliyun.com nomodify notrap noquery
pool ntp.aliyun.com
END


# (3) 重启服务&查看状态&开机自启
sudo systemctl restart ntp
sudo systemctl status ntp
  # ● ntp.service - Network Time Service
  # Loaded: loaded (/lib/systemd/system/ntp.service; enabled; vendor preset: enabled)
  # Active: active (running) since Fri 2021-03-19 11:51:19 CST; 2s ago
  # Docs: man:ntpd(8)
  # Process: 31997 ExecStart=/usr/lib/ntp/ntp-systemd-wrapper (code=exited, status=0/SUCCESS)
  # Main PID: 32015 (ntpd)
  # Tasks: 2 (limit: 9450)
  # Memory: 1.7M
  # CGroup: /system.slice/ntp.service
  # └─32015 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 112:117

  # Mar 19 11:51:19 dns-server ntpd[32015]: Listen and drop on 0 v6wildcard [::]:123
  # Mar 19 11:51:19 dns-server ntpd[32015]: Listen and drop on 1 v4wildcard 0.0.0.0:123
  # Mar 19 11:51:19 dns-server ntpd[32015]: Listen normally on 2 lo 127.0.0.1:123
  # Mar 19 11:51:19 dns-server ntpd[32015]: Listen normally on 3 ens160 192.168.12.254:123
  # Mar 19 11:51:19 dns-server ntpd[32015]: Listen normally on 4 lo [::1]:123
  # Mar 19 11:51:19 dns-server ntpd[32015]: Listen normally on 5 ens160 [fe80::250:56ff:fe8a:69b4%2]:123
  # Mar 19 11:51:19 dns-server ntpd[32015]: Listening on routing socket on fd #22 for interface updates
sudo systemctl enable ntpd

# (4) 验证监听情况
~$ netstat -ulnp | grep ":123"
  # (Not all processes could be identified, non-owned process info
  # will not be shown, you would have to be root to see it all.)
  # udp 0 0 192.168.12.254:123 0.0.0.0:\* -
  # udp 0 0 127.0.0.1:123 0.0.0.0:\* -
  # udp 0 0 0.0.0.0:123 0.0.0.0:\* -
  # udp6 0 0 fe80::250:56ff:fe8a:123 :::\* -
  # udp6 0 0 ::1:123 :::\* -
  # udp6 0 0 :::123 :::\* -

# (5) 测试效果
echo "Asia/Shanghai" > /etc/timezone
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
sudo hwclock --systohc  # 写入硬件之中
sudo timedatectl set-local-rtc 0
~$ sudo date --s="2019-09-04 17:30:00"
Wed 04 Sep 2019 05:30:00 PM CST
~$ systemctl restart ntp
~$ date  # 等待10s后会发现已同步
Fri 19 Mar 2021 03:25:42 PM CST

(2) Chrony 服务

描述:由于Redhat8系列的发行版本默认采用Chrony而废弃了ntpd,所以下面主要讲解一下chrony相关的配置;
Chrony是NTP(Network Time Protocol,网络时间协议,服务器时间同步的一种协议)的另一种实现与ntpd不同,它可以更快且更准确地同步系统时钟,最大程度的减少时间和频率误差。

chrony有三个时间参考:硬件时钟、实时时钟以及手动同步

Chrony包括两个核心组件:

  • chronyd:一个后台运行的守护进程,用于调整内核中运行的系统时钟与NTP服务器同步(它确定服务器增减时间的比率,并对此进行调整补偿)
  • chronyc:提供用户界面,用于监控性能并进行多样化的配置(它可以在chronyd实例控制的服务器上工作,也可以在一台不同的远程服务器上工作)
CentOS

需求分析与环境:
描述: 内网有一台NTP Server向阿里云提供的公网NTP服务器同步时间,并且作为内网NTP Server内网中的其它NTP Client服务器向其同步时间;

IP主机名时间同步方式
192.168.1.146ntp-serverchronyd服务平滑同步
192.168.1.147ntp-client1chronyd服务平滑同步

补充说明:

  • 以上基于Cent7.7以上的发行版本
  • 使用chronyd服务平滑同步时间的方式要优于crontab + ntpdate原因如下:
    • 因为ntpdate同步时间会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响,例如sleep、timer等,且chronyd服务可以在修正时间的过程中同时修正CPU tick。
  • 默认开放端口127.0.0.1:323-UDP 注意NTP服务器的开放端口为UDP-123

基础配置流程:

  • Step1.内网 NTP Server 相关配置
# 0.关闭防火墙,如果不想关闭防火墙可以放通端口
firewall-cmd --permanent --add-port=123/udp
firewall-cmd --permanent --add-port=123/tcp

#1.主机名设置
hostnamectl set-hostname ntp-server
echo "192.168.1.146 ntp-server" >> /etc/hosts

#2.重新登录终端并查看当前时间
hostname && date

#3.安装chrony由于我的机器已安装和centos8自带则可以不用自行安装
yum -y install chrony

#4.备份原始/etc/chrony.conf文件并修改
mv /etc/chrony.conf{,.bak}

#5.配置文件修改与说明
cat >/etc/chrony.conf<<'EOF'
# 指定上层NTP服务器为阿里云提供的公网NTP服务器
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# server 0.centos.pool.ntp.org iburst #缺省默认
server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
server ntp4.aliyun.com iburst minpoll 4 maxpoll 10
server ntp5.aliyun.com iburst minpoll 4 maxpoll 10
server ntp6.aliyun.com iburst minpoll 4 maxpoll 10
server ntp7.aliyun.com iburst minpoll 4 maxpoll 10

# 记录系统时钟获得/丢失时间的速率至drift文件中
driftfile /var/lib/chrony/drift

# 如果系统时钟的偏移量大于10秒,则允许在前三次更新中步进调整系统时钟
makestep 1.0 3

# 启用RTC(实时时钟)的内核同步
rtcsync

# Allow NTP client access from local network.
# 允许同步时间的客户端网段
allow 192.168.1.0/24

# Serve time even if not synchronized to a time source.
# 当阿里云提供的公网NTP服务器不可用时,采用本地时间作为同步标准
local stratum 10

# 指定包含NTP验证密钥的文件:Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# 指定存放日志文件的目录:Specify directory for log files.
logdir /var/log/chrony

# Enable hardware timestamping on all interfaces that support it.
# 在支持它的所有接口上启用硬件时间戳。
#hwtimestamp \*

# Increase the minimum number of selectable sources required to adjust the system clock.
# 增加调整系统时钟所需的可选源的最小数目。
#minsources 2

# 记录信息的选择:Select which information is logged.
#log measurements statistics tracking
EOF
备注:详细指令参数可以使用命令# man chrony.conf查看


#6.启动与查看chronyd服务
systemctl enable chronyd.service && systemctl restart chronyd.service &&  systemctl status chronyd.service
ss -tunlp | grep chronyd

WeiyiGeek.chronyd

#7.查看时间同步源:
chronyc sources -v
# 210 Number of sources = 2 #来源数量
#   .-- Source mode(模式)  '^' = server, '=' = peer, '#' = local clock.
#  / .- Source state(状态) '*' = current synced, '+' = combined , '-' = not combined,
# | /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
# ||                                                 .- xxxx [ yyyy ] +/- zzzz
# ||      Reachability register (octal) -.           |  xxxx = adjusted offset,
# || Log2(Polling interval|轮询间隔) --.  |          |  yyyy = measured offset,
# ||                                \     |          |  zzzz = estimated error.
# ||                                 |    |           \
# MS Name/IP address         Stratum Poll Reach LastRx Last sample
# ===============================================================================
# ^* 120.25.115.20 (正常)          2   4    17     1   -118us[+2986us] +/-   17ms
# ^- 203.107.6.88  (正常)          2   4    17     1  -7270us[-7270us] +/-   33ms
备注:120.25.115.20为ntp1.aliyun.com域名解析后的地址,203.107.6.88为ntp2.aliyun.com~ntp7.aliyun.com域名解析后的地址


#8.查看时间同步源状态:
chronyc sourcestats -v
# 210 Number of sources = 2 #来源数量
#                              .- Number of sample points in measurement set.
#                             /    .- Number of residual runs with same sign.
#                            |    /    .- Length of measurement set (time).
#                            |   |    /      .- Est. clock freq error (ppm).
#                            |   |   |      /           .- Est. error in freq.
#                            |   |   |     |           /         .- Est. offset.
#                            |   |   |     |          |          |   On the -.
#                            |   |   |     |          |          |   samples. \
#                            |   |   |     |          |          |             |
# Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
# ==============================================================================
# 120.25.115.20              16  11   201     -1.609     17.452  +1817us  1128us
# 203.107.6.88               16  10   201     +4.291     31.560  -3671us  2106us

备注:可直接输入命令chronyc进入交互式模式详细使用方法可以使用命令# man chronyd和# man chronyc查看

#常用指令说明
-  help:查看完整的命令帮助列表
-  tracking:显示系统时间信息
-  activity:检查多少NTP源在线/离线
-  add server:手动添加一台新的NTP服务器
-  delete:手动移除NTP服务器或对等服务器
-  accheck:检查NTP访问是否对特定主机可用
-  clients:在客户端报告已访问到的服务器

$ chronyc
# chronyc> tracking
# Reference ID : 78197314 (120.25.115.20)
# Stratum : 3
# Ref time (UTC) : Thu May 14 08:19:56 2020
# System time : 0.000818836 seconds slow of NTP time
# Last offset : -0.000368672 seconds
# RMS offset : 0.000605058 seconds
# Frequency : 12.746 ppm fast
# Residual freq : -0.245 ppm
# Skew : 6.550 ppm
# Root delay : 0.030892739 seconds
# Root dispersion : 0.001810989 seconds
# Update interval : 64.4 seconds
# Leap status : Normal
# chronyc> accheck
# Could not read address
# chronyc> clients
# Hostname NTP Drop Int IntL Last Cmd Drop Int Last
# ===============================================================================

  • Step2.NTP Client配置
#安装chrony
yum -y install chrony && mv /etc/chrony.conf /etc/chrony.conf.bak

#配置chrony.cnf
cat >/etc/chrony.conf<<'EOF'
server 192.168.10.254 iburst
server ntp1.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
local stratum 10
rtcsync
logdir /var/log/chrony
#keyfile /etc/chrony.keys
#stratumweight 0.05
#noclientlog
#logchange 0.5
EOF

#启动chronyd服务
systemctl enable chronyd.service && systemctl restart chronyd.service && ss -tunlp | grep chronyd

#查看时间同步源以及同步状态
chronyc sources -v && chronyc sourcestats -v

WeiyiGeek.Client

  • Step3.测试是否配置成功
#Client
$ date -s '-1year'
$ date 012000002019.00
2019年 01月 20日 星期日 00:00:00 CST
$ systemctl restart chronyd  #重启后大约三秒进行时间同步
$ date
2019年 01月 20日 星期日 00:00:55 CST
$ date
2019年 01月 20日 星期日 00:00:57 CST
$ date
2019年 01月 20日 星期日 00:00:58 CST
$ date
2020年 05月 14日 星期四 16:58:20 CST
$ date

#上面是手动下面我们可以将该命令写入timesync.sh加入到crond中每三分钟同步一次
crond -e
*/3 * * * * sh timesync.sh


0x02 NTP客户端配置

Windows 服务器

描述: 为了保证NTP服务配置成功后能正常同步时间,实例中必须开启NTP服务,Windows Server操作系统默认开启Windows Time服务

1.选择 开始 > 所有程序 > 附件 > 运行,打开运行对话框,并运行命令services.msc

2.在服务对话框中,找到并双击Windows Time服务,十分重要否则报错同步连接超时。

3.在Windows Time的属性(本地计算机)对话框中,执行以下操作:将启动类型设置为自动,确认服务状态为已启动。如果不是,单击启动。

4.修改服务器默认NTP服务器地址为:ntp.aliyun.com

WeiyiGeek.

Q: 如何修改NTP服务时间同步间隔?
答:NTP服务的时间同步间隔默认是5分钟,您可以根据业务需求自定义同步间隔;

#注册表编辑器: 
HKEY_LOCAL_MACHINE > SYSTEM > CurrentControlSet > services > W32Time > TimeProviders > NtpClient 
并双击SpecialPollInterval键值,填入的数值即是您需要的同步时间间隔(单位为秒)

Tips: 在编辑 DWORD (32 位)值对话框中,在基数栏里选择十进制,并按需要填写数值数据。

Linux 服务器

描述: 在Linux系统中您可以通过ntpdate和ntpd 两种命令方式实现NTP时间同步两种方式的异同;

  • ntpdate ntp.aliyun.com断点更新,在最新安装系统的时候或者没有业务运行的情况下;
  • ntpd ntp.aliyun.com步进式地逐渐调整时间。对已经承载有运行中业务的实例建议您使用ntpd同步时间;

以root身份打开并编辑时区配置文件:

sudo rm /etc/localtime  #删除系统里的当地时间链接
ls /usr/share/zoneinfo  #查询时区列表Shanghai为列表条目之一。
sudo vi /etc/sysconfig/clock #用vim打开并编辑配置文件/etc/sysconfig/clock。
输入i添加时区城市。例如添加Zone=Asia/Shanghai,按下Esc键退出编辑并输入:wq保存并退出。
sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  #更新时区修改内容。
hwclock -w  #更新硬件时钟(RTC)。
#验证执行命令date -R查看时区信息是否生效,未生效可按照上述步骤重新操作一遍。

启用NTP服务

  1. 执行命令ntpstat查看是否启用了NTP服务。
  2. 执行命令sudo vi /etc/ntp.conf用vim打开并编辑NTP服务配置文件。
  3. 新添加一行NTP服务器信息格式为:server 您需要添加的NTP服务器 iburst
  4. 执行命令sudo service ntpd start运行NTP服务。
  5. 执行命令chkconfig ntpd on启用NTP服务。
  6. 可选: 执行命令 ntpq -p 可查看NTP服务对等端的列表信息执行命令sudo chkconfig --list ntpd可查看NTP服务的运行级别。

Tips: 如果计划任务有时间同步,先注释两种用法会冲突。

# 关闭 ntpdate 方式同步
$ crontab -e
# time sync by oldboy at 2010-2-1
#\*/5 \* \* \* \* /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1

# 启用 ntpd 方式同步
/etc/init.d/ntpd start
ntpq -p


0x04 配置解析&验证

配置文件
/etc/ntp.conf

描述: ntpd 根据配置文件 (/etc/ntp.conf) 的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。

配置文件:

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help
driftfile /var/lib/ntp/ntp.drift

# 由tzdata提供的闰秒定义
# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# 如果希望记录统计数据,请启用此选项。
#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

# 请指定一个或多个NTP服务器。 - 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
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst

# 使用Ubuntu的ntp服务器作为后备。
# 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.

# 注意“restrict”同时适用于服务器和客户端 所以是一个配置
# 可能会阻止来自某些客户端的请求也可能会结束
# up阻塞来自你自己的上游服务器的回复。
# 默认情况下与所有人交换时间但不允许配置。
restrict -4 default kod notrap nomodify nopeer noquery 
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
# 当前节点IP地址
restrict 192.168.12.254 nomodify notrap nopeer noquery
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
# 集群所在网段的网关(Gateway),子网掩码(Genmask)
restrict 192.168.12.61 mask 255.255.255.0 nomodify notrap 

# 如果要为本地子网提供时间,请更改下一行。
# (Again, the address is an example only.)
broadcast 192.168.12.255

# 如果你想在你的本地子网上收听时间广播,取消注释
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

配置文件说明:

  • restrict 选项格式
- restrict [ 客户端IP ]  mask  [ IP掩码 ]  [参数]
# 操作对象:
“客户端IP” 和 “IP掩码” 指定了对网络中哪些范围的计算机进行控制,如果使用default关键字则表示对所有的计算机进行控制.
# 参数:
* kod: 向不安全的访问者发送 Kiss-Of-Death 报文。
* ignore:拒绝连接到NTP服务器。
* nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
* noquery: 不提供客户端的时间查询。
* notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
* notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
* nopeer: 提供时间服务,但不作为对等体。

  • server 选项格式
- server host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
# 参数:
- host :是上层NTP服务器的IP地址或域名
* key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
* version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
* prefer: 如果有多个server选项,具有该参数的服务器有限使用。
* mode: 指定数据报文mode字段的值。
* minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
* maxpoll:  指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
* iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。

  • fudge 选项格式:
# 参数: 
- stratum : 即层次根据上层server的层次而设定(+1) 对于提供 `network time service provider` 的主机来说 stratum 的设定要尽可能准确。而作为局域网的 `time service provider `通常将`stratum` 设置为10;

补充说明:
描述: 0 层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1 与 stratum 0 是直接相连的,往后的stratum 与上一层 stratum 通过网络相连同一层的 server 也可以交互。

Tips: ntpd 对下层 client 来说是 service server,对于上层 server 来说它是client。

WeiyiGeek.同步示意图

配置示例:

例如: 快速配置阿里巴巴 OPSX NTP 服务编辑文件 “/etc/ntp.conf”,根据情况修改文件内容为:

driftfile  /var/lib/ntp/drift
pidfile   /var/run/ntpd.pid
logfile /var/log/ntp.log
restrict    default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
server 127.127.1.0
fudge  127.127.1.0 stratum 10
server ntp.aliyun.com iburst minpoll 4 maxpoll 10
restrict ntp.aliyun.com nomodify notrap nopeer noquery

例如: 企业内部使用的ntp.conf示例(ntpd 4.2.8 )

# 1.漂移文件 -> 其内容为3.031
driftfile /var/lib/ntp/ntp.drift
# 2.由tzdata提供的闰秒定义
leapfile /usr/share/zoneinfo/leap-seconds.list
# 3.如果希望记录统计数据请启用此选项。
# 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

# 4.restrict 同时适用于服务器和客户端
restrict -4 default kod notrap nomodify nopeer noquery 
restrict -6 default kod notrap nomodify nopeer noquery
# 本地用户可能会更密切地询问ntp服务器。
restrict 192.168.12.254 nomodify notrap  noquery
restrict 127.0.0.1
restrict ::1

# 5.请指定一个或多个NTP服务器。
# 5.1 通过网络同步时间
server ntp1.aliyun.com iburst minpoll 4 maxpoll 10
server ntp2.aliyun.com iburst minpoll 4 maxpoll 10
server ntp3.aliyun.com iburst minpoll 4 maxpoll 10
server 0.cn.pool.ntp.org iburst minpoll 4 maxpoll 10
server 1.cn.pool.ntp.org iburst minpoll 4 maxpoll 10

# 5.2 使用本地时间
# server自身和谁同步
server ntp.aliyun.com prefer
# 把自身的时间同步给客户端。
server 127.127.1.0
fudge 127.127.1.0 stratum  8


# 6.需要添加池条目以及备用池
restrict source notrap nomodify noquery
restrict ntp.aliyun.com nomodify notrap noquery
pool ntp.aliyun.com

# 7.如果要为本地子网提供时间
# broadcast 192.168.12.255
# disable auth
# broadcastclient


# 8.补充说明
# 允许那些网段或者IP同步,不做限制则0.0.0.0 mask 0.0.0.0.需要注意的是4.2版本的ntpd的restrict参数不要加notrust否则客户端会同步不了。
# restrict 192.168.80.0 mask 255.255.255.0 nomodify notrap


/etc/chrony.conf

例如:对于使用 chrony 客户端的 linux 主机 , 配置 '/etc/chrony.conf' 文件的内容为:

server ntp.aliyun.com iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
logchange 0.5
logdir /var/log/chrony

核验工具
ntpd 命令

描述: NTP守护程序

语法参数:

ntpd - NTP daemon program - Ver. 4.2.8p12
Usage:  ntpd [ -<flag> [<val>] | --<name>[{=| }<val>] ]...  [ <server1> ... <serverN> ]
  Flg Arg Option-Name    Description
   -4 no  ipv4           Force IPv4 DNS name resolution
                                - prohibits the option 'ipv6'
   -6 no  ipv6           Force IPv6 DNS name resolution
                                - prohibits the option 'ipv4'
   -a no  authreq        Require crypto authentication
                                - prohibits the option 'authnoreq'
   -A no  authnoreq      Do not require crypto authentication
                                - prohibits the option 'authreq'
   -b no  bcastsync      Allow us to sync to broadcast servers
   -c Str configfile     configuration file name
   -d no  debug-level    Increase debug verbosity level
                                - may appear multiple times
   -D Num set-debug-level Set the debug verbosity level
                                - may appear multiple times
   -f Str driftfile      frequency drift file name
   -g no  panicgate      Allow the first adjustment to be Big
                                - may appear multiple times
   -G no  force-step-once Step any initial offset correction.
   -i Str jaildir        Jail directory
   -I Str interface      Listen on an interface name or address
                                - may appear multiple times
   -k Str keyfile        path to symmetric keys
   -l Str logfile        path to the log file
   -L no  novirtualips   Do not listen to virtual interfaces
   -n no  nofork         Do not fork
                                - prohibits the option 'wait-sync'
   -N no  nice           Run at high priority
   -p Str pidfile        path to the PID file
   -P Num priority       Process priority
   -q no  quit           Set the time and quit
                                - prohibits these options:
                                saveconfigquit
                                wait-sync
   -r Str propagationdelay Broadcast/propagation delay
      Str saveconfigquit Save parsed configuration and quit
                                - prohibits these options:
                                quit
                                wait-sync
   -s Str statsdir       Statistics file location
   -t Str trustedkey     Trusted key number
                                - may appear multiple times


![img](https://img-blog.csdnimg.cn/img_convert/042f2bf223cc200a6ba6e8f899b91c7f.png)
![img](https://img-blog.csdnimg.cn/img_convert/f6ea07521f771abccfbdd94163f40ac1.png)
![img](https://img-blog.csdnimg.cn/img_convert/0f692b7d48944542e3873612532ccabd.png)

           - prohibits the option 'wait-sync'
   -N no  nice           Run at high priority
   -p Str pidfile        path to the PID file
   -P Num priority       Process priority
   -q no  quit           Set the time and quit
                                - prohibits these options:
                                saveconfigquit
                                wait-sync
   -r Str propagationdelay Broadcast/propagation delay
      Str saveconfigquit Save parsed configuration and quit
                                - prohibits these options:
                                quit
                                wait-sync
   -s Str statsdir       Statistics file location
   -t Str trustedkey     Trusted key number
                                - may appear multiple times


[外链图片转存中...(img-9Dw1WtkT-1725642181744)]
[外链图片转存中...(img-otUF8qOY-1725642181744)]
[外链图片转存中...(img-q3U8mZwF-1725642181744)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值