Ubuntu

Ubuntu 20.04 時間同步問題和可能不正確的狀態資訊

  • May 7, 2022

我在我的 KVM 主機(Lubuntu 20.04)上遇到了一些崩潰問題,在進行故障排除時,我注意到了一些與時間相關的錯誤。

經過進一步調查,令我驚恐的是,我發現時間沒有同步。我確定它是以前設置的,我不知道它是如何變為未設置的。

admin@virtland:~$ sudo timedatectl
[sudo] password for admin: 
              Local time: Fri 2020-07-10 09:14:14 EDT  
          Universal time: Fri 2020-07-10 13:14:14 UTC  
                RTC time: Fri 2020-07-10 13:14:14      
               Time zone: America/New_York (EDT, -0400)
System clock synchronized: no                           
             NTP service: n/a                          
         RTC in local TZ: no                           
admin@virtland:~$ 

我找到了這個執行緒並嘗試了最佳答案,但無濟於事。 https://askubuntu.com/questions/929805/timedatectl-ntp-sync-cannot-set-to-yes

admin@virtland:~$ sudo systemctl stop ntp
admin@virtland:~$ sudo ntpd -gq
10 Jul 09:17:57 ntpd[34358]: ntpd 4.2.8p12@1.3728-o (1): Starting
10 Jul 09:17:57 ntpd[34358]: Command line: ntpd -gq
10 Jul 09:17:57 ntpd[34358]: proto: precision = 0.070 usec (-24)
10 Jul 09:17:57 ntpd[34358]: leapsecond file ('/usr/share/zoneinfo/leap-seconds.list'): good hash signature
10 Jul 09:17:57 ntpd[34358]: leapsecond file ('/usr/share/zoneinfo/leap-seconds.list'): loaded, expire=2020-12-28T00:00:00Z last=2017-01-01T00:00:00Z ofs=37
10 Jul 09:17:57 ntpd[34358]: Listen and drop on 0 v6wildcard [::]:123
10 Jul 09:17:57 ntpd[34358]: Listen and drop on 1 v4wildcard 0.0.0.0:123
10 Jul 09:17:57 ntpd[34358]: Listen normally on 2 lo 127.0.0.1:123
10 Jul 09:17:57 ntpd[34358]: Listen normally on 3 enp6s0 10.0.0.18:123
10 Jul 09:17:57 ntpd[34358]: Listen normally on 4 lo [::1]:123
10 Jul 09:17:57 ntpd[34358]: Listen normally on 5 enp6s0 [fe80::7285:c2ff:fe65:9f19%3]:123
10 Jul 09:17:57 ntpd[34358]: Listening on routing socket on fd #22 for interface updates
10 Jul 09:17:58 ntpd[34358]: Soliciting pool server 209.50.63.74
10 Jul 09:17:59 ntpd[34358]: Soliciting pool server 4.53.160.75
10 Jul 09:18:00 ntpd[34358]: Soliciting pool server 69.89.207.199
10 Jul 09:18:00 ntpd[34358]: Soliciting pool server 72.30.35.88
10 Jul 09:18:01 ntpd[34358]: Soliciting pool server 173.0.48.220
10 Jul 09:18:01 ntpd[34358]: Soliciting pool server 162.159.200.1
10 Jul 09:18:01 ntpd[34358]: Soliciting pool server 108.61.73.243
10 Jul 09:18:02 ntpd[34358]: Soliciting pool server 208.79.89.249
10 Jul 09:18:02 ntpd[34358]: Soliciting pool server 208.67.75.242
10 Jul 09:18:02 ntpd[34358]: Soliciting pool server 91.189.94.4
10 Jul 09:18:03 ntpd[34358]: Soliciting pool server 91.189.89.198
10 Jul 09:18:03 ntpd[34358]: Soliciting pool server 67.217.112.181
10 Jul 09:18:04 ntpd[34358]: Soliciting pool server 91.189.89.199
10 Jul 09:18:04 ntpd[34358]: Soliciting pool server 64.225.34.103             
10 Jul 09:18:05 ntpd[34358]: Soliciting pool server 91.189.91.157             
10 Jul 09:18:06 ntpd[34358]: Soliciting pool server 2001:67c:1560:8003::c8    
10 Jul 09:18:06 ntpd[34358]: ntpd: time slew +0.001834 s                      
ntpd: time slew +0.001834s                                                    
admin@virtland:~$ sudo service ntp start                                       
admin@virtland:~$ sudo timedatectl                             
              Local time: Fri 2020-07-10 09:18:21 EDT                        
          Universal time: Fri 2020-07-10 13:18:21 UTC                        
                RTC time: Fri 2020-07-10 13:18:21                            
               Time zone: America/New_York (EDT, -0400)                      
