CentOS7搭建时间服务器-chrony

操作系统:CentOS7
防火墙:  关闭防火墙和selinux
时间软件:chrony

CentOS7我们一直用的ntp时间服务器,虽然到CentOS7上也可以装ntp。chrony与ntp都是时间同步软件,两个软件不能够同时开启,会出现时间冲突,但是由于各种问题,所以建议CentOS7使用chrony同步工具。

Chrony是一个开源的自由软件,它能帮助你保持系统时钟与时钟服务器(NTP)同步,因此让你的时间保持精确。

Chrony它由两个程序组成,分别是chronyd和chronyc。

Chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。

Chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。


在CentOS7之类基于RHEL的操作系统上,已经默认安装有Chrony。

stratumweight - stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认情况下,CentOS中设置为0,让chronyd在选择源时忽略源的层级。


driftfile - chronyd程序的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能的话,会从时钟服务器获得较好的估值。
rtcsync - rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
allow / deny - 这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器。
local stratum 10
——————————————————————————————————————————

【服务端配置】
1.安装chrony软件
[root@server ~]# yum install chrony -y

2.修改配置文件
[root@server ~]# vim /etc/chrony.conf

server 0.centos.pool.ntp.org iburst     #可以修改ntp的服务器,最好用国内的。
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

allow 192.168.0.0/24                    #打开允许同步的IP
local stratum 10

或者修改为阿里的如下内容:
server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst

allow 192.168.0.0/24
local stratum 10

3.启动、开机自启动、查看状态、查看同步源
[root@server ~]# systemctl start chronyd
[root@server ~]# systemctl enable chronyd
[root@server ~]# systemctl status chronyd
[root@server ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2021-07-25 12:33:58 CST; 6min ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 11281 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 11277 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11279 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─11279 /usr/sbin/chronyd

7月 25 12:33:57 server systemd[1]: Starting NTP client/server...
7月 25 12:33:58 server chronyd[11279]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
7月 25 12:33:58 server chronyd[11279]: Frequency 8.760 +/- 2.817 ppm read from /var/lib/chrony/drift
7月 25 12:33:58 server systemd[1]: Started NTP client/server.
7月 25 12:34:23 server chronyd[11279]: Selected source 120.25.115.20
7月 25 12:34:25 server chronyd[11279]: Selected source 203.107.6.88
7月 25 12:35:29 server chronyd[11279]: Selected source 120.25.115.20
[root@server ~]#



【客户端配置】
1. 安装chrony
[root@client ~]# yum install chrony -y

2. 修改配置文件
[root@client ~]# vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# 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
server 192.168.0.100 iburst                     #添加本地server

3.启动、开机自启动、查看状态、查看同步源
[root@client ~]# date
Sat Sep  9 00:00:02 CST 2017

[root@client ~]#  systemctl start chronyd
[root@client ~]#  systemctl enable chronyd
[root@client ~]#  systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2020-01-02 00:03:07 CST; 1 years 5 months ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 5590 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 5586 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 5589 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─5589 /usr/sbin/chronyd

1月 02 00:03:07 client systemd[1]: Starting NTP client/server...
1月 02 00:03:07 client chronyd[5589]: chronyd version 3.2 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCF...DEBUG)
1月 02 00:03:07 client chronyd[5589]: Frequency -5.594 +/- 6.413 ppm read from /var/lib/chrony/drift
1月 02 00:03:07 client systemd[1]: Started NTP client/server.
1月 02 00:03:12 client chronyd[5589]: Selected source 192.168.0.104
1月 02 00:03:12 client chronyd[5589]: System clock wrong by 45777777.135097 seconds, adjustment started
6月 14 20:06:09 client chronyd[5589]: System clock was stepped by 45777777.135097 seconds
[root@client ~]#

[root@client ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.0.104                 3   6    37    21  +4979ns[ +340us] +/-   26ms
[root@client ~]#
[root@dbserver ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.0.111                 3   6   377    34  -1615us[ -7960d] +/-   34ms
[root@dbserver ~]# chronyc sources -v
210 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
===============================================================================
^* 192.168.0.111                 3   6   377    42  -1615us[ -7960d] +/-   34ms
[root@dbserver ~]#


##################
^* #已同步
^? #未同步



手动修改客户端时间后,在客户端执行如下命令,看到NTP synchronized: no,需要重新启动chronyd服务即可
[root@client ~]#  timedatectl
      Local time: 四 2020-01-02 00:01:26 CST
  Universal time: 三 2020-01-01 16:01:26 UTC
        RTC time: 一 2021-06-14 12:04:24
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@client ~]#

[root@client ~]#  systemctl restart chronyd

[root@client ~]#  date
2021年 06月 14日 星期一 20:07:19 CST
[root@dbserver ~]# timedatectl
      Local time: 一 2021-06-14 20:07:23 CST
  Universal time: 一 2021-06-14 12:07:23 UTC
        RTC time: 一 2021-06-14 12:07:23
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@client ~]#

  • 18
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是在CentOS 7上搭建NTP时间服务器的步骤: 1.安装NTP软件包 ```shell yum install ntp -y ``` 2.配置NTP服务器 编辑`/etc/ntp.conf`文件,添加如下内容: ```shell # Use public servers from the pool.ntp.org project. # 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 # Allow LAN traffic. restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap ``` 其中,`server`行指定了NTP服务器使用的公共时间服务器,`restrict`行指定了允许访问NTP服务器的IP地址范围。 3.启动NTP服务 ```shell systemctl start ntpd ``` 4.设置NTP服务开机自启动 ```shell systemctl enable ntpd ``` 5.检查NTP服务状态 ```shell systemctl status ntpd ``` 如果NTP服务正在运行,则输出类似以下内容: ```shell ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-08-10 14:20:20 CST; 1h 5min ago Main PID: 1234 (ntpd) CGroup: /system.slice/ntpd.service └─1234 /usr/sbin/ntpd -u ntp:ntp -g ``` 6.检查NTP服务器是否正常工作 ```shell ntpq -p ``` 如果NTP服务器正常工作,则输出类似以下内容: ```shell remote refid st t when poll reach delay offset jitter ============================================================================== *0.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 +1.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 +2.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 +3.centos.pool. .POOL. 16 p - 64 0 0.000 0.000 0.000 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值