运维小知识之企业内部NTP服务器基础安装与配置使用
0x00 前言简述
基础概念
服务方式
公共 NTP 服务器
0x01 服务器安装配置
CentOS
Ubuntu
(1) NTP 服务
(2) Chrony 服务
0x02 NTP客户端配置
Windows 服务器
Linux 服务器
0x04 配置解析&验证
ntpd 命令
ntpdate 命令
ntpstat 命令
ntpq 命令
/etc/ntp.conf
/etc/chrony.conf
配置文件
核验工具
0x05 入坑出坑
问题1.ntpdate[820110]: no server suitable for synchronization found
问题2.ntpdate[35934]: the NTP socket is in use, exiting
问题3.ntpd与ntpdate区别说明
0x00 前言简述
基础概念
简介:NTP 是网络时间协议 (Network Time Protocol)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调UTC,其精度在局域网内可达0.1ms,在互联网绝大多数的地方其精义可达1-50ms。
例如: 您在更新数据库或者分析日志时,时间顺序对结果有很大影响。为避免在实例上运行业务时出现逻辑混乱和网络请求错误等问题,您需要统一相关实例的时区设置。另外您还可以通过NTP服务同步网络中所有服务器的本地时间。
应用场景
在进行Linux运维以及同步的应用安装和使用都要求,双方的时间是需要保持一致的,而常常小型的服务器群是没有自己的NTP服务器的,这时候我们就需要采用外网的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.com | ntp1.aliyun.com |
ntp1.cloud.aliyuncs.com | ntp7.cloud.aliyuncs.com | ntp2.aliyun.com |
ntp2.cloud.aliyuncs.com | ntp8.cloud.aliyuncs.com | ntp3.aliyun.com |
ntp3.cloud.aliyuncs.com | ntp9.cloud.aliyuncs.com | ntp4.aliyun.com |
ntp4.cloud.aliyuncs.com | ntp10.cloud.aliyuncs.com | ntp5.aliyun.com |
ntp5.cloud.aliyuncs.com | ntp11.cloud.aliyuncs.com | ntp6.aliyun.com |
ntp6.cloud.aliyuncs.com | ntp12.cloud.aliyuncs.com | ntp7.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.listserver 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 10restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noqueryrestrict 192.168.12.254 nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1restrict 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.146 | ntp-server | chronyd服务平滑同步 |
192.168.1.147 | ntp-client1 | chronyd服务平滑同步 |
补充说明:
以上基于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
#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
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
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服务
执行命令
ntpstat
查看是否启用了NTP服务。执行命令sudo vi /etc/ntp.conf用vim打开并编辑NTP服务配置文件。
新添加一行NTP服务器信息格式为:server 您需要添加的NTP服务器 iburst
执行命令
sudo service ntpd start
运行NTP服务。执行命令
chkconfig ntpd on
启用NTP服务。可选: 执行命令
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。
配置示例:
例如: 快速配置阿里巴巴 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:saveconfigquitwait-sync-r Str propagationdelay Broadcast/propagation delayStr saveconfigquit Save parsed configuration and quit- prohibits these options:quitwait-sync-s Str statsdir Statistics file location-t Str trustedkey Trusted key number- may appear multiple times-u Str user Run as userid (or userid:groupid)-U Num updateinterval interval in seconds between scans for new or dropped interfacesStr var make ARG an ntp variable (RW)- may appear multiple timesStr dvar make ARG an ntp variable (RW|DEF)- may appear multiple times-w Num wait-sync Seconds to wait for first clock sync- prohibits these options:noforkquitsaveconfigquit-x no slew Slew up to 600 secondsopt version output version information and exit-? no help display extended usage information and exit-! no more-help extended usage information passed thru pager
基础命令:
# (1) 通过bin目录下的脚本启动
/usr/local/ntp/bin/ntpd -c /etc/ntp.conf -p /tmp/ntpd.pid
ntpdate 命令
描述: 通过NTP设置日期和时间;
语法参数:
$ ntpdate [-46bBdqsuv] [-a key] [-e authdelay] [-k keyfile] [-o version] [-p samples] [-t timeout] server [...]
基础实例:
$ ntpdate -d 192.168.12.254
19 Mar 15:44:30 ntpdate[989474]: ntpdate 4.2.8p12@1.3728-o (1)
Looking for host 192.168.12.254 and service ntp
host found : 192.168.12.254
transmit(192.168.12.254)
receive(192.168.12.254)
....
transmit(192.168.12.254)
receive(192.168.12.254)server 192.168.12.254, port 123
stratum 3, precision -24, leap 00, trust 000
refid [120.25.115.20], root delay 0.034073, root dispersion 0.014389
transmitted 4, in filter 4
reference time: e3fed42b.d735a7b4 Fri, Mar 19 2021 15:41:31.840
originate timestamp: e3fed4e4.d942360e Fri, Mar 19 2021 15:44:36.848
transmit timestamp: e3fed4e4.d9081cab Fri, Mar 19 2021 15:44:36.847
filter delay: 0.02577 0.02576 0.02576 0.025770.00000 0.00000 0.00000 0.00000
filter offset: 0.000817 0.000793 0.000781 0.0007690.000000 0.000000 0.000000 0.000000
delay 0.02576, dispersion 0.00000
offset 0.00079319 Mar 15:44:36 ntpdate[989474]: adjust time server 192.168.12.254 offset 0.000793 sec
ntpstat 命令
描述:
ntpq 命令
描述: 标准的NTP查询程序
语法参数:
ntpq - standard NTP query program - Ver. 4.2.8p12
Usage: ntpq [ -<flag> [<val>] | --<name>[{=| }<val>] ]... [ host ...]Flg Arg Option-Name Description-4 no ipv4 Force IPv4 name resolution- prohibits the option 'ipv6'-6 no ipv6 Force IPv6 name resolution- prohibits the option 'ipv4'-c Str command run a command and exit- may appear multiple times-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-i no interactive Force ntpq to operate in interactive mode- prohibits these options: command peers-n no numeric numeric host addressesno old-rv Always output status line with readvar-p no peers Print a list of the peers- prohibits the option 'interactive'-r KWd refid Set default display type for S2+ refids-w no wide Display the full 'remote' valueopt version output version information and exit-? no help display extended usage information and exit-! no more-help extended usage information passed thru pager-> opt save-opts save the option state to a config file-< Str load-opts load options from a config file- disabled as '--no-load-opts'- may appear multiple times
基础实例:
# 1) 打印对等体的列表即查看ntp服务器与上层ntp的状态(如开启nopeer选项其它主机无法获取)
dns-server:~$ ntpq -p
dns-server:~$ ntpq -p 192.168.12.254# remote refid st t when poll reach delay offset jitter# ==============================================================================# LOCAL(0) .LOCL. 8 l 332 64 40 0.000 0.000 0.000# ntp.aliyun.com .POOL. 16 p - 64 0 0.000 0.000 0.000# *120.25.115.20 10.137.53.7 2 u 8 16 377 33.766 -2.872 0.170# +203.107.6.88 10.137.38.86 2 u 8 16 377 53.024 5.287 1.349# 参数说明# remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先# refid:参考上一层ntp主机地址# st:stratum阶层# when:多少秒前曾经同步过时间# poll:下次更新在多少秒后# reach:已经向上层ntp服务器要求更新的次数# delay:网络延迟# offset:时间补偿# jitter:系统时间与bios时间差# ~$ ping ntp.aliyun.com# PING ntp.aliyun.com (203.107.6.88)
0x05 入坑出坑
问题1.ntpdate[820110]: no server suitable for synchronization found
描述:在ntp服务器启动之后需要一段时间才能够通过客户端进行时间的同步,所以如果这个问题是在服务器端刚启动的时候出现的是属于正常的情况,请等待一段时间在进行尝试。如果还是有问题可以查看网络或者是服务器端与客户端的配置情况。
问题复现:
~$ sudo ntpdate 192.168.12.254
19 Mar 04:50:24 ntpdate[820110]: no server suitable for synchronization found
问题原因:
1.防火墙未开放
2.ntp服务端未配置子网时间同步获取
解决办法:
# (1) 在ntp客户端用 ntpdate –d serverIP 观察服务状态如发现以下错误
- 错误1.Server dropped: strata too high 并且显示 stratum 16 表示 stratum 值有误;
- 错误2.Server dropped: no data 检查`ntp`的版本如果你使用的是ntp4.2(包括4.2)之后的版本, 在restrict的定义中使用了notrust的话,会导致以上错误。
# (2) 禁用防火墙
正常状态回显:
26 Mar 22:19:43 ntpdate[3156]: adjust time server 192.168.100.110 offset -0.001781 sec
问题2.ntpdate[35934]: the NTP socket is in use, exiting
描述: 在使用ntpdate进行同步时如果发现NTP端口被占用的提示,请使用ps命令查看系统是不是运行着ntp相关的服务,很多服务器安装上之后会自动的启动一些服务,之用将其关闭就可以了。
问题错误:
dns-server:~$ ntpdate 192.168.12.254
19 Mar 13:27:40 ntpdate[35934]: the NTP socket is in use, exiting
问题原因: 不能在启用了ntp服务的机器上再使用ntpdata进行时间同步,需要将ntp服务停止后才能使用该命令
dns-server:~$ ps aux | grep "ntp"
# ntp 41534 0.0 0.0 74636 4124 ? Ssl 15:32 0:00 /usr/sbin/ntpd -p /var/run/ntpd.pid -g -u 112:117
问题3.ntpd与ntpdate区别说明
描述: 使用之前得弄清楚一个问题 ntpd与ntpdate在更新时间时有什么区别?
ntpd : 它是时间同步服务器并且还可以做客户端与标准时间服务器进行同步时间(平滑同步)
ntpdate : 在生产环境中慎用ntpdate(跃变同步)
Tips: 正如此两者不可同时运行, 因为时钟的跃变,对于某些程序会导致很严重的问题。例如许多应用程序依赖连续的时钟(取得的时间是线性的),对于数据库事务通常会地依赖这样的事实,时间不会往回跳跃。
Q: ntpdate使用在生产环境中的坏处?
不安全: ntpdate 的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。
不精确: 一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
不够优雅: 由于是跳变,而不是使时间变快或变慢依赖时序的程序会出错, 最好的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间;ntpd 在和时间服务器的同步过程中,
会把BIOS计时器的振荡频率偏差——或者说Local Clock的自然漂移(drift)一一记录下来
。如此即使网络有问题本机仍然能维持一个相当精确的走时。
参考来源
阿里云: https://developer.aliyun.com/mirror/NTP?spm=a2c6h.13651102.0.0.53322f708XGXRj
运维小知识之企业内部NTP服务器基础安装与配置使用相关推荐
- 运维实战之企业内部NTP服务器基础安装与配置使用
关注「WeiyiGeek」公众号 将我设为「特别关注」,每天带你玩转网络安全运维.应用开发.物联网IOT学习! 文章目录 0x00 前言简述 基础概念 服务方式 公共 NTP 服务器 0x01 服 ...
- 【泛微系统】OA运维小知识
OA运维小知识 功能开关目录: D:\WEAVER\ecology\WEB-INF\prop\module 路径存放目录: D:\WEAVER\ecology\WEB-INF\prop\page 右键 ...
- 工作中有可能用到的运维小知识《现查现用》
第一部分:基础部分 1.1window到linux互传文件 1.1.1使用rz/sz命令 安装:yum install lrzsz –y 最好安装下面的内容: yum install telnet n ...
- 【运维小知识】单点登录是什么意思?有什么作用?
很多刚入行的运维小伙伴,对于单点登录相关知识不是很了解,例如单点登录是什么意思?有什么作用?单点登录与多点登录有什么区别?这里我们小编就给大家简单回答,希望可以加深大家的了解. 单点登录是什么意思? ...
- 【IT运维小知识】安全组是什么意思?
很多刚入行的IT运维小白,对于IT生活充满向往,对IT知识充满好奇心.有人问,安全组是什么意思? 安全组是什么意思? 安全组是一种虚拟防火墙,用于控制安全组内ECS实例的入流量和出流量,从而提高ECS ...
- 服务器运维相关知识,网络工程师,关于服务器的基本知识
对于非专业的用户来说,服务器它到底是一个什么东西?它的作用是什么呢,其实服务器也就是Server一词,其实是有软硬之分.从硬件的角度来说,Server是物理上存在的服务器:也就是我们看的见摸得着的,那 ...
- 运维小知识之CDN内容分发网络原理解析
0x00 前言简述 基础概念 工作原理 组成部分 应用场景 0x01 基础配置 CDN 入门配置 CDN 跨域设置 CDN 响应头参数 (扩充) 0x02 边缘脚本与程序 EdgeScript - 边 ...
- 运维小姐姐说这篇Consul集群和ACL配置超给力(保姆级)
前言 上一篇简单介绍了Consul,并使用开发模式(dev)进行流程演示,但在实际开发中需要考虑Consul的高可用和操作安全性,所以接着来聊聊集群和ACL的相关配置,涉及到的命令会在环境搭建过程中详 ...
- 业务运维:站在企业转型风口上的云智慧
文:胖头陀 云智慧绝对不是一间大的公司. 尽管在所处的"江湖"里,它已经是响当当的角色,然而毕竟原先的市场领域相对狭小,于是殷晋总会有些使不出力的感受. 殷晋是云智慧的创始人,云智 ...
最新文章
- 零基础入门学习Python(34) 面向对象
- Gotchas 44-引用和临时对象
- 求逆元(线性求逆元)及其扩展欧几里得
- ai算子是什么_肇观电子刷新端侧AI芯片性能记录并发布“5分钟部署”AI开发平台 - 企业资讯...
- JS面向对象,创建,继承
- 想不到吧?数学还有如此妙用!
- 【剑指offer】_17正则表达式的匹配
- 系统建模、分析、仿真和验证软件工具ModelCodoer
- 你看那个人他像一条狗
- (转)(VS2013 )由于应用程序配置不正确,程序未能启动”--原因及解决方法...
- python 网格搜索_调参必备--Grid Search网格搜索
- Stereoscopic Player播放器的控制
- 微信防撤回以及多开工具
- Python 爬虫---百度首页
- 简单快速生成序列化ID
- ISO/IEC 9126 软件质量模型
- 关于c#,PHP socket挂到云服务器上之后本机无法连接的问题
- TreeMap集合怎样依照Value进行排序
- 大于2TB的卷的知识.主要关于windows, EFI,GPT
- Hive基础知识概念
热门文章
- 人工智能现在已经可以参与操纵高超音速飞行器
- 多所高校官宣:寒假提前,延长假期!开启学生第一批返乡工作!
- Elasticsearch聚合查询多字段设置权重
- tssd加载lisp_求助帖:为什么我找到的这个LISP加载完不管用呢....
- Android自定义控件:imageview重写onMeasure方法实现图片按指定比例显示,拉伸永不变形,解决屏幕适配问题
- win11死机怎么办?教你解决电脑死机的方法
- 形态学重建 matlab,形态学灰度重建(一)
- 像素画动画教程:超级马里奥
- 满了18岁你就要这样
- 自动报靶系统可应用于室内室外射击场训练使用