原创

linux 配置ntp时间同步

因服务器未开通公网服务,导致服务器不能与公网同步时间,所以决定搭建一台内网ntp服务器,以下为操作记录,供参考。

注:本次使用系统为centos 7。

一、安装ntp服务

首先查看以下系统中是否安装了ntp服务,centos7应该是默认安装了:

rpm -qa | grep ntp

如果未安装,使用以下命令安装:

yum -y install ntp

安装完结果如下:

二、修改配置文件

安装完成后,会在/etc/目录下生成配置文件ntp.conf

修改其中以下条目:

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

2.添加以下行进配置文件:

#据说127.127.1.0为本地时钟,待验证server 127.127.1.0
Fudge 127.127.1.0 stratum 10

3.添加以下行进配置文件(ip处替换成本机ip,即内网ntp服务器所在ip):

#此条意为允许该网段所有服务器连接本机获取时间,但禁止修改本机时间restrict 10.8.1.0 mask 255.255.255.0 nomodify notrap#允许此ip修改本机时间
restrict 10.8.1.160

三、启动ntp服务器,并设置开机自启

#启动
systemctl start ntpd
#设置开机自启
systemctl enable ntpd
#查看服务状态
systemctl status ntpd

四、配置其他服务器ntp时间同步

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

2.添加以下条目进配置文件:

#允许此ip修改本机时间
restrict 10.8.1.160
#获取时间ntp服务器ip
server 10.8.1.160
fudge 10.8.1.160 stratum 10


五、查看ntp时间同步状态

1.使用ntpq -p查看时间同步状态:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*10.8.1.160   LOCAL(0)        11 u  526 1024  377    0.547   -0.046   0.092

remote:远程ntp服务器ip或主机名,带“*”的表示本地NTP服务器与该服务器同步
refid:远程NTP服务器使用的上一级ntp服务器的IP地址
st:ntp服务器所在stratum阶层
t:本地NTP与远程NTP服务器的通信方式。u:单播;b:广播;I:本地
when:上次成功同步时间距现在有多少秒
poll:本地NTP与远程NTP服务器同步的时间间隔。
reach:这是一个八进制的值,用来测试衡量前八次查询是否成功和服务器连接。377表示都成功,0表示不成功
delay:网络延迟
offset:本地NTP与远程NTP服务器的时间偏移
jitter:最近两次有变化的offset差的绝对值

2.使用ntpstat查看ntp时间同步状态,刚配置完成需要十五分钟左右等待才会出现如下结果,表示ntp服务正常:

synchronised to NTP server (10.209.22.160) at stratum 12 
   time correct to within 31 ms
   polling server every 1024 s

扩展说明



正文到此结束
温馨提示:
本文最后更新于 2024年05月01日,已超过 16 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我
本文目录