chrony实现局域网时间同步

chrony基于cs架构实现,c与s的区别在于chrony.conf中的allow选项:

打开allow选项表示允许其他主机从这台主机同步,即作为s

默认为关闭即表示不允许其他主机从这台主机同步,即为c

服务端

timedatectl set-timezone Asia/Shanghai   --时区修改为shanghai
yum install chrony --RHEL7默认已安装chrony,而没有安装ntpd.

systemctl status chronyd --查看chronyd服务状态
systemctl enable chronyd --开机启动

vi /etc/chrony.conf    --修改配置文件

#server 0.rhel.pool.ntp.org iburst   --注释这4行,由于是内网环境,所以无法跟外部时间服务器进行时间同步。
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 192.168.100.1 iburst    --添加这一行,表示与本机同步时间

  #server cn.pool.ntp.org iburst  --国内也常用这俩时间同步服务器
  #server tw.pool.ntp.org iburst


 

# Allow NTP client access from local network.
allow 192.168.100.0/24   --允许哪些服务器到这台服务器来同步时间

客户端

#server 0.rhel.pool.ntp.org iburst  --注释这4行
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
server 192.168.100.1 iburst    ---添加该行,表示到这台服务器去同步时间。

重启chrony服务,并开机自启

[root@rhel2 ~]# systemctl restart chronyd.service 
[root@rhel2 ~]# systemctl enable chronyd.service

 

 

常用命令

 

cat /etc/chrony.conf | grep -v ^# | grep -v ^$ 查看配置文件(去掉注释行和空行)

  

查看时间同步源:
$ chronyc sources -v
查看时间同步源状态:
$ chronyc sourcestats -v
校准时间服务器:
$ chronyc tracking

  

 

其他

查看时间同步

[root@rhel2 ~]# timedatectl 
       Local time: Sat 2017-10-07 11:15:43 CST
   Universal time: Sat 2017-10-07 03:15:43 UTC
         RTC time: Sat 2017-10-07 03:02:26
        Time zone: Asia/Shanghai (CST, +0800)
      NTP enabled: no
NTP synchronized: yes   --为yes表示已同步
  RTC in local TZ: no
       DST active: n/a

手动修改时间时,必须把NTP enabled 设置为no.

[root@rhel1 ~]# date
Sun Oct  8 11:00:51 CST 2017
[root@rhel1 ~]# timedatectl
       Local time: Sat 2017-10-07 11:31:45 CST
   Universal time: Sat 2017-10-07 03:31:45 UTC
         RTC time: Sun 2017-10-08 03:01:57
        Time zone: Asia/Shanghai (CST, +0800)
      NTP enabled: no
NTP synchronized: no
  RTC in local TZ: no
       DST active: n/a

如果把NTP enabled 修改为yes时,表示开启自动同步时间,此时,是不能手动修改时间的。

如果为No时,表示关闭自动同步时间。

[root@rhel1 ~]# timedatectl set-ntp yes
[root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50"
Failed to set time: Automatic time synchronization is enabled
[root@rhel1 ~]# timedatectl set-ntp no
[root@rhel1 ~]# timedatectl set-time "2017-10-08 11:00:50"
[root@rhel1 ~]# date
Sun Oct  8 11:00:51 CST 2017

 

posted @ 2019-05-06 18:25  豆浆D  阅读(3394)  评论(1编辑  收藏  举报