Linux NTP时间服务器

NTP 时间服务器

ntp也是一种协议

ntp软件(支持ntp协议)  CentOS6自带 CentOS7需要安装

chrony软件(支持ntp协议)   CentOS7自带

安装ntp

CentOS6系统自带ntp,CentOS7需要自己安装

[root@m02 ~]# yum -y install ntp
[root@m02 ~]# rpm -qa ntp
ntp-4.2.6p5-25.el7.centos.2.x86_64

配置ntp

权限参数含义

noquery:不提供ntp服务
notrap:不提供trap远程事件登录的功能
nomodiy:表示客户端不能更改ntp服务器的时间参数,但可以通过ntp服务器进行时间矫正
notrust:拒绝没有通过认证的客户端
nokod:kod技术可以组织一种dos攻击
ignore :关闭所有ntp服务

配置文件更改

更改之前可以备份一下,以防改错

[root@m02 ~]# vim /etc/ntp.conf
server ntp1.aliyun.com   加上这个,因为默认是国外的时间同步服务器,使用国外的相对于使用阿里的,速度对比可想而知。

加权限限制

格式:restrict IP 权限参数
IP可以为ip地址或者default
权限参数:nomodify notrap nopeer noquery
例如:restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap

ntp服务改后和改前配置文件对比

[root@m02 ~]# diff /etc/ntp.conf{,.ori}
18c18
< restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap
---
> 
21d20
< server ntp1.aliyun.com

启动ntp

[root@m02 ~]# systemctl start ntpd

服务端可以先查看一下

利用ntpq -p命令和ntpstat命令

[root@m02 ~]# ntpq -p     #显示上一级连接的服务器
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time5.aliyun.co 10.137.38.86     2 u   57   64  377    4.209    3.841   2.685
 time-d-b.nist.g .STEP.          16 u    -  512    0    0.000    0.000   0.000
[root@m02 ~]# ntpstat      #显示同步结果 现在的状态信息
synchronised to NTP server (182.92.12.11) at stratum 3 
   time correct to within 11 ms
   polling server every 64 s

客户端测试结果

客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。

而且同步有时会很慢

ntpdate -d 显示同步过程

[root@Cobbler ~]# ntpdate 172.16.1.62
16 Nov 11:42:34 ntpdate[1538]: adjust time server 172.16.1.62 offset -0.002221 sec
[root@Cobbler ~]# ntpdate -d 172.16.1.62
16 Nov 16:10:07 ntpdate[2058]: ntpdate 4.2.6p5@1.2349-o Wed Apr 12 21:24:06 UTC 2017 (1)
Looking for host 172.16.1.62 and service ntp
host found : 172.16.1.62
transmit(172.16.1.62)
receive(172.16.1.62)
transmit(172.16.1.62)
receive(172.16.1.62)
transmit(172.16.1.62)
receive(172.16.1.62)
transmit(172.16.1.62)
receive(172.16.1.62)
server 172.16.1.62, port 123
stratum 3, precision -24, leap 00, trust 000
refid [172.16.1.62], delay 0.02592, dispersion 0.00142
transmitted 4, in filter 4
reference time:    ddb7c566.e8e2acda  Thu, Nov 16 2017 16:05:58.909
originate timestamp: ddb7c665.aa402c9b  Thu, Nov 16 2017 16:10:13.665
transmit timestamp:  ddb7c665.abf2c8a8  Thu, Nov 16 2017 16:10:13.671
filter delay:  0.02592  0.02596  0.02597  0.02599 
         0.00000  0.00000  0.00000  0.00000 
filter offset: -0.00994 -0.00890 -0.00785 -0.00686
         0.000000 0.000000 0.000000 0.000000
delay 0.02592, dispersion 0.00142
offset -0.009942

16 Nov 16:10:13 ntpdate[2058]: adjust time server 172.16.1.62 offset -0.009942 sec

step和adjust的区别

[root@Cobbler ~]# date -s 20080101 
Tue Jan  1 00:00:00 CST 2008
[root@Cobbler ~]# ntpdate 172.16.1.62 
16 Nov 16:21:26 ntpdate[2085]: step time server 172.16.1.62 offset 311703675.400154 sec

说明: adjust和step,adjust同步时间的时候小于一秒,step同步时间的时候大于一秒

CentOS7自带软件chrony

配置文件

vim /etc/chrony.conf
内容和ntp是一样的,知识顺序颠倒了
systemctl status chronyd  查看状态
注意:chrony 不能和ntp同时存在 只有centos7上有chrony

 

posted @ 2018-01-09 10:19  酷酷的二连长  阅读(1197)  评论(0编辑  收藏  举报