Linux – timedatectl & chronyd (systemd)

Loading

測試作業系統 CentOS 8 x86_64 (虛擬機)

目前 Linux 上的時間是透過 timedatectl 與 chronyd 這兩個服務,下面來看一下用法.

timedatectl

timedatectl 指令是由 systemd 套件提供.

[root@localhost ~]# rpm -qf /usr/bin/timedatectl
systemd-239-39.el8.x86_64

之前通常會使用 date 來修改時間,不過在 CentOS 7 / CentOS 8 使用 systemd 的系統,建議使用 timedatectl 來設定與檢視日期時間.

[root@localhost ~]# timedatectl 
      Local time: Thu 2019-08-08 11:49:20 CST
  Universal time: Thu 2019-08-08 03:49:20 UTC
        RTC time: Thu 2019-08-08 03:49:20
       Time zone: Asia/Taipei (CST, +0800)
     NTP enabled: no
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

設定日期時間.

[root@localhost ~]# timedatectl set-time "2019-08-08 11:50:00"

設定時區 (TimeZone).

[root@localhost ~]# timedatectl list-timezones | grep -i taipei
Asia/Taipei
[root@localhost ~]# timedatectl set-timezone "Asia/Taipei"
[root@localhost ~]# timedatectl 
      Local time: Thu 2019-08-08 11:55:49 CST
  Universal time: Thu 2019-08-08 03:55:49 UTC
        RTC time: Thu 2019-08-08 03:55:49
       Time zone: Asia/Taipei (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

改時區需把 rsyslog – https://benjr.tw/34103 日誌 log 服務重啟,避免時間與 log 紀錄的時間不相同.

[root@localhost ~]# systemctl restart rsyslog

chronyd

從 RHEL / CentOS 7 之後使用 Chrony 為預設校時服務 (NTP :Network Time Protocol ) ,預設應該都有安裝, chrony 套件如下.

[root@localhost ~]# yum install chrony

timedatectl 可以設定 NTP (Network Time Protocol) 使用網路校時.

[root@localhost ~]# timedatectl set-ntp yes 
[root@localhost ~]# timedatectl 
      Local time: Thu 2019-08-08 11:54:53 CST
  Universal time: Thu 2019-08-08 03:54:53 UTC
        RTC time: Thu 2019-08-08 03:54:00
       Time zone: Asia/Taipei (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
  1. NTP Client
    當我們設定 NTP (Network Time Protocol) 為 Yes 時,會把 chronyd 這個 NTP 服務打開.

    [root@localhost ~]# cat /var/log/messages
    Mar 12 19:21:18 localhost dbus-daemon[921]: [system] Activating via systemd: service name='org.freedesktop.timedate1' unit='dbus-org.freedesktop.timedate1.service' requested by ':1.199' (uid=0 pid=2374 comm="timedatectl set-ntp yes ")
    Mar 12 19:21:18 localhost systemd[1]: Starting System clock and RTC settings service...
    Mar 12 19:21:18 localhost dbus-daemon[921]: [system] Successfully activated service 'org.freedesktop.timedate1'
    Mar 12 19:21:18 localhost systemd[1]: Started System clock and RTC settings service.
    Mar 12 19:21:18 localhost systemd[1]: Starting NTP client/server...
    Mar 12 19:21:18 localhost systemd[1]: Reloading.
    Mar 12 19:21:18 localhost chronyd[2400]: chronyd version 3.5 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
    Mar 12 19:21:18 localhost chronyd[2400]: Initial frequency 100000.000 ppm
    Mar 12 19:21:18 localhost chronyd[2400]: Using right/UTC timezone to obtain leap second data
    Mar 12 19:21:18 localhost systemd[1]: Started NTP client/server.
    Mar 12 19:21:24 localhost chronyd[2400]: Selected source 103.122.188.3
    Mar 12 11:38:38 localhost chronyd[2400]: System clock TAI offset set to 37 seconds
    Mar 12 11:38:38 localhost chronyd[2400]: System clock wrong by -27765.715096 seconds, adjustment started
    Mar 12 11:38:38 localhost chronyd[2400]: System clock was stepped by -27765.715096 seconds
    Mar 12 11:38:38 localhost rsyslogd[1240]: imjournal: journal reloaded... [v8.37.0-13.el8 try http://www.rsyslog.com/e/0 ]
    Mar 12 11:38:40 localhost chronyd[2400]: Selected source 183.177.72.202
    Mar 12 11:38:49 localhost chronyd[2400]: Source 2406:2000:fc:437::2000 replaced with 106.104.162.193
    Mar 12 11:38:59 localhost chronyd[2400]: Selected source 106.104.162.193
    Mar 12 11:38:59 localhost chronyd[2400]: System clock wrong by 1.888458 seconds, adjustment started
    Mar 12 11:38:59 localhost chronyd[2400]: System clock was stepped by 1.888390 seconds
    

    也可以透過 systemctl 來顯示 chronyd.service 的狀態.

    [root@localhost ~]# systemctl status chronyd.service
    ● chronyd.service - NTP client/server
       Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
       Active: active (running) since Thu 2020-03-12 19:21:18 CST; 7h left
         Docs: man:chronyd(8)
               man:chrony.conf(5)
      Process: 2406 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
      Process: 2379 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
     Main PID: 2400 (chronyd)
        Tasks: 1
       Memory: 1.9M
       CGroup: /system.slice/chronyd.service
               └─2400 /usr/sbin/chronyd
    

    設定檔位於 /etc/chrony.conf ,或是透過下面指令來檢視其狀態.

    • tracking : The tracking command displays parameters about the system’s clock performance.
      [root@localhost ~]# chronyc tracking
      Reference ID    : B7B148CA (t2.time.tw1.yahoo.com)
      Stratum         : 3
      Ref time (UTC)  : Thu Mar 12 03:43:18 2020
      System time     : 0.118893698 seconds slow of NTP time
      Last offset     : +0.091888450 seconds
      RMS offset      : 1.505703568 seconds
      Frequency       : 100000.000 ppm fast
      Residual freq   : -17442.777 ppm
      Skew            : 1000000.000 ppm
      Root delay      : 0.048257317 seconds
      Root dispersion : 19.612304688 seconds
      Update interval : 68.3 seconds
      Leap status     : Normal
      
    • sources : This command displays information about the current time sources that chronyd is accessing.
      [root@localhost ~]# chronyc sources
      210 Number of sources = 4
      MS Name/IP address         Stratum Poll Reach LastRx Last sample               
      ===============================================================================
      ^* t2.time.tw1.yahoo.com         2   6   377    40    +98ms[ +190ms] +/-   35ms
      ^- zimbra.alqualonde.org         3   6   377    40   +102ms[ +102ms] +/-  155ms
      ^- 103.122.188.3                 2   6   377    40    +99ms[  +99ms] +/-   51ms
      ^- 106-104-162-193.adsl.sta>     2   6   375    45    +95ms[  +95ms] +/-   84ms
      
  2. NTP Server (IP: 192.168.31.132)
    當 chronyd.service 服務啟動後,其他電腦就可以把這一台當作 NTP(Network Time Protocol) 伺服器,需調整 chrony.conf 設定檔(須設定哪些網段可以使用).

    [root@localhost ~]# cat /etc/chrony.conf
    # Allow NTP client access from local network.
    allow 192.168.0.0/16
    

    這一行設定允許特定網段的使用者把你當做時間伺服器.

    如果你的 Chrony (NTP Server) 是在內部無法對外,這樣還能當 NTP Server 嗎? 找了一下資料 https://serverfault.com/questions/995101/how-to-synchronize-clocks-on-servers-with-no-internet-access 設定如下.

    [root@localhost ~]# cat /etc/chrony.conf
    # Record the rate at which the system clock gains/losses time.
    driftfile /var/lib/chrony/drift
    # Serve time even if not synchronized to a time source.
    local stratum 10
    manual
    

    說明:

    • driftfile /var/lib/chrony/drift
      儲存本地端主機與上層 NTP Server 的誤差 (單位 : PPM – Parts Per Million , 1 PPM (Part Per Million) -> 1 microsecond per second -> 1*60*60/1000 (m) = 3.6ms per hour -> 3.6ms*24= 86.4ms per day)
    • local stratum 8
      NTP是階層架構 (stratum) ,把服務設定在 local 階層架構 (stratum) 8 .
    • manual
      ?
  3. NTP Client (Internal chronyd)
    要使用剛剛設定好的 NTP Server (chronyd IP: 192.168.31.132 ), Client 須把設定檔 /etc/chrony.conf 指定為這一台 IP.

    [root@localhost ~]# vi /etc/chrony.conf
    # Use public servers from the pool.ntp.org project.
    # Please consider joining the pool (http://www.pool.ntp.org/join.html).
    # pool 2.centos.pool.ntp.org iburst
    server 192.168.31.132 iburst
    [root@localhost ~]# systemctl restart chronyd
    

    測試一下.

    [root@localhost ~]# chronyc -a makestep
    200 OK
    

    測試時 Source state 必須為 ‘*’ ( current best )

    [root@localhost ~]# chronyc sources -v
    
      .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
     / .- Source state '*' = current best, '+' = combined, '-' = not combined,
    | /             'x' = may be in error, '~' = too variable, '?' = unusable.
    ||                                                 .- xxxx [ yyyy ] +/- zzzz
    ||      Reachability register (octal) -.           |  xxxx = adjusted offset,
    ||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
    ||                                \     |          |  zzzz = estimated error.
    ||                                 |    |           \
    MS Name/IP address         Stratum Poll Reach LastRx Last sample
    ===============================================================================
    ^* 192.168.31.132            3   6   377    52   +132us[ +143us] +/-   36ms
    
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料