建NTP

Network Time Protocol(NTP,网络时间协议)用于同步它所有客户端时钟的服务。

NTP服务器将本地的时钟与一个外网公共的NTP服务器同步,然后作为时间服务器。使内网所有客户端能同步时钟。       

最大的好处就是相关系统上日志文件中的数据,如果网络中使用中央日志主机集中管理日志,得到的日志结果就更能反映真实情况。在同步了时钟的网络中,集中式的性能监控、服务监控系统能实时的反应系统信息,系统管理员可以快速的检测和解决系统错误。

ntp时间服务器是一个简单的常用的服务器,工作中我们只要做到会用就行,能搭建起来就可以了,不用太去深入研究ntp时间服务器的原理。

 

服务端

yum镜像站源设置(略)

 

yum源设置好,安装ntp软件

yum -y install ntp*

 

备份ntp文件

cp /etc/ntp.conf /etc/ntp.conf.bak

 

编辑ntp配置

vim /etc/ntp.conf

 

1)、第一步:局域网内192.168.1.131指定为ntp服务器

2)、第二到四步:添加3个公网ntp服务器

3)、第五到六步:以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

4)、以下配置缺一不可。

 

启动ntp服务

/etc/init.d/ntpd start         当前启动ntpd服务   

chkconfig  ntpd on         下次开机自启ntpd服务

 

查看ntp 服务器时间。之前时间是不对的,如下:

现在正常。

 

再检查时间服务器是否同步

 

当所有公网NTP服务器(不是本地服务器)的jitter值都为4000,并且reach和dalay的值是0时,就表示时间同步有问题。可能原因:

  1)服务器端的防火墙设置,阻断了123端口(可以用 iptables -t filter -A INPUT -p udp --destination-port 123 -j ACCEPT 解决)   

2)每次重启ntp服务器之后,大约3-5分钟客户端才能与服务端建立连接,建立连接之后才能进行时间同步,否则客户端同步时间时会显示    no server suitable for synchronization found的报错信息,不用担心,等会就可以了。

 

 

附录:

1、ntp配置详解

restrict default kod nomodify notrap nopeer noquery       restrict、default定义默认访问规则,nomodify禁止远程主机修改本地服务器   

restrict -6 default kod nomodify notrap nopeer noquery   notrap拒绝特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询     

restrict 127.0.0.1                      (这里的查询是服务器本身状态的查询)。   

restrict -6 ::1      

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  意思是指定的192.168.1.0--192.168.1.254的服务器都 可以使用ntp服务器来同步时间。  server 192.168.1.117                     这句也是手动添加的,可以将局域网中的指定ip作为局域网内的ntp服务器。   

server 0.centos.pool.ntp.org   

server 1.centos.pool.ntp.org                 这3个域名都是互联网上的ntp服务器,也还有许多其他可用的ntp服务器,能连上外网时,本 地会跟这几个ntp服务器上的时间保持同步。  

server 2.centos.pool.ntp.org                                

server  127.127.1.0     # local clock               当服务器与公用的时间服务器失去联系时,就是连不上互联网时,以局域网内的时间服务器为客 户端提供时间同步服务。 

fudge   127.127.1.0 stratum 10                         

 

2、国内常用的NTP服务器

0.cn.pool.ntp.org

1.cn.pool.ntp.org

2.cn.pool.ntp.org

3.cn.pool.ntp.org

ntp1.aliyun.com

ntp2.aliyun.com

ntp3.aliyun.com

ntp4.aliyun.com

ntp5.aliyun.com

ntp6.aliyun.com

ntp7.aliyun.com

 

 

客户端

yum镜像站源设置(略)

 

yum源设置好,安装ntp软件

yum -y install ntp*

 

同步时间

ntpdate   服务器IP或者域名

比如:ntpdate 192.168.1.131

[root@client yum.repos.d]# ntpdate 192.168.1.131

5 Sep 17:23:51 ntpdate[3136]: step time server 192.168.1.131 offset -28798.617728 sec

 

原时间不对

同步后时间正确了

 

 

 

 

posted @ 2018-12-02 16:21  鱼结冰  阅读(172)  评论(0编辑  收藏  举报