IT评测·应用市场-qidao123.com

标题: Linux利用chrony让局域网内的服务器时间同步_chrony同步间隔,走进Linux运 [打印本页]

作者: 曂沅仴駦    时间: 2024-10-16 01:00
标题: Linux利用chrony让局域网内的服务器时间同步_chrony同步间隔,走进Linux运
在所有节点上执行chrony服务安装下令,将主节点设置时间服务器,其他的节点都从主节点同步时间。
  1. # 安装服务
  2. yum -y install chrony
  3. # 查看状态
  4. systemctl status  chronyd
  5. # 重启chronyd
  6. systemctl restart chronyd
复制代码
二、检查设置时区

在所有节点上设置统一的时间地区,本文中将其设置为亚洲时区,用户可自行界说。
  1. # 查看时区
  2. [root@x ~]# timedatectl
  3.       Local time: Sun 2022-04-17 11:15:48 CST
  4.   Universal time: Sun 2022-04-17 03:15:48 UTC
  5.         RTC time: Sun 2022-04-17 11:15:48
  6.        Time zone: Asia/Shanghai (CST, +0800)
  7.      NTP enabled: yes
  8. NTP synchronized: yes
  9. RTC in local TZ: yes
  10.       DST active: n/a
  11. # 筛选式查看在亚洲S开的上海可用时区:
  12. [root@x ~]# timedatectl list-timezones | grep -E "Asia/S.\*"
  13. Asia/Sakhalin
  14. Asia/Samarkand
  15. Asia/Seoul
  16. Asia/Shanghai
  17. Asia/Singapore
  18. Asia/Srednekolymsk
  19. # 设置当前系统为Asia/Shanghai上海时区:
  20. [root@x ~]# timedatectl set-timezone Asia/Shanghai
  21. # 修改日期时间(可选,主节时钟源无法同步,可以先关闭NTP同步)
  22. timedatectl set-ntp false
  23. timedatectl set-time "2022-04-17 15:50:20"
  24. # 开启 NTP
  25. timedatectl set-ntp true
  26. #设置完时区后,强制同步下系统时钟:
  27. [root@x ~]# chronyc -a makestep
  28. 200 OK
复制代码
三、防火墙设置

因NTP利用123/UDP端口协议,所以答应NTP服务即可。但是我一样平常都会把防火墙和selinux禁止。
  1. # 查看防火墙状态
  2. [root@x ~]# systemctl status firewalld.service
  3. ● firewalld.service - firewalld - dynamic firewall daemon
  4.    Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
  5.    Active: inactive (dead)
  6.      Docs: man:firewalld(1)
  7. # 启动防火墙
  8. [root@x ~]# systemctl start firewalld.service
  9. # 防火墙内放行NTP服务
  10. [root@x ~]# firewall-cmd --add-service=ntp --permanent
  11. success
  12. # reload才能生效
  13. [root@x ~]# firewall-cmd --reload
  14. success
复制代码
内部访问一样平常直接关闭防火墙
  1. [root@x ~]# systemctl stop firewalld
  2. [root@x ~]# systemctl disalbe firewalld
复制代码
四、配置Chrony

1.服务器端配置

​ 将主节点设置为内部NTP Server,编辑“/etc/chrony.conf”文件,如有公网的情况,可以配置阿里云的ntp服务地址作为源 。
   ntp1.aliyun.com
  授时中心参考
  
  ​ master01作为服务端,当前节点IP地址为172.16.24.65,网段是172.16.24.0/24,配置详情如下
   server ntp1.aliyun.com iburst
  allow 172.16.24.0/24
  local stratum 10
  1. [root@master01 ~]# vim /etc/chrony.conf
复制代码
  1. # Use public servers from the pool.ntp.org project.
  2. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  3. # server 0.centos.pool.ntp.org iburst
  4. # server 1.centos.pool.ntp.org iburst
  5. # server 2.centos.pool.ntp.org iburst
  6. #server 3.centos.pool.ntp.org iburst
  7. # 公网时间服务器
  8. server ntp1.aliyun.com iburst
  9. # Record the rate at which the system clock gains/losses time.
  10. driftfile /var/lib/chrony/drift
  11. # Allow the system clock to be stepped in the first three updates
  12. # if its offset is larger than 1 second.
  13. makestep 1.0 3
  14. # Enable kernel synchronization of the real-time clock (RTC).
  15. rtcsync
  16. # Enable hardware timestamping on all interfaces that support it.
  17. #hwtimestamp \*
  18. # Increase the minimum number of selectable sources required to adjust
  19. # the system clock.
  20. #minsources 2
  21. # Allow NTP client access from local network.
  22. # 指定一台主机、子网,或者网络以允许或拒绝访问本服务器
  23. allow 172.16.24.0/24
  24. # Serve time even if not synchronized to a time source.
  25. # 即使server指令中时间服务器不可用,也允许将本地时间作为标准时间授时给其它客户端chronyc命令
  26. local stratum 10
  27. # Specify file containing keys for NTP authentication.
  28. #keyfile /etc/chrony.keys
  29. # Specify directory for log files.
  30. logdir /var/log/chrony
  31. # Select which information is logged.
  32. #log measurements statistics tracking
