RH254小结(九)rhel7新的ntp对时服务Chrony
Chrony 应用本身已经有几年了,其是是网络时间协议的 (NTP) 的另一种实现。一直以来众多发行版里标配的都是ntpd对时服务,自rhel7/centos7 起,Chrony做为了发行版里的标配服务,不过老的ntpd服务依旧在rhel7/centos7里可以找到 。Chrony可以同时做为ntp服务的客户端和服务端。默认安装完后有两个程序chronyd和chronyc 。chronyd是一个在系统后台运行的守护进程,chronyc是用来监控chronyd性能和配置其参数程序。
一、安装启用
可以通过如下步骤安装启用chrony服务:
- # yum install -y chrony -->安装服务
- # systemctl start chronyd.service -->启动服务
- # systemctl enable chronyd.service -->设置开机自启动,默认是enable的
二、chrony.conf的主要配置
chrony服务使用的配置文件为/etc/chrony.conf,配置内容格式和ntpd服务基本相似。默认内容如下:
- [[email protected] ~]# cat /etc/chrony.conf |grep -v ^#|grep -v ^$
- 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
- stratumweight 0
- driftfile /var/lib/chrony/drift
- rtcsync
- makestep 10 3
- bindcmdaddress 127.0.0.1
- bindcmdaddress ::1
- keyfile /etc/chrony.keys
- commandkey 1
- generatecommandkey
- noclientlog
- logchange 0.5
- logdir /var/log/chrony
如果本局域网内有对时服务开启的话,通过将上面的几条serer记录删除,增加指定局域网内的对时服务器并restart chrony服务即可。其中主要的配置参数有如下几个:
- server - 该参数可以多次用于添加时钟服务器,必须以"server "格式使用。一般而言,你想添加多少服务器,就可以添加多少服务器;
- stratumweight - stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级;
- driftfile - chronyd程序的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能的话,会从时钟服务器获得较好的估值;
- rtcsync - rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC);
- allow / deny - 这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器;
- cmdallow / cmddeny - 跟上面相类似,只是你可以指定哪个IP地址或哪台主机可以通过chronyd使用控制命令;
- bindcmdaddress - 该指令允许你限制chronyd监听哪个网络接口的命令包(由chronyc执行)。该指令通过cmddeny机制提供了一个除上述限制以外可用的额外的访问控制等级。
- makestep - 通常,chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时步进调整系统时钟,但只有在因为chronyd启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效。
三、查看同步状态
检查ntp源服务器状态:
- [[email protected] ~]# chronyc sourcestats
- 210 Number of sources = 3
- Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
- ==============================================================================
- dns.sjtu.edu.cn 4 3 302 6.440 90.221 +13ms 694us
- dns1.synet.edu.cn 0 0 0 0.000 2000.000 +0ns 4000ms
- 202.118.1.130 7 5 323 -0.174 7.323 -8406ns 303us
检查ntp详细同步状态:
- [[email protected] ~]# chronyc sources -v
- 210 Number of sources = 3
- .-- Source mode '^' = server, '=' = peer, '#' = local clock.
- / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
- | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
- || .- xxxx [ yyyy ] +/- zzzz
- || / xxxx = adjusted offset,
- || Log2(Polling interval) -. | yyyy = measured offset,
- || \ | zzzz = estimated error.
- || | |
- MS Name/IP address Stratum Poll Reach LastRx Last sample
- ===============================================================================
- ^- dns.sjtu.edu.cn 3 7 13 20 +11ms[ +11ms] +/- 98ms
- ^? dns1.synet.edu.cn 0 8 0 10y +0ns[ +0ns] +/- 0ns
- ^* 202.118.1.130 2 6 377 125 -122us[ -305us] +/- 31ms
四、使用chronyc
可以通过运行chronyc命令来修改设置,命令如下:
accheck - 检查NTP访问是否对特定主机可用
activity - 该命令会显示有多少NTP源在线/离线
add server - 手动添加一台新的NTP服务器。
clients - 在客户端报告已访问到服务器
delete - 手动移除NTP服务器或对等服务器
settime - 手动设置守护进程时间
tracking - 显示系统时间信息
输入help命令可以查看更多chronyc的交互命令。
- [[email protected] ~]# chronyc
- chrony version 1.29.1
- Copyright (C) 1997-2003, 2007, 2009-2013 Richard P. Curnow and others
- chrony comes with ABSOLUTELY NO WARRANTY. This is free software, and
- you are welcome to redistribute it under certain conditions. See the
- GNU General Public License version 2 for details.
- chronyc> activity
- 200 OK
- 3 sources online
- 0 sources offline
- 0 sources doing burst (return to online)
- 0 sources doing burst (return to offline)
- 0 sources with unknown address
- chronyc> help
- Commands:
- accheck <address> : Check whether NTP access is allowed to <address>
- activity : Check how many NTP sources are online/offline
- add peer <address> ... : Add a new NTP peer
- add server <address> ... : Add a new NTP server
- allow [<subnet-addr>] : Allow NTP access to that subnet as a default
- allow all [<subnet-addr>] : Allow NTP access to that subnet and all children
- burst <n-good>/<n-max> [<mask>/<masked-address>] : Start a rapid set of measurements
- clients : Report on clients that have accessed the server
- cmdaccheck <address> : Check whether command access is allowed to <address>
- cmdallow [<subnet-addr>] : Allow command access to that subnet as a default
- cmdallow all [<subnet-addr>] : Allow command access to that subnet and all children
- cmddeny [<subnet-addr>] : Deny command access to that subnet as a default
- ……………………省略
五、其他时间设置相关指令
相关指令如下:
- 查看日期时间、时区及NTP状态:# timedatectl
- 查看时区列表:# timedatectl list-timezones
- 修改时区# timedatectl set-timezone Asia/Shanghai
- 修改日期时间:# timedatectl set-time "2015-01-21 11:50:00"(可以只修改其中一个)
- 开启NTP:# timedatectl set-ntp true/flase
还有另外一个有趣的指令system-config-date ,在rhel7 里也给了我们一个可以图形化配置chrony服务的工具 。安装命令如下:
- # yum -y install system-config-date
安装完成后运行system-config-date命令,界面如下:
六、chrony的优势
Chrony 的优势包括:
- 更快的同步只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,这对于并非全天 24 小时运行的台式计算机或系统而言非常有用。
- 能够更好地响应时钟频率的快速变化,这对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用。
- 在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响。
- 在应对临时非对称延迟时(例如,在大规模下载造成链接饱和时)提供了更好的稳定性。
- 无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟。
参考文档: