NTP是用来使计算机时间同步化的一种协议,全称是Network Time Protocol。它可以在大规模的设备范围内同步矫正时间到几ms级别的精度,在网络稳定的局域网内,精度甚至可以达到微秒级别。 ntp端口使用的是 123。此次安装操作系统是在centos7.9环境下。
NTP时间同步配置需要配置server端与client端,server端是源时间地址,client端的时间都会与server端保持一致。
server端IP:192.168.1.201
client端IP:192.168.1.201-205
1、server端的配置
server端可以作为时间同步的源服务器,其他的服务器可以使用ntp同步到server端的时间。配置之前需先安装ntp,ntpdate软件,执行如下命令即可。
yum -y install ntp.x86_64
yum -y install ntpdate.x86_64
修改ntp配置文件,vim /etc/ntp.conf
注释以下配置:
#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
根据个人需要添加以下配置:
#日志文件
logfile /var/log/ntpd.log
#时间服务器列表
server server 122.207.105.253 iburst
#当外部时间不可用时,使用本地时间
server 127.127.1.0 fudge
127.127.1.0 stratum 8
#允许上层时间服务器主动修改本机时间
restrict 122.207.105.253 nomodify notrap noquery
保存退出,关闭防火墙,重启ntp服务、加入开启自启:
systemctl stop firewalld
systemctl enable ntpd
systemctl restart ntpd
如果需要手动同步时间的话,需要先手动关闭ntpd服务,然后再进行手动同步:
systemctl stop ntpd
ntpdate 122.207.105.253
systemctl start ntpd
2、client端配置
client端同样也需要安装以下软件包:
yum -y install ntp.x86_64
yum -y install ntpdate.x86_64
修改ntp配置文件,vim /etc/ntp.conf,注释以下内容:
#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
按照个人需求添加如下配置:
#源时间服务器
server 192.168.1.201 iburst
restrict 192.168.1.201 nomodify notrap noquery #允许上层时间服务器修改本地机器时间
#若外部时间不可用,采用本地时间
server 127.127.1.0 fudge
127.127.1.0 stratum 8
修改/etc/ntp/step-tickers文件,注释掉原有内容,添加如下内容:
#0.centos.pool.ntp.org
192.168.1.201
修改完之后保存退出,关闭防火墙,重启ntp服务、加入开启自启:
systemctl stop firewalld
systemctl enable ntpd
systemctl restart ntpd
如果需要手动同步时间的话,需要先手动关闭ntpd服务,然后再进行手动同步:
systemctl stop ntpd
ntpdate 192.168.1.201
systemctl start ntpd
至此时间同步服务器配置完成。
3、常见报错处理
(1)同步时ntpdate -d 192.168.1.201报错,
192.168.1.201: Server dropped: no data
或者
192.168.1.201: Server dropped: strata too high
解决办法,在/etc/ntp.conf中添加如下内容即可:
server 127.127.1.0 fudge
127.127.1.0 stratum 8
(2)no server suitable for synchronization found
防火墙未关闭,关闭即可。
systemctl stop firewalld