基于NTP的支持加密认证的时间同步

基于NTP的支持加密认证的时间同步


网络上关于支持加密认证的NTP时间同步大部分只写到一部分,故总结如下,hope it helps~

Server端

  1. 安装NTP服务端,例如在Debian可通过:
sudo apt install ntp

或在yocto通过修改配置build/conf/local.confg添加:

IMAGE_INSTALL += "ntp"
  1. 生成MD5 keys,并拷贝到NTP目录并重命名:
sudo ntp-keygen -M
sudo cp ntpkey_MD5key_debian.xxxxx /etc/ntp.keys	

MD5 keys的内容形如:

# ntpkey_MD5key_lmde5.3915164985
# Thu Jan 25 17:49:45 2024
 
1 MD5 yk&c[A|$v+jMjHsa1}V(  # MD5 key
2 MD5 -";'Hb.PHua9i">|2sL]  # MD5 key
3 MD5 z":YG6EXmkpcic,4fiTu  # MD5 key
4 MD5 A1@_mRm0]=ocppFfA2)I  # MD5 key
5 MD5 T["Dok3]b9qzfRVFUyLG  # MD5 key
6 MD5 eL3m[WjW(RqT%Q'}8_42  # MD5 key
7 MD5 *;v9)SYs]8\dJr';@KXX  # MD5 key
8 MD5 Bfb(?2b0H,ED>A&]O=UN  # MD5 key
9 MD5 Gel48u{+[f/Kcg,nxf~_  # MD5 key
10 MD5 =R.!hs(,SjFC6duRS-/A  # MD5 key
11 SHA1 9f876c7c86a81195f0ca2d04f335347720a56add  # SHA1 key
12 SHA1 0b1732bad68f1f30c2ab4aed6477eb5b3e11ee9b  # SHA1 key
13 SHA1 b5be2fd1a4b99bfb02f25a0656be6ca2164055ca  # SHA1 key
14 SHA1 bf353554d3f6e5ad3fadcf331b98f6c3f65506e8  # SHA1 key
15 SHA1 3d871ef5440fa508755f04a7e9cf2db6d9a3a48e  # SHA1 key
16 SHA1 273685ffd408257867a210484fab4188a491243e  # SHA1 key
17 SHA1 3ae726fa67a1d94780c4af130f070db914fd1228  # SHA1 key
18 SHA1 14f9fb173c49c4df3882a32664745f2712206962  # SHA1 key
19 SHA1 a0deb6b837396f092c52f9644e425c4ddf0482ca  # SHA1 key
20 SHA1 e3ae5a912d1d214fe76676108c092820d640bc0e  # SHA1 key
  1. 修改NTP配置文件/etc/ntp.conf(以下示例仅保留使用项目,其中10.0.0.0 mask 255.255.255.0即为10.0.0.1~254):
# /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

# You do need to talk to an NTP server or two (or three).
server cn.pool.ntp.org
server ntp.ntsc.ac.cn
server ntp1.aliyun.com
server 127.127.1.0
fudge 127.127.1.0 stratum 5
 
# By default, exchange time with everybody, but don't allow configuration.
restrict default kod notrap nomodify nopeer noquery limited
 
# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1
 
# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap notrust

keys /etc/ntp.keys 
trustedkey 1 2 3 4 5 6
enable auth
  1. 启动或重启NTP服务,例如在debian为:
	/etc/init.d/ntp start
	/etc/init.d/ntp restart
  1. 等待NTP Server与其更上层授时服务器完成授时(一般约为几分钟),可使用以下命令观察其状态:
	watch ntpq -np
  1. 待NTP Server接受授时稳定后,可使用Client发起时间同步请求。

Client端

  1. 安装NTP客户端,例如在Debian可通过:
sudo apt install ntpdate

或在yocto通过修改配置build/conf/local.confg添加:

IMAGE_INSTALL += "ntpdate"
  1. 将Server端生成的MD5 keys拷贝到Client端NTP目录并重命名,在Server端:
sudo scp /etc/ntp.keys client_username@client_ip:~/ntp.keys

在Client端:

sudo cp ~/ntp.keys /etc/ntp.keys
  1. 待NTP Server接受授时稳定后,可使用Client发起时间同步请求(其中-vd为调试信息,可以不使用):
sudo ntpdate -vd -a 1 -k /etc/ntp.keys server_ip

如果身份验证及同步成功,会得到如下输出:

26 Jan 09:51:10 ntpdate[5025]: ntpdate 4.2.8p15@1.3728-o Wed Sep 23 11:46:38 UTC 2020 (1)
Looking for host 10.0.0.162 and service ntp
10.0.0.162 reversed to bogon
bogon forward check lookup fail: Name or service not known
host found : 10.0.0.162 (bogon)
transmit(10.0.0.162)
receive(10.0.0.162)
receive: authentication passed
transmit(10.0.0.162)
receive(10.0.0.162)
receive: authentication passed
transmit(10.0.0.162)
receive(10.0.0.162)
receive: authentication passed
transmit(10.0.0.162)
receive(10.0.0.162)
receive: authentication passed
 
server 10.0.0.162, port 123
stratum 3, precision -24, leap 00, trust 000
refid [111.230.189.174], root delay 0.060165, root dispersion 0.037521
reference time:      e95d8cd9.d40bb678  Fri, Jan 26 2024  9:43:53.828
originate timestamp: e95d8e94.d050bbca  Fri, Jan 26 2024  9:51:16.813
transmit timestamp:  e95d8e95.0fc893fa  Fri, Jan 26 2024  9:51:17.061
filter delay:  0.07553    0.16777    0.04654    0.24440   
               ----       ----       ----       ----      
filter offset: -0.435020  -0.391993  -0.452223  -0.357607 
               ----       ----       ----       ----      
delay 0.04654, dispersion 0.03546, offset -0.452223
 
26 Jan 09:51:17 ntpdate[5025]: adjust time server 10.0.0.162 offset -0.452223 sec
  1. 如果需要在每天的固定时间进行时间同步,可以通过使用root用户进行crontab定时任务实现
sudo crontab -u root -e

并编辑crontab任务(下例会在每天10:30进行时间同步操作):

30	10	*	*	*	ntpdate -vd -a 1 -k /etc/ntp.keys server_ip
  1. 如果需要更复杂的功能(如自定义报警或日志),可以考虑编写shell脚本并在crontab任务中进行定时调用完成。

Ref:很多参考打开的网页都在调试机浏览器,懒得倒腾过来了,都是公开内容,基本可通过关键字ntp/ntpdate/crontab/加密等搜到,自行搜索吧,此处略去。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值