首页 > 系统相关 >Linux上chrony配置时间同步、主从时间同步设备配置

Linux上chrony配置时间同步、主从时间同步设备配置

时间:2024-09-23 19:21:16浏览次数:3  
标签:同步 Linux chrony systemctl localhost time root pool

一.计算机设备上的两种时间

1.硬件时间

计算机主板BIOS上依靠主板电池供电维持运行的时间,设备关机后依然在持续计时,计算机系统开机启动时会自动读取这个时间。

2.系统时间

软件系统上的时间,软件启动后会去读取硬件时间,之后会独立于硬件时间运行。


二.配置同步时间服务器

1.安装服务

(1)安装chrony

[root@localhost ~]# yum install -y chrony

(2)开启chronyd服务

[root@localhost ~]# systemctl start chronyd

(3)设置开机自启动

[root@localhost ~]# systemctl enable chronyd

2.配置服务

chrony服务的配置主要是修改配置文件/etc/chrony.conf,在配置文件中修改要求参数

(1)添加时间服务器(参照源头)

[root@localhost ~]# vim /etc/chrony.conf
 
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
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool ntp3.aliyun.com iburst
pool ntp4.aliyun.com iburst
pool ntp5.aliyun.com iburst
pool ntp6.aliyun.com iburst
 
//推荐使用阿里的时间端
//也可以书写ip地址,以此来同步目标的系统时间

Linux上chrony配置时间同步、主从时间同步设备配置_vim

(2) allow选项

/etc/chrony.conf中文本“Allow NTP client access from local network.”处可以指定ntp可以从客户端从本地网络访问。格式为allow ip/掩码

(3)从配置文件中保存退出后,重启chronyd服务

[root@localhost ~]# systemctl restart chronyd

(4)通过命令进行查看是否同步,出现^*即表示有可同步的源,符号具体含义参照后文

[root@localhost ~]# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^+ 110.42.98.138                 2   6   377    59  -2084us[-2084us] +/-   39ms
^- ntp8.flashdance.cx            2   6   377    63   +104us[  -60us] +/-   89ms
^- ntp1.flashdance.cx            2   6   377   126  -9104us[-9114us] +/-   96ms
^* time.neu.edu.cn               1   6   377    60  +2861us[+2696us] +/-   29ms
[root@localhost ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- 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               
===============================================================================
^+ 110.42.98.138                 2   6   377    65  -2228us[-2084us] +/-   39ms
^- ntp8.flashdance.cx            2   6   377    69    -39us[  -60us] +/-   89ms
^- ntp1.flashdance.cx            2   6   377     2  +1024us[+1172us] +/-   93ms
^* time.neu.edu.cn               1   6   377     1  +3214us[+3362us] +/-   29ms

Linux上chrony配置时间同步、主从时间同步设备配置_IP_02

(5)通过date或者命令timedatectl查询

通过date或者命令timedatectl查询,“NTP synchronized: yes”即表示同步成功。符号具体含义参照后文。

[root@localhost ~]# timedatectl
               Local time: 一 2024-09-23 18:08:58 CST
           Universal time: 一 2024-09-23 10:08:58 UTC
                 RTC time: 一 2024-09-23 10:08:58
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@localhost ~]#

Linux上chrony配置时间同步、主从时间同步设备配置_vim_03


三.搭建主从时间服务器

搭建两台虚拟机,一台作为服务器端,一台作为客户端

1.服务器端

先保证能够正确同步时间,后将自己搭建为服务器,并允许第二台虚拟机ip所在网段可以通过此服务器进行时间同步

vim /etc/chrony.conf

allow 192.168.202.0/24

Linux上chrony配置时间同步、主从时间同步设备配置_配置文件_04

下面是完整的配置文件内容。

Linux上chrony配置时间同步、主从时间同步设备配置_IP_05

[root@localhost ~]# cat /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool 2.rocky.pool.ntp.org iburst

# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 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

# Allow NTP client access from local network.
allow 192.168.202.0/24

# Serve time even if not synchronized to a time source.
#local stratum 10

# Require authentication (nts or key option) for all NTP sources.
#authselectmode require

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# Insert/delete leap seconds by slewing instead of stepping.
#leapsecmode slew

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

重启chronyd服务。

2.客户端

像上文一样将虚拟机1的ip地址作为server写入配置文件,重启服务即可。

(如下IP仅为演示,实际替换为你实验的服务器端的IP)

vim /etc/chrony.conf

server 192.168.202.41 iburst

Linux上chrony配置时间同步、主从时间同步设备配置_IP_06

下面是完整的配置文件内容。

Linux上chrony配置时间同步、主从时间同步设备配置_配置文件_07

[root@localhost ~]# cat /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
server 192.168.202.41 iburst
# Please consider joining the pool (https://www.pool.ntp.org/join.html).
pool 2.rocky.pool.ntp.org iburst

