Linux——NTP时间服务器

1.NTP协议

NTP服务器监听端口为UDP的 123,NTP服务器【Network Time Protocol(NTP)】是用来使计算机时间同步化的一种协议,

2.ntp服务配置

#配置文件
/etc/ntp.conf
#记录系统时间与BIOS事件偏差的文件driftfile
/var/lib/ntp/drift

#指定ntp服务器
server 202.120.2.101 prefer iburst minpoll 1 maxpoll 8
prefer:优先使用该时间服务器
burst:当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
iburst:当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。
minpoll和maxpoll指定NTP消息的最小和最大轮询间隔(以秒为单位)。默认minpoll 6 maxpoll 10,表示的时间间隔分别为2的6次(64秒)方和2的10次方(1024秒)
minpoll最小可设置为3(2的3次方=8s),maxpoll最大可设置为17(2的17次方=36h)
tinker [ allan allan | dispersion dispersion | freq freq | huffpuff huffpuff | panic panic | step step | stepout stepout ]

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

restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery
restrict 127.0.0.1
restrict ::1

#restrict–控制相关权限:
其中IP地址也可以是default ,default 就是指所有的IP
ignore:关闭所有的 NTP 联机服务
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust:客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery:不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap:不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer:用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
kod:访问违规时发送 KoD 包。
restrict -6 表示IPV6地址的权限设置。

2.1 服务端配置

vim /etc/ntp.conf
#新增-权限配置
restrict 127.127.1.0
restrict 192.168.0.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.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

2.2 客户端配置

#新增-权限配置
restrict 192.168.0.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.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

#新增时间服务器地址
server 192.168.31.223 prefer

2.3 测试

手动同步一次时间:ntpdate
启动NTP服务(客户、服务端):systemctl start ntpd
查看时间同步状况:ntpq -p
查看时间同步结果:ntpstat

3.ntp相关命令
3.1 ntpq命令

ntpq是查询ntpd工作状态的命令,一般使用“ntpq -np”参数。

[root@host3 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 210.72.145.44   .INIT.          16 u    -   64    0    0.000    0.000   0.000
+ntp1.ams1.nl.le 130.133.1.10     2 u   39   64   17  164.059    4.156  28.802
*sircabirus.von- 36.224.68.195    2 u   42   64   17  200.129   -2.154   5.885
-ntp2.flashdance 194.58.202.148   2 u   40   64   15  326.854   -2.366  29.832
+darwin.kenyonra 127.67.113.92    2 u   36   64   17  167.833    4.292  15.224

remote:即NTP主机的IP或主机名称。注意最左边的符号,如果由“+”则代表目前正在作用钟的上层NTP,如果是“*”则表示也有连上线,不过是作为次要联机的NTP主机。
t: 本地NTP服务器与远程NTP服务器的通信方式,u: 单播; b: 广播; l: 本地。
refid:参考的上一层NTP主机的地址
st:即stratum阶层,值越小表示ntp serve的精准度越高;
when:几秒前曾做过时间同步更新的操作;
Poll表示,每隔多少毫秒与ntp server同步一次;
reach:已经向上层NTP服务器要求更新的次数;
delay:网络传输过程钟延迟的时间;
offset:时间补偿的结果;
jitter:本地与remote同步的时间源的平均偏差(多个时间样本中的 offset的偏差,单位是毫秒),这个数值的绝对值越小,主机的时间就越精确

注意: NTP服务端重启后,客户机要等5分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。等待的时间可以通过命令 watch ntpq -p来监控。

3.2 ntptime命令

描述一个内核模型精确计时显示,调用ntp_gettime()读取和显示时间相关的内核变量。类似的显示可以使用ntpdc程序的kerninfo命令。

[root@host2 ~]# ntptime
ntp_gettime() returns code 0 (OK)
  time de5061d9.7034f000  Mon, Mar 12 2018 10:18:01.438, (.438308),
  maximum error 116705 us, estimated error 7753 us, TAI offset 0
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 0.000 us, frequency -13.286 ppm, interval 1 s,
  maximum error 116705 us, estimated error 7753 us,
  status 0x0 (),
  time constant 4, precision 1.000 us, tolerance 500 ppm,

需要关注的数据:
maximum error:最大误差
offset:系统时间偏移量

3.3 ntpstat

ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。所以,服务器启动后需要稍等下。
刚启动的时候:
[root@host2 ~]# ntpstat
timeout
[root@host2 ~]# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
连接成功后
[root@host2 scripts]# ntpstat
unsynchronised
polling server every 8 s

3.4 ntpdate命令

设置本地日期和时间。从指定的每个服务器获得了一些样本,并应用标准 NTP 时钟过滤器和选择算法来选择最好的样本。
安装ntpdate工具,安装ntp的同时会同步安装ntpdate命令
[root@localhost sbin]# yum install -y ntp
时间同步
ntpdate ntp_server_address
[root@localhost sbin]# ntpdate ntp.ubuntu.com
3 Sep 20:28:54 ntpdate[3407]: step time server 91.189.91.157 offset 31565561.035581 sec
使用ntpdate时候需要关闭ntpd服务;
虽然你的Linux防火墙允许123端口的udp协议,但是可能你的上层网络服务ISP是禁用特殊端口来传输ntp协议的。这时候使用-u 参数可。

4.ntpd和ntpdate的区别

ntpd在实际同步时间时是一点点的校准过来时间的,最终把时间慢慢的校正对(平滑同步)。而ntpdate不会考虑其他程序是否会阵痛,直接调整时间(“跃变”)。一个是校准时间,一个是调整时间。
“跃变”的危害:不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。
因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
————Blueicex 2020/05/17 11:45 blueice1980@126.com

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值