ubuntu自動同步時間

方法一:選擇時區(qū)
1.執(zhí)行命令:
dpkg-reconfigure tzdata
2.選擇Asia->Shanghai

  1. 防止系統(tǒng)重啟后時區(qū)改變,執(zhí)行下面一行命令
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

方法二:網(wǎng)上同步時間

  1. 安裝ntpdate工具
    sudo apt-get install ntpdate

方法三:在 Ubuntu 上設置時間同步
通常,我們在安裝時設置時區(qū)。但是,你可以根據(jù)需要更改或設置不同的時區(qū)。

首先,讓我們使用 date 命令查看 Ubuntu 系統(tǒng)中的當前時區(qū):

$ date

示例輸出:

Tue Jul 30 11:47:39 UTC 2019

如上所見,date 命令顯示實際日期和當前時間。這里,我當前的時區(qū)是 UTC,代表協(xié)調世界時。

或者,你可以在 /etc/timezone 文件中查找當前時區(qū)。

$ cat /etc/timezone
UTC

現(xiàn)在,讓我們看看時鐘是否與互聯(lián)網(wǎng)同步。只需運行:

$ timedatectl

示例輸出:

Local time: Tue 2019-07-30 11:53:58 UTC
Universal time: Tue 2019-07-30 11:53:58 UTC
RTC time: Tue 2019-07-30 11:53:59
Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no

如你所見,timedatectl 命令顯示本地時間、世界時、時區(qū)以及系統(tǒng)時鐘是否與互聯(lián)網(wǎng)服務器同步,以及 systemd-timesyncd.service 是處于活動狀態(tài)還是非活動狀態(tài)。就我而言,系統(tǒng)時鐘已與互聯(lián)網(wǎng)時間服務器同步。

如果時鐘不同步,你會看到顯示的時間同步已禁用。

System clock synchronized: no。

如果你看到 System clock synchronized: 值設置為 no,那么 timesyncd 服務可能處于非活動狀態(tài)。因此,只需重啟服務并看下是否正常。

$ sudo systemctl restart systemd-timesyncd.service

現(xiàn)在檢查 timesyncd 服務狀態(tài):

$ sudo systemctl status systemd-timesyncd.service
 systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-07-30 10:50:18 UTC; 1h 11min ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 498 (systemd-timesyn)
Status: "Synchronized to time server [2001:67c:1560:8003::c7]:123 (ntp.ubuntu.com)."
Tasks: 2 (limit: 2319)
CGroup: /system.slice/systemd-timesyncd.service
└─498 /lib/systemd/systemd-timesyncd

Jul 30 10:50:30 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:31 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:31 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:32 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:32 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
Jul 30 10:51:06 ubuntuserver systemd-timesyncd[498]: Synchronized to time server [2001:67c:1560:800

如果此服務已啟用并處于活動狀態(tài),那么系統(tǒng)時鐘應與互聯(lián)網(wǎng)時間服務器同步。

你可以使用命令驗證是否啟用了時間同步:

$ timedatectl

如果仍然不起作用,請運行以下命令以啟用時間同步:

$ sudo timedatectl set-ntp true

現(xiàn)在,你的系統(tǒng)時鐘將與互聯(lián)網(wǎng)時間服務器同步。

使用 timedatectl 命令更改時區(qū)

如果我想使用 UTC 以外的其他時區(qū)怎么辦?這很容易!

首先,使用命令列出可用時區(qū):

$ timedatectl list-timezones

你將看到類似于下圖的輸出。

使用 timedatectl 命令列出時區(qū)

你可以使用以下命令設置所需的時區(qū)(例如,Asia/Shanghai):

(LCTT 譯注:本文原文使用印度時區(qū)作為示例,這里為了便于使用,換為中國標準時區(qū) CST。另外,在時區(qū)設置中,要注意 CST 這個縮寫會代表四個不同的時區(qū),因此建議使用城市和 UTC+8 來說設置。)

$ sudo timedatectl set-timezone Asia/Shanghai
使用 date 命令再次檢查時區(qū)是否已真正更改:

$ date
Tue Jul 30 20:22:33 CST 2019
或者,如果需要詳細輸出,請使用 timedatectl 命令:

$ timedatectl
Local time: Tue 2019-07-30 20:22:35 CST
Universal time: Tue 2019-07-30 12:22:35 UTC
RTC time: Tue 2019-07-30 12:22:36
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no
如你所見,我已將時區(qū)從 UTC 更改為 CST(中國標準時間)。()

要切換回 UTC 時區(qū),只需運行:

$ sudo timedatectl set-timezone UTC
使用 tzdata 更改時區(qū)
在較舊的 Ubuntu 版本中,沒有 timedatectl 命令。這種情況下,你可以使用 tzdata(Time zone data)來設置時間同步。

$ sudo dpkg-reconfigure tzdata
選擇你居住的地理區(qū)域。對我而言,我選擇 Asia。選擇 OK,然后按回車鍵。

接下來,選擇與你的時區(qū)對應的城市或地區(qū)。這里,我選擇了 Kolkata(LCTT 譯注:中國用戶請相應使用 Shanghai 等城市)。

最后,你將在終端中看到類似下面的輸出。

Current default time zone: 'Asia/Shanghai'
Local time is now: Tue Jul 30 21:59:25 CST 2019.
Universal Time is now: Tue Jul 30 13:59:25 UTC 2019.
在圖形模式下配置時區(qū)
有些用戶可能對命令行方式不太滿意。如果你是其中之一,那么你可以輕松地在圖形模式的系統(tǒng)設置面板中進行設置。

點擊 Super 鍵(Windows 鍵),在 Ubuntu dash 中輸入 settings,然后點擊設置圖標。

從 Ubuntu dash 啟動系統(tǒng)的設置

或者,單擊位于 Ubuntu 桌面右上角的向下箭頭,然后單擊左上角的“設置”圖標。

從頂部面板啟動系統(tǒng)的設置

在下一個窗口中,選擇“細節(jié)”,然后單擊“日期與時間”選項。打開“自動的日期與時間”和“自動的時區(qū)”。

在 Ubuntu 中設置自動時區(qū)

關閉設置窗口就行了!你的系統(tǒng)始終應該與互聯(lián)網(wǎng)時間服務器同步了。

via: https://www.ostechnix.com/how-to-set-up-time-synchronization-on-ubuntu/


獲取更多知識,請點擊關注:
嵌入式Linux&ARM
CSDN博客
簡書博客


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容