NTP网络时间协议

1、确认ntp服务是否安装

Linux系统默认有安装NTP,若只有ntpdate而未见ntp,则需删除原有ntpdate重新安装. 如:

查看ntp安装情况:

image.png

删除 ntpdate : yum -y remove ntpdate-4.2.6p5-22.el7.centos.x86_64

安装 ntp : yum -y install ntp

2、配置ntp服务器

1)、配置前检查

配置之前确认时间和当前时间一致,可通过date命令手动进行设置

date -s 'yyyy-MM-dd HH-mm-ss'    #设置时间

hwclock -w    #系统时间同步到硬件

ntp端口为123,通过netstat命令查看端口对应的进程,特别注意防火墙是否有打开123端口,最好的处理办法就是关闭防火墙,如:

netstat -ntlp | grep 123    #查看123端口对应的进程

systemctl stop firewallds 和 systemctl disable firewallds    #关闭防火墙和开机自启动

2)、配置ntp服务器

如有两台Linux服务器,IP分别为10.10.10.111和10.10.10.222, 111的服务器做ntp server,222的服务器做ntp client.

配置ntp server端

vim /etc/ntp.conf

#与上级服务器所花费的时间存入指定文件
driftfile /var/lib/ntp/drift

#对ip设定权限 default为所有ip
restrict default nomodify notrap nopear noquery

restrict 10.10.10.111 nomodify notrap nopear noquery
restrict 10.10.10.201 mask 255.255.255.0 nomodify notrap nopear noquery

restrict 127.0.0.1
restrict ::1

#设定上级时间服务器
#国内时间服务器
server 0.cn.pool.ntp.org prefer
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
#centos系统默认上级时间服务器
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 127.127.1.0 #local clock
fudge 127.127.1.0 stratum 10

#
includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor 

 

启动ntpd服务并配置开机自启

systemctl start ntpd  #启动ntpd服务
systemctl enable ntpd  #配置开机自启

note: ntpd启动后, 客户端需要等待几分钟再同步时间服务器

配置ntp client端

vim /etc/ntp.conf

#与上级服务器所花费的时间存入指定文件
driftfile /var/lib/ntp/drift

#对ip设定权限 default为所有ip
restrict default nomodify notrap nopear noquery

restrict 10.10.10.222 nomodify notrap nopear noquery
restrict 10.10.10.201 mask 255.255.255.0 nomodify notrap nopear noquery

restrict 127.0.0.1
restrict ::1

#同步111时间服务器
server 10.10.10.111 minpoll 4 maxpoll 6 iburst
fudge 10.10.10.111 stratum 10

#
includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor 

note: 先关闭客户端ntpd服务,手动同步时间,再启动ntpd服务

systemctl stop ntpd    #停止ntpd服务
ntpdate -u 10.10.10.111   #同步时间服务器
systemctl start ntpd
systemctl enable ntpd

3)、ntp配置文件以及参数介绍

ntp.conf配置文件意义

driftfile /var/lib/ntp/drift    //和上层NTP服务器频率误差的记录文件
restrict IP mask . nomodify ... //指定可以和NTP通信的IP或者网段
server <ip|fqdn> [prefer]      //指定为本地服务器提供时间的上层NTP服务器的IP地址或者是FQDN。在指定多个上层NTP服务器时,使用prefer参数的服务器的优先级更高,将优先使用。如果没有指定prefer的话,则按照顺序从上往下,由高到低。如果要以本地的时间作为时间源的话,则是server 127.127.1.0
logfile /var/log/ntp            //指定日志文件
logconfig all                   //指定日志级别, info, event, all三种可以选择
broadcastclient key 4           //接收NTP广播消息
broadcast 192.168.0.1           //使用广播模式, 指定广播地址
crypto                          //启用公钥加密
key /etc/ntp/keys               //私钥文件
minpoll n                       //最小轮询时间,2的n次方(广播时每个客户端的轮询时间)
fudge <ip|fqdn> stratum <1-15>  //修改NTP服务器的stratum信息。如果stratum为16就认为该NTP服务器不可用。

restrict参数意义

restrict [ 客户端IP ]  mask  [ IP掩码 ]  [参数]

“客户端IP” 和 “IP掩码” 指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:

◆ ignore:拒绝连接到NTP服务器
◆ nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
◆ noquery: 不提供客户端的时间查询
◆ notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
◆ notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
◆ nopeer: 提供时间服务,但不作为对等体。
◆ kod: 向不安全的访问者发送Kiss-Of-Death报文。

server参数意义

server host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:

◆ key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
◆ version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
◆ prefer: 如果有多个server选项,具有该参数的服务器优先使用。
◆ mode: 指定数据报文mode字段的值。
◆ minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
◆ maxpoll:  指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
◆ iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。

 3、ntp相关命令介绍

1)、ntpdate命令:将客户端时间同步为时间服务器的时间

ntpdate -u 10.10.10.111   #客户端时间同步111时间服务器

ntpdate -u cn.pool.ntp.org #指定名字同步时间

hwclock --systohc(或-w)   #将系统时间同步到硬件时钟

2)、ntpq命令:查看NTP服务器,显示客户端和每个服务器的关系

image.png

“* ”:响应的NTP服务器和最精确的服务器。 
“+”:响应这个查询请求的NTP服务器。 
“blank(空格)”:没有响应的NTP服务器。 
“remote” :响应这个请求的NTP服务器的名称。 
“refid ”:NTP服务器使用的更高一级服务器的名称。 
“st”:正在响应请求的NTP服务器的级别。 
“when”:上一次成功请求之后到现在的秒数。 
“poll”:当前的请求的时钟间隔的秒数。 
“offset”:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms)。

note: 客户端同步server端时间服务器轮询时间长短,可通过minpoll和maxpoll两个参数配置调整

参考文档

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值