CentOS 7 设置日期和时间

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

timedatectl

[root@ls236 ~]# timedatectl
Local time: 日 2022-06-05 00:40:37 CST
Universal time: 六 2022-06-04 16:40:37 UTC
RTC time: 日 2022-06-05 00:40:36
Time zone: Asia/Beijing (CST, +0800)
NTP enabled: no
NTP synchronized: no
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@ls236 ~]# timedatectl set-ntp no
[root@ls236 ~]# timedatectl set-time "2020-06-05 17:01:37"
[root@ls236 ~]# timedatectl set-ntp yes
[root@ls236 ~]# timedatectl
Local time: 五 2020-06-05 17:01:45 CST
Universal time: 五 2020-06-05 09:01:45 UTC
RTC time: 五 2020-06-05 17:01:46
Time zone: Asia/Beijing (CST, +0800)
NTP enabled: yes
NTP synchronized: no
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@ls236 ~]#

UTC+8 =北京时间

一、基本概念

1.1 GMT、UTC、CST、DST 时间

(1) UTC

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

(2) GMT

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

(3) CST

中国标准时间 (China Standard Time)

1

GMT + 8 = UTC + 8 = CST

(4) DST

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

1.2 硬件时钟和系统时钟

(1) 硬件时钟

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

(2) 系统时钟

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

二、timedatectl 命令

2.1 读取时间

1

timedatectl //等同于 timedatectl status

2.2 设置时间

1

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

2.3 列出所有时区

1

timedatectl list-timezones

2.4 设置时区

1

timedatectl set-timezone Asia/Shanghai

2.5 是否NTP服务器同步

1

timedatectl set-ntp yes //yes或者no

2.6 将硬件时钟调整为与本地时钟一致

1

2

timedatectl set-local-rtc 1

hwclock --systohc --localtime //与上面命令效果一致

注意 硬件时钟默认使用UTC时间,因为硬件时钟不能保存时区和夏令时调整,修改后就无法从硬件时钟中读取出准确标准时间,因此不建议修改。修改后系统会出现警告。

2.6 硬件时间设置成 UTC:

1

2

timedatectl set-local-rtc 1

hwclock --systohc --utc //与上面命令效果一致

三、设置系统时间为中国时区并启用NTP同步

1

2

3

4

5

6

yum install ntp //安装ntp服务

systemctl enable ntpd //开机启动服务

systemctl start ntpd //启动服务

timedatectl set-timezone Asia/Shanghai //更改时区

timedatectl set-ntp yes //启用ntp同步

ntpq -p //同步时间

如需更改时间服务器, 修改 /etc/ntp.conf 文件中的服务器地址 server 即可.

====================================================================

解决Failed to set time: Automatic time synchronization is enabled错误

关闭同步

timedatectl set-ntp no 
[root@ match]# timedatectl set-time 2018-11-13

date 
Tue Nov 13 00:00:26 UTC 2018

开启同步

timedatectl set-ntp yes

CentOS 7 设置日期和时间 timedatectl的更多相关文章

  1. CentOS 7 设置日期和时间

    现代操作系统分为以下两种类型的时钟: 实时时钟(Real-Time Clock,RTC),通常称为硬件时钟(一般是系统主板上的集成电路),它完全独立于操作系统的当前状态,即使在计算机关闭时也能运行. ...

  2. CentOS 7: 设置时区和时间

    查看当前时区和时间 $ date $ ls -l /etc/localtime 查看所有可用时区 $ timedatectl list-timezones | grep Asia 设置时区 $ tim ...

  3. Centos 7 修改日期和时间的命令

    timedatectl set-ntp no //关闭时间动态更新timedatectl set-time "YYYY-MM-DD HH:MM:SS" //设置时间和日期timed ...

  4. CentOS 7 设置时区、日期和时间

    CentOS 7 设置时区.日期和时间 changhr2013关注 2019.04.19 01:33:09字数 307阅读 139 在 CentOS 7 中,引入了一个叫 timedatectl 的设 ...

  5. Centos date 设置自定义时间

    [1]手动修改 (1)设置日期 # date -s 20190315 (2)设置时间 # date -s 15:23:34 (3)设置日期和时间 # date -s "20190315 15 ...

  6. Centos 7设置静态IP,修改时区,关闭防火墙

    Centos 7设置静态IP # vi /etc/sysconfig/network-scripts/ifcfg-enxxx BOOTPROTO="static" ...... I ...

  7. 【转】Pro Android学习笔记(十七):用户界面和控制(5):日期和时间控件

    目录(?)[-] DatePicker和TimePicker控件 DigitalClock和AnalogClock控件 DatePicker和TimePicker控件 使用DatePicker和Tim ...

  8. matlab中datest() 将日期和时间转换为字符串格式

    来源:https://ww2.mathworks.cn/help/matlab/ref/datestr.html?searchHighlight=datestr&s_tid=doc_srcht ...

  9. CentOS下date命令 - 显示和设置系统日期与时间

    显示系统日期 要显示系统日期,只要输入: $ date Thu Dec 5 22:55:41 WIB 2013 格式化显示日期 日期有很多格式.如果你不喜欢默认的格式,你可以换一种格式.你可能会想&q ...

随机推荐

  1. 创建线程的方式三:实现Callable接口 --- JDK 5.0新增

    /** * 创建线程的方式三:实现Callable接口. --- JDK 5.0新增 * * * 如何理解实现Callable接口的方式创建多线程比实现Runnable接口创建多线程方式强大? * 1 ...

  2. JWT 介绍 - Step by Step

    翻译自 Mohamad Lawand 2021年3月11日的文章 <Intro to JWT - Step by Step> [1] 在本文中,我将向您介绍 JWT[2]. 我们今天要讲的 ...

  3. 解决SQLPLUS无法使用上下箭头

    1 问题描述 SQLPLUS中使用上下箭头无法获取历史命令,如下图所示: 按上下箭头会显示^[[A/^[[B. 2 解决方案 需要安装rlwrap,可以的话可以用包管理器安装,笔者环境CentOS,这 ...

  4. 记canvas画笔笔迹的多次优化过程

    我们的项目是面向学校老师的教学软件,所以肯定少不了互动白板的功能,而这个里面的画笔功能是由我来开发的,下面介绍这个过程中遇到的问题以及解决方法. 首先给大家明确下由于软件中的画布可以自由移动,会超出屏 ...

  5. 解决github不能访问的问题

    亲测有效,授之以鱼不如授之以渔,网上看了很多方法,也试着做了,很多都是治标不治本,最后找到个靠谱的方式:利用DNS查询工具,找到最快的IP地址,然后把host地址换成查询到的结果,方法如下: 在系统的 ...

  6. 1443. Minimum Time to Collect All Apples in a Tree

    Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in t ...

  7. python正则表达式基本语法

    一.最常用的匹配语法 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回 re.split  以匹配到的字符当做列表分隔符 r ...

  8. php抽象类,接口,特性的比较

    php抽象类 抽象方法必须被子类继承实现,所以不能为私有,只能是受保护的或公有的; 抽象类子类的方法访问控制级别必须和抽象类相等或更宽松.例如,父类的抽象方法是受保护的,子类实现时则必须为受保护的或者 ...

  9. 【Springboot】Springboot监听器Demo

    /** * @author: yq * @date: 2020/8/31 0:01 * @description 自定义事件 */ @Data public class MyEvent extends ...

  10. dalvik浅析三:类加载

    android的安装包是个apk文件,其中包含dex.资源及签名文件.其中dex是包含程序运行的类代码,而android是运行在dalvik(5.0之前)上的.本篇我们就来看下dalvik是如何把de ...