# Use NTP servers from DHCP.
sourcedir /run/chrony-dhcp

# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# 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

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Require authentication (nts or key option) for all NTP sources.
#authselectmode require

# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Save NTS keys and cookies.
ntsdumpdir /var/lib/chrony

# Insert/delete leap seconds by slewing instead of stepping.
#leapsecmode slew

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

重启chronyd服务。

3.注意:关闭防火墙+关闭selinux

配置主从时间同步,服务器端需要关闭防火墙(可参照专栏相关文章) +SElinux。

###关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl start firewalld.service

systemctl is-enabled firewalld.service
systemctl disable firewalld.service
systemctl enable firewalld.service


###关闭SELinux
getenforce
setenforce 0
vim /etc/selinux/config
把
SELINUX=enforcing
修改成
SELINUX=disabled


四.chrony命令参数详解

上述步骤完成后通过命令行查看同步状态以及解析输出信息含义

格式:chronyc+参数 

参数

参数含义

sources (-v)

查看时间同步生效情况,-v可以生成详细符号解释便于判断

sourcestats (-v)

查看服务状态, -v可以生成详细符号解释便于判断

activity

查看服务器是否在线

tracking

查看服务详细信息

-a makestep

强制同步系统时间


五.chrony展示信息中的特殊名词解释

1.sources -v中显示的符号含义

[root@localhost ~]# chronyc sources -v

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- 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               
===============================================================================
^+ 110.42.98.138                 2   6   377    65  -2228us[-2084us] +/-   39ms
^- ntp8.flashdance.cx            2   6   377    69    -39us[  -60us] +/-   89ms
^- ntp1.flashdance.cx            2   6   377     2  +1024us[+1172us] +/-   93ms
^* time.neu.edu.cn               1   6   377     1  +3214us[+3362us] +/-   29ms
[root@localhost ~]#

Linux上chrony配置时间同步、主从时间同步设备配置_IP_08

(1)M: 表示信号源的模式。^表示服务器,=表示对等方,#表示本地连接的参考时钟。

(2)S:指源的状态:

*表示chronyd当前已经同步到的源。

+表示可接受的信号源,与选定的信号源组合在一起。

-表示被合并算法排除的可接受源

?指已失去连接性或者其数据包未通过所有测试的源。

x表示chronyd认为时虚假行情的时钟,即标记该时间与其他多数时间不一致

~表示时间似乎具有太多可变性

(3)Name/IP address:显示源的名称或IP地址,或参考时钟的参考ID。

(4)Stratum:显示时间来源的层,如其最近收到的样本中所报告。层1表示一台具有本地连接的参考时钟的计算机,与第1层计算机同步的计算机位于第2层,与第2层计算机同步的计算机位于第3层,依此类推。

(5)Poll:显示轮询源的速率,以秒为单位的时间间隔的以2为底的对数。因此,值为6表示每64秒进行一次测量。chronyd会根据当前情况自动更改轮询速率。

(6)Reach:显示源的可达性寄存器以八进制数字打印。寄存器有8位,并在每个从源接收或丢失的数据包上更新。值377表示从最后八次传输中收到了对所有用户的有效答复。

(7)LastRx:显示多长时间前从来源接收到了最后一个好的样本(在下一列中显示)。未通过某些测试的测量将被忽略。通常以秒为单位。字母m,h,d或y表示分钟,小时,天或年。

(8)Last sample:此列显示上次测量时本地时钟与源之间的偏移。方括号中的数字表示实际测得的偏移量。可以用ns(表示纳秒),us (表示微秒),ms(表示毫秒)或s(表示秒)作为后缀。方括号左侧的数字表示原始测量值,已调整为允许此后施加于本地时钟的任何摆度。+/-指示器后面的数字表示测量中的误差范围。正偏移表示本地时钟位于源时钟之前。

2.sourcestats -v中符号的含义

[root@localhost ~]# chronyc sourcestats -v
                             .- 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
==============================================================================
110.42.98.138              24  12   30m     -0.852      2.852  -3988us  1805us
ntp8.flashdance.cx         20  11   20m     -0.966      2.943  -2030us  1379us
ntp1.flashdance.cx         21  10   32m     -0.109      2.557   -240us  1961us
time.neu.edu.cn            24  12   29m     +0.187      1.155  +2965us   690us
[root@localhost ~]#

Linux上chrony配置时间同步、主从时间同步设备配置_IP_09

(1)NP:测量集中的采样点数。

(2)NR: 具有相同符号的残差运行数。

(3)Span:测量集的长度(时间)。

(4)Frequency:估计时钟频率误差 (ppm)。

(5)Freq Skew:频率错误估计。

(6)Offset:估计偏移量。

(7)Std Dev:标准偏差。

3.timedatectl查询到的符号信息

