目录

1.NTP简介... 1

2.NTP器安装... 1

3. 配置NTP... 1

4. 启动NTP服务器... 2

5. 客户机时间同步... 3

6.注意事项... 3

 

1.NTP简介

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。

NTP服务器就是利用NTP协议提供时间同步服务的。

参考博文:http://blog.oldboyedu.com/ntp/


2.NTP器安装

查看系统是否安装ntp服务

[root@oldboy ~]# rpm -qa ntp

ntp-4.2.6p5-5.el6.centos.x86_64


若没有安装可以使用YUM命令进行安装即可

[root@oldboy ~]# yum -y install ntp


3. 配置NTP

ntp服务器默认是不运行客户端进行时间同步的,所有我们需要配置文件设置允许

[root@oldboy ~]# vim/etc/ntp.conf        NTP服务的默认配置文件

#restrict default kod nomodify notrap nopeernoquery   注释此行内容

# nomodify允许客户端不可以修改时间参数但是可以同步时间服务器,添加以下内容

restrict default 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

添加新的时间同步源(以下时间服务器选择一个即可)

server ntp1.aliyun.com

server time.nist.gov

 

4. 启动NTP服务器

客户端先将系统优化是定时任务自动同步时间服务器的定时任务注释,如果不注释的话可能会冲突

提示:本地的ntp时间服务器会跟互联网的时间服务器冲突,只能选择一个进行同步。

[root@oldboy ~]# crontab -e

#time sync bync oldboy 2015-5-27

#*/5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null2>&1


启动NTP时间同步服务

[root@oldboy ~]# /etc/init.d/ntpd start

Startingntpd:                                 [  OK  ]

设置ntp服务器开机自动启动

[root@oldboy ~]# chkconfig ntpdon   

[root@oldboy ~]# cchkconfig --list ntpd

ntpd           
0:off   1:off  2:on   3:on    4:on   5:on   6:off

 

[root@oldboy ~]# ntpq -p             查看ntp服务器的详细状态

    remote       refid     st t when poll reach   delay  offset  jitter

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

*132.163.4.101  .ACTS.     1 u  25   64   3  269.478 -18.374   6.065


显示网络时间同步状态

[root@oldboy ~]# ntpstat            查看ntp服务的状态

synchronised to NTP server (132.163.4.101) atstratum 2

   timecorrect to within 2078 ms

   pollingserver every 64 s


设置通过NTP的日期和时间

[root@oldboy ~]# ntpdate 10.0.0.101

 8 Jun 00:20:57 ntpdate[1441]: the NTP socketis in use, exiting


5. 客户机时间同步

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

[root@oldboy ~]# ntpdate 10.0.0.101

 8 Jun 00:22:39 ntpdate[12852]: no serversuitable for synchronization found


客户端同步时间的方法:

[root@zyops ~]# ntpdate 10.0.0.9

 7 Dec 18:40:16 ntpdate[1453]: step timeserver 10.0.0.9 offset 40.880807 sec

 # 将命令放入计划任务即可。

 

windows同步时间演示

NTP时间服务器搭建_NTP


6.注意事项

(1)当我们搭建NTP服务器的时候一定要先将后面优化的时间同步定时任务注释,不然会产生冲突


常见错误1:

No association ID’s returned

检查网络是否连通:ping www.baidu.com


常见错误2:

[root@oldboy ~]# ntpdate 10.0.0.102
Error resolving 10.0.0.102: Servname not supported for ai_socktype (-8)
 7 Jun 22:43:32 ntpdate[1411]: Cant find host 10.0.0.102: Servname not supported for ai_socktype (-8)
 7 Jun 22:43:32 ntpdate[1411]: no servers can be used, exiting


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

可能原因:客户端缺少什么配置文件

检查客户端是否可以与互联网的时间服务器同步,若不行,就是客户端的问题!排查客户端故障