百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

CentOS - 时间同步服务ntp 时间服务器同步失败

lipiwang 2024-10-28 17:29 9 浏览 0 评论

简介

在一些集群服务中,不同的主机时间不一致,会导致服务异常。因此都会使用ntp(Network Time Protocol)进行时间同步。

操作系统

CentOS Linux release 7.6.1810 (Core)

安装

yum -y install ntp 

配置

默认配置会,同步外网的ntp时间服务。

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
#系统时间与BIOS事件的偏差记录
driftfile /var/lib/ntp/drift 
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
#restrict 控制相关权限。
#语法为: restrict IP地址 mask 子网掩码 参数
#其中IP地址也可以是default ,default 就是指所有的IP
#参数有以下几个:
#ignore :关闭所有的 NTP 联机服务
#nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
#notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
#noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
#notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
#nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
#kod : 访问违规时发送 KoD 包。
#restrict -6 表示IPV6地址的权限设置。
#对本机不做限制,拥有所有的权限
restrict 127.0.0.1 
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
#设置默认策略为允许任何主机进行时间同步
restrict default ignore
#此处表示限制向从192.168.1.1-192.168.1.254这些IP段的服务器提供NTP服务。
#restrict 192.168.1.0 mask 255.255.255.0 notrust nomodify notrap noquery
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#设定NTP主机来源(其中prefer表示优先主机),192.168.1.123是本地的NTP服务器,所以优先指定从该主机同步时间。
server 192.168.1.123 prefer 
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
#broadcast 192.168.1.255 autokey	# broadcast server
#broadcastclient			# broadcast client
#broadcast 224.0.1.1 autokey		# multicast server
#multicastclient 224.0.1.1		# multicast client
#manycastserver 239.255.254.254		# manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor

服务启动时,同步的时间服务器

修改/etc/ntp/stpe-tickers文件

server 192.168.1.123 
server 0.centos.pool.ntp.org 
server 1.centos.pool.ntp.org 
server 2.centos.pool.ntp.org 
server 3.centos.pool.ntp.org 

配置硬件时间同步

ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。

#允许BIOS与系统时间同步,也可以通过hwclock -w 命令

SYNC_HWCLOCK=yes

启动服务

systemctl start ntpd 

查看ntp服务器有无和上层ntp连通

ntpstat

synchronised to NTP server (120.25.115.20) at stratum 3

time correct to within 85 ms

polling server every 64 s

查看ntp服务器与上层ntp的状态

ntpq -p

remote refid st t when poll reach delay offset jitter

==============================================================================

*120.25.115.20 10.137.53.7 2 u 39 64 7 34.227 -1.895 5.605

amy.chl.la .INIT. 16 u - 64 0 0.000 0.000 0.000

+xk-6-95-a8.bta. 10.41.14.199 2 u 36 64 7 38.472 1.974 1.703

ntp7.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000

remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先

refid - 参考上一层ntp主机地址

st - stratum阶层

when - 多少秒前曾经同步过时间

poll - 下次更新在多少秒后

reach - 已经向上层ntp服务器要求更新的次数

delay - 网络延迟

offset - 时间补偿

jitter - 系统时间与bios时间差


总结

时间同步服务,是一个很简单的服务。

相关推荐

Java 实体映射工具 MapStruct(jpa实体类映射类)

简介:让你的DO(业务实体对象),DTO(数据传输对象)数据转换更简单强大前言在软件架构中,分层式结构是最常见,各层之间有其独立且隔离的业务逻辑,也因而各层有自己的输入输出对象,也就是代码中见到各...

@Date不管用怎么办,想少写get和setter方法,怎么办

学习交流群:293911833,有遇到问题的,可以加一下群,大家互相交流,一起进步今天在使用lombok的时候,为什么@Date不管用,是我映射没做好吗?还是其他的,后来查了一些大佬的资料终于找到问...

Spring系列之集成MongoDB的2种方法

MongoDB是最流行的NoSQL数据库,SpringBoot是使用Spring的最佳实践。今天带大家讲一讲SpringBoot集成MongoDB的两种方式,MongoDB的安装自行去官网查询,本地开...

Spring Boot集成SLF4j详解(springboot集成knife4j)

SpringBoot集成SLF4j详解:从基础到高级实践SLF4j(SimpleLoggingFacadeforJava)是一个日志门面框架,提供统一的日志接口,允许开发者灵活切换底层...

自定义代码生成器(上)(代码自动生成器)

1概述1.1介绍在项目开发过程中,有很多业务模块的代码是具有一定规律性的,例如controller控制器、service接口、service实现类、mapper接口、model实体类等等,这部分代...

Java系统开发从入门到精通第四讲(文字版)

课程目标:了解重要的JavaAPI和一些必备框架的使用,这些都是系统开发的标配需要掌握日期时间APIJava8通过发布新的Date-TimeAPI(JSR310)来进一步加强对日期与时间...

重拾JAVA:这种编程语言为什么不行了?

全文共2322字,预计学习时长6分钟为了应对新工作,笔者在过去两周一直在重新熟悉一位老朋友:JAVA。我以JAVA开启了我的软件事业,与之共行了两年半左右的时间。但是随着容器和微服务的出现,JAVA很...

一款提高Java开发效率的工具(java怎么提高技术)

今天来介绍一款Java常用插件:Lombokhttps://projectlombok.org/通常在用Java代码开发项目过程中,都会建立各种各种的Bean类,如下:publicclassSea...

JDK从8升级到21的问题集(jdk1.6升级到1.8要考虑的因素)

一、背景与挑战1.升级动因oOracle长期支持策略o现代特性需求:协程、模式匹配、ZGC等o安全性与性能的需求oAI新技术引入的版本要求2.项目情况o100+项目并行升级的协同作战o多技术栈并存o持...

Lombok:让Java代码变得优雅简洁的秘密武器

Lombok:让Java代码变得优雅简洁的秘密武器在Java的世界里,代码量往往是一个开发者幸福感的重要衡量指标。代码写得越少,出错的可能性就越小,同时维护起来也更轻松。而Lombok正是这样一个让J...

SpringToolSuite(STS)安装lombok插件

一、打开maven仓库,找到lombok所在的文件夹二、在命令行中运行java-jarlombok-1.18.20.jar,打开lombok插件安装的可视化界面。三、选择STS安装目录,点击Ins...

年末将至,Java 开发者必须了解的 15 个Java 顶级开源项目

专注于Java领域优质技术,欢迎关注作者:SnailClimbStar的数量统计于2019-12-29。1.JavaGuideGuide哥大三开始维护的,目前算是纯Java类型项目中Sta...

相见恨晚,一个架构师也不会用的Lombok注解

原创:不羡鸳鸯不羡仙,一行代码调半天。小姐姐味道(微信公众号ID:xjjdog),欢迎分享,转载请保留出处。我见过很多反对Lombok的同学,背地里又偷偷的把插件添加了进去,这是真香原理在搞鬼。嘴上说...

第二弹!安排!安利几个让你爽到爆的IDEA必备插件

作者:Guide哥来自:JavaGuide大家好,我是Guide哥。上一篇关于IDEA插件推荐的文章:《第一弹!安排!安利10个让你爽到爆的IDEA必备插件!》收到了很多小伙伴的好评,时隔大半个月...

Java @Data注解(java @order注解)

1、@Data注解是lombok.jar包下的注解,该注解通常用在实体bean上,不需要写出set和get方法,但是具备实体bean所具备的方法,简化编程提高变成速度。2、@Data相当于@Gette...

取消回复欢迎 发表评论: