【转】Ubuntu系统配置时间服务器

2024-01-04  本文已影响0人  pumpdev

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

但是现在在 Ubuntu 系统上,默认使用 timesyncd 代替 ntpd 。timesyncd 通过连接到相同的时间服务器,但它是轻量级的,并且与 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 服务器信息。
原文地址

上一篇 下一篇

猜你喜欢

热点阅读