chrony服务配置,微服务配置中心


chrony软件使用说明

  chrony简介

  chrony是一个开源的自由软件,它能保持系统时钟与时间服务器(ntp)同步,让时间保持精确。

  它由两个程序组成:chrongd和chronyc。

  chronyd是一个后台运行的守护进程,用于调整内核运行的系统时钟和时间服务器同步。

  它确定计算机增减时间的比率,并对此进行补偿。

  chrony是CentOs7.x上自带的时间同步软件
  • 本地服务器作为时间服务器同步时间
  • 本地服务器不同步互联网时间服务器的时间
 1 #yum install chrony --RHEL7默认已安装chrony,而没有安装ntpd。
 2 
 3 #systemctl status chronyd --查看chronyd服务状态
 4 
 5 #systemct enable chronyd --开机启动
 6 
 7 #vim /etc/chrony.conf --修改配置文件
 8 
 9 #server 0.rhel.pool.ntp.org iburst --注释这4行,表示的是外网时间服务器的网址。
10 
11 #server 1.rhel.pool.ntp.org iburst 
12 
13 #server 1.rhel.pool.ntp.org iburst 
14 
15 #server 1.rhel.pool.ntp.org iburst 
16 
17  server 192.168.56.112 iburst --添加这一行,表示与本机同步时间
18 
19  local stratum 10 -->>该行注释取消掉不然NTP synchronized: 为no 取消掉后变为 NTP synchronized:yes
20 
21 #Allow NTP client access from local network.
22 
23  allow 192.168.56.0/24 -->>允许哪些服务器或客户端到这台时间服务器来同步时间。必须配置
24 
25 #systemctl restart chronyd -->>修改完配置文件后,重启chrony服务
[root@chronyd ~]# netstat -antulp|grep chronyd  -->>查看时间服务器是否允许
udp 0 0 0.0.0.0:123 0.0.0.0:* 12505/chronyd 
udp 0 0 127.0.0.1:323 0.0.0.0:* 12505/chronyd 
udp6 0 0 ::1:323 :::* 12505/chronyd 
[root@chronyd ~]# ss -antulp|grep chronyd
udp UNCONN 0 0 *:123 *:* users:(("chronyd",pid=12505,fd=7))
udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=12505,fd=5))
udp UNCONN 0 0 ::1:323 :::* users:(("chronyd",pid=12505,fd=6))

显示系统当前日期和时间

[root@chronyd ~]#timedatectl 

Local time: 二 2019-11-12 11:06:46 CST
Universal time: 二 2019-11-12 03:06:46 UTC
RTC time: 二 2019-11-12 03:06:46
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

注意NTP synchronized值,只有时间服务器自己同步完成时间后,才能为其他服务器提供时间同步。

 设置日期和时间 

timedatectl set-time "YYYY-MM-DD HH:MM:SS"

timedatectl set-time "YYYY-MM-DD"

timedatectl set-time "HH:MM:SS"

查看所有可用的市区

1 [root@chronyd ~]# timedatectl list-timezones |grep Asia/Shanghai
2 Asia/Shanghai

 设置时区

 1 [root@chronyd ~]# timedatectl set-timezone Asia/Shanghai 

设置硬件时间

#硬件时间默认为UTC

1 [root@chronyd ~]# timedatectl set-local-rtc 1
2 [root@chronyd ~]# hwclock --systohc --localtime
3 #两条命令效果等同

 启用NTP时间同步

1 [root@chronyd ~]# timedatectl set-ntp yes
2 [root@chronyd ~]# timedatectl set-ntp true
1 #两条命令效果等同
2 问题:ntp设置为yes,手动设置时间报错。

查看时间源信息

 1 [root@chronyd ~]# chronyc sources -v
 2 210 Number of sources = 1
 3 
 4 .-- Source mode '^' = server, '=' = peer, '#' = local clock.
 5 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
 6 | / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
 7 || .- xxxx [ yyyy ] +/- zzzz
 8 || Reachability register (octal) -. | xxxx = adjusted offset,
 9 || Log2(Polling interval) --. | | yyyy = measured offset,
10 || \ | | zzzz = estimated error.
11 || | | \
12 MS Name/IP address Stratum Poll Reach LastRx Last sample 
13 ===============================================================================
14 ^* chronyd 10 6 177 9 -15ns[ -15us] +/- 25us
15 
16 [root@chronyd ~]#

客户端时间同步配置

#yum install chrony -y  -->>安装时间服务器软件

修改配置文件

1 [root@Client ~]# vim /etc/chrony.conf
2 
3 # Use public servers from the pool.ntp.org project.
4 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
5 #server 0.centos.pool.ntp.org iburst
6 #server 1.centos.pool.ntp.org iburst
7 #server 2.centos.pool.ntp.org iburst
8 #server 3.centos.pool.ntp.org iburst
9 server 192.168.56.112 iburst -->>添加该行,表示到这台服务器去同步时间。

重启chrony服务,并设置开机自启动

1 [root@Client ~]# systemctl restart chronyd.service -->>重启chrony服务
2 [root@Client ~]# systemctl enable chronyd.service -->>设置开机自启动模式

查看时间同步状态

1 [root@Client ~]# timedatectl 
2 Local time: 四 2013-02-14 12:37:25 CST
3 Universal time: 四 2013-02-14 04:37:25 UTC
4 RTC time: 日 2019-05-12 04:24:01
5 Time zone: Asia/Shanghai (CST, +0800)
6 NTP enabled: no
7 NTP synchronized: yes -->>为yes表示已同步
8 RTC in local TZ: no
9 DST active: n/a

重启chronyd服务后,需要过几分钟才会自动完成同步。

如果需要手动,可以使用下面的命令

[root@Client ~]# ntpdate 192.168.56.112
14 Feb 12:40:09 ntpdate[8038]: adjust time server 192.168.56.112 offset -0.000067 sec

也可以使用crontab -e定时任务同步时间

1 [root@Client ~]# crontab -l
2 */2 * * * * /usr/sbin/ntpdate 192.168.56.112  -->>创建脚本每个2分钟来同步时间一次


1 如果同步失败,有可能是时间服务器的时间为同步,即时间服务器NTP  synchronized 为no
2 
3 手动修改时间时,必须把NTP enable 设置为no
4 
5 如果把 NTP enable修改为yes时,表示开启自动同步时间,此时,是不能手动修改时间的。如果为no时,表示关闭自动同步时间。

 

1 [root@chronyd ~]# timedatectl set-ntp true 
2 
3 [root@chronyd ~]# timedatectl set-time "2019-11-11 12:56:11"
4 Failed to set time: Automatic time synchronization is enabled

 

 

相关内容

    暂无相关文章