Debian12配置NTP时间同步

环境

查看系统版本:lsb_release -a

配置NTP时间同步

下面的配置需要用到管理员权限,可以使用su切换到管理员权限。

查看/修正 时区

查看系统时区:timedatectl

如果时区不是Asia/Shanghai需要修改时区为东八区

root@debian:/home/test# timedatectl set-timezone "Asia/Shanghai"

查看本地时间

如果本地时间与NTP服务器时间误差太大(1000s以上),需要先手动校正本地系统时间,手动校正可以使用ntpdate命令强制修正本地时间

ntpdate NTPServerIP后面跟NTP服务器的IP地址

root@debian:/home/test# ntpdate -d ntp.ntsc.ac.cn
ntpdig: querying 114.118.7.163 (ntp.ntsc.ac.cn)
ntpdig: querying 114.118.7.161 (ntp.ntsc.ac.cn)
org t1: e86314a0.b6c0a000 rec t2: e8646973.584fce78
xmt t3: e8646973.58e812a7 dst t4: e86314a1.c05f7000
org t1: 1689818656.713877 rec t2: 1689905907.344968
xmt t3: 1689905907.347291 dst t4: 1689818657.751456
rec-org t21: 87250.631091  xmt-dst t34: 87249.595835
2023-07-21 10:18:27.347291 (+0800) +87250.113463 +/- 0.517644 ntp.ntsc.ac.cn 114.118.7.161 s1 no-leap
CLOCK: time stepped by 87250.113463
CLOCK: time changed from 2023-07-20 to 2023-07-21
root@debian:/home/test# date
2023年 07月 21日 星期五 10:18:44 CST

如果没有ntpdate命令,可以使用date -s "YYYY-MM-DD HH:MM:SS"手动修改本地时间

root@debian:/home/test# date -s "2023-7-20 10:00:00"
2023年 07月 20日 星期四 10:00:00 CST

配置NTP自动校时

编辑/etc/ntpsec/ntp.conf配置NTP服务器地址,将默认的pool配置注释掉(前面加#号代表注释),添加新的配置,下面是ntp.conf文件的所有配置

root@debian:/home/test# cat /etc/ntpsec/ntp.conf 
# /etc/ntpsec/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntpsec/ntp.drift
leapfile /usr/share/zoneinfo/leap-seconds.list

# To enable Network Time Security support as a server, obtain a certificate
# (e.g. with Let's Encrypt), configure the paths below, and uncomment:
# nts cert CERT_FILE
# nts key KEY_FILE
# nts enable

# You must create /var/log/ntpsec (owned by ntpsec:ntpsec) to enable logging.
#statsdir /var/log/ntpsec/
#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

# This should be maxclock 7, but the pool entries count towards maxclock.
tos maxclock 11

# Comment this out if you have a refclock and want it to be able to discipline
# the clock by itself (e.g. if the system is not connected to the network).
tos minclock 4 minsane 3

# Specify one or more NTP servers.

# Public NTP servers supporting Network Time Security:
# server time.cloudflare.com nts
server 192.168.6.200                 #添加NTP服务器地址


# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <https://www.pool.ntp.org/join.html>
#pool 0.debian.pool.ntp.org iburst    #注释
#pool 1.debian.pool.ntp.org iburst    #注释
#pool 2.debian.pool.ntp.org iburst    #注释
#pool 3.debian.pool.ntp.org iburst    #注释


# Access control configuration; see /usr/share/doc/ntpsec-doc/html/accopt.html
# for details.
#
# 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 default kod nomodify nopeer noquery limited

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

重启ntpd服务

service ntpd restart

查看同步状态

ntpq -pn

 等待系统识别并选择后,在IP前面会标*/+如下图所示

注:这里offset误差500+ms是因为系统装在了VMware虚拟机上,hwclock发现没有硬件时间。

修改虚拟机设置中的处理器虚拟化引擎后,识别到了硬件时间,再次查看offset:

服务器主机名前符号的含义

-意味着本地NTP服务不太喜欢此服务器
+表示本地 NTP 服务喜欢此服务器
x标记错误的主机
*指示当前收藏夹

附:每列的含义

remote
远程 NTP 服务器的名称。如果使用 -n 开关,您将看到这些服务器的 IP 地址,而不是它们的主机名。
refid
指示每个服务器现在从何处获取时间。它可以是服务器主机名或类似 .全球定位系统。 ,表示全球定位系统源。
st
tratum 是一个介于 1 到 16 之间的数字,表示远程服务器的精度。 1 表示最准确,16 表示“服务器无法访问”。您的层将等于精确的远程服务器加 1。切勿连接到第 1 层服务器,请使用第 2 层服务器!第 2 层服务器也适合我们的目的,此策略适用于减少第 1 层服务器的流量。
poll
时间请求之间的轮询间隔(以秒为单位)。该值的范围介于允许的最小和最大轮询值之间。最初,该值将较小,以便快速进行同步。时钟“同步”后,轮询值将增加,以减少网络流量和常用时间服务器上的负载。
reach
这是 8 位数组的八进制表示形式,表示本地计算机尝试到达服务器的最后 8 次。如果到达远程服务器,则设置该位。
delay
接收“现在几点了”请求的响应所需的时间(秒)。
offset
最重要的价值。本地服务器和远程服务器之间的时间差。在同步过程中,偏移时间降低,表明本地机器时间越来越准确。单位:毫秒
jitter
离散(也称为抖动)是几个连续请求/响应对之间偏移的统计方差的度量。较低的色散值优于较高的色散值。较低的色散可实现更精确的时间同步。

ntpd服务加入开机启动

使用下面命令将ntpd服务加入开机启动

chkconfig --level 2345 ntpd on 

end。

posted @ 2023-07-21 13:11  xjournal  阅读(5849)  评论(0编辑  收藏  举报