Loading

Centos7配置时间服务器

1.什么是时间服务?

时间同步服务
多主机协作工作时,各个主机的时间同步很重要,时间不一致会造成很多重要应用的故障,如:加密协
议,日志,集群等,利用NTP(Network Time Protocol)协议使网络中的各个计算机时间达到同步。目前NTP协议属于运维基础架构中必备的基本服务之一。

时间同步软件
NTP
将系统时钟和世界协调时UTC同步,精度在局域网内可达0.1ms,在互联网上绝大多数的地方精度可以
达到1-50ms。

2.什么是chrony?

实现NTP协议的的自由软件。可使系统时钟与NTP服务器,参考时钟(例如GPS接收器)以及使用手表
和键盘的手动输入进行同步。还可以作为NTPv4(RFC 5905)服务器和对等体运行,为网络中的计算机
提供时间服务。设计用于在各种条件下良好运行,包括间歇性和高度拥挤的网络连接,温度变化(计算
机时钟对温度敏感),以及不能连续运行或在虚拟机上运行的系统。
centos6是NTP,在centos7变成了chrony。

2.1 chrony的优势

更快的同步只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,对于并非全天,24小时运行的虚拟计算机而言非常有用。
能够更好地响应时钟频率的快速变化,对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用。

2.2 官方文档

chrony官网: https://chrony.tuxfamily.org
chrony官方文档: https://chrony.tuxfamily.org/documentation.html
image

3. chrony的安装与配置

centos7默认是安装chrony的。查看chrony的文件组成部分。

rpm -ql chrony
/etc/NetworkManager/dispatcher.d/20-chrony
/etc/chrony.conf
/etc/chrony.keys
/etc/dhcp/dhclient.d/chrony.sh
/etc/logrotate.d/chrony
/etc/sysconfig/chronyd
/usr/bin/chronyc
/usr/lib/systemd/ntp-units.d/50-chronyd.list
/usr/lib/systemd/system/chrony-dnssrv@.service
/usr/lib/systemd/system/chrony-dnssrv@.timer
/usr/lib/systemd/system/chrony-wait.service
/usr/lib/systemd/system/chronyd.service
/usr/libexec/chrony-helper
/usr/sbin/chronyd
/usr/share/doc/chrony-3.4
/usr/share/doc/chrony-3.4/COPYING
/usr/share/doc/chrony-3.4/FAQ
/usr/share/doc/chrony-3.4/NEWS
/usr/share/doc/chrony-3.4/README
/usr/share/man/man1/chronyc.1.gz
/usr/share/man/man5/chrony.conf.5.gz
/usr/share/man/man8/chronyd.8.gz
/var/lib/chrony
/var/lib/chrony/drift
/var/lib/chrony/rtc
/var/log/chrony

3.1 配置时间服务器。

10.0.0.8    充当NTP服务器
10.0.0.30   客户端
10.0.0.190  客户端
10.0.0.9    客户端

3.2 修改配置文件

监听端口: 323/udp,123/udp
配置文件: /etc/chrony.conf

vim /etc/chrony.conf
pool  ntp.aliyun.com iburst   #可以写阿里云的,也可以写其他的。

3.3 重启配置文件后查看

# 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

===============================================================================

^ 203.107.6.88                  2   6    17    51   +411us[-2720us] +/-   37ms

3.4 查看端口

端口是323和123

ss -ntulp
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 127.0.0.1:876 *:* users:(("rpc.statd",pid=1123,fd=7))
udp UNCONN 0 0 *:904 *:* users:(("rpcbind",pid=743,fd=7))
udp UNCONN 0 0 *:38293 *:* users:(("rpc.statd",pid=1123,fd=8))
udp UNCONN 0 0 *:2049 *:*
udp UNCONN 0 0 *:20048 *:* users:(("rpc.mountd",pid=1143,fd=7))
udp UNCONN 0 0 *:111 *:* users:(("rpcbind",pid=743,fd=6))
udp UNCONN 0 0 *:123 *:* users:(("chronyd",pid=2542,fd=7))
udp UNCONN 0 0 *:50933 *:*
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=2542,fd=5))
udp UNCONN 0 0 [::]:42338 [::]:*
udp UNCONN 0 0 [::]:904 [::]:*

3.5 服务节点同步时间

在chrony配置文件加上如下选项即可。

ntpdate 10.0.0.8
17 Mar 16:40:44 ntpdate[1483]: no server suitable for synchronization found
没有找到适合同步的服务器
设置本地网络可以同步我的ntp服务请求
按照别人的配置往下写就好了
allow 192.168.0.0/16
allow 10.0.0.0/24

重启服务并加入开机启动
systemctl restart chronyd && systemctl enable chronyd

3.6 服务节点同步信息

ntpdate 10.0.0.8
-bash: ntpdate: 未找到命令

#解决方法:
apt install ntpdate -y
yum install ntpdate -y

#都同步OK
ntpdate 10.0.0.8
17 Mar 16:49:14 ntpdate[1502]: adjust time server 10.0.0.8 offset -0.005113 sec
ntpdate 10.0.0.8

17 Mar 16:51:05 ntpdate[4147]: adjust time server 10.0.0.8 offset 0.009203 sec

ntpdate 10.0.0.8

17 Mar 17:03:52 ntpdate[1727]: adjust time server 10.0.0.8 offset 0.006905 sec

3.7 在时间服务器查看。


#客户端查看
chronyc
chrony version 3.4
Copyright (C) 1997-2003, 2007, 2009-2018 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> clinets

Unrecognized command

chronyc> clients

Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last

===============================================================================

10.0.0.190                      4      0   1   -   20m       0      0   -     -

10.0.0.9                        4      0   1   -  1140       0      0   -     -

10.0.0.30                       4      0   1   -   373       0      0   -     -

3.8 命令行同步阿里云时间服务器。

# ntpdate ntp1.aliyun.com
13 Sep 21:20:49 ntpdate[3751]: adjust time server 120.25.115.20 offset -0.000147 sec
posted @ 2021-03-17 17:47  梨花海棠  阅读(909)  评论(0编辑  收藏  举报