环境说明:
两台Linux虚拟机,系统版本:Centos 7.9,ntp版本4.2.6p5(更推荐chronyd)
NTP 服务端 IP地址:192.168.100.11
NTP 客户端 IP地址:192.168.100.12
服务端配置:
1、关闭防火墙、关闭selinux
# systemctl stop firewalld && systemctl disable firewalld

# setenforce 0
setenforce: SELinux is disabled
2、安装ntp服务
# yum -y install ntp

3、生成密钥,修改权限
# cd /etc/ntp
# ntp-keygen -M ##会自动生成软链接将ntp.key指向生成密钥

# chmod 600 /etc/ntp/ntp.keys
# chown ntp:ntp /etc/ntp/ntp.keys
4、修改配置文件
# cat /etc/ntp.conf
restrict 127.0.0.1 #允许IPv4本机使用NTP
restrict -6 ::1 #允许IPv6本机使用NTP
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap nopeer noquery #只允许192.168.100.0/24网段获取时间
server cn.ntp.org.cn iburst #同步公网国家授时中心同步时间
server ntp.aliyun.com iburst #同步公网阿里云时钟源时间同步
keys /etc/ntp/ntp.keys #指定认证密钥位置
trustedkey 11 #指定11编号密钥
requestkey 11 #时间同步请求必须用 11 编号密钥
controlkey 11 #NTP 控制命令必须用 11 编号密钥
server 127.127.1.0 #公网不可用,使用本机自己的时钟继续提供时间服务
fudge 127.127.1.0 stratum 10 #本地时钟层级为10,低于公网 NTP层级
driftfile /var/lib/ntp/drift #记录硬件时钟偏差,下次启动更快同步
logfile /var/log/ntp.log #ntp启动相关日志
5、启动服务,配置开机自启
# systemctl start ntpd && systemctl enable ntpd
6、确认NTP服务同步状态
# ntpq -p

客户端配置:
1、关闭防火墙、selinux,参考如服务端(如有严格端口访问限制自行配置)
2、安装ntp服务,参考服务端配置
3、获取密钥文件、配置文件权限
# scp root@192.168.100.11:/etc/ntp/ntp.keys /etc/ntp/
输入服务端系统密码
# chmod 600 /etc/ntp/ntp.keys
# chown ntp:ntp /etc/ntp/ntp.keys
4、修改配置配置文件
# cat /etc/ntp.conf
# 禁止外部主机访问本机
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# 允许本地访问
restrict 127.0.0.1
restrict -6 ::1
# 指向服务端,启用11号密钥认证
server 192.168.100.11 key 11 iburst
# 密钥配置(和服务端一致)
keys /etc/ntp/ntp.keys
trustedkey 11
# 漂移文件与日志
driftfile /var/lib/ntp/drift
logfile /var/log/ntp.log
5、启动服务,开机自启
# systemctl start ntpd && systemctl enable ntpd
6、确认同步服务端是否正常
# ntpq -p

交换机参考配置:
<H3C> system-view
[H3C] ntp-service enable
[H3C] ntp-service authentication enable
# 配置11号SHA1密钥(值需与服务端ntp.keys完全一致)
[H3C] ntp-service authentication-keyid 11 authentication-mode hmac-sha-1 simple a28ed27fefabc056b18947c36ebbe2b799027234
[H3C] ntp-service reliable authentication-keyid 11
# 指向服务端并使用11号密钥
[H3C] ntp-service unicast-server 192.168.100.11 keyid 11
[H3C] save
浙公网安备 33010602011771号