Ubuntu 20.04 设置时区、配置NTP同步 timesyncd 代替 ntpd

本文的服务器环境为 Ubuntu 20.04 系统,一个拥有 sudo 权限的非 root用户,并开启了防火墙。

要在 Ubuntu 20.04 系统上查看服务器时间,可以使用 date 命令。任何用户都可以运行该命令来查询日期和时间:

通常,服务器默认使用的是UTC时间(世界标准时间)。

date
Thu Aug 5 15:55:20 UTC 2021

UTC 时间也叫世界标准时间,即经度为零的时间。UTC 时间可能无法反映您当前的时区时间,但如果您的服务跨越多个时区时,使用UTC 时间可以防止混淆。

0时经线是变动的 任意一条经线在特定时间内都可以是0时经线
0°经线=本初子午线

但是,如果要更改时区,我们可以使用 timedatectl 命令。

首先,运行下面的命令以生成可用时区列表:

#查看时区列表
timedatectl list-timezones

时区列表会打印到屏幕上。您可以按 SPACE 向下翻页使用 b 向上翻页。找到正确的时区后,记下它,然后键入 q 退出列表。

接下来,使用 timedatectl set-timezone 命令来设置时区,将下面高亮显示的部分替换为刚刚在列表中找到的本地时区:

#设置上海时区
sudo timedatectl set-timezone Asia/Shanghai

以前,大多数网络时间同步通过“ntpd”处理,通过连接到其他 NTP 服务器,来提供持续且准确的时间更新。

但是现在在 Ubuntu 系统上,默认使用 timesyncd 代替 ntpdtimesyncd 通过连接到相同的时间服务器,但它是轻量级的,并且与 Ubuntu 的 systemd 集成得更好。

可以通过直接运行 timedatectl 来查询 timesyncd 的状态。

#查看NTP状态
timedatectl status 或者 timedatectl
               Local time: Fri 2023-01-06 14:41:01 CST
           Universal time: Fri 2023-01-06 06:41:01 UTC
                 RTC time: Fri 2023-01-06 06:41:01
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

System clock synchronized: yes 指令表示时间同步成功,NTP service: active 指令表示 timesyncd 已启动并运行。

如果输出显示 NTP 服务未激活,可使用以下命令将其打开:

sudo timedatectl set-ntp on

之后,再次运行 timedatectl 以确认网络时间状态。

*NTP服务器配置路径:/etc/systemd/timesyncd.conf

格式:

# See timesyncd.conf(5) for details.

[Time]
NTP=ntp-z.gwmfc.com

 

 


切换到 ntpd

在大多数情况下 timesyncd 都可以使用。然而,在某些情况下,timesyncd 程序可能被干扰。在这种情况下,可以使用 ntpd

我们首先需要先关闭 timesyncd,以免两个服务相互冲突:

sudo timedatectl set-ntp no

检查时间同步是否已禁用:

timedatectl

检查输出是否为 NTP service: inactive。如果是意味着 timesyncd 已经停止。

现在,运行 apt update 以更新本地包索引:

sudo apt update

然后,运行 apt install ntp 安装 ntp 包:

sudo apt install ntp

ntpd 安装完成后将自动运行。可以通过 ntpd 查询运行状态是否正常:

ntpq -p
Output
           remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 0.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 1.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 2.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 3.ubuntu.pool.n .POOL.          16 p    -   64    0    0.000    0.000   0.000
 ntp.ubuntu.com  .POOL.          16 p    -   64    0    0.000    0.000   0.000
+t1.time.bf1.yah 129.6.15.28      2 u   16   64    1   61.766  -20.068   1.964
+puppet.kenyonra 80.72.67.48      3 u   16   64    1    2.622  -18.407   2.407
*ntp3.your.org   .GPS.            1 u   15   64    1   50.303  -17.499   2.708
+time.cloudflare 10.4.1.175       3 u   15   64    1    1.488  -18.295   2.670
+mis.wci.com     216.218.254.202  2 u   15   64    1   21.527  -18.377   2.414
+ipv4.ntp1.rbaum 69.89.207.99     2 u   12   64    1   49.741  -17.897   3.417
+time.cloudflare 10.4.1.175       3 u   15   64    1    1.039  -16.692   3.378
+108.61.73.243   129.6.15.29      2 u   14   64    1   70.060  -16.993   3.363
+ny-time.gofile. 129.6.15.28      2 u   21   64    1   75.349  -18.333   2.763
 golem.canonical 17.253.34.123    2 u   28   64    1  134.482  -21.655   0.000
 ntp3.junkemailf 216.218.254.202  2 u   19   64    1    2.632  -16.330   4.387
 clock.xmission. .XMIS.           1 u   18   64    1   24.927  -16.712   3.415
 alphyn.canonica 142.3.100.2      2 u   26   64    1   73.612  -19.371   0.000
 strongbad.voice 192.5.41.209     2 u   17   64    1   70.766  -18.159   3.481
 chilipepper.can 17.253.34.123    2 u   25   64    1  134.982  -19.848   0.000
 pugot.canonical 145.238.203.14   2 u   28   64    1  135.694  -21.075   0.000

ntpq 是一个 ntpd 的查询工具。这里的 -p 参数为请求 ntpd 连接到的 NTP 服务器信息。

 

posted @ 2023-01-06 14:56  Magiclala  阅读(12255)  评论(1编辑  收藏  举报