天天看點

作業系統時間不同步導緻無法連接配接阿裡雲源

作者:海淘世界

打開虛拟機進入AlmaLinux,使用 dnf install mysql安裝mysql資料庫,突然彈出如下所示的錯誤:

# dnf module list mysql
AlmaLinux 8 - BaseOS                                                                                                                         0.0  B/s |   0  B     00:10
Errors during downloading metadata for repository 'baseos':
  - Curl error (60): Peer certificate cannot be authenticated with given CA certificates for https://mirrors.almalinux.org/mirrorlist/8/baseos [SSL certificate problem: certificate is not yet valid]
Error: Failed to download metadata for repo 'baseos': Cannot prepare internal mirrorlist: Curl error (60): Peer certificate cannot be authenticated with given CA certificates for https://mirrors.almalinux.org/mirrorlist/8/baseos [SSL certificate problem: certificate is not yet valid]           

頓時蒙了,使用ping檢查一下網絡,一切正常,哪裡出錯了呢?隻能搜尋一下,可能是虛拟機的系統時間不對導緻的。可能是因為虛拟機沒有電池,無法記住時間,是以需要同步時間,centos8 和 rocky linux 8 預設不帶ntpdate,可以使用chrony進行時間同步,chrony也是一個ntp協定的實作,預設安裝。

作業系統時間不同步導緻無法連接配接阿裡雲源

首先使用 timedatectl 查詢一下系統時鐘。

timedatectl指令可以查詢和更改系統時鐘和設定,你可以使用此指令來設定或更改目前的日期,時間和時區,或實作與遠端NTP伺服器的自動系統時鐘同步。
# timedatectl
Warning: The system is configured to read the RTC time in the local time zone.

This mode cannot 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'.
// 關閉硬體時鐘校準
# timedatectl set-local-rtc 0           

根據輸出結果,系統啟用了硬體校準時間,可是虛拟機沒有硬體時鐘,根據提示輸入指令 timedatectl set-local-rtc 0 關閉硬體校準時間。

再次運作 timedatectl,輸出結果中的 RTC in local TZ: no 表示硬體時鐘已經關閉。

# timedatectl
               Local time: Sat 2022-11-26 05:17:00 EST
           Universal time: Sat 2022-11-26 10:17:00 UTC
                 RTC time: Sat 2022-11-26 10:17:00
                Time zone: America/New_York (EST, -0500)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no           

如果輸出結果中的NTP service服務處于不活動狀态,那麼可以使用下面的指令激活它

timedatectl set-ntp true           

此時運作 dnf install mysql 應該正常了,如果不正常,那麼重新開機chrony服務或者等一會。

chrony是一個ntp協定的實作程式,既可以當做服務端,也可以充當用戶端;它專為間歇性網際網路連接配接的系統而設計,當然也能良好應用于持久網際網路連接配接的環境;chrony有三個時間參考:硬體時鐘、實時時鐘以及手動同步。

RHEL7中預設使用chrony作為時間伺服器,也支援NTP,需要額外安裝。NTP與chrony不能同時存在,隻能用其中一個,并将另一個mask掉。

還可以在chrony配置檔案中添加 阿裡雲和國家授時中心,這樣校時更快。

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.pool.ntp.org iburst
//添加阿裡雲和國家授時中心
server ntp.aliyun.com iburst
server ntp.ntsc.ac.cn iburst           

重新開機服務

systemctl restart chronyd           

檢視服務

# chronyc sourcestats -v           

使用timedatectl還可以将時區設定為亞洲上海。

timedatectl set-timezone Asia/Shanghai           

繼續閱讀