需求:最近发现多个服务器之间的时间不一致,导致很多问题。
解决:使用ntp实现多台服务器时间同步
[ntp] 网络时间协议,英文名称:Network Time Protocol(NTP)
概念:是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。

start

前景
如果是多台的话选择一台机器为主节点,其他的为从节点,主节点用来同步从节点时间。

安装

  • 搭建主节点时间服务器
  • 需要手动安装ntp服务,这个服务有的linux版本上会默认装上,我们可以通过如下命令来查看是否有装上
  • rpm -qa | grep ntp
[root@localhost etc]# rpm -qa |grep ntpfontpackages-filesystem-1.44-8.el7.noarchntp-4.2.6p5-29.el7.centos.2.x86_64ntpdate-4.2.6p5-29.el7.centos.2.x86_64

出现以上 ,说明已安装
如果没有的话手动安装一下即可

1、yum list | grep ntp
2、yum -y install ntp.x86_64
或者 yum install ntp –y

配置文件路径

  /etc/ntp.conf

编辑文件

vi /etc/ntp.conf
# 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).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.
# 这个默认是关闭的,注意要打开,不然执行nptd 命令会超时
# 因为是内网,所以用本地时间做为服务器时间
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# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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#inux自带的时间同步,需要注释掉
#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.
#cryptoincludefile /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
# 代表允许的网段,设置自己的网段,在这个网段的所有机器都可以作为时间同步服务端
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
server  127.127.1.0
fudge   127.127.1.0 stratum 10

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap代表允许192.168网段的服务器与此服务器进行时间同步
然后 保存 :wq!

开启服务

service ntpd start   或者 systemctl start ntpd

设置开机自启

chkconfig ntpd on

放行端口123

需要对ntp的默认端口123进行放行,这个要配,或者你关闭了防火墙,否则会报错,
问题:不开启会报 no server suitable for synchronization found 错误

iptables -I INPUT -p tcp --dport 123 -j ACCEPT

查看端口占用
sudo lsof -i -P -n
-i:如果没有指定IP地址,这个选项选择列出所有网络文件
-P:禁止将端口号转换为端口名称, 如 3306 转为 MySQL
-n:禁止IP转换为hostname,缺省是不加上-n参数

查看状态

service ntpd status 或者 systemctl status ntpd 或者 /etc/init.d/ntpd start

停止

service ntpd stop 或者 systemctl stop ntpd

配置客户端(从节点)

安装按以上步骤

编辑配置文件

vi /etc/ntp.conf
# 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).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 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# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#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.
#cryptoincludefile /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
restrict 192.168.54.0 mask 255.255.255.0 nomodify notrap
#server  127.127.1.0server  192.168.54.xxx  #增加主服务器ip即可
#fudge   127.127.1.0 stratum 10fudge   192.168.54.xxx startum 10  #设置stratum级别

保存退出::wq!

注意

如果想要让ntp同时同步硬件时间,可设置

vi /etc/sysconfig/ntpd

添加【SYNC_HWCLOCK=yes】就可以让硬件时间与系统时间一起同步

启动

service ntpd status
service ntpd start
service ntpd restart

查看是否会定时同步

ntpq -p
[root@minio3 etc]# ntpq -premote           refid      st t when poll reach   delay   offset  jitter
==============================================================================192.168.54.xxx  LOCAL(0)        11 u   55   64    7    0.260   -0.001   0.026

remote 就是配置的远程地址
when就代表上次同步距离现在的时间,通过这个时间久可以判断是否有在自动同步。

设置自启动

chkconfig ntpd on

可尝试立即同步

ntpdate -d 192.168.54.xxx  主服务器地址
[root@minio3 etc]# ntpdate -d 192.168.54.xxx
31 May 16:51:36 ntpdate[23931]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
Looking for host 192.168.54.xxx and service ntp
host found : 192.168.54.xxx
transmit(192.168.54.xxx)
receive(192.168.54.xxx)
transmit(192.168.54.xxx)
receive(192.168.54.xxx)
transmit(192.168.54.xxx)
receive(192.168.54.xxx)
transmit(192.168.54.xxx)
receive(192.168.54.xxx)
server 192.168.54.xxx, port 123
stratum 11, precision -24, leap 00, trust 000
refid [192.168.54.xxx], delay 0.02582, dispersion 0.00130
transmitted 4, in filter 4
reference time:    e640558a.4c2701f9  Tue, May 31 2022 16:51:22.297
originate timestamp: e64055b2.98e02642  Tue, May 31 2022 16:52:02.597
transmit timestamp:  e64055b2.77791f84  Tue, May 31 2022 16:52:02.466
filter delay:  0.02614  0.02582  0.02647  0.02589 0.00000  0.00000  0.00000  0.00000
filter offset: 0.127412 0.128211 0.128878 0.1302580.000000 0.000000 0.000000 0.000000
delay 0.02582, dispersion 0.00130
offset 0.12821131 May 16:52:02 ntpdate[23931]: adjust time server 192.168.54.xxx offset 0.128211 sec

