方案一:使用 date 命令調整時間
date #查看當前系統時間和日期
date -s 02/21/2019 #設定日期,例如將系統日期設定成 2019 年 02 月 21 日
date -s 19:21:45 #設定時分秒,例如將系統時間設定成 19 點 21 分 45 秒
hwclock -w #將當前時間和日期寫入 BIOS,即設定為硬體時間,避免重啟後失效
注意:手動設定時間會存在一定的誤差,所以調整各個系統時間時,建議參照秒錶設定,儘量降低誤差值。
方案二:使用 NTP 調整系統時間
NTP(Network Time Protocol,網路時間協定),是用來使計算機時間同步化的一種協定,它可以使計算機對其伺服器或時鐘源(如石英鐘,GPS 等等)做同步化,並提供高精準度的時間校正(LAN 上與標準間差小於1毫秒,WAN 上幾十毫秒),且可透過加密確認的方式來防止惡毒的協定攻擊。
1. 檢查 ntp 是否已安裝
rpm -qa | grep ntp #檢查命令
若只有 ntpdate 而未見 ntp,則需刪除原有 ntpdate:
rpm -e ntpdate-4.2.6p5-22.el7.x86_64 #刪除 ntpdate
2. 網路連結環境——用ntpdate自動更新系統時間
2.1 採用微軟的校時伺服器調整系統時間
yum install -y ntp #安裝 ntp 伺服器
ntpdate time.windows.com #採用微軟的校時伺服器調整系統時間
出現如下圖所示內容,說明已經同步成功了。
2.2 設定自動同步,同步頻率:每十分鐘同步一次
crontab -e #編輯crontab
*/10 * * * * /usr/sbin/ntpdate time.windows.com >> /tmp/crontab.log
3. 內網環境——用 ntp 搭建自己的時間伺服器
3.1 安裝ntp
範例環境:主節點 IP-192.168.61.250,子節點 IP-192.168.61.251。叢集下各個節點均需要進行安裝,RedHat&CentOS 的系統可直接使用以下 rpm 包:
autogen-libopts-5.18-5.el7.x86_64.rpm
ntpdate-4.2.6p5-25.el7.centos.x86_64.rpm
ntp-4.2.6p5-25.el7.centos.x86_64.rpm
下載安裝包後,上傳到伺服器指定目錄,例如上傳到/usr/ntp目錄下,然後執行以下安裝命令:
cd /usr/ntp
rpm -Uvh *.rpm --nodeps --force #一鍵安裝目錄下所有的 rpm 包
3.2 修改所有節點的 /etc/ntp.conf
vi /etc/ntp.conf
restrict 192.168.6.2 mask 255.255.255.0 nomodify notrap #找到 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 並去掉註釋#修改其IP地址
# 192.168.6.2 為範例,請按照實際叢集所在網段的網關IP填寫,255.255.255.0 為子網遮罩,查詢網段網關的命令為 IP route show,查詢結果中default via 後面的 IP 即是網關 IP
3.3 配置 ntp 伺服器
選擇一個節點作為主節點即 ntp 伺服器,修改其/etc/ntp.conf
vi /etc/ntp.conf
#註釋掉 server 0 ~ n,並在 server 部分新增 server 和 fudge行
#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
server 127.127.1.0 #使用本地時鐘作為ntp伺服器時間,這裏的 127.127.1.0 在 ntp 中代表本機
fudge 127.127.1.0 stratum 10 #127.127.1.0 為第10層,ntp 和127.127.1.0同步完後,就變成了 11 層
3.4 配置ntp用戶端
其他節點作為子節點,需要配置 ntp 用戶端,修改其/etc/ntp.conf
vi /etc/ntp.conf
註釋掉 server 0 ~ n,並在 server 部分新增 server 行,如下:
#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
server 192.168.61.250 #使用主節點 192.169.61.250 的時間作為基準更新時間
3.5 ntp 啟動命令
1)主節點啟動命令
service ntpd start #開啟 ntpd 服務
chkconfig ntpd on #設定開機啟動
2)子節點啟動命令
ntpdate 192.168.61.250 #向主節點傳送請求,同步時間
service ntpd start #開啟 ntpd 服務
chkconfig ntpd on #設定開機啟動
3)查看 ntp 伺服器有無和上層 ntp 連通
使用 ntpstat 行查詢,結果一般如下:
4)查看 ntp 伺服器與上層 ntp 的狀態
使用 ntpq -p 進行查詢,結果一般如下:
4. 維運知識
4.1 常用命令
service ntpd status #查看 ntpd 服務狀態
service ntpd start #啟動 ntpd 服務
service ntpd stop #停止 ntpd 服務
service ntpd restart #重啟 ntpd 服務
4.2 檢查同步是否成功
查看與時間同步伺服器的時間偏差,offset 偏差小,同步成功。
5. FAQ文檔
5.1 內網環境安裝後,使用 ntpstat 查看 ntp 伺服器有無和上層 ntp 連通時,可能會出現,如下情況:
1)unsynchronised time server re-starting polling server every 8 s
2)unsynchronised polling server every 8 s
這種情況屬於正常,ntp 伺服器配置完畢後,需要等待 3~5 分鐘才能與/etc/ntp.conf中配置的標準時間進行同步,等一段時間之後,再次使用 ntpstat 命令查看狀態,就會變成正常結果。
5.2 ntp 啟動命令,啟動完主節點,再啟動子節點時,可能出現如下情況 no server suitable for synchronization found:
在 ntp server上重新啟動ntp服務後,ntp server 自身或者與其 server 的同步的需要一個時間段,大約三五分鐘,在這個時間之內在用戶端運作 ntpdate 命令時會產生 no server suitable for synchronization found 的錯誤。
5.3 手動更改主節點的時間,重啟 ntp,子節點時間不同步,存在 3~5 分鐘的生效時間。