如何在Ubuntu上设置时间同步
你可能设置过 cron 任务 来在特定时间备份重要文件或执行系统相关任务。也许你配置了一个日志服务器在特定时间间隔轮转日志。但如果你的时钟不同步,这些任务将无法按时执行。这就是要在 Linux 系统上设置正确的时区并保持时钟与互联网同步的原因。本指南介绍如何在 Ubuntu Linux 上设置时间同步。下面的步骤已经在 Ubuntu 18.04 上进行了测试,但是对于使用 systemd 的 timesyncd
服务的其他基于 Ubuntu 的系统它们是相同的。
在 Ubuntu 上设置时间同步
通常,我们在安装时设置时区。但是,你可以根据需要更改或设置不同的时区。
首先,让我们使用 date
命令查看 Ubuntu 系统中的当前时区:
<span class="pln">$ </span><span class="kwd">date</span>
示例输出:
<span class="typ">Tue</span><span class="pln"> </span><span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">47</span><span class="pun">:</span><span class="lit">39</span><span class="pln"> UTC </span><span class="lit">2019</span>
如上所见,date
命令显示实际日期和当前时间。这里,我当前的时区是 UTC,代表协调世界时。
或者,你可以在 /etc/timezone
文件中查找当前时区。
<span class="pln">$ </span><span class="kwd">cat</span><span class="pln"> </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">timezone</span>
<span class="pln">UTC</span>
现在,让我们看看时钟是否与互联网同步。只需运行:
<span class="pln">$ </span><span class="kwd">timedatectl</span>
示例输出:
<span class="typ">Local</span><span class="pln"> </span><span class="kwd">time</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">53</span><span class="pun">:</span><span class="lit">58</span><span class="pln"> UTC</span>
<span class="typ">Universal</span><span class="pln"> </span><span class="kwd">time</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">53</span><span class="pun">:</span><span class="lit">58</span><span class="pln"> UTC</span>
<span class="pln">RTC </span><span class="kwd">time</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">11</span><span class="pun">:</span><span class="lit">53</span><span class="pun">:</span><span class="lit">59</span>
<span class="typ">Time</span><span class="pln"> zone</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Etc</span><span class="pun">/</span><span class="pln">UTC </span><span class="pun">(</span><span class="pln">UTC</span><span class="pun">,</span><span class="pln"> </span><span class="pun">+</span><span class="lit">0000</span><span class="pun">)</span>
<span class="typ">System</span><span class="pln"> clock synchronized</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">yes</span>
<span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service active</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">yes</span>
<span class="pln">RTC </span><span class="kwd">in</span><span class="pln"> </span><span class="kwd">local</span><span class="pln"> TZ</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">no</span>
如你所见,timedatectl
命令显示本地时间、世界时、时区以及系统时钟是否与互联网服务器同步,以及 systemd-timesyncd.service
是处于活动状态还是非活动状态。就我而言,系统时钟已与互联网时间服务器同步。
如果时钟不同步,你会看到下面截图中显示的 System clock synchronized: no
。
时间同步已禁用。
注意:上面的截图是旧截图。这就是你看到不同日期的原因。
如果你看到 System clock synchronized:
值设置为 no
,那么 timesyncd
服务可能处于非活动状态。因此,只需重启服务并看下是否正常。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> restart </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service</span>
现在检查 timesyncd
服务状态:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">systemctl</span><span class="pln"> status </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service</span>
<span class="pun">●</span><span class="pln"> </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service </span><span class="pun">-</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> </span><span class="typ">Time</span><span class="pln"> </span><span class="typ">Synchronization</span>
<span class="typ">Loaded</span><span class="pun">:</span><span class="pln"> loaded </span><span class="pun">(</span><span class="str">/lib/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="pln">system</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service</span><span class="pun">;</span><span class="pln"> enabled</span><span class="pun">;</span><span class="pln"> vendor preset</span><span class="pun">:</span><span class="pln"> enabled</span><span class="pun">)</span>
<span class="typ">Active</span><span class="pun">:</span><span class="pln"> active </span><span class="pun">(</span><span class="pln">running</span><span class="pun">)</span><span class="pln"> since </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">18</span><span class="pln"> UTC</span><span class="pun">;</span><span class="pln"> </span><span class="lit">1h</span><span class="pln"> </span><span class="lit">11min</span><span class="pln"> ago</span>
<span class="typ">Docs</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">man</span><span class="pun">:</span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service</span><span class="pun">(</span><span class="lit">8</span><span class="pun">)</span>
<span class="typ">Main</span><span class="pln"> PID</span><span class="pun">:</span><span class="pln"> </span><span class="lit">498</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyn</span><span class="pun">)</span>
<span class="typ">Status</span><span class="pun">:</span><span class="pln"> </span><span class="str">"Synchronized to time server [2001:67c:1560:8003::c7]:123 (ntp.ubuntu.com)."</span>
<span class="typ">Tasks</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">(</span><span class="pln">limit</span><span class="pun">:</span><span class="pln"> </span><span class="lit">2319</span><span class="pun">)</span>
<span class="typ">CGroup</span><span class="pun">:</span><span class="pln"> </span><span class="str">/system.slice/</span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service</span>
<span class="pun">└─</span><span class="lit">498</span><span class="pln"> </span><span class="pun">/</span><span class="pln">lib</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">/</span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">30</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">31</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">31</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">32</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">32</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">50</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Network</span><span class="pln"> configuration changed</span><span class="pun">,</span><span class="pln"> trying to estab</span>
<span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">10</span><span class="pun">:</span><span class="lit">51</span><span class="pun">:</span><span class="lit">06</span><span class="pln"> ubuntuserver </span><span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">[</span><span class="lit">498</span><span class="pun">]:</span><span class="pln"> </span><span class="typ">Synchronized</span><span class="pln"> to </span><span class="kwd">time</span><span class="pln"> server </span><span class="pun">[</span><span class="lit">2001</span><span class="pun">:</span><span class="lit">67c</span><span class="pun">:</span><span class="lit">1560</span><span class="pun">:</span><span class="lit">800</span>
如果此服务已启用并处于活动状态,那么系统时钟应与互联网时间服务器同步。
你可以使用命令验证是否启用了时间同步:
<span class="pln">$ </span><span class="kwd">timedatectl</span>
如果仍然不起作用,请运行以下命令以启用时间同步:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">timedatectl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="pln">ntp </span><span class="kwd">true</span>
现在,你的系统时钟将与互联网时间服务器同步。
使用 timedatectl 命令更改时区
如果我想使用 UTC 以外的其他时区怎么办?这很容易!
首先,使用命令列出可用时区:
<span class="pln">$ </span><span class="kwd">timedatectl</span><span class="pln"> </span><span class="kwd">list</span><span class="pun">-</span><span class="pln">timezones</span>
你将看到类似于下图的输出。
使用 timedatectl 命令列出时区
你可以使用以下命令设置所需的时区(例如,Asia/Shanghai):
(LCTT 译注:本文原文使用印度时区作为示例,这里为了便于使用,换为中国标准时区 CST。另外,在时区设置中,要注意 CST 这个缩写会代表四个不同的时区,因此建议使用城市和 UTC+8 来说设置。)
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">timedatectl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="pln">timezone </span><span class="typ">Asia</span><span class="pun">/</span><span class="typ">Shanghai</span>
使用 date
命令再次检查时区是否已真正更改:
<span class="pln">$ </span><span class="kwd">date</span>
<span class="typ">Tue</span><span class="pln"> </span><span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">20</span><span class="pun">:</span><span class="lit">22</span><span class="pun">:</span><span class="lit">33</span><span class="pln"> CST </span><span class="lit">2019</span>
或者,如果需要详细输出,请使用 timedatectl
命令:
<span class="pln">$ </span><span class="kwd">timedatectl</span>
<span class="typ">Local</span><span class="pln"> </span><span class="kwd">time</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">20</span><span class="pun">:</span><span class="lit">22</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> CST</span>
<span class="typ">Universal</span><span class="pln"> </span><span class="kwd">time</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">12</span><span class="pun">:</span><span class="lit">22</span><span class="pun">:</span><span class="lit">35</span><span class="pln"> UTC</span>
<span class="pln">RTC </span><span class="kwd">time</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="lit">2019</span><span class="pun">-</span><span class="lit">07</span><span class="pun">-</span><span class="lit">30</span><span class="pln"> </span><span class="lit">12</span><span class="pun">:</span><span class="lit">22</span><span class="pun">:</span><span class="lit">36</span>
<span class="typ">Time</span><span class="pln"> zone</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Asia</span><span class="pun">/</span><span class="typ">Shanghai</span><span class="pln"> </span><span class="pun">(</span><span class="pln">CST</span><span class="pun">,</span><span class="pln"> </span><span class="pun">+</span><span class="lit">0800</span><span class="pun">)</span>
<span class="typ">System</span><span class="pln"> clock synchronized</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">yes</span>
<span class="kwd">systemd</span><span class="pun">-</span><span class="pln">timesyncd</span><span class="pun">.</span><span class="pln">service active</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">yes</span>
<span class="pln">RTC </span><span class="kwd">in</span><span class="pln"> </span><span class="kwd">local</span><span class="pln"> TZ</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">no</span>
如你所见,我已将时区从 UTC 更改为 CST(中国标准时间)。()
要切换回 UTC 时区,只需运行:
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> </span><span class="kwd">timedatectl</span><span class="pln"> </span><span class="kwd">set</span><span class="pun">-</span><span class="pln">timezone UTC</span>
使用 tzdata 更改时区
在较旧的 Ubuntu 版本中,没有 timedatectl
命令。这种情况下,你可以使用 tzdata
(Time zone data)来设置时间同步。
<span class="pln">$ </span><span class="kwd">sudo</span><span class="pln"> dpkg</span><span class="pun">-</span><span class="pln">reconfigure tzdata</span>
选择你居住的地理区域。对我而言,我选择 Asia。选择 OK,然后按回车键。
接下来,选择与你的时区对应的城市或地区。这里,我选择了 Kolkata(LCTT 译注:中国用户请相应使用 Shanghai 等城市)。
最后,你将在终端中看到类似下面的输出。
<span class="typ">Current</span><span class="pln"> </span><span class="kwd">default</span><span class="pln"> </span><span class="kwd">time</span><span class="pln"> zone</span><span class="pun">:</span><span class="pln"> </span><span class="str">'Asia/Shanghai'</span>
<span class="typ">Local</span><span class="pln"> </span><span class="kwd">time</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> now</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">21</span><span class="pun">:</span><span class="lit">59</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> CST </span><span class="lit">2019.</span>
<span class="typ">Universal</span><span class="pln"> </span><span class="typ">Time</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> now</span><span class="pun">:</span><span class="pln"> </span><span class="typ">Tue</span><span class="pln"> </span><span class="typ">Jul</span><span class="pln"> </span><span class="lit">30</span><span class="pln"> </span><span class="lit">13</span><span class="pun">:</span><span class="lit">59</span><span class="pun">:</span><span class="lit">25</span><span class="pln"> UTC </span><span class="lit">2019.</span>
在图形模式下配置时区
有些用户可能对命令行方式不太满意。如果你是其中之一,那么你可以轻松地在图形模式的系统设置面板中进行设置。
点击 Super 键(Windows 键),在 Ubuntu dash 中输入 settings,然后点击设置图标。
从 Ubuntu dash 启动系统的设置
或者,单击位于 Ubuntu 桌面右上角的向下箭头,然后单击左上角的“设置”图标。
从顶部面板启动系统的设置
在下一个窗口中,选择“细节”,然后单击“日期与时间”选项。打开“自动的日期与时间”和“自动的时区”。