在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. JAVA设计模式——开篇

    设计模式很重要,重要性我就不再复述了.最主要的是,通常我们在写一定量代码后,常用的方法什么的都熟悉后,想再提高代码能力,我找到的最好的方法还是去学习,理解设计模式.不理解设计模式,看一些开源框架和ja ...

  2. JSON WEB TOKEN(JWT)的分析

    JSON WEB TOKEN(JWT)的分析 一般情况下,客户的会话数据会存在文件中,或者引入redis来存储,实现session的管理,但是这样操作会存在一些问题,使用文件来存储的时候,在多台机器上 ...

  3. Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境

    好记性不如烂笔头! 一.简介 一直以来,微软只对自家平台提供.NET支持,这样等于让这个“理论上”可以跨平台的框架在Linux和macOS上的支持只能由第三方项目提供(比如Mono .NET).直到微 ...

  4. 从零学习Fluter(五):Flutter中手势滑动拖动已经网络请求

    从六号开始搞Flutter,到今天写这篇blog已经过了4天时间,文档初步浏览了一遍,写下了这个demo.demo源码分享在github上,现在对flutter有种说不出的喜欢了.大家一起搞吧! 废话 ...

  5. Vue一个案例引发的递归组件的使用

    今天我们继续使用 Vue 的撸我们的实战项目,只有在实战中我们才会领悟更多,光纸上谈兵然并卵,继上篇我们的<Vue一个案例引发的动态组件与全局事件绑定总结> 之后,今天来聊一聊我们如何在项 ...

  6. ASP.NET中弹出消息框的几种常见方法

    在ASP.NET网站开发中,经常需要使用到alert消息框,尤其是在提交网页的时候,往往需要在服务器端对数据进行检验,并给出提示或警告. 这里,仅介绍几种不同的实现方法. 1.众所周知的方法是采用如下 ...

  7. SQLServer之创建分区视图

    分区视图定义 分区视图是通过对成员表使用 UNION ALL 所定义的视图,这些成员表的结构相同,但作为多个表分别存储在同一个 SQL Server实例中,或存储在称为联合数据库服务器的自主 SQL ...

  8. win10系统的“USB选择性暂停设置”怎么打开

    Win10系统自带的“USB选择性暂停设置”功能开启后会帮助我们节省电源,这一项功能对于笔记本来说用处很大.那么怎样才能打开这一功能呢?下面小编就来告诉大家打开“USB选择性暂停设置”功能的方法. 1 ...

  9. Gitlab利用Webhook实现Push代码后的jenkins自动构建

    之前部署了Gitlab的代码托管平台和Jenkins的代码发布平台.通常是开发后的代码先推到Gitlab上管理,然后在Jenkins里通过脚本构建代码发布.这种方式每次在发版的时候,需要人工去执行je ...

  10. 最小化spring XML配置,Spring提供了4种自动装配策略。

    1.ByName自动装配:匹配属性的名字 在配置文件中的写法: <bean name="course" class="course类的全包名">&l ...