复制代码

2.客户端节点配置

   其他work节点利用主节点(master01)作为时钟源,增加内网时钟源
  server master01 iburst
  1. [root@worker01 ~]# vim /etc/chrony.conf
复制代码
  1. # Use public servers from the pool.ntp.org project.
  2. # Please consider joining the pool (http://www.pool.ntp.org/join.html).
  3. # server 0.centos.pool.ntp.org iburst
  4. # server 1.centos.pool.ntp.org iburst
  5. # server 2.centos.pool.ntp.org iburst
  6. #server 3.centos.pool.ntp.org iburst
  7. # 内网时钟服务端
  8. server master01 iburst
  9. # Record the rate at which the system clock gains/losses time.
  10. driftfile /var/lib/chrony/drift
  11. # Allow the system clock to be stepped in the first three updates
  12. # if its offset is larger than 1 second.
  13. makestep 1.0 3
  14. # Enable kernel synchronization of the real-time clock (RTC).
  15. rtcsync
  16. # Enable hardware timestamping on all interfaces that support it.
  17. #hwtimestamp \*
  18. # Increase the minimum number of selectable sources required to adjust
  19. # the system clock.
  20. #minsources 2
  21. # Allow NTP client access from local network.
  22. #allow 192.168.0.0/16
  23. # Serve time even if not synchronized to a time source.
  24. #local stratum 10
  25. # Specify file containing keys for NTP authentication.
  26. #keyfile /etc/chrony.keys
  27. # Specify directory for log files.
  28. logdir /var/log/chrony
  29. # Select which information is logged.
  30. #log measurements statistics tracking
复制代码

3.配置文件分析:

   /etc/chrony.conf
  

四、启动chrony服务

在所有节点上将其配置为在系统引导时启动。
  1. # 设置开机启动
  2. [root@x~]# systemctl enable chronyd.service
  3. # 重启服务
  4. [root@x~]# systemctl restart chronyd.service
  5. # 查看时间同步状态
  6. [root@x~]# timedatectl status
  7. # 手动强制同步下系统时钟
  8. [root@x~]# chronyc -a makestep
  9. # 查看时间同步源
  10. [root@x~]# chronyc sources -v
  11. # 查看时间同步源状态
  12. [root@x~]# chronyc sourcestats -v
  13. # 硬件时间默认为UTC
  14. [root@x~]# timedatectl set-local-rtc 1
  15. # 启用NTP时间同步
  16. [root@x~]# timedatectl set-ntp yes