System clock synchronized: no                                                 
             NTP service: n/a                                                
         RTC in local TZ: no                                                 
admin@virtland:~$                  

我想也許我需要使用一些最新的指令,所以我嘗試了這個: https ://linuxconfig.org/how-to-sync-time-on-ubuntu-20-04-focal-fossa-linux

admin@virtland:~$ timedatectl set-ntp off
Failed to set ntp: NTP not supported                                          
admin@virtland:~$ timedatectl set-ntp on
Failed to set ntp: NTP not supported                                          

然後我從另一個執行緒嘗試了這個:

admin@virtland:~$ sudo systemctl status systemd-timesyncd.service
[sudo] password for admin: 
● systemd-timesyncd.service
    Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)
    Active: inactive (dead)

我從未接觸過 timesyncd.conf,但無論如何它都被完全註釋掉了:

cat /etc/systemd/timesyncd.conf                         
#  This file is part of systemd.                                              
#                                                                             
#  systemd is free software; you can redistribute it and/or modify it         
#  under the terms of the GNU Lesser General Public License as published by   
#  the Free Software Foundation; either version 2.1 of the License, or        
#  (at your option) any later version.                                        
#                                                                             
# Entries in this file show the compile time defaults.                        
# You can change settings by editing this file.                               
# Defaults can be restored by simply deleting this file.                      
#                                                                             
# See timesyncd.conf(5) for details.                                          
                                                                             
[Time]                                                                        
#NTP=                                                                         
#FallbackNTP=ntp.ubuntu.com                                                   
#RootDistanceMaxSec=5                                                         
#PollIntervalMinSec=32                                                        
#PollIntervalMaxSec=2048 

我再次檢查了 timedatectl,現在它打開了,但仍然沒有使用 NTP。我知道 NTP 更精確,這在某些情況下可能很重要。不確定帶有 pci 直通的虛擬化是否需要非常精確的時間。

從我正在閱讀的其他內容中,我認為 NTP 可能與 timesyncd 衝突。所以暫時去掉ntp:

sudo systemctl stop ntp
sudo apt-get purge ntp

但在清除 ntp 後,NTP 顯示為活動狀態!

admin@virtland:~$ timedatectl
              Local time: Fri 2020-07-10 09:34:52 EDT  
          Universal time: Fri 2020-07-10 13:34:52 UTC  
                RTC time: Fri 2020-07-10 13:34:52      
               Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes                          
             NTP service: active                       
         RTC in local TZ: no     

我要瘋了嗎?NTP 是否還在這裡?沒有。

admin@virtland:~$ sudo systemctl start ntp
Failed to start ntp.service: Unit ntp.service not found.

很抱歉沒有提出更集中的問題,但這裡到底發生了什麼?

我很好,真的迷路了。另外,我稍後會編輯這篇文章,並就刪除 NTP(並因此啟動它?!)是否解決了導致我陷入這個兔子洞的穩定性問題做出了一個否定。

