NTP服务器企业实战

NTP服务器企业实战

 

NTP服务器简介

NTP(Network Time Protocol):网络时间协议,用来同步网络中的网络设备、服务器、计算机和其它终端设备的时间协议。NTP服务器是用于局域网内设备时间同步使用,可以保证局域网内所有服务器与时间服务器的时间保持一致。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

常见的互联网时间服务器:

上海交通大学的NTP

ntp.sjtu.edu.cn

aliyun的NTP

ntp1.aliyun.com

ntp2.aliyun.com

ntp3.aliyun.com

ntp4.aliyun.com

ntp5.aliyun.com

ntp6.aliyun.com

ntp7.aliyun.com

NTP服务器配置

NTP服务器: 10.0.0.240

NTP客户端: 10.0.0.8

1、安装NTP软件包

yum install -y ntp ntpdate

2、NTP的配置文件详解

#在与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文#件内

driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery

#开启内部递归网络接口lo端口
restrict 127.0.0.1
restrict -6 ::1
#允许内网其他机器同步时间,但不能修改NTP服务器时间参数
restrict 10.0.0.0 mask 255.255.255.0 nomodify
#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
#定义使用的上游 ntp服务器,将原来的注释
server ntp1.aliyun.com
server time1.aliyun.com
#允许上层时间服务器主动修改本机时间
restrict time1.aliyun.com nomodify notrap noquery
restrict ntp1.aliyun.com nomodify notrap noquery
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

#广播延迟时间

#broadcastdelay 0.008

3、启动ntp服务

Centos6: /etc/init.d/ntpd start

Centos7: systemctl start ntpd

4、服务器端显示时间节点

[root@yum etc]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

 120.25.115.20   10.137.53.7      2 u   23   64    1   34.125   43.874   0.000

 203.107.6.88    10.137.55.181    2 u   22   64    1   50.264   51.479   0.000

*LOCAL(0)        .LOCL.          10 l   21   64    1    0.000    0.000   0.000

5、使用客户端10.0.0.8来测试时间服务器

1)   先客户端时间更改为2009-01-01

[root@mysql ~]# date -s "20090101"

Thu Jan  1 00:00:00 CST 2009

[root@mysql ~]# date

Thu Jan  1 00:00:03 CST 2009

2)   使用时间服务器来同步时间

[root@mysql ~]# ntpdate 10.0.0.240

 3 Oct 10:11:06 ntpdate[23572]: step time server 10.0.0.240 offset 339329339.566811 sec

[root@mysql ~]# date

Thu Oct  3 10:11:10 CST 2019

6、客户端配置crontab每过1小时自动同步ntp服务器1次

[root@mysql ~]# crontab -e

* 1 * * * /sbin/ntpdate 10.0.0.240 >> /usr/local/logs/crontab/ntpdate.log

[root@mysql ~]# crontab -l

* 1 * * * /sbin/ntpdate 10.0.0.240 >> /usr/local/logs/crontab/ntpdate.log

7、客户端显示时间节点

[root@mysql ~]# ntpq -p

     remote           refid      st t when poll reach   delay   offset  jitter

==============================================================================

 10.0.0.240      LOCAL(0)        11 u    1   64    1    1.192    0.144   0.000

8、客户端同步问题

[root@mysql ~]# ntpdate 10.0.0.240

 1 Jan 00:00:13 ntpdate[23539]: the NTP socket is in use, exiting

改为题为存在已经启动的ntpdate服务,重复启动导致的

处理方案:通过lsof -i:123 来查看ntp服务的进程后,kill ntp的服务进程即可

[root@mysql ~]# lsof -i:123

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

ntpd    23590  ntp   16u  IPv4 179721      0t0  UDP *:ntp

ntpd    23590  ntp   17u  IPv6 179722      0t0  UDP *:ntp

ntpd    23590  ntp   18u  IPv4 179727      0t0  UDP localhost:ntp

ntpd    23590  ntp   19u  IPv4 179728      0t0  UDP mysql:ntp

ntpd    23590  ntp   20u  IPv4 179729      0t0  UDP mysql:ntp

ntpd    23590  ntp   21u  IPv4 179730      0t0  UDP mysql:ntp

ntpd    23590  ntp   22u  IPv6 179731      0t0  UDP localhost:ntp

ntpd    23590  ntp   23u  IPv6 179732      0t0  UDP mysql:ntp

ntpd    23590  ntp   24u  IPv6 179733      0t0  UDP mysql:ntp

ntpd    23590  ntp   25u  IPv6 179734      0t0  UDP mysql:ntp

kill掉ntp服务进程

[root@mysql ~]# kill -9 23590

重新同步成功

[root@mysql ~]# ntpdate 10.0.0.240

 3 Oct 10:21:37 ntpdate[23627]: adjust time server 10.0.0.240 offset -0.000162 sec

猜你喜欢

转载自www.cnblogs.com/yaokaka/p/11619262.html