复制代码
五、chronyc相关下令


  1. [root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc sources
  2. 210 Number of sources = 15
  3. MS Name/IP address         Stratum Poll Reach LastRx Last sample
  4. ===============================================================================
  5. ^+ 120.25.115.20                 2  10   255   987   -982us[ -985us] +/-   15ms
  6. ^? 10.143.33.49                  0  10     0     -     +0ns[   +0ns] +/-    0ns
  7. ^+ 100.100.3.1                   2  10   377   112   +126us[ +126us] +/- 1601us
  8. ^+ 100.100.3.2                   2  10   377   258    -75us[  -75us] +/- 1738us
  9. ^+ 100.100.3.3                   2  10   375   720   -191us[ -191us] +/- 1708us
  10. ^+ 203.107.6.88                  2  10   377   344  +2988us[+2988us] +/-   18ms
  11. ^? 10.143.33.50                  0  10     0     -     +0ns[   +0ns] +/-    0ns
  12. ^? 10.143.33.51                  0  10     0     -     +0ns[   +0ns] +/-    0ns
  13. ^? 10.143.0.44                   0  10     0     -     +0ns[   +0ns] +/-    0ns
  14. ^? 10.143.0.45                   0  10     0     -     +0ns[   +0ns] +/-    0ns
  15. ^? 10.143.0.46                   0  10     0     -     +0ns[   +0ns] +/-    0ns
  16. ^+ 100.100.5.1                   2  10   377   537   +324us[ +324us] +/- 1975us
  17. ^+ 100.100.5.2                   2  10   377   269   -209us[ -209us] +/- 1946us
  18. ^+ 100.100.5.3                   2  10   377   281   +126us[ +126us] +/- 2078us
  19. ^* 100.100.61.88                 1  10   377   913   -465us[ -468us] +/-   11ms
复制代码
这里统共输出8列信息,分别对应寄义如下:
列名寄义具体分析M表示授时时钟源^表示服务器,= 表示二级时钟源 ,#表示当地毗连的参考时钟S指示源的状态*当前同步的源,+表示其他可接受的源,?表示毗连丢失的源,x表示一个认为是falseticker 的时钟(即它的时间与大多数其他泉源不同等),~表示当时间好像具有太多可变性的泉源Name/IP address表示源的名称或IP地址,大概参考时钟的refid值无Stratum表示源的层级层级1表示当地毗连的参考时钟,第2层表示通过第1层级计算机的时钟实现同步,依此类推。Poll表示源轮询的频率以秒为单元,值是基数2的对数,例如值6表示每64秒进行一次测量,chronyd会根据当时的情况自动改变轮询频率Reach表示源的可达性的锁存值(八进制数值)该锁存值有8位,并在当接收或丢失一次时进行一次更新,值377表示末了八次传输都收到了有用的回复LastRx表示从源收到最近的一次的时间通常是几秒钟,字母m,h,d或y分别表示分钟,小时,天或年,值10年表示从未从该泉源收到时间同步信息Last sample表示当地时钟与上次测量时源的偏移量方括号中的数字表示实际测量的偏移值,这可以以ns(表示纳秒),us(表示微秒),ms(表示毫秒)或s(表示秒)为后缀;方括号左侧的数字表示原始测量值,这个值是颠末调解以答应应用于当地时钟的任何偏差;方括号右侧表示偏差值,+/-指示器背面的数字表示测量中的误差范围,+偏移表示当地时钟快速泉源
  1. [root@iZbp1fy7y89tqjvmlp1dvhZ ~]# chronyc sourcestats
  2. 210 Number of sources = 15
  3. Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
  4. ==============================================================================
  5. 120.25.115.20              37  15   12h     -0.001      0.024   -216us   516us
  6. 10.143.33.49                0   0     0     +0.000   2000.000     +0ns  4000ms
  7. 100.100.3.1                26  18  431m     -0.001      0.004    +94us    44us
  8. 100.100.3.2                22  12  362m     +0.001      0.005  +5134ns    44us
  9. 100.100.3.3                17   9  362m     -0.003      0.006   -236us    38us
  10. 203.107.6.88               64  32   18h     +0.013      0.039   +553us  1730us
  11. 10.143.33.50                0   0     0     +0.000   2000.000     +0ns  4000ms
  12. 10.143.33.51                0   0     0     +0.000   2000.000     +0ns  4000ms
  13. 10.143.0.44                 0   0     0     +0.000   2000.000     +0ns  4000ms
  14. 10.143.0.45                 0   0     0     +0.000   2000.000     +0ns  4000ms
  15. 10.143.0.46                 0   0     0     +0.000   2000.000     +0ns  4000ms
  16. 100.100.5.1                16   8  258m     +0.001      0.009   +321us    44us
  17. 100.100.5.2                17   8  276m     +0.001      0.007   -219us    35us
  18. 100.100.5.3                21  11  344m     +0.001      0.006    +80us    48us
  19. 100.100.61.88              17  11  276m     -0.001      0.009   -389us    39us
复制代码
列名寄义Name/IP address表示源的名称或IP地址,大概参考时钟的refid值NP这是当前为服务器保留的采样点数,通过这些点执行线性回归方法来估算出偏移值



9us 35us
100.100.5.3 21 11 344m +0.001 0.006 +80us 48us
100.100.61.88 17 11 276m -0.001 0.009 -389us 39us
  1. | 列名 | 含义 |
  2. | --- | --- |
  3. | Name/IP address | 表示源的名称或IP地址,或者参考时钟的refid值 |
  4. | NP | 这是当前为服务器保留的采样点数,通过这些点执行线性回归方法来估算出偏移值 |
  5. [外链图片转存中...(img-WJNzuxmw-1727248514732)]
  6. [外链图片转存中...(img-sozEjHiv-1727248514732)]
  7. [外链图片转存中...(img-qBbD3lld-1727248514733)]
  8. **既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) Powered by Discuz! X3.4