編輯:我做的下一件事是在 timesyncd 上禁用 ntp 並(重新)安裝 NTP,如此處所述。https://www.digitalocean.com/community/tutorials/how-to-set-up-time-synchronization-on-ubuntu-18-04

結果是:

admin@virtland:~$ ntpq -p
    remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
0.us.pool.ntp.o .POOL.          16 p    -   64    0    0.000    0.000   0.000
1.us.pool.ntp.o .POOL.          16 p    -   64    0    0.000    0.000   0.000
2.us.pool.ntp.o .POOL.          16 p    -   64    0    0.000    0.000   0.000
3.us.pool.ntp.o .POOL.          16 p    -   64    0    0.000    0.000   0.000
0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000
admin@virtland:~$ timedatectl
              Local time: Fri 2020-07-10 10:35:39 EDT  
          Universal time: Fri 2020-07-10 14:35:39 UTC  
                RTC time: Fri 2020-07-10 14:35:40      
               Time zone: America/New_York (EDT, -0400)
System clock synchronized: no                                                 
             NTP service: n/a                                                
         RTC in local TZ: no                                                 
admin@virtland:~$ systemctl status systemd-timesyncd 
● systemd-timesyncd.service                                                   
    Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)       
    Active: inactive (dead)                                                  
admin@virtland:~$ nano /etc/ntp.conf
admin@virtland:~$ systemctl status systemd-timesyncd                           
● systemd-timesyncd.service                                                   
    Loaded: masked (Reason: Unit systemd-timesyncd.service is masked.)       
    Active: inactive (dead)                                                  
admin@virtland:~$ ntpstat
unsynchronised                                                                
  polling server every 8 s 

我按照 Michael Hampton 的建議撤銷了這些更改:這是否意味著它正在工作?

boss@virtland:~$ sudo systemctl stop ntp
Failed to stop ntp.service: Unit ntp.service not loaded.
boss@virtland:~$ sudo timedatectl set-ntp yes
boss@virtland:~$ sudo timedatectl set-ntp on
boss@virtland:~$ ntpq -p
bash: /usr/bin/ntpq: No such file or directory
boss@virtland:~$ systemctl status systemd-timesyncd 
● systemd-timesyncd.service - Network Time Synchronization
    Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; >
    Active: active (running) since Fri 2020-07-10 10:49:18 EDT; 50s ago
      Docs: man:systemd-timesyncd.service(8)
  Main PID: 108365 (systemd-timesyn)
    Status: "Initial synchronization to time server 91.189.94.4:123 (ntp.ubu>
     Tasks: 2 (limit: 154317)
    Memory: 1.8M
    CGroup: /system.slice/systemd-timesyncd.service
            └─108365 /lib/systemd/systemd-timesyncd

Jul 10 10:49:17 virtland systemd[1]: Starting Network Time Synchronization...
Jul 10 10:49:18 virtland systemd[1]: Started Network Time Synchronization.
Jul 10 10:49:18 virtland systemd-timesyncd[108365]: Initial synchronization t>
lines 1-14/14 (END)
timedatectl
              Local time: Fri 2020-07-10 10:52:56 EDT  
          Universal time: Fri 2020-07-10 14:52:56 UTC  
                RTC time: Fri 2020-07-10 14:52:56      
               Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes                          
             NTP service: active                       
         RTC in local TZ: no            

所以我想它正在工作。由於讓我走上這條道路的崩潰仍在發生,我想時間不是問題。

當啟用或執行時,該timedatectl命令報告 NTP 正在使用中。這兩個都是適用於大多數伺服器和工作站的輕量級 NTP 客戶端。它不辨識該服務,該服務被設計為既是 NTP 客戶端又是伺服器,現在我們有專用的 NTP 客戶端軟體,不再適合不需要為其他系統提供 NTP 或使用專門的系統的系統GPS等硬體獲取時間。chronyd``systemd-timesyncd``ntpd

引用自:https://serverfault.com/questions/1024770