17、CentOS7.2 chrony替代ntp搭建时间服务器
Ø chrony既可作时间服务器服务端,也可作客户端。
Ø chrony性能比ntp要好很多,且chrony配置简单、管理方便。
Ø chrony服务可以实现多台服务器的时间同步
Ø chronyd是一个在系统后台运行的守护进程
Ø chronyc是用来监控chronyd性能和配置其参数程序
Ø 配置文件:/etc/chrony.conf
步1、安装chrony
默认情况下,Cent0S7已经安装,不用再行安装。
[root@server102 etc]# yum install -y chrony
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.huaweicloud.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.aliyun.com
软件包 chrony-3.2-2.el7.x86_64 已安装并且是最新版本
无须任何处理
步2、修改配置文件-主服务器
设置server101主机的配置如下:
依次执行以下命令:
# systemctl status chronyd.service(检查chronyd服务状态,默认情况下启动状态)
#netstat -antulp | grep chrony #查看使用的端口号
[root@server101 etc]# netstat -antulp | grep chrony
udp 0 0 127.0.0.1:323 0.0.0.0:* 504/chronyd
udp6 0 0 ::1:323 :::*
vim etc/chrony.conf #配置文件
注释掉默认的时间服务器
#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
开启local stratum 10 #和自己对表
Local stratum 10
也可以设置allow 192.168.56.0/24 #允许那个网段访问,如果不设置,默认都可以连接
#允许的网段
allow 192.168.56.0/24
重新启动
# systemctl restart chronyd.service
查看当前主机的服务器状态:
[root@server101 etc]# chronyc sources -v
210 Number of sources = 0
.-- 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
===============================================================================
#这儿什么也没有,即没有任何的时间服务器
如果有:^表示时间服务器,*表示成功,?表示同步没有成功
步3、修改配置文件-从服务器-即客户端
设置server102从server101同步时间:
# vim chrony.conf
#配置上server101服务器的地址,等上几分钟才会对表
server 192.168.56.101 iburst
重新启动:
# systemctl restart chronyd.service
查看时间状态:
[root@server102 etc]# timedatectl
Local time: 四 2018-11-08 21:55:55 CST
Universal time: 四 2018-11-08 13:55:55 UTC
RTC time: 四 2018-11-08 13:54:13
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
查看同步状态:
[root@server102 etc]# 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
===============================================================================
^? server101 0 7 0 - +0ns[ +0ns] +/- 0ns
#上面显示?号为没有同步
#^表示时间服务器
#如果显示 * 才表示已经同步
修改时区:
# timedatectl set-timezone Asia/Shanghai
如果通过 chronyc sources -v 依然显示没有同步成功,则可以关闭服务器的防火墙:
[root@server102 etc]# 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
===============================================================================
^* server101 10 6 37 37 +691us[+1076us] +/- 331us
#注意,上面已经显示为 ^*即表示同步成功。
确实比NTP的配置要简单一些。
步4、同步时间
可以使用ntpdate 192.168.56.101让客户端立即同步时间。如果没有这个命令,可以安装ntpdate即,yum install -y ntpdate。注意,只安装ntpdate即可。不需要安装ntp和ntpd两个服务。
使用这个命令,可以立刻执行时间同步:
[root@server102 ~]# ntpdate server101
8 Nov 22:18:27 ntpdate[20323]: adjust time server 192.168.56.101 offset -0.001308 sec





