CentOS7 的时间同步方法

Posted Ghost_02

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS7 的时间同步方法相关的知识,希望对你有一定的参考价值。

centos7 时间同步和时区设置


首先centos7的时间同步是使用chrony这个工具(
[root@localhost ~]# systemctl status chrony
● chrony.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)


1.安装chrony
[root@localhost ~]# yum -y install chrony
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile

2.启动chrony
[root@localhost ~]# systemctl start chronyd
[root@localhost ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2016-10-24 17:52:52 CST; 3h 35min ago
 Main PID: 807 (chronyd)
   CGroup: /system.slice/chronyd.service
           └─807 /usr/sbin/chronyd

Oct 24 17:52:44 localhost.localdomain systemd[1]: Starting NTP client/server...
Oct 24 17:52:44 localhost.localdomain chronyd[807]: chronyd version 2.1.1 sta...
Oct 24 17:52:45 localhost.localdomain chronyd[807]: Frequency -10.892 +/- 15....
Oct 24 17:52:52 localhost.localdomain systemd[1]: Started NTP client/server.
Oct 24 17:53:49 localhost.localdomain chronyd[807]: Forward time jump detected!
Oct 24 17:53:55 localhost.localdomain chronyd[807]: Selected source 202.118.1.81
Oct 24 17:53:55 localhost.localdomain chronyd[807]: System clock wrong by -45...
Oct 24 17:53:09 localhost.localdomain chronyd[807]: System clock was stepped ...
Oct 24 21:27:55 localhost.localdomain systemd[1]: Started NTP client/server.
Hint: Some lines were ellipsized, use -l to show in full.

3。用本机电脑同步另一台主机。做为服务器端需要配置 /etc/chrony.conf
等等。。先看一下本机的时间(localhost)
[root@localhost ~]# timedatectl 
      Local time: Mon 2016-10-24 21:30:33 CST
  Universal time: Mon 2016-10-24 13:30:33 UTC
        RTC time: Mon 2016-10-24 13:30:32
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
在设置一下另一台主机的时间(recehost)(错误的时间)
[root@recvhost ~]# timedatectl set-time "2018-11-11 11:11:11"
[root@recvhost ~]# timedatectl 
      Local time: Sun 2018-11-11 11:11:17 CST
  Universal time: Sun 2018-11-11 03:11:17 UTC
        RTC time: Sun 2018-11-11 11:11:18
        Timezone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a


好了,开始配置文件了(注意:现在是服务器端的修改)
22 allow 192.168.1.0/24
 23 
 24 # Listen for commands only on localhost.
 25 bindcmdaddress 127.0.0.1
 26 bindcmdaddress ::1
 27 
 28 # Serve time even if not synchronized to any NTP server.
 29 local stratum 10
第22行设置为本网段 第29行的注释取消
重启时间同步服务,再关闭防火墙
[root@localhost ~]# vim /etc/chrony.conf 
[root@localhost ~]# systemctl restart chronyd.service 
[root@localhost ~]# systemctl stop firewalld.service 




这下配置客户端的配置文件是同一个文件(/etc/chrony.conf)
删掉哪些没用的server   xxxxxxxxxx   iburst
  1 # Use public servers from the pool.ntp.org project.
  2 # Please consider joining the pool (http://www.pool.ntp.org/j    oin.html).
  3 server  192.168.1.192      iburst
  4 # Ignore stratum in source selection.

同样需要重启同步服务,关闭防火墙
[root@recvhost ~]# systemctl restart chronyd.service 
[root@recvhost ~]# systemctl stop firewalld.service 

使用下面的命令就可以查看同步情况
[root@recvhost ~]# 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
||                                                /   xxxx = adjusted offset,
||         Log2(Polling interval) -.             |    yyyy = measured offset,
||                                  \\            |    zzzz = estimated error.
||                                   |           |                         
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 192.168.1.192                 3   6    37    29  -1430ns[+7000ns] +/-   15ms


下来再看时间
[root@recvhost ~]# timedatectl 
      Local time: Mon 2016-10-24 21:47:23 CST
  Universal time: Mon 2016-10-24 13:47:23 UTC
        RTC time: Mon 2016-10-24 21:47:24
        Timezone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: n/a


timedatectl      status   显示当前时间信息
                 set-time  设定当前时间
                 set-timezone 设定当前时区
                 set-local-rtc  设定bios时间

以上是关于CentOS7 的时间同步方法的主要内容,如果未能解决你的问题,请参考以下文章

Linux NTP时间同步全解析(CentOS7环境)

Linux杂记-配置ntp时间同步服务

centos7上使用chrony自动同步时间

CentOS7 设置 MySQL 主备同步

CentOS7 设置 MySQL 主备同步

CentOS7 设置 MySQL 主备同步