一、服务介绍

NTPNetwork Time Protocol,网络时间协议)是用来使计算机时间同步化的一种协议。它可以使计

算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,

WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议***。

二、运行模式

模式: C/S 模式

运行模式:

wKiom1ZF8xDzKFVvAABPhWp3Z_k567.png

三、NTP服务器环境的配置

1.实验环境

NTP服务端:localhost1.cn   IP192.168.10.63

NTP客户端:localhost2.cn   IP192.168.10.64


wKiom1ZF81CxL_P1AAAtl3auOfM248.png

 2NTP服务器的配置

[root@localhost ~]# rpm -ivh/mnt/Packages/ntpdate-4.2.6p5-1.el6.x86_64.rpm

[root@localhost ~]# /etc/init.d/ntpd  restart


[root@localhost ~]# vim /etc/ntp.conf            ntp 的配置文件

[root@localhost ~]# ntpdate server0.rhel.pool.ntp.org         同步ntp时间报错

13 Nov 18:33:47 ntpdate[8145]: the NTPsocket is in use, exiting

原因

出现该错误的原因是系统ntpd服务器正在运行中,通过 ps aux | grep ntpd 查看,会看到ntpd正在运行。

wKiom1ZF86WgroAhAAAnHl-fhuM895.png

解决办法

[root@localhost ~]# /etc/init.d/ntpd stop  停止服务


wKiom1ZF89DDeBd2AAA0hMAjgfo819.png

备注:这里需要虚拟机可以连接到外网


搭建一个内网NTP服务器,让内网服务器通过此NTP服务器进行时间同步

[root@localhost ~]# vim /etc/ntp.conf

11 # Permit all access over the loopbackinterface.  This could

 12 #be tightened as well, but to do so would effect some of

 13 #the administrative functions.

 14restrict 127.0.0.1  如果无同步时间,就和本地系统时间同步。

 15restrict -6 ::1

 16

 17 #Hosts on local network are less restricted.

 18 restrict 192.168.10.0mask 255.255.255.0 nomodify notrap

允许同步的本地局域网时间

 19

 20 # Use public serversfrom the pool.ntp.org project.

 21 # Please considerjoining the pool (http://www.pool.ntp.org/join.html).

 22 server 128.138.141.172

指定本NTP服务器的上游NTP服务器为128.138.141.172,并且设置为首先服务器。同步时间为,从上到下,写的越靠上,优先级越高。(写在所有server最前面IP地址为首先服务器,此服务器同步不了时间,寻找下一个NTP服务器)。此IP地址是日本福冈大学NTP服务器。  在原配置文件中添加一个外网NTP服务器

23 server 1.rhel.pool.ntp.org iburst

 24server 2.rhel.pool.ntp.org iburst

 25server 3.rhel.pool.ntp.org iburst

 

在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间。 ntpd 有一个自我保护设置: 如果本机与上源时间相差太大, ntpd 不运行.

[root@localhost ~]# ntpdate cn.pool.ntp.org

13 Nov 18:58:31 ntpdate[8214]: adjust timeserver 202.112.29.82 offset 0.000931 sec

 

[root@localhost ~]# /etc/init.d/ntpd start  启动服务


wKioL1ZF9GHQsAMxAABUCt8NvpM546.png


[root@localhost ~]# ntpstat

synchronised to NTP server(128.138.141.172) at stratum 2   #可以看到我们当前在2层。

  time correct to within 470 ms   # ms毫秒是一种较为微小的

  polling server every 128 s     轮询时间

 

[root@localhost ~]# ntpq –p  列出我们的NTP服务器与上游服务器的连接状态

    remote           refid      st t when poll reach   delay  offset  jitter

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

*utcnist2.colora .ACTS.           1 u 466  128  150 337.011   14.735  60.291

 dns.sjtu.edu.cn .INIT.          16 u    - 1024   0    0.000    0.000  0.000

+dns1.synet.edu. 202.118.1.46     2 u 233  128  176 115.893   99.310  68.327

参数说明

指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:

remote:即remote - 本机和上层ntpip或主机名,“+”表示优先,“*”表示次优先。

refid:参考的上一层NTP主机的地址

st:即stratum阶层             poll:下次更新在几秒之后          offset:时间补偿的结果

客户端同步时间

说明:在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间。 ntpd 有一个自我保护设置: 如果本机与上源时间相差太大, ntpd 不运行.

[root@localhost ~]# date

2015 11 13 星期五 20:11:34 CST

[root@localhost1 ~]# date  同步时间前

2015 11 13 星期五 20:09:39 CST

[root@localhost1 ~]# ntpdate 192.168.10.63  同步时间

13 Nov 20:12:53 ntpdate[7737]: step timeserver 192.168.10.63 offset 130.847558 sec

[root@localhost1 ~]# date

2015 11 13 星期五 20:13:00 CST

拓展

linux系统时间和BIOS时间是不是一定一样?

查看硬件BIOS时间:

hwclock-r    :读出BIOS的时间参数

hwclock-w    :将当前系统时间写入BIOS中。

 

为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。

同步时间,可以使用ntpdate命令,也可以使用ntpd服务。

通常采用crontab 做计划任务

0 12 *  * * /usr/sbin/ntpdate 0.rhel.pool.ntp.org