linux-ntp时间同步

42 篇文章 3 订阅

一、ntp服务概述

NTP(Network TimeProtocol,网络时间协议)是用来使计算机时间同步的一种协议。它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),切可介由加密确认的方式来防止恶意的协议攻击。

1. 使用的端口

默认NTP服务端口:UDP/123

2. ntp和ntpdate区别

  1. 两个服务都是centos自带的(centos7中不自带ntp)。ntp的安装包名是ntp;ntpdate的安装包是ntpdate。他们并非由一个安装包提供。
  2. ntp守护进程为ntpd,配置文件是/etc/ntp.conf
  3. ntpdate用于客户端的时间矫正,非NTP服务器可以不启动NTP。

二、需求:集群中(所有节点)设置1个server,其他client从server同步时间,server从外网或者本地同步时间

三、安装(所有节点)

yum安装方式:yum install -y ntp,rpm -q ntp

tar包编译安装方式:

下载tar包:http://www.ntp.org/downloads.html

tar -zxvf ntp-4.2.8p8.tar.gz
cd ntp-4.2.8p8
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make && make install

启动并设置开自,systemctl start ntpd,systemctl enable ntpd

四、server

备份原来的cp /etc/ntp.conf /etc/ntp.conf.bak
vi /etc/ntp.conf

driftfile /var/lib/ntp/drift
 
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
 
restrict 127.0.0.1 
restrict -6 ::1
 
#允许192.168.100网段机器同步时间,也就是集群中节点ip的前三位,第四位写0
restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap
 
#远程时间服务器的地址,有优先级
server 210.72.145.44 perfer #中国国家授时中心
server 1.cn.pool.ntp.org
server time.windows.com #微软windows的时间服务器
 
#允许上层服务器主动修改本机时间
restrict 210.72.145.44 nomodify notrap noquery
restrict 1.cn.pool.ntp.ofg nomodify notrap noquery
 
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge  127.127.1.0 stratum 10 
 
includefile /etc/ntp/crypto/pw
 
keys /etc/ntp/keys

1. server配文

# vim /etc/ntp.conf
====================================================================================
#设置默认策略为拒绝所有访问方式的请求
restrict default ignore
#允许哪些机器同步
restrict 127.0.0.1
restrict -6 ::1
#允许局域网内机器同步时间,nomodify拒绝让他们修改服务器上的时间
restrict 172.xx.xx.xx mask 255.255.255.0 nomodify notrap 
#将该服务器的本地时间作为ntp server的时间,如果其他客户端同步,将同步这台服务器的时间
server 127.127.1.0
#stratum这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0
fudge 127.127.1.0 stratum 2
#每一个system clock的频率都有小小的误差,这个就是为什么机器运行一段时间后会不精确. NTP会自动来监测我们时钟的误差值并予以调整.
#但问题是这是一个冗长的过程,所以它会把记录下来的误差先写入driftfile.这样即使你重新开机以后之前的计算结果也就不会丢失了
driftfile /var/lib/ntp/drift
# Enable public key cryptography.
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
# Disable the monitoring facility to prevent amplification attacks using ntpdc
disable monitor
====================================================================================

关于restrict格式如下:

其中parameter如下:

ignore:忽略所有类型的ntp连接
nomodify:不允许客户端修改服务器的时间参数,但是允许客户端透过这部主机进行时间校验。
noquery: 不允许客户端进行时间校验。
notrap:不提供trap时间登录
notrust:拒绝没有认证的客户端

五、client

备份原来的cp /etc/ntp.conf /etc/ntp.conf.bak
vi /etc/ntp.conf

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift

# Enable this if you want statistics to be logged.

#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats

filegen loopstats file loopstats type day enable

filegen peerstats file peerstats type day enable

filegen clockstats file clockstats type day enable


# By default, exchange time with everybody, but don't allow configuration.

restrict -4 default kod notrap nomodify nopeer noquery limited

restrict -6 default kod notrap nomodify nopeer noquery limited

 

# Local users may interrogate the ntp server more closely.

restrict 127.0.0.1

restrict ::1

 
#设置server,ip和主机名均可

server 192.168.100.105

# Needed for adding pool entries

restrict source notrap nomodify noquery

六、查看

ntpq -p

在这里插入图片描述
在这里插入图片描述
说明:

remote: 它指的就是本地机器所连接的远程NTP服务器
refid: 它指的是给远程服务器(e.g. 193.60.199.75)提供时间同步的服务器
st: 远程服务器的层级别(stratum). 由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端. 所以服务器从高到低级别可以设定为1-16. 为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.
when: 一个计时器用来告诉我们还有多久本地机器就需要和远程服务器进行一次时间同步
poll: 本地机和远程服务器多少时间进行一次同步(单位为秒). 在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围.之后poll值会逐渐增大,同步的频率也就会相应减小
reach: 这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
delay: 从本地机发送同步要求到服务器的round trip time
offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别. offset越接近于0,我们就和服务器的时间越接近
jitter: 这是一个用来做统计的值. 它统计了在特定个连续的连接数里offset的分布情况. 简单地说这个数值的绝对值越小我们和服务器的时间就越精确

ntpstat,要等5分钟左右才能同步完

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值