修改系统时间(可在主节点设置,从节点查看效果)

date -s '2022-04-29 10:39:00'
clock -w    强制将时间写入coms同步bios时间,强制把系统时间写入CMOS:

ntp实现多台服务器时间同步[实测]相关推荐

  1. windows服务器局域网内与某台服务器时间同步

      本文章转发的别人的,只有一步跟他不一样,原文如下:windows服务器局域网内与某台服务器时间同步 时间同步有两个端,如下: ntp server(被同步的服务器) ntp client (要同步 ...

  2. linux如何让两台或多台服务器时间同步

    缘起 有时候我们有需求让我们的多台服务器时间保持一致,那么怎么做才能做到呢?大概分为以下几步 搭建一台时间服务器 使其他服务器与此时间服务器的时间保持一致并定期更新 搭建时间服务器 要搭建时间服务器手 ...

  3. 局域网内多台linux服务器时间同步的一种解决方案

    局域网内多台服务器时间同步的一种解决方案: 1.将一台能够上网的服务器作为时间服务器:  # /usr/bin/rdate -s time-b.timefreq.bldrdoc.gov //将时间服务 ...

  4. 多台CentOS服务器时间同步(NTP时间同步)

    同一个程序运行在多台服务器上时,有时需要这些服务器的时钟相同,但是当服务器运行一定时间之后,各个服务器上的时钟会有一定差异,此时就需要让所有服务器自动校准各自时钟. NTP是一个很方便的时钟服务,可以 ...

  5. NTP服务器时间同步

    一.简要说明 二.安装步骤 三.配置文件 四.常用命令 五.注意事项 六.运行截图 七.参考资料 一.简要说明           搭建Kubernetes环境,需要几台.几十台机器配合运作,许多集群 ...

  6. java ntp服务器ip地址_NTP服务器时间同步

    一.简要说明 二.安装步骤 三.配置文件 四.常用命令 五.注意事项 六.运行截图 七.参考资料 一.简要说明 搭建Kubernetes环境,需要几台.几十台机器配合运作,许多集群服务比如Etcd等都 ...

  7. ntp如何确认与服务器偏差_LED电子时钟显示屏如何实现时间同步统一校时?

    LED电子时钟显示屏采用一体式铝合金边框设计,更坚固美观节能环保,更以其简单的操作和稳定的性能,广泛应用于学校.考场.医院.金融.移动通信.石油.电力.交通.工业以及国防等同步时钟系统的显示终端,LE ...

  8. NTP服务器时间同步部署 -- 内网环境下,亲测有效.

    1.服务端操作(主服务器) 选定主节点为NTP服务器,其他服务器时间均以此为准. 比如当前我选择 192.168.6.3作为NTP服务器  1.1下载ntp服务端: [命令]yum -y instal ...

  9. ntp时间服务器——时间同步

    具体两种模式: 1.服务器数量比较少,可以直接与时间服务器同步 2.本地服务器较多,在本地自建时间同步服务器 时间同步的两个命令: ntpd : 校准时间,一点点的校准过来时间的,最终把时间慢慢的校正 ...

最新文章

  1. 企业级监控ZABBIX深度实践安装部分
  2. golang经典书籍--go并发编程
  3. 案例:验证用户名是否可用
  4. 使用javaMail收邮件 支持附件下载
  5. python 数据离散化和面元划分
  6. ad域 禁用账号_IST-AD域信息同步平台来袭
  7. Django中的Object Relational Mapping(ORM)
  8. mac硬盘空间怎么清理?这样也能清理出上10G的磁盘空间
  9. vmware下安装mac os虚拟机问题,最后还是最终攻克了被一个小失误给陷害了
  10. 【软碟通 | UltraISO】刻录光盘教程(制作U盘启动盘)
  11. 自然语言处理的学习心得
  12. 问题 M: 【枚举】桐桐数
  13. Python爬虫入门教程 93-100 帮粉丝写Python爬虫之【获取CSDN周榜所有大佬的收费专栏】
  14. seo需要注意哪些细节
  15. 矩阵的特征:主成分分析(PCA)
  16. 吴宇森导演的《赤壁》电影观后感
  17. Httpd:无法找到/etc/httpd/conf
  18. Linus下EDB的安装
  19. 2021NBA全明星正赛+技巧+三分+扣篮大赛下载(英语+国语解说)
  20. 数模优秀论文总结 — 2019 “机场的出租车问题”

热门文章

  1. 产品:关键词与分类---人人都是产品经理day02
  2. 关于考试系统的ajax提交单个答案的压力测试
  3. APS系统在企业生产中的场景应用
  4. 大幅降低PUE绝非不可能 浪潮MDC如何打造梦幻王国?
  5. 怎样编写大智慧指标公式
  6. 淘宝官方商品、交易、订单、物流接口列表(淘宝api)
  7. 软件需求规格说明书模版
  8. JavaWeb学习:http协议与状态码
  9. 学习笔记——web网页庐山旅游网
  10. 学会了这个配音方法,我的视频居然成了热门爆款