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)

1

GMT + 8 = UTC + 8 = CST

  • DST

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

二、硬件时间和系统时间

  • 硬件时间

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

  • 系统时间

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

三、timedatectl 命令

  3.1使用指南

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[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命令示例(查看当前系统时间)

1

2

#timedatectl

#timedatectl status

示例

  3.3设置当前时间

1

2

3

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

timedatectl set-time "YYYY-MM-DD"

timedatectl set-time "HH:MM:SS"

  3.4查看所有可用的时区

1

2

3

timedatectl list-timezones

# 亚洲

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

  3.5设置时区

1

timedatectl set-timezone Asia/Shanghai

  3.6设置硬件时间

1

2

3

4

# 硬件时间默认为UTC

timedatectl set-local-rtc 1

# hwclock --systohc --localtime

# 两条命令效果等同

  3.7启动自动同步时间

1

2

timedatectl set-ntp yes

# yes或no; 1或0也可以

  

四、Chrony 服务器

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

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

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

优势:

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

注意:在CentOS7下为标配的时间同步服务,当然也可以使用以前的NTP同步方式,不过要安装NTP服务。

  服务端

  4.1安装使用

1

2

3

yum install chrony

systemctl start chronyd

systemctl enable chronyd

  4.2配置文件修改

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

[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 

说明

 

  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案例介绍

  查看当前时间服务器状态

1

2

3

4

5

6

7

8

9

10

11

12

13

14

[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

  查看时间同步的信息来源

1

2

3

4

5

6

[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从外网同步时间和时区设置

1

2

3

4

5

6

7

8

9

# 安装

yum install chrony

# 启用

systemctl start chronyd

systemctl enable chronyd

# 设置亚洲时区

timedatectl set-timezone Asia/Shanghai

# 启用NTP同步

timedatectl set-ntp yes

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# 安装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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

[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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值