Centos 集群时间同步chrony

青旅半醒 2023-02-21 06:29 70阅读 0赞

实验环境

  1. 系统 角色 IP
  2. Centos7.6 Server 192.168.14.217
  3. Centos7.6 Client 192.168.14.218

一、Server端配置

1、Centos7系统已经默认安装chrony服务,没有再执行以下命令

  1. [root@server ~]# yum install -y chrony

2、启动服务

  1. [root@server ~]# systemctl start chronyd
  2. [root@server ~]# systemctl enable chronyd

3、修改配置文件

  1. [root@server ~]# vi /etc/chrony.conf

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70

4、查看时间同步源(^*表示已经同步)

  1. [root@server ~]# chronyc sources -v
  2. 210 Number of sources = 4
  3. .-- Source mode '^' = server, '=' = peer, '#' = local clock.
  4. / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
  5. | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
  6. || .- xxxx [ yyyy ] +/- zzzz
  7. || Reachability register (octal) -. | xxxx = adjusted offset,
  8. || Log2(Polling interval) --. | | yyyy = measured offset,
  9. || \ | | zzzz = estimated error.
  10. || | | \
  11. MS Name/IP address Stratum Poll Reach LastRx Last sample
  12. ===============================================================================
  13. ^- sv1.ggsrv.de 2 6 327 88 +26ms[ +26ms] +/- 131ms
  14. ^- ntp8.flashdance.cx 2 6 377 18 +3759us[+3759us] +/- 152ms
  15. ^- 119.28.183.184 2 6 317 26 -653us[ -653us] +/- 49ms
  16. ^* 120.25.115.20 2 6 377 89 +50us[ +119us] +/- 4323us

二、Clinet端配置

1、修改配置文件

  1. [root@client ~]# cat /etc/chrony.conf |grep -v "^$"|grep -v "^#"
  2. server server iburst
  3. driftfile /var/lib/chrony/drift
  4. makestep 1.0 3
  5. rtcsync
  6. logdir /var/log/chrony

注释默认的server地址,添加本地时间同步地址

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70 1

2、修改主机名解析

  1. #底部添加一条记录
  2. [root@client ~]# vi /etc/hosts
  3. 192.168.14.217 server

3、重启服务

  1. [root@client ~]# systemctl restart chronyd

4、查看时间是否同步

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70 2

5、就算chronyc sources -v显示^*,但是如果NTP显示no也是不会同步时间的。(解决方法是重启服务)

20200629134027929.png

6、模拟时间不同步,随意修改一个时间

  1. [root@client ~]# date -s "2020-06-29 08:00:00"

7、查看时间同步状态(^?没同步,NTP状态为no)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70 3

8、NTP状态为不会自动变为yes,需要重启服务

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70 4

9、因此可以编辑脚本,添加到自动任务

  1. #当检测NTP synchronized状态为no的时候,自动执行重启服务
  2. [root@client ~]# vi time.sh
  3. #!/bin/bash
  4. status=`timedatectl | grep "NTP synchronized" | awk -F' ' '{print $3}'`
  5. if [ "$status" = "no" ]; then
  6. systemctl restart chronyd
  7. fi
  8. #添加到自动任务,一分钟执行一次
  9. [root@client ~]# crontab -e
  10. */1 * * * * /root/time.sh
  11. #查看日志
  12. [root@client ~]# tail -f /var/log/cron

10、再次查看时间同步状态,至此完成

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70 5

发表评论

表情:
评论列表 (有 0 条评论,70人围观)

还没有评论,来说两句吧...

相关阅读

    相关 linux 时间同步 chrony

    Chrony介绍 chrony 是基于NPT协议的实现时间同步服务,它既可以当做服务端,也可以充当客户端。chrony是ntp的代替品,能更精确的时间和更快的速度同步时钟

    相关 chrony时间同步

    简介 chrony是网络时间协议(NTP)的多用途实现。它可以使系统时钟与NTP服务器、参考时钟(例如GPS接收器)以及使用手表和键盘的手动输入同步。它还可以作为NTPV