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

时间:2022-12-16 16:22:44

在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
 [root@localhost ~]# timedatectl
Local time: Thu -- :: CST
Universal time: Thu -- :: UTC
RTC time: Thu -- ::
Time zone: Asia/Shanghai (CST, +)
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'.
[root@localhost ~]# timedatectl status
Local time: Thu -- :: CST
Universal time: Thu -- :: UTC
RTC time: Thu -- ::
Time zone: Asia/Shanghai (CST, +)
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'.

示例

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

说明

  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

centos7之关于时间和日期以及时间同步的应用的更多相关文章

  1. CentOS7时间和日期的同步 (chrony和)

    CentOS 6版本,使用 hwclock CentOS 7版本,使用timedatectl 1.基本概念 1.1 GMT,UTC,CST,DST时间 世界标准时间 整个地球分为二十四时区,每个时区都 ...

  2. centos7设置系统时间与网络时间同步

    Linux的时间分为System Clock(系统时间)和Real Time Clock (硬件时间,简称RTC). 系统时间:指当前Linux Kernel中的时间. 硬件时间:主板上有电池供电的时 ...

  3. CentOS7--配置时间和日期

    CentOS7提供三个命令行工具,可用于配置和显示有关系统日期和时间的信息. timedatectl:Linux 7中的新增功能,也是systemd其中的一部分. date:系统时钟,也成为软件时钟, ...

  4. ☕【Java深层系列】「技术盲区」让我们一起完全吃透针对于时间和日期相关的API指南

    技术简介 java中的日期处理一直是个问题,没有很好的方式去处理,所以才有第三方框架的位置比如joda.文章主要对java日期处理的详解,用1.8可以不用joda. 时间概念 首先我们对一些基本的概念 ...

  5. Android随笔之——Android时间、日期相关类和方法

    今天要讲的是Android里关于时间.日期相关类和方法.在Android中,跟时间.日期有关的类主要有Time.Calendar.Date三个类.而与日期格式化输出有关的DateFormat和Simp ...

  6. Lua库之时间和日期操作

    Lua库之时间和日期操作 (2010-02-07 18:41:20) 转载▼ os.time() <== 返回当前系统的日历时间os.date() <== 返回本地化的时间字符串,这里是& ...

  7. date 显示或设置系统时间和日期

    显示或设置系统时间和日期 date [options] [+format] date [options] [new date] date用来显示系统的时间和日期,超级用户可以使用date来更改系统时钟 ...

  8. java时间和日期类型

    在java中,代表时间和日期的类型包括:java.util.Date和java.util.Calendar,此外,在JDBC API中还提供了3个扩展类,java.UtilDate类的子类:java. ...

  9. iOS中的时间和日期

    怎么说?时间和日期不是了不起的属性.了不起的功能,但是,我们决不能够因此就“冷落”它. 一:怎么“搞到货”--如何获取时间.日期 //-=-==当前时间------默认显示“0时区”时间 NSDate ...

随机推荐

  1. Md5 签名算法

    /// <summary>        /// MD5签名        /// </summary>        /// <param name="pre ...

  2. Ubuntu Server 15&period;04的安装

    U盘启动工具的制作就跟Windows系统以及Linux各版本的desktop版不同,用的工具也是我第一次见到的“Win32_Disk_Imager”(点击下载) 安装过程请参考:http://www. ...

  3. Android4&period;4 耳机检测分析

    在ALSA架构中,ASOC是由3个部分组成:Platform.CODEC & Machine.而耳机检测一般是在Machine driver里实现,当然也可以在CODEC driver里实现. ...

  4. 解决linux &period;so的链接时符号依赖问题

    问题描述 target: a.out SO:libmyfile.so 依赖描述: a.out: libmyfile.so libmyfile.so:  libssl.so.1.0.0 libssl.s ...

  5. SQL学习之联结表的使用

    1.简介:"联结(join)表"是SQL最强大的功能之一.联结是利用SQL的SELECT能执行的最重要的操作,很好地理解联结及其语法是学习SQL的极为重要的部分! 在能够有效的使用 ...

  6. CodeSmith 生成代码

    使用CodeSmith 生成代码 CodeSmith是一款优秀的代码生成工具.在ORM中,它能帮助我们生成实体类.XML配置文件,从而简化了我们一部分的开发工作.下面简要说说它的基本用法. 1.  打 ...

  7. Mysql 使用CMD 登陆

    1. 将mysql 的bin 目录加到 系统环境变量中: 这样的目的是为了可以直接在cmd中使用mysql命令,而不用先CD到mysql的bin目录中. 3. 连接目标数据库 . Syntax:   ...

  8. linux系统中的时间

    1.编程显示系统时间: #include <stdio.h> #include <time.h> /* gcc -o fix fixedFormatTime.c ./fix * ...

  9. java 常用异常及作用

    先看看图, Exception就明白了 关于异常 大体分为 不可查异常 可查异常 runtimeException三类~异常都继承throwable这个类~ 下面有error和Exception两大类 ...

  10. 使用机器学习检测TLS 恶意加密流——业界调研&ast;&ast;&ast;有开源的数据集,包括恶意证书的,以及恶意tls pcap报文&ast;&ast;&ast;

    2018 年的文章, Using deep neural networks to hunt malicious TLS certificates from:https://techxplore.com ...