Centos使用chrony做时间同步
<p>Chrony是一个开源的自由软件,在RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对NTP时间同步软件,速度更快、配置和依赖都更简单<br>Chrony有两个核心组件,分别是:chronyd:是守护进程,主要用于调整内核中运行的系统时间和时间服务器同步。它确定计算机增减时间的比率,并对此进行调整补偿。chronyc:提供一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。</p><p>配置的时候,需要首先配置chrony的服务器端,然后配置客户端与服务器端进行同步。如果基于外网的时钟服务器,那可以不用配置服务器器端</p>
<h3>1、chrony工具安装</h3>
<p>1、系统版本检查,使用cat /etc/system-release<br>2、使用rpm -qa |grep chrony查看系统是否已安装chrony,可看到默认已安装chrony的包。<br>3、如果没有安装环境可使用yum install chrony命令安装或者离线下载rpm包安装,下载地址:http://rpm.pbone.net/index.php3?stat=3&limit=2&srodzaj=3&dl=40&search=chrony,找到对应版本下载即可。<br>4、下载完后使用rpm -ivh chrony-2.1.1-4.el7.centos.x86_64.rpm安装即可</p>
<h3>2、设置chrony的服务状态,并关闭防火墙</h3>
<p>1、服务状态:</p>
<p>使用systemctl start chronyd.service 启动chrony服务<br>使用systemctl enable chronyd.service 设置开机同步时间<br>使用systemctl status chronyd.service 查看服务状态</p>
<p>2、直接关闭防火墙<br>systemctl stop firewalld.service #停止firewall<br>systemctl disable firewalld.service #禁止firewall开机启动<br>2、或者不关闭防火墙、但允许NTP服务<br>firewall-cmd --add-service=ntp --permanent<br>firewall-cmd --reload<br>因NTP使用123/UDP端口协议,所以允许NTP服务即可</p>
<h3>3、服务端和客户端chrony配置</h3>
<h4>1、服务端配置</h4>
<p>1)、配置文件修改<br> vi /etc/chrony.conf<br> a、修改第22行,Allow NTP client access from local network,配置允许访问的客户端列表,支持CIDR,例如:</p>
<p> allow 192.168/16</p>
<p> b、修改第29行设置同步,Serve time even if not synchronized to any NTP server.,打开注释即可,即:</p>
<p> local stratum 10<br>2)、重启下服务端chrony服务,使用systemctl restart chronyd.service重启即可。</p>
<h4>2、客户端配置</h4>
<p>1)、配置文件修改<br> vim /etc/chrony.conf<br> a、修改server即可,删掉其他的,添加要同步时间的源服务器ip,格式如下:</p>
<p> server x.x.x.x iburst<br>2)、重启下客户端chrony服务,使用systemctl restart chronyd.service重启即可。</p>
<p> 客户端使用chronyc sources -v命令完成同步即可</p>
<p>3)、查看同步状态</p>
<div class="cnblogs_code">
<pre># systemctl status chronyd -<span style="color: rgba(0, 0, 0, 1)">l
● chronyd.service </span>- NTP client/<span style="color: rgba(0, 0, 0, 1)">server
Loaded: loaded (</span>/usr/lib/systemd/system/<span style="color: rgba(0, 0, 0, 1)">chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed </span><span style="color: rgba(128, 0, 128, 1)">2019</span>-<span style="color: rgba(128, 0, 128, 1)">09</span>-<span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">55</span>:<span style="color: rgba(128, 0, 128, 1)">58</span><span style="color: rgba(0, 0, 0, 1)"> CST; 36s ago
Docs: </span><span style="color: rgba(0, 0, 255, 1)">man</span>:chronyd(<span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">man</span>:chrony.conf(<span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)">)
Process: </span><span style="color: rgba(128, 0, 128, 1)">16160</span> ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=<span style="color: rgba(128, 0, 128, 1)">0</span>/<span style="color: rgba(0, 0, 0, 1)">SUCCESS)
Process: </span><span style="color: rgba(128, 0, 128, 1)">16156</span> ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=<span style="color: rgba(128, 0, 128, 1)">0</span>/<span style="color: rgba(0, 0, 0, 1)">SUCCESS)
Main PID: </span><span style="color: rgba(128, 0, 128, 1)">16158</span><span style="color: rgba(0, 0, 0, 1)"> (chronyd)
Memory: </span><span style="color: rgba(128, 0, 128, 1)">372</span><span style="color: rgba(0, 0, 0, 1)">.0K
CGroup: </span>/system.slice/<span style="color: rgba(0, 0, 0, 1)">chronyd.service
└─</span><span style="color: rgba(128, 0, 128, 1)">16158</span> /usr/sbin/<span style="color: rgba(0, 0, 0, 1)">chronyd
Sep </span><span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">55</span>:<span style="color: rgba(128, 0, 128, 1)">58</span> k8s-master systemd[<span style="color: rgba(128, 0, 128, 1)">1</span>]: Starting NTP client/<span style="color: rgba(0, 0, 0, 1)">server...
Sep </span><span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">55</span>:<span style="color: rgba(128, 0, 128, 1)">58</span> k8s-master chronyd[<span style="color: rgba(128, 0, 128, 1)">16158</span>]: chronyd version <span style="color: rgba(128, 0, 128, 1)">3.2</span> starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SECHASH +SIGND +ASYNCDNS +IPV6 +<span style="color: rgba(0, 0, 0, 1)">DEBUG)
Sep </span><span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">55</span>:<span style="color: rgba(128, 0, 128, 1)">58</span> k8s-master chronyd[<span style="color: rgba(128, 0, 128, 1)">16158</span>]: Frequency -<span style="color: rgba(128, 0, 128, 1)">39.629</span> +/- <span style="color: rgba(128, 0, 128, 1)">0.032</span> ppm read from /var/lib/chrony/<span style="color: rgba(0, 0, 0, 1)">drift
Sep </span><span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">55</span>:<span style="color: rgba(128, 0, 128, 1)">58</span> k8s-master systemd[<span style="color: rgba(128, 0, 128, 1)">1</span>]: Started NTP client/<span style="color: rgba(0, 0, 0, 1)">server.
Sep </span><span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">56</span>:<span style="color: rgba(128, 0, 128, 1)">31</span> k8s-master chronyd[<span style="color: rgba(128, 0, 128, 1)">16158</span>]: Selected source <span style="color: rgba(128, 0, 128, 1)">78.46</span>.<span style="color: rgba(128, 0, 128, 1)">102.180</span></pre>
</div>
<p> </p>
<h3>4、常用命令</h3>
<p> 查看时间同步源:<br> $ chronyc sources -v</p>
<p> 立即手工同步</p>
<p> $chronyc -a makestep<br> 查看时间同步源状态:<br> $ chronyc sourcestats -v<br> 设置硬件时间<br> 硬件时间默认为UTC:<br> $ timedatectl set-local-rtc 1<br> 启用NTP时间同步:<br> $ timedatectl set-ntp yes<br> 校准时间服务器:<br> $ chronyc tracking<br> 最后需要注意的是,配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效。</p>
<h3>5、各类参数说明</h3>
<p> </p>
<h4> 配置参数说明</h4>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="168">
<p align="center"><strong>参数</strong></p>
</td>
<td width="529">
<p align="center"><strong>参数说明</strong></p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>server</strong></p>
</td>
<td width="529">
<p>该参数可以多次用于添加时钟服务器,必须以"server "格式使用。一般而言,你想添加多少服务器,就可以添加多少服务器</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>stratumweight</strong></p>
</td>
<td width="529">
<p>stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>driftfile</strong></p>
</td>
<td width="529">
<p>chronyd程序的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能的话,会从时钟服务器获得较好的估值</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>rtcsync</strong></p>
</td>
<td width="529">
<p>rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>allow/deny</strong></p>
</td>
<td width="529">
<p>这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>cmdallow/cmddeny</strong></p>
</td>
<td width="529">
<p>跟上面相类似,只是你可以指定哪个IP地址或哪台主机可以通过chronyd使用控制命令</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>bindcmdaddress</strong></p>
</td>
<td width="529">
<p>该指令允许你限制chronyd监听哪个网络接口的命令包(由chronyc执行)。该指令通过cmddeny机制提供了一个除上述限制以外可用的额外的访问控制等级</p>
</td>
</tr>
<tr>
<td width="168">
<p><strong>makestep</strong></p>
</td>
<td width="529">
<p>通常,chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。该指令强制chronyd在调整期大于某个阀值时步进调整系统时钟,但只有在因为chronyd启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效</p>
</td>
</tr>
</tbody>
</table>
<p> chronyc命令参数说明:</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="132">
<p align="center"><strong>参数</strong></p>
</td>
<td valign="top" width="331">
<p align="center"><strong>参数说明</strong></p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>accheck </strong></p>
</td>
<td valign="top" width="331">
<p>检查NTP访问是否对特定主机可用</p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>activity </strong></p>
</td>
<td valign="top" width="331">
<p>该命令会显示有多少NTP源在线/离线</p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>add server</strong></p>
</td>
<td valign="top" width="331">
<p>手动添加一台新的NTP服务器。</p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>clients </strong></p>
</td>
<td valign="top" width="331">
<p>在客户端报告已访问到服务器</p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>delete </strong></p>
</td>
<td valign="top" width="331">
<p>手动移除NTP服务器或对等服务器</p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>settime </strong></p>
</td>
<td valign="top" width="331">
<p>手动设置守护进程时间</p>
</td>
</tr>
<tr>
<td valign="top" width="132">
<p><strong>tracking </strong></p>
</td>
<td valign="top" width="331">
<p>显示系统时间信息</p>
<p> </p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<h3>6、设置时区(非必须)</h3>
<p> 查看当前系统时区:</p>
<pre>$ timedatectl
Local time: Fri 2018-2-29 13:31:04 CST
Universal time: Fri 2018-2-29 05:31:04 UTC
RTC time: Fri 2018-2-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
如果你当前的时区不正确,请按照以下操作设置。
查看所有可用的时区:
$ timedatectl list-timezones
筛选式查看在亚洲S开的上海可用时区:
$ timedatectl list-timezones |grep-E "Asia/S.*"
Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk
设置当前系统为Asia/Shanghai上海时区:
$ timedatectl set-timezone Asia/Shanghai
设置完时区后,强制同步下系统时钟:
$ chronyc -a makestep
200 OK<br><span style="font-size: 16px"><strong>快速步骤</strong></span></pre>
<p>CentOS7.1采用chronyd进行时钟同步</p>
<p>systemctl status chronyd 查看时钟同步状态</p>
<p>chronyc -a makestep 手动同步时钟</p>
<p>配置时钟同步服务器</p>
<p>vim /etc/chrony.conf</p>
<p>里面会有类似</p>
<p>server 0.pool.ntp.org iburst</p>
<p>server 1.pool.ntp.org iburst</p>
<p>server 2.pool.ntp.org iburst</p>
<p>server 3.pool.ntp.org iburst</p>
<p>把你的ntpd server放进去,然后执行</p>
<p>systemctl restart chronyd.service</p>
<p>systemctl enable chronyd.service </p>
<p> </p>
<p>参考:</p>
<p>详解:Linux Chrony 设置服务器集群同步时间</p>
<p>RHEL7 -- 使用Chrony设置时间与时钟服务器同步</p>
</div>
<div id="MySignature" role="contentinfo">
<div id="AllanboltSignature">
<div>作者:驴得水。 只做精品</div></div><br><br>
来源:https://www.cnblogs.com/lizhaoxian/p/11260041.html
頁:
[1]