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