Linux使用NTP服務透過網路自動或手動同步系統時間教學

本篇文章介紹如何在Linux上使用NTP服務網路校正系統時間,本文使用的執行環境為CentOS 7。以下教學主要分了兩種方式:

  1. 使用ntpdate指令校正
  2. 使用ntp服務校正

為何要對Linux校正呢?因為系統時間對Linux伺服器來說十分重要,時間不準確會產生很多安全性問題或不能預測的問題。所以為了Linux系統維持正確的時間,可以使用NTP這個服務來處理。

使用ntpdate指令校正系統時間

# ntpdate [ntp server domain] 26 Sep 13:34:53 ntpdate[4084]: step time server ip offset 254809.473977 sec

網上有很多提供NTP伺服器的服務商,只要找一個可用的便可。執行完畢後會顯示這樣的結果,建議在校正完後將正確的時間寫入BIOS時鐘。

# hwclock -w

另外,時間久了系統的時間有可以出現徧差,所以可使用crontab指令設定讓系統定期自動校正。這裹的範例是每天的晚上00點00分進行校正,並將時間寫入BIOS時鐘

# crontab -e
# 0 0 * * * root (/usr/sbin/ntpdate [ntp server domain] && /sbin/hwclock -w) &> /dev/null

使用ntp服務校正系統時間

檢查系統是否有安裝ntp服務套件

# yum list installed | grep ntp

如果沒有安裝,使用yum指令進行安裝

# yum install ntp

設定ntp服務

編輯/etc/ntp.conf設定檔,加入想校正的伺服器網域,可以提供多個伺服器網域。

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# 自己指定 NTP 伺服器
server [ntp server domain 1]
server [ntp server domain 2]
server [ntp server domain 3]

啟用ntp服務

修改好設定檔後,使用ntpd指令啟用服務,啟用後查看一下運作狀態。

# systemctl start ntpd
# systemctl status ntpd

若狀態內容 Active 欄位顯示 active (running) 就表示 ntpd 為正常。

最後設置開機自動啟動ntpd服務

# systemctl enable ntpd

發佈留言

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

*

驗證碼 * Time limit is exhausted. Please reload CAPTCHA.

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