chrony详解

关于chrony:

    chrony is a versatile implementation of the Network Time Protocol (NTP). It can synchronize the system clock with NTP servers, reference clocks (e.g. GPS receiver), and manual input using wristwatch and keyboard. It can also operate as an NTPv4 (RFC 5905) server and peer to provide a time service to other computers in the network.

    It is designed to perform well in a wide range of conditions, including intermittent network connections, heavily congested networks, changing temperatures (ordinary computer clocks are sensitive to temperature), and systems that do not run continuosly, or run on a virtual machine.

    Typical accuracy between two machines synchronised over the Internet is within a few milliseconds; on a LAN, accuracy is typically in tens of microseconds. With hardware timestamping, or a hardware reference clock, sub-microsecond accuracy may be possible.

    Two programs are included in chrony, chronyd is a daemon that can be started at boot time and chronyc is a command-line interface program which can be used to monitor chronyd’s performance and to change various operating parameters whilst it is running.

    程序环境:
        配置文件:/etc/chrony.conf
        主程序文件:chronyd
        工具程序:chronyc
        unit file: chronyd.service
        
    配置文件:chrony.conf
        server:指明时间服务器地址;
        allow NETADD/NETMASK 自己作为ntp服务器,让客户端来同步自己的时间
        allow all:允许所有客户端主机;
        deny NETADDR/NETMASK
        deny all:拒绝所有客户端;
        bindcmdaddress:命令管理接口监听的地址;
        local stratum 10:即使自己未能通过网络时间服务器同步到时间,也允许将本地时间作为标准时间授时给其它客户端;

OS环境:

NTP Server:192.168.170.8

NTP Client:192.168.170.9 

CentOS Linux release 7.2.1504 (Core)

1.安装Chrony

系统默认已经安装,如未安装,请执行以下命令安装:

[root@node3 ~]# yum install chrony -y
2.启动并加入开机自启动

[root@node3 ~]# systemctl enable chronyd.service
[root@node3 ~]# systemctl start chronyd.service
[root@node3 ~]# systemctl status chronyd.service
3.Firewalld设置

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

4.配置Chrony

修改默认配置文件,并做说明:

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

# 使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.170.8 iburst

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
driftfile /var/lib/chrony/drift

# chronyd根据需求减慢或加速时间调整,
# 在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
# 该指令强制chronyd调整时期,大于某个阀值时步进调整系统时钟。
# 只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3

# 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
rtcsync

# Enable hardware timestamping on all interfaces that support it.
# 通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#allow 192.168.0.0/16
#deny 192.168/16
allow 192.168.0.0/16  允许该网段客户端来同步时间

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

# 指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys

# 指定日志文件的目录。
logdir /var/log/chrony

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

[root@node3 ~]# systemctl restart chronyd


NTP Client 做测试
[root@node2 ~]# yum -y install ntp  安装ntp工具
[root@node3 ~]# ntpdate 192.168.170.8


5.设置时区

查看当前系统时区:

[root@node3 ~]# timedatectl
      Local time: Fri 2018-10-29 13:31:04 CST
  Universal time: Fri 2018-10-29 05:31:04 UTC
        RTC time: Fri 2018-10-29 08:17:20
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

如果你当前的时区不正确,请按照以下操作设置。

查看所有可用的时区:

[root@node3 ~]# timedatectl list-timezones

筛选式查看在亚洲S开的上海可用时区:

[root@node3 ~]# timedatectl list-timezones |  grep  -E "Asia/S.*"

Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

设置当前系统为Asia/Shanghai上海时区:

[root@node3 ~]# timedatectl set-timezone Asia/Shanghai

设置完时区后,强制同步下系统时钟:

[root@node3 ~]# chronyc -a makestep
200 OK


7.常用命令

查看时间同步源:

[root@node3 ~]# chronyc sources -v

查看时间同步源状态:

[root@node3 ~]# chronyc sourcestats -v

设置硬件时间

硬件时间默认为UTC:

[root@node3 ~]# timedatectl set-local-rtc 1

启用NTP时间同步:

[root@node3 ~]# timedatectl set-ntp yes

校准时间服务器:

[root@node3 ~]# chronyc tracking


 

  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值