如何在Ubuntu上设置时间同步
我们可能已经设置了在特定时间运行的Cron作业备份重要文件或者执行任何系统相关任务。
或者,我们可能已经配置了日志服务器,以定期将LOGS旋转从系统中旋转。
如果时钟不同步,那么这些作业将不会在正确的时间执行。
这就是为什么在Linux系统上设置正确的时区并保持与Internet同步的时钟很重要。
本教程介绍了如何在Ubuntu Linux上设置时间同步。
下面给出的步骤已经在Ubuntu 18.04上进行了测试,但它们对于使用SystemD的超时服务的基于Ubuntu的系统是相同的。
在Ubuntu上设置时间同步
通常,我们在安装期间设置时区。
但是,如果我们愿意,我们可以更改它或者设置不同的时区。
首先,让我们使用"日期"命令查看Ubuntu系统中的当前时区:
$date
示例输出:
Tue May 30 11:47:39 UTC 2019
如上所述,"日期"命令显示实际日期以及当前时间。
其中我当前的时区是UTC,它代表协调的世界时间。
或者,我们可以查找/etc/timezone文件以查找当前的时区。
$cat /etc/timezone UTC
现在,让我们看看时钟是否与Internet同步。
为此,只需运行:
$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"命令显示本地时间,通用时间,时区以及系统时钟是否与Internet服务器同步,以及Systemd-timeyncd.service是否处于活动状态或者非活动状态。
在我的情况下,系统时钟与Internet时间服务器同步。
如果时钟不同步,则会看到"系统时钟同步:否",如下下面的屏幕截图所示。
注意:上面的屏幕截图是旧的。
这就是你看到不同日期的原因。
如果看到"系统时钟同步:值设置为否,则TimeyNCD服务可能是非活动的。
因此,只需重新启动服务并查看它是否有帮助。
$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 May 30 10:50:30 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:31 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:31 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:32 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:32 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:50:35 ubuntuserver systemd-timesyncd[498]: Network configuration changed, trying to estab May 30 10:51:06 ubuntuserver systemd-timesyncd[498]: Synchronized to time server [2001:67c:1560:800
如果启用此服务并激活,则系统时钟应与Internet Time Server同步。
我们可以验证是否启用了时间同步或者不使用命令:
$timedatectl
如果它仍然不起作用,请运行以下命令以启用时间同步:
$sudo timedatectl set-ntp true
现在,系统时钟将与Internet时间服务器同步。
使用TimedAtect命令更改时区
如果我想使用除UTC以外的不同时区怎么办?
这很容易!
首先,使用命令的可用时区列表:
$timedatectl list-timezones
我们将看到类似于图像的输出。
我们可以使用命令设置所需的时区(例如Asia/Kolkata):
$sudo timedatectl set-timezone Asia/Kolkata
如果使用"日期"命令,则重新检查时区是否真的更改:
$date Tue May 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命令不可用。
在这种情况下,我们可以使用TZDATA(时区数据)来设置时间同步。
$sudo dpkg-reconfigure tzdata
选择我们居住的地理区域。
在我的情况下,我选择了亚洲。
选择确定并按Enter键。
接下来,选择与时区对应的城市或者区域。
其中我选择了Kolkata。
最后,我们将在终端中看到下面的输出。
Current default time zone: 'Asia/Kolkata' Local time is now: Tue May 30 19:29:25 IST 2019. Universal Time is now: Tue May 30 13:59:25 UTC 2019.
在图形模式下配置时区
有些用户可能对CLI方式不满意。
如果我们是其中之一,则可以在图形模式下轻松更改所有这些从系统设置面板。
点击超级键(Windows键),在Ubuntu破折号中键入设置,然后单击"设置"图标。
或者,单击位于Ubuntu桌面右上角的向下箭头,然后单击左角的设置图标。
在下一个窗口中,选择"详细信息",然后单击"日期&时间"选项。
打开自动日期&时间和自动时区选项。
关闭设置窗口我们完成的!系统时钟现在应该与Internet时间服务器同步。