如何在 Ubuntu 上设置时间同步

你可能已经设置 cron 工作 在特定时间运行以备份重要文件或执行任何与系统相关的任务。 或者,您可能已经配置了一个 日志服务器轮换日志 定期离开您的系统。 如果您的时钟不同步,这些作业将不会在正确的时间执行。 这就是为什么在 Linux 系统上设置正确的时区并保持时钟与 Internet 同步很重要的原因。 本指南介绍如何在 Ubuntu Linux 上设置时间同步。 下面给出的步骤已经在 Ubuntu 18.04 上进行了测试,但是对于使用 systemd 的其他基于 Ubuntu 的系统,它们是相同的 时间同步 服务。

在 Ubuntu 上设置时间同步

通常,我们在安装时设置时区。 但是,您可以根据需要更改它或设置不同的时区。

首先,让我们使用“date”命令查看 Ubuntu 系统中的当前时区:

$ date

样本输出:

Tue Jul 30 11:47:39 UTC 2019

正如您在上面的输出中看到的,“date”命令显示了实际日期和当前时间。 在这里,我当前的时区是 世界标准时间 代表 协调世界时.

或者,您可以查找 /etc/时区 文件以查找当前时区。

$ cat /etc/timezone
UTC

现在,让我们看看时钟是否与互联网同步。 为此,只需运行:

$ 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”命令显示了本地时间、世界时间、时区以及系统时钟是否与互联网服务器同步以及是否 systemd-timesyncd.service 是活跃的还是不活跃的。 在我的例子中,系统时钟与 Internet 时间服务器同步。

如果时钟不同步,您会看到 “系统时钟同步:否” 如下图所示。

时间同步被禁用。

笔记: 上面的截图是旧的。 这就是为什么你看到不同的日期。

如果你看到 “系统时钟同步: 值设置为 , timesyncd 服务可能处于非活动状态。 因此,只需重新启动服务,看看它是否有帮助。

$ sudo systemctl restart systemd-timesyncd.service

现在检查 timesyncd 服务状态:

$ 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

如果此服务已启用并处于活动状态,您的系统时钟应与 Internet 时间服务器同步。

您可以使用命令验证是否启用了时间同步:

$ timedatectl

如果还是不行,运行以下命令开启时间同步:

$ sudo timedatectl set-ntp true

现在您的系统时钟将与 Internet 时间服务器同步。

使用 Timedatectl 命令更改时区

如果我想使用 UTC 以外的其他时区怎么办? 这很容易!

首先,使用命令列出可用时区:

$ timedatectl list-timezones

您将看到类似于下图的输出。

使用 timedatectl 命令列出时区

您可以使用以下命令设置所需的时区(例如亚洲/加尔各答):

$ sudo timedatectl set-timezone Asia/Kolkata

使用“date”命令再次检查时区是否真的改变了:

$ date
Tue Jul 30 17:52:33 IST 2019

或者,如果您想要详细的输出,请使用 timedatectl 命令:

$ timedatectl 
Local time: Tue 2019-07-30 17:52:35 IST
Universal time: Tue 2019-07-30 12:22:35 UTC
RTC time: Tue 2019-07-30 12:22:36
Time zone: Asia/Kolkata (IST, +0530)
System clock synchronized: yes
systemd-timesyncd.service active: yes
RTC in local TZ: no

如您所见,我已将时区从 UTC 更改为 IST(印度标准时间)。

要切换回 UTC 时区,只需运行:

$ sudo timedatectl set-timezone UTC

使用 Tzdata 更改时区

在较旧的 Ubuntu 版本中,Timedatectl 命令不可用。 在这种情况下,您可以使用 数据(时区数据)设置时间同步。

$ sudo dpkg-reconfigure tzdata

选择您居住的地理区域。 就我而言,我选择了 亚洲. 选择确定并按 ENTER 键。

使用 tzdata 命令配置时区

使用 tzdata 命令配置时区

接下来,选择与您的时区对应的城市或地区。 这里我选择了 加尔各答.

使用 tzdata 2 配置时区

使用 tzdata 配置时区

最后,您将在终端中看到类似下面的输出。

Current default time zone: 'Asia/Kolkata'
Local time is now: Tue Jul 30 19:29:25 IST 2019.
Universal Time is now: Tue Jul 30 13:59:25 UTC 2019.

在图形模式下配置时区

一些用户可能不习惯 CLI 方式。 如果您是其中之一,您可以从系统设置面板以图形模式轻松更改所有这些操作。

超级钥匙 (Windows 键),键入 设置 在 Ubuntu dash 中并点击 设置 图标。

从 Ubuntu dash 启动系统设置

从 Ubuntu dash 启动系统设置

或者,单击位于 Ubuntu 桌面右上角的向下箭头,然后单击左角的设置图标。

从顶部面板启动系统设置

从顶部面板启动系统设置

在下一个窗口中,选择 细节 然后点击 约会时间 选项。 开启两者 自动日期和时间自动时区 选项。

在ubuntu中设置自动时区

在 Ubuntu 中设置自动时区

关闭设置窗口就完成了! 您的系统时钟现在应该与 Internet 时间服务器同步。

感谢您的光临!

帮助我们帮助您:

祝你有美好的一天!!