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)

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
 1 [root@localhost ~]# timedatectl
 2       Local time: Thu 2018-04-05 16:22:59 CST
 3   Universal time: Thu 2018-04-05 08:22:59 UTC
 4         RTC time: Thu 2018-04-05 08:22:59
 5        Time zone: Asia/Shanghai (CST, +0800)
 6      NTP enabled: yes
 7 NTP synchronized: yes
 8  RTC in local TZ: yes
 9       DST active: n/a
10 
11 Warning: The system is configured to read the RTC time in the local time zone.
12          This mode can not be fully supported. It will create various problems
13          with time zone changes and daylight saving time adjustments. The RTC
14          time is never updated, it relies on external facilities to maintain it.
15          If at all possible, use RTC in UTC by calling
16          'timedatectl set-local-rtc 0'.
17 [root@localhost ~]# timedatectl status
18       Local time: Thu 2018-04-05 16:23:08 CST
19   Universal time: Thu 2018-04-05 08:23:08 UTC
20         RTC time: Thu 2018-04-05 08:23:08
21        Time zone: Asia/Shanghai (CST, +0800)
22      NTP enabled: yes
23 NTP synchronized: yes
24  RTC in local TZ: yes
25       DST active: n/a
26 
27 Warning: The system is configured to read the RTC time in the local time zone.
28          This mode can not be fully supported. It will create various problems
29          with time zone changes and daylight saving time adjustments. The RTC
30          time is never updated, it relies on external facilities to maintain it.
31          If at all possible, use RTC in UTC by calling
32          '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 
 1 [root@localhost ~]# grep -Ev "^$|^#" /etc/chrony.conf
 2 # 該參數可以多次用於添加時鍾服務器,必須以"server "格式使用。一般而言,你想添加多少服務器,就可以添加多少服務器。
 3 server 0.centos.pool.ntp.org iburst
 4 server 1.centos.pool.ntp.org iburst
 5 server 2.centos.pool.ntp.org iburst
 6 server 3.centos.pool.ntp.org iburst
 7 # stratumweight指令設置當chronyd從可用源中選擇同步源時,每個層應該添加多少距離到同步距離。默認情況下,設置為0,讓chronyd在選擇源時忽略源的層級。
 8 stratumweight 0
 9 # chronyd程序的主要行為之一,就是根據實際時間計算出計算機增減時間的比率,將它記錄到一個文件中是最合理的,它會在重啟后為系統時鍾作出補償,甚至可能的話,會從時鍾服務器獲得較好的估值。
10 driftfile /var/lib/chrony/drift
11 # rtcsync指令將啟用一個內核模式,在該模式中,系統時間每11分鍾會拷貝到實時時鍾(RTC)。
12 rtcsync
13 # 通常,chronyd將根據需求通過減慢或加速時鍾,使得系統逐步糾正所有時間偏差。在某些特定情況下,系統時鍾可能會漂移過快,導致該調整過程消耗很長的時間來糾正系統時鍾。
14 # 該指令強制chronyd在調整期大於某個閥值時步進調整系統時鍾,但只有在因為chronyd啟動時間超過指定限制(可使用負值來禁用限制),沒有更多時鍾更新時才生效。
15 makestep 10 3
16 # 這里你可以指定一台主機、子網,或者網絡以允許或拒絕NTP連接到扮演時鍾服務器的機器。
17 #allow 192.168/16
18 # 該指令允許你限制chronyd監聽哪個網絡接口的命令包(由chronyc執行)。該指令通過cmddeny機制提供了一個除上述限制以外可用的額外的訪問控制等級。
19 bindcmdaddress 127.0.0.1
20 bindcmdaddress ::1
21 keyfile /etc/chrony.keys
22 # 指定了/etc/chrony.keys中哪一條密碼被使用
23 commandkey 1
24 # 此參數指定了產生一個SHA1或MD5加密的密碼,存放在/etc/chrony.keys中
25 generatecommandkey
26 noclientlog
27 logchange 0.5
28 logdir /var/log/chrony
29 
30 作者:無限小BUG
31 鏈接:https://www.jianshu.com/p/fb32239ccf2b
32 來源:簡書
33 著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
說明

 

  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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM