linux在运行时间长了之后,尤其在虚拟环境中。系统时间可能会存在一定的误差,时间同步在大型集群环境中是很重要的,而通过ntp协议来同步时间,则是一个很好的解决方案。linux默认使用ntpd来同步时间的,但ntpd同步时间并不理想,有可能需要数小时来同步时间,所以在Centos7中可换成chrony来实现时间同步。chrony兼容ntpd监听在udp123端口上,另外还监听在udp的323端口上。
Chrony
Chrony是一个开源的自由软件,如果在chrony配置文件中指定了ntp服务器的地址,那么chrony就是一台客户端,会去同步ntp服务器的时间,如果在chrony配置了允许某些客户端来向自己同步时间,则chrony也充当了一台服务器,所以,安装了chrony即可充当客户端也可以充当服务端。
Chrony有两个核心组件,分别是:chronyd:是守护进程,主要用于调整内核中运行的系统时间和时间服务器同步。它确定计算机增减时间的比率,并对此进行调整补偿。chronyc:提供一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。
安装配置
环境
OS: CentOS7.4_x64
主机名
角色
配置目录
端口
centos-test1
Chrony Server
/etc/chrony.conf
udp/123,udp/323
centos-test2
Chrony Client
/etc/chrony.conf
udp/123,udp/323
安装
配置详解
配置文件:/etc/chrony.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 # 使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 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 # 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。 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 # 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
一般主要配置以下参数:
1 2 3 4 5 6 7 server:指明时间服务器地址; allow NETADD/NETMASK allow all:允许所有客户端主机; deny NETADDR/NETMASK deny all:拒绝所有客户端; bindcmdaddress:命令管理接口监听的地址; local stratum 10:即使自己未能通过网络时间服务器同步到时间,也允许将本地时间作为标准时间授时给其它客户端;
客户端与服务端的区别就在于把自己的server指定为服务主机,而服务端的server就指向上游(外网)NTP服务器。
防火墙规则
1 2 firewall-cmd --add-service=ntp --permanent firewall-cmd --reload
设置时区
1 timedatectl set-timezone Asia/Shanghai
强制同步系统时钟
启动
1 2 systemctl enable chronyd systemctl restart chronyd
常用命令
硬件时间默认为UTC:
1 timedatectl set-local-rtc 1
chrony自带一个交互式工具chronyc,在配置文件中指定了时间服务器之后,如果想查看同步状态,可以进入这个交互式工具的交互界面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 chronyc有很多的子命令,可以输入help来查看 chronyc> help 选项: sources [-v] 显示关于当前来源的信息 sourcestats [-v] 显示时间同步状态(如时间偏移了多少之类) # 例如:chronyc> sources -v210 Number of sources = 1 .-- 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 || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || \ | | zzzz = estimated error. || | | \ MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 10 104 64m +1485us[ +417us] +/- 81ms # 主要关注第一列的MS,^* ^是指该行所给出的IP是服务器,也就是我们指定的互联网时间服务器;*是指当前已同步 # #######chronyc> chronyc> sourcestats -v #sourcestats是显示同步状态,-v是详细西信息210 Number of sources = 1 .- Number of sample points in measurement set. / .- Number of residual runs with same sign. | / .- Length of measurement set (time). | | / .- Est. clock freq error (ppm). | | | / .- Est. error in freq. | | | | / .- Est. offset. | | | | | | On the -. | | | | | | samples. \ | | | | | | | Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ============================================================================== 203.107.6.88 29 18 18h -0.003 0.132 -14us 5055us