centos7之关于时间和日期以及时间同步的应用

在CentOS 6版本,时间设置有date、hwclock命令,从CentOS 7开始,使用了一个新的命令timedatectl。

基本概念:

一、GMT、UTC、CST、DST 时间

  • UTC

   整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。

  • GMT

   格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的皇家格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)

  • CST

   中国标准时间 (China Standard Time)

GMT + 8 = UTC + 8 = CST
  • DST

   夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用)

二、硬件时间和系统时间

  • 硬件时间

  RTC(Real-Time Clock)或CMOS时间,一般在主板上靠电池供电,服务器断电后也会继续运行。仅保存日期时间数值,无法保存时区和夏令时设置。

  • 系统时间

  一般在服务器启动时复制RTC时间,之后独立运行,保存了时间、时区和夏令时设置。

三、timedatectl 命令

  3.1使用指南

[root@localhost ~]# timedatectl -h
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled

  3.2命令示例(查看当前系统时间)

#timedatectl
#timedatectl status
 1 [root@localhost ~]# timedatectl
 2       Local time: Thu 2018-04-05 16:22:59 CST
 3   Universal time: Thu 2018-04-05 08:22:59 UTC
 4         RTC time: Thu 2018-04-05 08:22:59
 5        Time zone: Asia/Shanghai (CST, +0800)
 6      NTP enabled: yes
 7 NTP synchronized: yes
 8  RTC in local TZ: yes
 9       DST active: n/a
10 
11 Warning: The system is configured to read the RTC time in the local time zone.
12          This mode can not be fully supported. It will create various problems
13          with time zone changes and daylight saving time adjustments. The RTC
14          time is never updated, it relies on external facilities to maintain it.
15          If at all possible, use RTC in UTC by calling
16          'timedatectl set-local-rtc 0'.
17 [root@localhost ~]# timedatectl status
18       Local time: Thu 2018-04-05 16:23:08 CST
19   Universal time: Thu 2018-04-05 08:23:08 UTC
20         RTC time: Thu 2018-04-05 08:23:08
21        Time zone: Asia/Shanghai (CST, +0800)
22      NTP enabled: yes
23 NTP synchronized: yes
24  RTC in local TZ: yes
25       DST active: n/a
26 
27 Warning: The system is configured to read the RTC time in the local time zone.
28          This mode can not be fully supported. It will create various problems
29          with time zone changes and daylight saving time adjustments. The RTC
30          time is never updated, it relies on external facilities to maintain it.
31          If at all possible, use RTC in UTC by calling
32          'timedatectl set-local-rtc 0'.
示例

  3.3设置当前时间

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

  3.4查看所有可用的时区

timedatectl list-timezones
# 亚洲
timedatectl list-timezones |  grep  -E "Asia/S.*"

  3.5设置时区

timedatectl set-timezone Asia/Shanghai

  3.6设置硬件时间

# 硬件时间默认为UTC
timedatectl set-local-rtc 1
# hwclock --systohc --localtime
# 两条命令效果等同

  3.7启动自动同步时间

timedatectl set-ntp yes
# yes或no; 1或0也可以

  

四、Chrony 服务器

Chrony:是网络时间协议的 (NTP) 的另一种实现,由两个程序组成,分别是chronyd和chronyc。

chronyd:是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。

chronyc:提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

优势:

  • 更快的同步只需要数分钟而非数小时时间,从而最大程度减少了时间和频率误差,这对于并非全天 24 小时运行的台式计算机或系统而言非常有用。
  • 能够更好地响应时钟频率的快速变化,这对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技术而言非常有用。
  • 在初始同步后,它不会停止时钟,以防对需要系统时间保持单调的应用程序造成影响。
  • 在应对临时非对称延迟时(例如,在大规模下载造成链接饱和时)提供了更好的稳定性。
  • 无需对服务器进行定期轮询,因此具备间歇性网络连接的系统仍然可以快速同步时钟。
注意:在CentOS7下为标配的时间同步服务,当然也可以使用以前的NTP同步方式,不过要安装NTP服务。
  服务端

  4.1安装使用

yum install chrony
systemctl start chronyd
systemctl enable chronyd

  4.2配置文件修改

  当Chrony启动时,它会读取/etc/chrony.conf配置文件中的设置。也就是锁,如果需要更改时间同步的服务器,修改此配置文件即可。

[root@localhost ~]# cat  /etc/chrony.conf 
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server ntp1.aliyun.com
server time1.aliyun.com
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

