Ubuntu开启NTP时间同步

Ubuntu 使用 timesyncd 开启NTP时间同步,并替换为ntpd步进式的逐渐校正时间。

环境为 Ubuntu 22.04 LTS

时区
在 Ubuntu 20.04 及之后的版本中,内置了时间同步功能,并且默认使用systemd的timesyncd服务来激活。timesyncd 替代了老旧的 ntpdate 的功能。

检查当前时区
命令:

timedatectl status
1
命令的输出为

Local time: Mon 2022-12-12 23:35:24 CST
Universal time: Mon 2022-12-12 15:35:24 UTC
RTC time: Mon 2022-12-12 15:35:28
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: no
NTP service: n/a
RTC in local TZ: no

System clock synchronized: no 反映了没有和远程NTP服务器成功同步, NTP service: n/a 意味着timesyncd没有启动和运行。RTC in local TZ: no表示硬件时钟(RTC)设置为协调世界时(UTC),yes表示硬件时钟设置为本地时间.

输出显示NTP服务没有激活,所以启动NTP服务

sudo timedatectl set-ntp on

开启成功后再次查看状态,输出为

System clock synchronized: yes
NTP service: active
RTC in local TZ: no

如果开启时出现错误提示 Failed to set ntp: NTP not supported ,则可以查看软件包systemd-timesyncd有没有被安装。如果没有则用包管理安装,再安装后尝试再次开启timedatectl。

apt list --installed | grep systemd-timesyncd
sudo apt-get install systemd-timesyncd
1
2
设置时区
时区命名约定通常使用“地区/城市”格式

列出所有可用的时区:

timedatectl list-timezones
1
修改时区:

sudo timedatectl set-timezone Asia/Shanghai
1
修改NTP时间同步服务器
NTP服务器(Network Time Protocol)是用来使计算机时间同步化的一种协议,NTP服务器确保不同的系统之间时间戳保持同步。

在新安装的ubuntu系统上,基于 systemd 的工具的NTP服务器默认为ntp.ubuntu.com。查看系统日志,有时会出现时间超时记录:

systemd-timesyncd[354]: Timed out waiting for reply from 91.189.94.4:123 (ntp.ubuntu.com).
1
要修改时间同步服务器,需要修改配置文件/etc/systemd/timesyncd.conf

sudo vim /etc/systemd/timesyncd.conf
1
这个文件的默认内容是这样的:

# ...

[Time]
#NTP=
#
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

把 [Time]下的注释取消掉。NTP为主时间同步服务器,FallbackNTP 为备用服务器。

[Time]
NTP=ntp.tencent.com
FallbackNTP=ntp1.tencent.com,ntp2.tencent.com,ntp3.tencent.com
RootDistanceMaxSec=5
PollIntervalMinSec=32
PollIntervalMaxSec=2048

重启服务

service systemd-timesyncd restart
1
使用 ntpd 渐进式更新时间
timesyncd(和ntpdate)是断点更新,ntpd 为步进式的逐渐校正时间,不会出现时间跳变。另外有些应用程序可能对时间的任何干扰很敏感。ntpd使用复杂的技术来不断地、逐步地保持系统的时间。

关闭 timesyncd
在安装 ntpd 之前,需要关闭 timesyncd,以防止这两个服务之间的相互冲突。

sudo timedatectl set-ntp no
1
使用 timedatectl status 检查输出是否出现了NTP service: inactive。这意味着timesyncd已经停止。

安装 ntpd:
使用apt安装ntp软件包:

sudo apt update
sudo apt install ntp
1
2
ntpd 将在你的安装完成后自动开始运行和工作。

ntpd 可能需要几分钟的时间来建立连接。

检查 ntpd 的运行状态:

systemctl status ntp
1
查看 NTP 服务端口 UDP 123 端口是否被正常监听:

netstat -nupl
1
查询ntpd的详细状态信息:

ntpq -p
1
ntpq 是 ntpd 的一个查询工具。-p 标志要求提供 ntpd 所连接的 NTP 服务器的信息。

如果提示 No association ID's returned 则多半是配置文件有问题。

配置 ntpd
vim /etc/ntp.conf
1
原始的配置文件如下:

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Leap seconds definition provided by tzdata
leapfile /usr/share/zoneinfo/leap-seconds.list

# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

# Use Ubuntu's ntp server as a fallback.
pool ntp.ubuntu.com

# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery limited
restrict -6 default kod notrap nomodify nopeer noquery limited

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Needed for adding pool entries
restrict source notrap nomodify noquery

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines. Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

修改配置文件之后,需要重新加载 ntpd

Systemctl是一个systemd工具,它负责控制systemd系统和服务管理程序。Systemd是一个系统管理守护进程,工具和库的集合,功能为用于类Unix系统的中心管理和配置平台。

systemctl reload ntp.service
1
参考链接:

How To Set Up Time Synchronization on Ubuntu 20.04 - DigitalOcean
————————————————
版权声明:本文为CSDN博主「现代的三流魔法使」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sorcererr/article/details/128675919

posted @ 2023-12-11 17:33  疯子110  阅读(810)  评论(0编辑  收藏  举报