如何使用 timedatectl 从命令行设置时区和启用网络时间同步 (NTP)

[ad_1]

本文介绍了如何使用 timedatectl 更改时区,并在 Linux 上使用 NTP(网络时间协议)启用系统时钟与远程服务器的自动同步。

timedatectl 是作为 systemd 的一部分提供的命令行实用程序,它允许更改系统时钟的各种设置。

如何使用 timedatectl 在 Linux 上更改时区

在更改时区之前,首先使用 timedatectl 找出当前设置的时区(还显示有关系统时间设置的其他信息):

timedatectl

这与
timedatectl status

.

输出示例:

$ timedatectl
                      Local time: Fri 2019-07-19 13:23:38 IST
                  Universal time: Fri 2019-07-19 12:23:38 UTC
                        RTC time: Fri 2019-07-19 12:23:38
                       Time zone: Europe/Dublin (IST, +0100)
       System clock synchronized: no
systemd-timesyncd.service active: yes
                 RTC in local TZ: no

现在让我们列出所有可用的时区,以便您知道将在系统上使用的时区的确切名称(更改时区的命令需要):

timedatectl list-timezones

时区列表非常大。 您可以过滤它以仅显示大陆或国家/州首府的时区,通过使用 grep,例如仅显示可能的欧洲时区:

timedatectl list-timezones | grep Europe

现在
使用以下命令在 Linux 系统上设置时区:

timedatectl set-timezone <timezone>

在哪里
<timezone>

是列出的时区
timedatectl list-timezones

. 例如,将您的 Linux 系统时区设置为
America/Lost_Angeles

和:

timedatectl set-timezone America/Los_Angeles

此命令为您选择的时区创建符号链接
/usr/share/zoneinfo/


/etc/localtime

. 您也可以手动创建此链接并实现相同的目的。 使用相同的示例,将时区设置为
America/Los_Angeles

,
/etc/localtime

需要是一个符号链接
/usr/share/zoneinfo/America/Los_Angeles

.

如何使用 timedatectl 将系统时钟与远程服务器同步(启用 NTP)

首先使用 timedatectl 查找网络时间同步 (NTP) 服务是否处于活动状态并且您的系统时钟是否同步:

timedatectl

使用以下命令在 Linux 系统上启用 NTP 服务:

timedatectl set-ntp true

如果你想禁用它,只需使用
false

代替
true

.

值得注意的是,如果未安装 NTP 服务(例如 timesyncd、ntpd、Chrony 或其他),则此命令将失败。 在许多情况下,timesyncd 应该默认安装(例如,它默认安装在 Ubuntu 16.04 和更新版本中)。

如果使用诸如 chrony 或 ntpd 之类的服务进行更改,则 timedatectl 直到
systemd-timedated

重新启动:

sudo systemctl restart systemd-timedated

在 Ubuntu 18.04 服务器上,我也必须重新启动
systemd-timesyncd

(但例如,这在我的 Ubuntu 19.04 或 Solus OS 系统上不需要),否则系统时间不会同步。 如果您还使用 timesyncd,并且 timedatectl 显示
System clock synchronized:

作为
no

, 虽然
NTP service

显示为
active

, 重新开始
systemd-timesyncd

sudo systemctl restart systemd-timesyncd

我还应该补充一点,当使用默认的 systemd timesyncd 服务时,您可以看到比 timedatectl 提供的信息更多的信息,例如使用的 NTP 时间服务器,以及显示上次执行同步的日志,包括:

systemctl status systemd-timesyncd

输出示例:

$ systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendo
   Active: active (running) since Fri 2019-07-19 13:28:49 IST; 11min ago
     Docs: man:systemd-timesyncd.service(8)
 Main PID: 2232 (systemd-timesyn)
   Status: "Synchronized to time server 91.189.94.4:123 (ntp.ubuntu.com)."
    Tasks: 2 (limit: 3159)
   CGroup: /system.slice/systemd-timesyncd.service
           └─2232 /lib/systemd/systemd-timesyncd

Jul 19 13:28:49 logix22 systemd[1]: Starting Network Time Synchronization...
Jul 19 13:28:49 logix22 systemd[1]: Started Network Time Synchronization.
Jul 19 13:28:48 logix22 systemd-timesyncd[2232]: Synchronized to time server 91.

在 systemd 239 和更新版本上(例如,这在 Ubuntu 18.04 上不起作用,因为它使用 systemd 237)您可以使用以下命令显示 systemd-timesyncd 状态
timedatectl show-timesync

$ timedatectl show-timesync
SystemNTPServers=0.pool.ntp.org 1.pool.ntp.org 2.pool.ntp.org
FallbackNTPServers=ntp.ubuntu.com
ServerName=0.pool.ntp.org
ServerAddress=194.102.58.173
RootDistanceMaxUSec=5s
PollIntervalMinUSec=32s
PollIntervalMaxUSec=34min 8s
PollIntervalUSec=1min 4s
NTPMessage={ Leap=0, Version=4, Mode=4, Stratum=2, Precision=-23, RootDelay=22.003ms, RootDispersion=21.194ms, Reference=C102015C, OriginateTimestamp=Fri 2019-07-19 13:59:53 IST, ReceiveTimestamp=Fri 2019-07-19 13:59:53 IST, TransmitTimestamp=Fri 2019-07-19 13:59:53 IST, DestinationTimestamp=Fri 2019-07-19 13:59:53 IST, Ignored=no PacketCount=1, Jitter=0 }
Frequency=1647689

和属性 systemd-timesyncd 使用
timedatectl timesync-status

$ timedatectl timesync-status
       Server: 194.102.58.173 (0.pool.ntp.org)
Poll interval: 1min 4s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 2
    Reference: C102015C
    Precision: 1us (-23)
Root distance: 32.195ms (max: 5s)
       Offset: +3.652ms
        Delay: 2.903ms
       Jitter: 0
 Packet count: 1
    Frequency: +25.142ppm

您可以通过编辑
/etc/systemd/timesyncd.conf

配置文件。 例如,要更改 NTP 服务器(您可以使用 NTP Pool Project 提供的服务器),取消注释 NTP 行,然后添加要使用的服务器,用空格分隔。 修改配置文件后重启
systemd-timesyncd

sudo systemctl restart systemd-timesyncd

[ad_2]

Related Posts