[root@localhost ~]# timedatectl
               Local time: 一 2024-09-23 18:08:58 CST
           Universal time: 一 2024-09-23 10:08:58 UTC
                 RTC time: 一 2024-09-23 10:08:58
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@localhost ~]#

Linux上chrony配置时间同步、主从时间同步设备配置_配置文件_10

(1)CST:中国、澳大利亚等的标准时间。

(2)UTC:协调世界时间。将地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间。

(3)GMT:格林威治标准时间。指位于英国伦敦格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。

(4)DST:夏令时,指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。

(5)RTC:实时时钟,可以提供时钟日历的功能。



关闭防火墙+关闭selinux

###关闭防火墙
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl start firewalld.service

systemctl is-enabled firewalld.service
systemctl disable firewalld.service
systemctl enable firewalld.service


###关闭SELinux
getenforce
setenforce 0
vim /etc/selinux/config
把
SELINUX=enforcing
修改成
SELINUX=disabled


标签:同步,Linux,chrony,systemctl,localhost,time,root,pool
From: https://blog.51cto.com/lenglingx/12091168

相关文章

  • 用Asio实现同步echo服务器
    使用Asio实现同步echo服务器服务端sync_server.h#ifndefASYNC_SERVER_H#define ASYNC_SERVER_H#include<boost/asio/ip/tcp.hpp>#include<memory>#include<set>#include<thread>namespaceMS{ typedefstd::shared_ptr<boost::asio::ip::......
  • 线程同步:锁,条件变量,并发
    1)锁mutex2)条件变量头文件<condiction_variable>condition_variablecv;cv.wait(_lock,谓语)//使用谓语检查是否满足唤醒条件,防止假唤醒usingnamespacestd; mutex_mutex; condition_variablecv; //condition_variablecv2; intnum=1; threadth1([&](){ int......
  • 主从数据库同步配置详解(MySQL/MariaDB)
    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、环境准备与安装配置本地部署MySQLUbuntu系统:CentOS系统:MariaDBUbuntu系统:CentOS系统:容器部署MySQLMariaDB二、配置主从库的同步设置四、测试与维护总结前言在数据库管理中,......
  • C#实现信创国产Linux桌面录制成MP4(源码,银河麒麟、统信UOS)
    信创国产化已是大势所趋,在国产操作系统上的应用开发的需求越来越多,比如,有客户需要在银河麒麟和统信UOS上实现录制桌面生成一个mp4文件。那么这个要如何实现了?一.技术方案要完成这些功能,具体来说,需要解决如下几个技术问题:(1)桌面图像数据采集。(2)视频数据编码(H264)。(3)将编码后的......
  • 学Linux费用多少?有什么好处?
    Linux是一款开源、稳定、安全的操作系统,在云计算、大数据、物联网、金融、教育、电子商务等多个领域得到了广泛应用。尤其是随着互联网技术的发展,Linux专业人才需求量持续增长,其薪资待遇也较为丰厚,吸引了很多小伙伴前来学习。那么北京Linux培训要多少钱?包括哪些服务?以下是具......
  • 【Linux】Shell 编程规范及检查工具推荐
    本文内容均来自个人笔记并重新梳理,如有错误欢迎指正!如果对您有帮助,烦请点赞、关注、转发、订阅专栏!专栏订阅入口| 精选文章 | Kubernetes |Docker |Linux |羊毛资源 | 工具推荐 |往期精彩文章【Docker】(全网首发)KylinV10下MySQL容器内存占用异常的解决......
  • Linux 文件压缩和解压缩命令
    Linux文件压缩和解压缩命令在Linux操作系统中,文件压缩和解压缩是日常管理和维护任务中的重要一环。通过压缩文件,可以显著减少存储空间的使用,并加快网络传输速度。Linux提供了多种压缩和解压缩工具,每种工具都有其特定的格式和优势。以下是一些常用的Linux文件压缩和解压缩命令及......
  • 【PAM】Linux登录认证限制
    PAM(PluggableAuthenticationModules,可插拔认证模块)是一种灵活的认证框架,用于在Linux和其他类Unix系统上管理用户的身份验证。PAM允许系统管理员通过配置不同的认证模块来定制应用程序和服务的认证方式,而不需要修改这些应用程序的源代码。0、PAM介绍PAM的核心概念......
  • g++。gcc在linux下的适用
    gcc和g++编译器的常用命令行参数非常丰富,这些参数在编译过程中起着至关重要的作用。以下是一些常用的命令行参数及其解释:一、编译过程控制预处理(Pre-processing)-E:只执行预处理操作,不进行编译、汇编和链接。预处理过程包括宏替换、条件编译、头文件展开、删除注释等,输出预处理......
  • Linux网络命令
    1.ping测试网络连通性-c指定ping次数      ping -c4   4次停-i   指定间隔时间-s   指定数据包的大小2.ifconfig查询本机网络信息  作用与  ipa|addr相同ifconfig命令使用方法命令:ifconfig              ......