NTP协议是通过服务器和客户机的方式进行的,通讯的过程是:

1.首先,NTP服务器需要启动这个NTP服务

2.NTP客户机会向 NTP服务器发送出调校时间的信息

3.然后 NTP服务器会送出目前的标准时间给NTP客户机

4.NTP客户机接收了来自NTP服务器 的时间后,会据以调整自己的时间,以实现网络校时。

虽然网络上有很多公开的NTP 服务器,但在使用的时候需要注意。由于网络传输是有延时的,当NTP 客户机对一个延时比较厉害的NTP 服务器请求后,直到最后NTP 客户机获得时间,期间可能会出现由于网络延时而导致的的时间差。所以,建议大家选择离自己最近的NTP 服务器作为校验的标准。

 

搭建过程:

 

server 端 1, grep -v  ^# /etc/ntp.conf |grep  -v  ^$

 

.[root@ntpserver ~]# grep -v ^# /etc/ntp.conf |grep -v ^$

2.driftfile /var/lib/ntp/drift

3.restrict default kod nomodify notrap nopeer noquery

4.restrict -6 default kod nomodify notrap nopeer noquery

5.restrict 127.0.0.1

6.restrict -6 ::1

7.server 0.rhel.pool.ntp.org

8.server 1.rhel.pool.ntp.org

9.server 2.rhel.pool.ntp.org

10.includefile /etc/ntp/crypto/pw

11.keys /etc/ntp/keys

RHEL系统默认与互联网的三个上游的NTP服务器0.rhel.pool.ntp.org1.rhel.pool.ntp.org2.rhel.pool.ntp.org同步时间。换句话说NTP服务器默认也是NTP客户机。可以将上图中的第789行的服务器地址改成当地稳定并较近的服务器地址。本文假定上述三台服务器已经符合要求,不再修改了。此外RHEL系统的NTP服务器默认情况下是不允许任何远程NTP客户机与之同步时间的。需要修改NTP服务器的配置文件ntp.conf才可以让远程客户机同步时间。

将文件/etc/ntp.conf中上图中所示的第4行注释掉(前添加“#”), 以允许任意客户机获取时间数据。如下图所示:

在RHEL6.3<wbr>实现NTP服务

只允许特定网段的NTP客户机同步时间。假设NTP客户机都在192.168.1.0/24这一网段。则需要在ntp.conf中增加一行,如下图所示:

在RHEL6.3<wbr>实现NTP服务

restrict语法:

restrict IP mask子网掩码 参数

说明:

IP是允许或不允许访问的地址,配合掩码可以对一段网段使用;

参数包括:

§ignore:关闭所有NTP联网服务

§nomodify:客户端不能修改服务端的时间,但可以作为客户端的校正服务器

§notrust:除通过认证外,否则指定的网段为不信任网段

§noquery:不提供时间查询

NTP服务器上先执行ntpdate命令与上游NTP服务器同步时间,再启动ntpd服务,并设置开机自动启动ntpd服务。具体过程如下:

[root@ntpserver ~]# ntpdate 0.rhel.pool.ntp.org

Feb 22:28:34 ntpdate[2887]: adjust time server 202.112.29.82 offset 0.020157 sec

[root@ntpserver ~]# chkconfig ntpd on [root@ntpserver ~]# service ntpd start

Starting ntpd: [ OK ]

[root@ntpserver ~]# netstat -ntupl |grep ntp 

udp 0 0 192.168.1.101:123 0.0.0.0:* 2901/ntpd 

udp 0 0 127.0.0.1:123 0.0.0.0:* 2901/ntpd 

udp 0 0 0.0.0.0:123 0.0.0.0:* 2901/ntpd 

udp 0 0 ::1:123 :::* 2901/ntpd 

udp 0 0 fe80::20c:29ff:feae:a4e1:123 :::* 2901/ntpd 

udp 0 0 :::123 :::* 2901/ntpd 

NTP服务器上执行netstat命令,可以看到ntp服务相关的udp 123端口已经处于侦听状态。

[root@ntpserver ~]# ntpstat

synchronised to NTP server (180.153.100.115) at stratum 3

time correct to within 33 ms

polling server every 64 s

NTP服务器上执行ntpstat命令,可以看到NTP服务器和上游NTP服务器的同步情况。如下所示和上游NTP服务器校正了时间33毫秒,并且每64秒会自动和上游NTP服务器同步时间。

[root@ntpserver ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

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

*180.153.100.115 223.255.185.2 2 u 33 64 377 30.442 1.705 4.958

NTP服务器上执行ntpq命令,可以列出目前我们的 NTP 与相关的上层 NTP 的状态;

参数详解:

remote:亦即是 NTP 主机的 IP 或主机名啰~注意最左边的符号

如果有 * 代表目前正在作用当中的上层 NTP

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

st:就是 stratum 阶层

when:几秒钟前曾经做过时间同步化更新的动作;

poll:下一次更新在几秒钟之后;

reach:已经向上层 NTP 服务器要求更新的次数

delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒

offset:时间补偿的结果,单位与 10^(-3) 秒

jitterLinux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

另外,你也可以检查一下你的BIOS 时间与 Linux 系统时间的差异,就是/var/lib/ntp/drift 这个档案的内容,就能了解到咱们的 Linux 系统时间与 BIOS 硬件时钟到底差多久?单位为 10^(-6) 秒

 配置客户端

假设NTP客户机也是RHEL6.3系统,配置NTP客户机步骤如下:

1:修改/etc/ntp.conf ,内容如下:

[root@ntpclient ~]# grep -v ^# /etc/ntp.conf |grep -v ^$

driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict -6 ::1

server ntpserver.example.com

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

2、启动ntpd服务

[root@ntpclient ~]# service ntpd start

Starting ntpd: [ OK ]

3、先用ntpdate命令让NTP客户机和NTP服务器同步时间。

出现了 unsynchronization . 报错; 解决办法:检查时区 ,不对的话可以用  ln -sf  /usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime 设置;

也有可能会发现上面的同步失败信息。在NTP服务器上启动ntp服务后,NTP服务器自身或者与其服务器的同步的需要一个时间段,这个过程可能是5分钟。等待一段时间后再执行

[root@ntpclient ~]# ntpdate ntpserver.example.com

3 Feb 00:43:58 ntpdate[3043]: step time server 192.168.1.101 offset 0.956542 sec

4、验证时间同步

[root@ntpclient ~]# ntpstat

synchronised to NTP server (192.168.1.101) at stratum 4

time correct to within 555 ms

polling server every 64 s

[root@ntpclient ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

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

*ntpserver.examp 180.153.100.115 3 u 12 64 37 0.286 2.020 0.265

上面命令显示时间同步成功。

注:用ntpdate (时间服务器地址)可能会出现 socket 被占用的情况 可以用 lsof -i:123 查看PID 然后kill pid 然后再执行ntpdate