安装NTP软件包
一、安装NTP软件包
首先确认系统是否已预装NTP相关组件。执行以下命令检查已安装的包: rpm -qa | grep ntp
若未安装,可使用YUM工具进行一键安装: yum -y install ntp
二、关键参数解析
ignore:该指令用于关闭所有NTP网络连接服务,禁止任何时间同步请求。
nomodify:限制客户端无法修改服务端的时间参数,但允许客户端利用服务端进行网络校时操作。
notrust:除非客户端通过身份认证,否则其来源将被视为不可信任的子网,拒绝提供服务。
noquery:禁止客户端使用ntpq、ntpc等命令查询NTP服务器的状态信息。
notrap:拒绝为匹配的主机提供模式6控制消息陷阱服务,防止远程事件日志记录程序滥用该功能。
nopeer:阻止主机尝试与服务器建立对等关系,从而防止恶意或欺诈性服务器干扰本地时钟。
kod:当检测到访问违规时,向客户端发送KoD(Kiss-o'-Death)数据包以终止连接。
三、修改NTP配置文件
在调整配置前,建议先查看默认配置文件的内容,了解初始状态。
# grep ^[^#] /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
若需允许所有IP地址的客户机进行时间同步,可参考如下修改后的配置片段(见资源a0): # grep ^[^#] /etc/ntp.conf driftfile /var/lib/ntp/drift restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1 server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor
# grep ^[^#] /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap
restrict 127.0.0.1
restrict ::1
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
若仅需允许特定网段(如192.168.1.0/24)的客户机同步时间,配置应调整为(见资源a2): # grep ^[^#] /etc/ntp.conf driftfile /var/lib/ntp/drift restrict default nomodify notrap nopeer noquery restrict 127.0.0.1 restrict ::1 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.ntp.org iburst server 3.centos.pool.ntp.org iburst includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor
# grep ^[^#] /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.ntp.org iburst
server 3.centos.pool.ntp.org iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor
四、启动NTP服务及防火墙策略
完成配置后,需启动NTP服务并设置为开机自启。同时,配置防火墙以允许UDP 123端口通信,并处理SELinux限制。此外,还需配置BIOS与系统时间同步。 systemctl start ntpd systemctl enable ntpd iptables -A INPUT -p UDP -i eno16777736 -s 192.168.1.0/24 --dport 123 -j ACCEPT setsebool -P ntp_disable_trans 1 # SELinux设置 vi /etc/sysconfig/ntpd # 允许BIOS与系统时间同步,添加下面一行。 SYNC_HWCLOCK=yes
五、检测NTP服务状态
首先检查NTP服务进程是否正常运行,可通过netstat命令监听UDP 123端口来验证(见资源a3): # netstat -tlunp | grep ntp udp 0 0 192.168.1.101:123 0.0.0.0:* 2563/ntpd udp 0 0 127.0.0.1:123 0.0.0.0:* 2563/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 2563/ntpd udp6 0 0 fe80::20c:29ff:fe7b:123 :::* 2563/ntpd udp6 0 0 ::1:123 :::* 2563/ntpd udp6 0 0 :::123 :::* 2563/ntpd
# netstat -tlunp | grep ntp
udp 0 0 192.168.1.101:123 0.0.0.0:* 2563/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 2563/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 2563/ntpd
udp6 0 0 fe80::20c:29ff:fe7b:123 :::* 2563/ntpd
udp6 0 0 ::1:123 :::* 2563/ntpd
udp6 0 0 :::123 :::* 2563/ntpd
接着,使用ntpstat命令查看当前服务器是否与上层NTP源保持连通(见资源a4): # ntpstat synchronised to NTP server (120.25.108.11) at stratum 3 time correct to within 99 ms polling server every 64 s
# ntpstat
synchronised to NTP server (120.25.108.11) at stratum 3
time correct to within 99 ms
polling server every 64 s
最后,通过ntpq -p命令详细查看与上层NTP服务器的同步状态、延迟及抖动情况(见资源a5): # ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== news.neu.edu.cn .INIT. 16 u - 64 0 0.000 0.000 0.000 x202.118.1.130 202.118.1.47 2 u 7 64 377 153.659 9.605 19.941 *time4.aliyun.co 10.137.38.86 2 u 10 64 377 39.666 -47.661 15.944 remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先 refid - 参考上一层ntp主机地址 st - stratum阶层 when - 多少秒前曾经同步过时间 poll - 下次更新在多少秒后 reach - 已经向上层ntp服务器要求更新的次数 delay - 网络延迟 offset - 时间补偿 jitter - 系统时间与bios时间差
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
news.neu.edu.cn .INIT. 16 u - 64 0 0.000 0.000 0.000
x202.118.1.130 202.118.1.47 2 u 7 64 377 153.659 9.605 19.941
*time4.aliyun.co 10.137.38.86 2 u 10 64 377 39.666 -47.661 15.944
remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
refid - 参考上一层ntp主机地址
st - stratum阶层
when - 多少秒前曾经同步过时间
poll - 下次更新在多少秒后
reach - 已经向上层ntp服务器要求更新的次数
delay - 网络延迟
offset - 时间补偿
jitter - 系统时间与bios时间差
六、配置客户端时间同步
在客户端机器上,首先手动执行ntpdate命令进行一次性时间同步。假设NTP服务器IP为192.168.1.101:
ntpdate 192.168.1.101 #192.168.1.101为ntp服务器IP地址
同步完成后,需将系统时间写入硬件时钟(BIOS),以确保重启后时间准确。
hclock -w
为实现自动定期同步,可将ntpdate命令加入crontab计划任务中。
编辑/etc/crontab文件,添加如下行,例如每天8点30分执行同步并写入硬件时钟: vi /etc/crontab 30 8 * * * root /usr/sbin/ntpdate 192.168.1.101; /sbin/hwclock -w
修改crontab后,需重启crond服务以使配置生效。
service crond restart
以上就是小编为大家带来的linux配置ntp服务器的方法全部内容了,希望大家多多支持本站~