zoukankan      html  css  js  c++  java
  • centos7搭建ntp时间同步服务器chrony服务

    centos7搭建ntp时间同步服务器chrony服务

    前言:

    在centos6的时候我们基本使用的是ntp服务用来做时间同步,但是在centos7后推荐是chrony作为时间同步器的服务端使用,避免的ntp在centos7支持的不是很友好 。
    

    什么是chrony?

    第一部分的第3条说明对比关系,详见centos6和centos7的对比,连接地址:https://www.cnblogs.com/liych/p/11741632.html
    
    在centos7上可以用它做时间服务器使用,是默认支持的,也是更新后centos7默认的时间服务器,另外还有就是,它作为网络时间协议的客户机/服务器,此程序保持计算机时钟的准确性。它是专门为支持断断续续的互联网连接而设计的,但它在永久连接的环境中也能很好地工作。它还可以使用硬件参考时钟、系统实时时钟或手动输入作为时间参考。至此,推荐使用。
    
    chrony有2个命令,详见如下: chronyd:是守护进程,主要用于调整内核中运行的系统时间和时间服务器同步。它确定计算机增减时间的比率,并对此进行调整补偿。 chronyc:提供一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

    开始部署chrony环境

    1.环境说明

    实现目的:需要两台或集群内的多台设备时间同步一致。
    这里准备两台服务器:192.168.152.129 (bogon)做服务器端; 192.168.152.132 (liych-01)做客户端。也可以多台集群使用。
    [root@bogon ~]# cat /etc/redhat-release 
    CentOS Linux release 7.6.1810 (Core)
    
    
    
    
    

    2.关闭防火墙

    [root@bogon ~]# systemctl stop firewalld 
    [root@bogon ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
       Active: inactive (dead) since 四 2019-09-19 03:12:44 CST; 17s ago
         Docs: man:firewalld(1)

    3.关闭selinux

    
    
    [root@bogon ~]#  sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
    [root@bogon ~]# sestatus
    SELinux status:                 disabled
    
    

    4.安装chrony

    这里注意的是需要把需要同步时间服务器均安装chrony服务。
    版本号:chrony.x86_64 0:3.4-1.el7
    
    
    
    [root@bogon ~]# yum install chrony -y

    4.1启动服务

    [root@bogon ~]# systemctl start chronyd
    [root@bogon ~]# systemctl status chronyd
    ● chronyd.service - NTP client/server
       Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
       Active: active (running) since 四 2019-09-19 03:19:49 CST; 1 months 7 days ago
    
    

    4.2更新配置文件

    这里修改的是服务器端的配置文件,客户端不需要修改。
    时间服务器获取地址:https://www.ntppool.org/zone/asia
    
    
    
    [root@bogon ~]# cp /etc/chrony.conf{,.backup}
    [root@bogon ~]# cat > /etc/chrony.conf <<EOF 
    server cn.pool.ntp.org 
    server 0.asia.pool.ntp.org
    server 1.asia.pool.ntp.org
    server 2.asia.pool.ntp.org
    server 3.asia.pool.ntp.org
    server ntp1.aliyun.com
    server time1.aliyun.com
    stratumweight 0
    driftfile /var/lib/chrony/drift
    makestep 1.0 3
    rtcsync
    allow 192.168.152.0/24
    bindcmdaddress 127.0.0.1
    bindcmdaddress ::1
    local stratum 10
    keyfile /etc/chrony.keys
    commandkey 1
    generatecommandkey
    noclientlog
    logchange 0.5
    logdir /var/log/chrony
    EOF
    
    

    4.3配置文件简单介绍

    可以通过"man chrony.conf"了解主配置文件的配置格式,需要注意的是增加allow后才会生效。
    server “必须以server”格式使用,添加的是时钟服务器,可以是多个。
    stratumweight 0 设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认设置为0,让chronyd在选择源时忽略源的层级。
    driftfile /var/lib/chrony/drift  根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
    makestep 1.0 3 chronyd 将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差

    allow 192.168.152.0/24

    deny 192.168.152.0/24

    指定一台主机、子网,或者网络以允许或拒绝NTP连接到时钟服务器的机器
    local stratum 10  这个功能开启后,本机不去同步别人的时间到本机
    logdir /var/log/chrony
    记录日志
    
    

    4.4重启并检查服务端口123,323

    
    
    [root@bogon chrony]# systemctl restart chronyd
    [root@bogon chrony]# systemctl enable chronyd
    [root@bogon chrony]# netstat -lntup |grep chronyd
    udp        0      0 0.0.0.0:123             0.0.0.0:*                           4078/chronyd        
    udp        0      0 127.0.0.1:323           0.0.0.0:*                           4078/chronyd        
    udp6       0      0 ::1:323                 :::*                                4078/chronyd 
    
    

    5.客户端(单台)启用时间同步服务

    单台配置很简单,请继续往下看吧。
    

    5.1客户端启用时间同步服务

    这里需要注意的是 时区为:Time zone: Asia/Shanghai (CST, +0800)然后执行下面法一同步时间。
    
    
    
    法一:推荐使用
    [root@liych-01 ~]# timedatectl set-timezone Asia/Shanghai;chronyc -a makestep
    [root@liych-01 ~]# yum install ntpdate -y;ntpdate 192.168.152.129
    27 Oct 13:41:22 ntpdate[12018]: step time server 192.168.152.129 offset 62.490796 sec
    法二:
    [root@liych-01 ~]# echo "server 192.168.152.129 iburst " >>/etc/chrony.conf 
    [root@liych-01 ~]# systemctl restart chronyd
    [root@liych-01 ~]# systemctl status chronyd 

    6.客户端(集群)启用时间同步服务器

    本机设备很旧了,使用多台的时候总是迟钝,故此此处采用132一台做测试,多台就在host添加主机即可。
    
    
    
    [root@bogon ~]# cat /etc/ansible/hosts
    [ntpcli]
    192.168.152.132
    #192.168.152.133
    #192.168.152.134

    命令集合:

    
    
    [root@bogon ~]# ansible all -m ping
    [root@bogon ~]# echo "server 192.168.152.129 iburst " >/root/chrony.conf
    [root@bogon ~]# ansible ntpcli -m copy -a "src=/root/chrony.conf dest=/etc/"
    [root@bogon ~]# ansible ntpcli -m shell -a "systemctl start chronyd.service"      
    [root@bogon ~]# ansible ntpcli -m shell -a "systemctl status chronyd.service"
    [root@bogon ~]# ansible ntpcli -m shell -a "ntpdate 192.168.152.129" 
    [root@bogon ~]# ansible ntpcli -m shell -a "date" 
    [root@bogon ~]# ansible ntpcli -m cron -a "name='time sync' minute=*/5 job='/usr/sbin/ntpdate 192.168.152.129 &>/dev/null'" 
    [root@bogon ~]# ansible ntpcli -m shell -a "crontab -l" 
    
    

    操作示例:

    [root@bogon ~]# ansible all -m ping
    192.168.152.132 | SUCCESS => {
        "changed": false, 
        "ping": "pong"
    }
    [root@bogon ~]# echo "server 192.168.152.129 iburst " >/root/chrony.conf  
    [root@bogon ~]# ansible ntpcli -m copy -a "src=/root/chrony.conf dest=/etc/"      
    192.168.152.132 | SUCCESS => {
        "changed": true, 
        "checksum": "3ccbe3a0fc44dd0ce06623653f5a9c940f453bdb", 
        "dest": "/etc/chrony.conf", 
        "gid": 0, 
        "group": "root", 
        "md5sum": "bb305fff45f35fda88ed9ad01231a25c", 
        "mode": "0644", 
        "owner": "root", 
        "size": 31, 
        "src": "/root/.ansible/tmp/ansible-tmp-1572158118.63-84760854509278/source", 
        "state": "file", 
        "uid": 0
    }
    [root@bogon ~]# ansible ntpcli -m shell -a "systemctl start chronyd.service"      
    192.168.152.132 | SUCCESS | rc=0 >>
    
    
    [root@bogon ~]# ansible ntpcli -m shell -a "systemctl status chronyd.service"
    192.168.152.132 | SUCCESS | rc=0 >>
    ● chronyd.service - NTP client/server
       Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
       Active: active (running) since 日 2019-10-27 13:43:03 CST; 53min ago
         Docs: man:chronyd(8)
               man:chrony.conf(5)
      Process: 12034 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
      Process: 12031 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
     Main PID: 12033 (chronyd)
       CGroup: /system.slice/chronyd.service
               └─12033 /usr/sbin/chronyd
    
    10月 27 13:43:03 liych-01 systemd[1]: Stopped NTP client/server.
    10月 27 13:43:03 liych-01 systemd[1]: Starting NTP client/server...
    10月 27 13:43:03 liych-01 chronyd[12033]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
    10月 27 13:43:03 liych-01 chronyd[12033]: Initial frequency -5.115 ppm
    10月 27 13:43:03 liych-01 systemd[1]: Started NTP client/server.
    10月 27 13:43:08 liych-01 chronyd[12033]: Selected source 192.168.152.129
    10月 27 13:43:08 liych-01 chronyd[12033]: System clock wrong by -2.747341 seconds, adjustment started
    10月 27 13:44:14 liych-01 chronyd[12033]: System clock wrong by 2.501240 seconds, adjustment started
    10月 27 13:45:19 liych-01 chronyd[12033]: System clock wrong by -2.304266 seconds, adjustment started
    10月 27 13:45:33 liych-01 chronyd[12033]: System clock was stepped by -0.921109 seconds
    
    [root@bogon ~]# ansible ntpcli -m shell -a "crontab -l"        
    192.168.152.132 | SUCCESS | rc=0 >>
    [root@bogon ~]# ansible ntpcli -m shell -a "ntpdate 192.168.152.129"      
    192.168.152.132 | SUCCESS | rc=0 >>
    27 Oct 14:37:35 ntpdate[12702]: adjust time server 192.168.152.129 offset -0.000112 sec
    
    [root@bogon ~]#  ansible ntpcli -m shell -a "date"      
    192.168.152.132 | SUCCESS | rc=0 >>
    2019年 10月 27日 星期日 14:38:53 CST
    
    [root@bogon ~]# ansible ntpcli -m cron -a "name='time sync' minute=*/5 job='/usr/sbin/ntpdate 192.168.152.129 &>/dev/null'"      
    192.168.152.132 | SUCCESS => {
        "changed": true, 
        "envs": [], 
        "jobs": [
            "time sync"
        ]
    }
    [root@bogon ~]# ansible ntpcli -m shell -a "crontab -l"                      
    192.168.152.132 | SUCCESS | rc=0 >>
    #Ansible: time sync
    */5 * * * * /usr/sbin/ntpdate 192.168.152.129 &>/dev/null

    7.防火墙设置

    因NTP使用123/UDP端口协议,所以允许NTP服务即可。
    
    
    
    [root@bogon ~]# firewall-cmd --add-service=ntp --permanent
    [root@bogon ~]# firewall-cmd --reload

    8.常用的服务器命令

    8.1查看当前系统时区并设置时区常用命令

    
    
    [root@liych-01 ~]# timedatectl      #查看当前系统时区
    [root@liych-01 ~]# timedatectl list-timezones |  grep  -E "Asia/S.*" 
    [root@liych-01 ~]# timedatectl set-timezone Asia/Shanghai  #设置当前系统为Asia/Shanghai上海时区
    [root@liych-01 ~]# chronyc -a makestep #强制同步下系统时钟

    8.2查看系统同步时间

    
    
    [root@liych-01 ~]# chronyc sources -v                   #查看时间同步源
    [root@liych-01 ~]# chronyc sourcestats -v               #查看时间同步源状态
    [root@liych-01 ~]# timedatectl set-local-rtc 1          #设置硬件时间,硬件时间默认为UTC
    [root@liych-01 ~]# timedatectl set-ntp yes              #启用NTP时间同步
    [root@liych-01 ~]# chronyc tracking                     #校准同步时间服务器
    
    
    chronyc参数 涵义
    tracking(最为常用,其他的并不重要) 显示系统时间信息
    activity   显示在线状态
    add sever 添加时间服务器
    delete 移除时间服务器
    clients 查看报告
    settime 设置守护进程
    accheck 检查特定主机是否可以用

    参考链接地址:

     https://wiki.archlinux.org/index.php/Chrony
    
     https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-using_chrony
  • 相关阅读:
    最艰难的采访IT公司ThoughtWorks代码挑战——FizzBuzzWhizz游戏
    SQL Server 存储字符数较大字段的问题
    c# var的含义与用法
    ListBox和ComboBox绑定数据简单例子
    “C# 未在本地计算机上注册microsoft.Jet.OLEDB.12.0”的解决方案
    [转] c# 操作Word
    C++ Regsvr32订购具体解释
    数字计算的有序排列的号码出现二分法
    合作信息处理模型
    内存四个领域,变量声明和定义,注册,c内联汇编,auto,堆,不变,静态变量
  • 原文地址:https://www.cnblogs.com/liych/p/11747551.html
Copyright © 2011-2022 走看看