加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

如何在Ubuntu上设置时间同步

发布时间:2019-08-14 06:40:34 所属栏目:Windows 来源:Sk
导读:副标题#e# 你可能设置过 cron 任务 来在特定时间备份重要文件或执行系统相关任务。也许你配置了一个日志服务器在特定时间间隔轮转日志。但如果你的时钟不同步,这些任务将无法按时执行。这就是要在 Linux 系统上设置正确的时区并保持时钟与互联网同步的原因
副标题[/!--empirenews.page--]

如何在Ubuntu上设置时间同步

你可能设置过 cron 任务 来在特定时间备份重要文件或执行系统相关任务。也许你配置了一个日志服务器在特定时间间隔轮转日志。但如果你的时钟不同步,这些任务将无法按时执行。这就是要在 Linux 系统上设置正确的时区并保持时钟与互联网同步的原因。本指南介绍如何在 Ubuntu Linux 上设置时间同步。下面的步骤已经在 Ubuntu 18.04 上进行了测试,但是对于使用 systemd 的 timesyncd 服务的其他基于 Ubuntu 的系统它们是相同的。

在 Ubuntu 上设置时间同步

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

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

  1. $ date

示例输出:

  1. Tue Jul 30 11:47:39 UTC 2019

如上所见,date 命令显示实际日期和当前时间。这里,我当前的时区是 UTC,代表协调世界时。

或者,你可以在 /etc/timezone 文件中查找当前时区。

  1. $ cat /etc/timezone
  2. UTC

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

  1. $ timedatectl

示例输出:

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

如你所见,timedatectl 命令显示本地时间、世界时、时区以及系统时钟是否与互联网服务器同步,以及 systemd-timesyncd.service 是处于活动状态还是非活动状态。就我而言,系统时钟已与互联网时间服务器同步。

如果时钟不同步,你会看到下面截图中显示的 System clock synchronized: no

时间同步已禁用。

时间同步已禁用。

注意:上面的截图是旧截图。这就是你看到不同日期的原因。

如果你看到 System clock synchronized: 值设置为 no,那么 timesyncd 服务可能处于非活动状态。因此,只需重启服务并看下是否正常。

  1. $ sudo systemctl restart systemd-timesyncd.service

现在检查 timesyncd 服务状态:

  1. $ sudo systemctl status systemd-timesyncd.service
  2. ● systemd-timesyncd.service - Network Time Synchronization
  3. Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
  4. Active: active (running) since Tue 2019-07-30 10:50:18 UTC; 1h 11min ago
  5. Docs: man:systemd-timesyncd.service(8)
  6. Main PID: 498 (systemd-timesyn)
  7. Status: "Synchronized to time server [2001:67c:1560:8003::c7]:123 (ntp.ubuntu.com)."
  8. Tasks: 2 (limit: 2319)
  9. CGroup: /system.slice/systemd-timesyncd.service
  10. └─498 /lib/systemd/systemd-timesyncd
  11.  
  12. Jul 30 10:50:30 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  13. Jul 30 10:50:31 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  14. Jul 30 10:50:31 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  15. Jul 30 10:50:32 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  16. Jul 30 10:50:32 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  17. Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  18. Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  19. Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  20. Jul 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab
  21. Jul 30 10:51:06 ubuntuserver systemd-timesyncd[498]: Synchronized to time server [2001:67c:1560:800

如果此服务已启用并处于活动状态,那么系统时钟应与互联网时间服务器同步。

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

  1. $ timedatectl

如果仍然不起作用,请运行以下命令以启用时间同步:

  1. $ sudo timedatectl set-ntp true

现在,你的系统时钟将与互联网时间服务器同步。

使用 timedatectl 命令更改时区

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

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读