driftfile /var/lib/chrony/drift
makestep 10 3
allow 172.16.5.0/24
rtcsync
logdir /var/log/chrony
makestep 10 3
allow 192.16.5.0/24
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
local stratum 10
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony 
 1 [root@localhost ~]# grep -Ev "^$|^#" /etc/chrony.conf
 2 # 该参数可以多次用于添加时钟服务器,必须以"server "格式使用。一般而言,你想添加多少服务器,就可以添加多少服务器。
 3 server 0.centos.pool.ntp.org iburst
 4 server 1.centos.pool.ntp.org iburst
 5 server 2.centos.pool.ntp.org iburst
 6 server 3.centos.pool.ntp.org iburst
 7 # stratumweight指令设置当chronyd从可用源中选择同步源时,每个层应该添加多少距离到同步距离。默认情况下,设置为0,让chronyd在选择源时忽略源的层级。
 8 stratumweight 0
 9 # chronyd程序的主要行为之一,就是根据实际时间计算出计算机增减时间的比率,将它记录到一个文件中是最合理的,它会在重启后为系统时钟作出补偿,甚至可能的话,会从时钟服务器获得较好的估值。
10 driftfile /var/lib/chrony/drift
11 # rtcsync指令将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
12 rtcsync
13 # 通常,chronyd将根据需求通过减慢或加速时钟,使得系统逐步纠正所有时间偏差。在某些特定情况下,系统时钟可能会漂移过快,导致该调整过程消耗很长的时间来纠正系统时钟。
14 # 该指令强制chronyd在调整期大于某个阀值时步进调整系统时钟,但只有在因为chronyd启动时间超过指定限制(可使用负值来禁用限制),没有更多时钟更新时才生效。
15 makestep 10 3
16 # 这里你可以指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器。
17 #allow 192.168/16
18 # 该指令允许你限制chronyd监听哪个网络接口的命令包(由chronyc执行)。该指令通过cmddeny机制提供了一个除上述限制以外可用的额外的访问控制等级。
19 bindcmdaddress 127.0.0.1
20 bindcmdaddress ::1
21 keyfile /etc/chrony.keys
22 # 指定了/etc/chrony.keys中哪一条密码被使用
23 commandkey 1
24 # 此参数指定了产生一个SHA1或MD5加密的密码,存放在/etc/chrony.keys中
25 generatecommandkey
26 noclientlog
27 logchange 0.5
28 logdir /var/log/chrony
29 
30 作者:无限小BUG
31 链接:https://www.jianshu.com/p/fb32239ccf2b
32 來源:简书
33 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
说明

 

  4.3客户端配置

  客户端的配置文件是同一个文件(/etc/chrony.conf),删掉哪些没用的server xxxxxxxxxx iburst

# Use public servers from the pool.ntp.org project.  
# Please consider joining the pool (http://www.pool.ntp.org/j    oin.html).  
server  192.168.1.238     iburst  
# Ignore stratum in source selection.   

  4.4常用命令

  • accheck 检查NTP访问是否对特定主机可用

  • activity 该命令会显示有多少NTP源在线/离线

  • add server 手动添加一台新的NTP服务器

  • clients 在客户端报告已访问到服务器

  • delete 手动移除NTP服务器或对等服务器

  • settime 手动设置守护进程时间

  • tracking 显示系统时间信息

  4.5案例介绍

  查看当前时间服务器状态

[root@localhost ~]# chronyc  #进入chronyc控制界面
chrony version 3.1
Copyright (C) 1997-2003, 2007, 2009-2017 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> activity #查看当前服务器状态,如下服务器有两个NTP源在线
200 OK
2 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

  查看时间同步的信息来源

[root@localhost ~]# chronyc  sources
210 Number of sources = 2
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* time5.aliyun.com              2   6   377    31   +267us[ +573us] +/- 3769us
^- 203.107.6.88                  2   6   377    31  -2586us[-2586us] +/-   38ms

  

五、日常使用案例

  centos7从外网同步时间和时区设置

# 安装
yum install chrony
# 启用
systemctl start chronyd
systemctl enable chronyd
# 设置亚洲时区
timedatectl set-timezone Asia/Shanghai
# 启用NTP同步
timedatectl set-ntp yes

  也可以用NTP同步时间(不推荐)

# 安装ntp服务
yum install ntp
# 开机启动服务
systemctl enable ntpd
# 启动服务
systemctl start ntpd
# 设置亚洲时区
timedatectl set-timezone Asia/Shanghai
# 启用NTP同步
timedatectl set-ntp yes
# 重启ntp服务
systemctl restart ntpd
# 手动同步时间
ntpq -p

  常见问题:

  1、#date看到上的时间和#timedatectl看到的本地时间不同,需要我们注意一样。

  2、(Real-Time Clock)或CMOS时间和本地时间不同,需要执行timedatectl set-local-rtc 0

[root@localhost ~]# date
Thu Apr  5 17:22:34 CST 2018
[root@localhost ~]# timedatectl
      Local time: Thu 2018-04-05 17:22:38 CST
  Universal time: Thu 2018-04-05 09:22:38 UTC
        RTC time: Thu 2018-04-05 17:22:35
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

  参考:https://www.jianshu.com/p/fb32239ccf2b

 

posted @ 2018-04-05 16:22  Charles.L  阅读(32743)  评论(1编辑  收藏  举报