实现【linux--NTP 时间同步服务搭建】 🔻 前言🔻 一、NTP 校时🔰 1.1 NTP 服务校时与 ntpdate 校时的区别
👈【上一篇】 | 💖The Begin💖 点点关注,收藏不迷路💖 | 【下一篇】👉 |
当应用系统走向多节点、高并发、高可用部署需求时
,不同节点时间不一致
,不仅仅会导致业务报错,还会出现将硬件时间直接频繁同步导致硬件故障。
为解决以上问题-----引入NTP 校时服务
。
ntp 服务校时不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步
,并非 ntpdate 立即同步
,在生产环境中慎用 ntpdate,ntp 与 ntpdate不可同时运行
。
简单来讲 NTP 服务本身是为了搭建校时服务器对外提供授时服务的。但是这个服务本
身有个功能,就是可以从这个服务工作的机器,配置一个外网的时间服务器,将本机的时间
校时成网络时间。自己时间不对,咋给别人校时?
主节点:本身可以上网,配置校时服务的时候配置上游国家授时中心服务地址,并对内网提供授时服务—这里环境均采用内网,手动设置主节点为标准时间,分节点同步。
分节点:本身属于内网运行,上游地址是主节点。
###1、这里已安装了ntp和ntpdate,为了方便演示,先移除[root@pg-node01 ~]# rpm -qa | grep ntpfontpackages-filesystem-1.44-8.el7.noarchntp-4.2.6p5-29.el7_8.2.x86_64ntpdate-4.2.6p5-29.el7_8.2.x86_64[root@pg-node01 ~]####2、先卸载移除ntp 与 ntpdateyum -y remove ntp-4.2.6p5-29.el7_8.2.x86_64yum -y remove ntpdate-4.2.6p5-29.el7_8.2.x86_64###3、重新安装 ntpyum -y install ntpdateyum -y install ntp###4、安装检查[root@pg-node01 ~]# rpm -qa | grep ntpfontpackages-filesystem-1.44-8.el7.noarchntp-4.2.6p5-29.el7_8.2.x86_64ntpdate-4.2.6p5-29.el7_8.2.x86_64[root@pg-node01 ~]#
1、选择主节点pg-node01,修改其/etc/ntp.conf
。
210.72.145.44
(国家授时中心服务器IP地址)。
### 在 server 部分添加如下内容,并注释掉 server 0 ~ nserver 210.72.145.44 preferserver 127.127.1.0 iburst
📖 说明:
server 210.72.145.44 prefer
这是国家新的授时服务器地址,prefer 是优先使用的意思,还有更多,可以直接百度,国内的NTP Server基本都属于科研教育机构所有,普通社会组织发布的NTP Server并不多。
server 127.127.1.0 iburst
指定本地NTP服务地址,这个 ip 不要改,意思是如果网络授时服务器源访问不到了则直接使用本机作为授时服务器源。
2、主节点以外的节点pg-node02,继续修改/etc/ntp.conf。
在 server 部分添加如下语句,将 server 指向主节点。
server 主节点 ip 地址如:server 192.168.181.11
3、操作系统防火墙开放 123 端口
由于 NTP 服务需要使用到 UDP
端口号 123
,所以当系统的防火墙(Iptables)启动的情况下,必须开放 UDP
端口号 123
。
###开放 `UDP` 端口号 `123`firewall-cmd --zone=public --add-port=123/udp --permanent###查看所有已经开放的端口#临时端口(默认为空)firewall-cmd --list-ports#永久开放端口(默认为空)[root@pg-node01 ~]# firewall-cmd --list-ports --permanent123/udp[root@pg-node01 ~]#
### 启动 ntp 服务[root@pg-node01 ~]# systemctl start ntpd.service### ntp 服务管理命令#查看 ntpd 服务状态service ntpd status#启动 ntpd 服务service ntpd start #停止 ntpd 服务service ntpd stop #重启 ntpd 服务service ntpd restart
检查 ntp 服务是否开机启动,将其设置为开机启动。
### 启动服务systemctl start ntpd.service ### 停止服务systemctl stop ntpd.service ### 将服务设置为自启动systemctl enable ntpd.service ### 将服务自启动关闭systemctl disable ntpd.service
重启后用 service ntpd status
命令查看服务是否启动,从给出的信息可知 ntp 已被设置为开机启动,但是开机启动并未成功。
[root@pg-node02 ~]# service ntpd statusRedirecting to /bin/systemctl status ntpd.service● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: inactive (dead)[root@pg-node02 ~]#
通过命令 chkconfig ntpd on
设置开机自启动,发现会将此命令转发到 systemctl enable ntpd.service 命令,也就是说最好使用后面的命令设置开机自启动。
[root@pg-node02 ~]# chkconfig ntpd onNote: Forwarding request to 'systemctl enable ntpd.service'.[root@pg-node02 ~]#
原因是系统上安装了一个与 NTP 相冲突的工具:chrony。使用 systemctl is-enabled chronyd
来查看, chrony 被设置为enabled
。
[root@pg-node02 ~]# systemctl is-enabled chronydenabled[root@pg-node02 ~]#
将 chronyd 设置为 disable 后,再重启即可实现NTP自启动。
[root@pg-node02 ~]# systemctl disable chronydRemoved symlink /etc/systemd/system/multi-user.target.wants/chronyd.service.[root@pg-node02 ~]#
### 查看命令ntpstat
ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
等一段时间之后,再次使用 ntpstat 命令查看状态,就会变成如下正常结果:
[root@pg-node02 ~]# ntpstatsynchronised to NTP server (192.168.181.11) at stratum 7 time correct to within 21 ms polling server every 64 s[root@pg-node02 ~]#
### 命令ntpq -p
[root@pg-node01 ~]# ntpq -p remote refid st t when poll reach delay offset jitter============================================================================== 210.72.145.44 .INIT. 16 u - 1024 0 0.000 0.000 0.000*LOCAL(0) .LOCL. 5 l 23 64 377 0.000 0.000 0.000[root@pg-node01 ~]#
[root@pg-node02 ~]# ntpq -p remote refid st t when poll reach delay offset jitter============================================================================== pg-node01 LOCAL(0) 6 u 26 64 1 2.346 -679.68 0.000[root@pg-node02 ~]#
📖 说明:
remote:本机和上层 ntp 的 ip 或主机名,“+”表示优先,“*”表示次优先
refid:参考上一层 ntp 主机地址
st:stratum 阶层
when:多少秒前曾经同步过时间
poll:下次更新在多少秒后
reach:已经向上层 ntp 服务器要求更新的次数
delay:网络延迟
offset:时间补偿
jitter:系统时间与 biOS 时间差
分节点不能上网,属于内网服务器从主节点获取时间,本身支持对内网提供授时服务,
但是不再对内网提供服务,仅仅满足自己的时间与主节点保持一致。
####修改pg-node02分节点时间与主节点相差3h,主节点-now time :13:00:00date -s 10:00:00####写入BioSclock -w ###显示bios时间hwclock -r####date查看---会自动同步pg-node01的时间[root@pg-node02 ~]# dateFri Jul 7 13:42:17 CST 2023[root@pg-node02 ~]#
❓ 该章详细介绍和实现了Linux--NTP 时间同步服务搭建。
👈【上一篇】 | 💖The End💖 点点关注,收藏不迷路💖 | 【下一篇】👉 |
来源地址:https://blog.csdn.net/qq_41840843/article/details/131587397
--结束END--
本文标题: 实现【Linux--NTP 时间同步服务搭建】
本文链接: https://www.lsjlt.com/news/384924.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0