NTP时间服务器

1.1 NTP简介

NTP(Network Time Protocol,网络时间协议)是用来使网络中的计算机时间同步的一种协议。

NTP服务器利用NTP协议来提供时间同步服务。

1.2 环境准备

主机名 服务器/客户端 外网IP      内网IP

ntp-s Server      10.0.0.41 172.16.1.41

ntp-c Client      10.0.0.31 172.16.1.31

1.3 NTP服务端

1.3.1 安装NTP

CentOS6.7默认已安装ntp服务。如果没安装,就用yum安装即可。

[root@ntp-s ~]# rpm -qa ntp
ntp-4.2.6p5-5.el6.centos.x86_64

1.3.2 配置NTP

NTP服务默认的配置文件为:/etc/ntp.conf 

1.3.2.1 操作前备份

cp /etc/ntp.conf{,.ori}

1.3.2.2 修改配置文件

[root@ntp-s ~]# grep -vE "^$|#" /etc/ntp.conf ##<==过滤空行和注释行,前面三行为修改部分
restrict default nomodify
server ntp1.aliyun.com
server s1a.time.edu.cn
driftfile /var/lib/ntp/drift
restrict 127.0.0.1 
restrict -6 ::1
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys

1.3.2.3 NTP配置重要参数详解

driftfile /var/lib/ntp/drift  #<==记录上次的NTP server与上层NTP server联接所花费的时间
restrict default nomodify     #<==允许所有(nomodify)主机进行时间同步
server ntp1.aliyun.com        #<==同步时间服务器,阿里云NTP服务器
server s1a.time.edu.cn prefer #<==prefer表示优先,北京邮电大学NTP服务器

1.3.3 启动NTP

注意:如果有ntpdate定时任务,需要先关闭;否则两者会有冲突。

/etc/init.d/ntpd start
chkconfig ntpd on
chkconfig --list ntpd
ss -tunlp |grep :123

1.3.4 查看本机和上层服务器的时间同步结果

[root@ntp-s ~]# ntpq -p      ##<==查看本机和上层服务器的时间同步结果( p == peer )
remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*time5.aliyun.co 10.137.38.86     2 u    6   64    3   52.847  -115.81  52.877
202.112.10.60   202.118.1.46     2 u    3   64    3   64.112  -113.12  54.567

1.4 NTP客户端

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

[root@ntp-c ~]# date -s "20160606"   #<==修改当前时间
2016年 06月 06日 星期一 00:00:00 CST
[root@ntp-c ~]# ntpdate 172.16.1.41  #<==手动执行与NTP server进行时钟同步
28 Jul 15:55:27 ntpdate[8510]: step time server 172.16.1.41 offset 4550114.397444 sec
[root@ntp-c ~]# date                 #<==检查
2016年 07月 28日 星期四 15:55:30 CST

1.4.1 添加到定时任务

[root@ntp-c ~]# crontab -l#<==5分钟同步一次
#time sync by ChenDianHu at 2016-06-28
*/5 * * * * /usr/sbin/ntpdate 172.16.1.41 >/dev/null 2>&1

1.5 故障:ntp时间服务器,自动断开!

原因:

ntp本地时间与上级ntp时间差超过1000s,那么ntp进程就会退出并在系统日志文件中记录。

解决方法:

在配置文件/etc/ntp.conf的第一行加上这么一句

tinker panic 0

1.6 参考博文

http://blog.youkuaiyun.com/iloli/article/details/6431757

http://www.zyops.com/ntp