NTP时间服务器配置以及错误的总结

NTP全称是Network Time Protocol,也就是互联网时间协议,说到时间,就谈谈时间的一些概念吧;

时区按照常识来说,一天被划分24小时,近似球体的地球是360度,所以使用经纬度为坐标,将全球划分为24个时区,每个时区是15度;东八区格林尼治时间为世界标准时间,又因为,东半球(格林尼治以东)的时间比较早,中国的经度在120E,是位于第八个时区,这里的中国表示北京或者上海,因为横跨的时区不止一个;UTCCoordinated Universal Time表示协调通用时间,根据原子振荡周期所计算的物理时钟,这种计算方式对于时间的计算误差时很小的;GMTGreenwich Mean Time表示的是格林尼治时间,是根据地球的自转周期计算的标准时间,由于各种不稳定因素的影响,计时不是很理想;软件时钟表示的是LInux[这里指提Linux]操作系统从1970/01/01开始计算的秒数;硬件时钟主机硬件上面的时钟,主要是BIOS内部时间的记录了;

时间校对的几个步骤

1.server端启动daemon;2.Client向server发送校对时间的报文;3.NTP server返回当前的标准时间给Client;4.Client根据返回的时间校对自己当前的系统时间;关于NTP服务相关的配置文件

/etc/ntp.conf

//表示NTP服务的主要配置文件,也是为一个的配置文件

/usr/share/zoneinfo/

//表示对应的各个时区的文件

/etc/sysconfig/clock

//表示用于设置时钟和是否使用UTC的配置文件

/etc/localtime

//用于复制/usr/share/zoneinfo/里面对应的,/etc/sysconfig/clock里面设定的时区文件,作为当前系统使用的本地时间配置文件;

关于时间的几个命令

/bin/date

//Linux软件时钟的修改和查看

/sbin/hwclock

//用于硬件时钟的修改和显示,如果修改了软件时钟需要使用这个方式同步到硬件时钟上面;

/usr/sbin/ntpd

//NTP服务的主要程序,配置文件为/etc/ntp.conf

/usr/sbin/ntpdate

//用于实现Client时间的校正

关于/etc/ntp.conf配置文件

利用restrict来实现权限控制

restrict [IP] [mask] [netmask_IP] [patameter]

patameter支持的参数:

igonre:拒绝所有类型的`NTP`连接

nomodify:Client不能够使用命令ntpc以及ntpdq来修改修改服务器的时间参数,但是可以进行网络校时;

noquery:表示不提供NTP网络校时;

notrap:表示不提供远程事件登陆的功能;

notrust:表示不接受没有认证的Clent;

如果没有任何参数,表示不接受任何的限制;

server [IP or hostname] [prefer]

用于设置上层提供NTP服务的服务器;

prefer:表示优先使用

driftfile [可以被ntpd写入的进程或者文件]

NTP Server计算时间是通过本机的时钟振荡来记录的,这个时间和上层Time Server的时钟振荡频率不一定是一致的,需要将这个振荡差异记录下来;

NTP服务的规划

rhel7.2作为提供时间同步服务的主机172.25.23.250

rhel6.5作为某个集群中的节点,进行时间同步,这里选择server21.com

需要注意的几点:

1.自己配置的NTP是需要设置上层服务器来进行时间同步的,172.25.23.250主机通过另一块网卡可以上网,使用的上层主机是cn.ntp.org.cn ;2.NTP服务器和上层NTP服务的时间不能够相差太多;3.对于防火墙的设置,一定需要允许123/UDP端口;4.防火墙还需要放行时间同步请求的网段;首先安装ntp服务的软件包,这里使用Yum配置安装

[root@my Desktop]# yum install ntp -y

[root@my Desktop]# rpm -q ntp

ntp-4.2.6p5-22.el7.x86_64

然后根据上面的提示修改配置文件/etc/ntp.conf

# For more information about this file, see the man pages

# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).

driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not

# permit the source to query or modify the service on this system.

restrict default nomodify notrap nopeer noquery

# Permit all access over the loopback interface. This could

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

# the administrative functions.

restrict 127.0.0.1

restrict ::1

restrict cn.ntp.org.cn

# Hosts on local network are less restricted.

#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

restrict 172.25.23.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

#server 0.rhel.pool.ntp.org iburst

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

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

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

#

server cn.ntp.org.cn prefer

#broadcast 192.168.1.255 autokey # broadcast server

#broadcastclient # broadcast client

#broadcast 224.0.1.1 autokey # multicast server

#multicastclient 224.0.1.1 # multicast client

#manycastserver 239.255.254.254 # manycast server

#manycastclient 239.255.254.254 autokey # manycast client

# Enable public key cryptography.

#crypto

includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating

# with symmetric key cryptography.

keys /etc/ntp/keys

# Specify the key identifiers which are trusted.

#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.

#requestkey 8

# Specify the key identifier to use with the ntpq utility.

#controlkey 8

# Enable writing of statistics records.

#statistics clockstats cryptostats loopstats peerstats

# Disable the monitoring facility to prevent amplification attacks using ntpdc

# monlist command when default restrict does not include the noquery flag. See

# CVE-2013-5211 for more details.

# Note: Monitoring will not be disabled with the limited restriction flag.

disable monitor

然后启动或者重新启动服务

[root@my Desktop]# !systemc

systemctl start ntpd

确保端口已经启动

使用命令ntpstat查看NTP服务器的状态

[root@my Desktop]# ntpstat

synchronised to NTP server (202.108.6.95) at stratum 3

time correct to within 106 ms

polling server every 64 s

或者使用ntpq -p来查看NTP服务器时间同步的过程

[root@my Desktop]# ntpq -p

remote refid st t when poll reach delay offset jitter

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

*xk-6-95-a8.bta. 10.69.2.34 2 u 35 64 377 28.381 -30.163 17.439

解释上述几个子段的作用

remote :表示NTP主机的IP或者是主机名,最左边的

*:表示目前正在作用当中的上层NTP服务器;+:表示已经连接成功,并且可以作为下一个提供时间更新的候选者;refid:表示参考的上一层NTP主机的地址;st:表示的含义是startnum,一般范围是从0-->15;when:表示几秒前做过时间同步化更新操作;poll:表示下一次更新在几秒钟之后;reach:表示已经向上一层服务器要求更新的次数;delay: 表示数据在传输过程中延迟的时间;offset:表示时间补偿的结果;jitter:Linux系统时间和BIOS硬件时间的差异;Client仅仅需要使用简单的命令就可以完成时间和NTP的同步操作

[root@server20 ~]# ntpdate 172.25.23.250

10 Mar 15:49:08 ntpdate[13911]: adjust time server 172.25.23.250 offset -0.049370 sec

关于NTP服务器配置过程中出现额的几个错误,以及解决额的办法和上层NTP服务器不能够正常通信;

[root@server21 ~]# ntpstat -p

unsynchronised

time server re-starting

polling server every 8 s

上面这个错误出现的原因是没有设置上层服务器,导致和上述NTP服务器的同步没有办法完成,或者是因为上层服务器设置错误;解决,这里使用的上层服务器是cn.ntp.org.cn, 可以使用联网主机测试和上层时间同步服务器的连接是否正常

[root@my Desktop]# ntpdate cn.ntp.org.cn

10 Mar 15:55:08 ntpdate[8952]: adjust time server 202.112.29.82 offset -0.003693 sec

出现的错误

[root@server20 ~]# ntpdate 172.25.23.250

9 Mar 10:46:17 ntpdate[1129]: no server suitable for synchronization found

对于上述出现的错误,使用-d选项来查看相信的出错信息

[root@server20 ~]# ntpdate -d 172.25.23.21

10 Mar 15:56:52 ntpdate[13913]: ntpdate 4.2.6p5@1.2349-o Mon Jul 15 09:22:50 UTC 2013 (1)

Looking for host 172.25.23.21 and service ntp

host found : server21.com

transmit(172.25.23.21)

transmit(172.25.23.21)

transmit(172.25.23.21)

transmit(172.25.23.21)

transmit(172.25.23.21)

172.25.23.21: Server dropped: no data

server 172.25.23.21, port 123

stratum 0, precision 0, leap 00, trust 000

refid [172.25.23.21], delay 0.00000, dispersion 64.00000

transmitted 4, in filter 4

reference time: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000

originate timestamp: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000

transmit timestamp: de4e0e47.784f2a34 Sat, Mar 10 2018 15:56:55.469

filter delay: 0.00000 0.00000 0.00000 0.00000

0.00000 0.00000 0.00000 0.00000

filter offset: 0.000000 0.000000 0.000000 0.000000

0.000000 0.000000 0.000000 0.000000

delay 0.00000, dispersion 64.00000

offset 0.000000

10 Mar 15:56:56 ntpdate[13913]: no server suitable for synchronization found

上面出现的原因是server droped,也就是说服务器丢弃了数据,可能存在的原因

1.NTP服务器启动不正常,也就是NTP服务器启动过程中存在错误,使用tail -f /var/log/messages查看并且确定错误的详细信息;

上面存在的原因可能有两个:1.selinux设置不正常,使用命令setence 0;还有可能是因为iptables没有添加防火墙例外,也就是对于123/UDP的请求被拒绝;对于上面的错误还有可能遇到一种情况

[root@server20 ~]# ntpdate -d 172.25.23.21

10 Mar 15:56:52 ntpdate[13913]: ntpdate 4.2.6p5@1.2349-o Mon Jul 15 09:22:50 UTC 2013 (1)

Looking for host 172.25.23.21 and service ntp

host found : server21.com

transmit(172.25.23.21)

transmit(172.25.23.21)

transmit(172.25.23.21)

transmit(172.25.23.21)

transmit(172.25.23.21)

172.25.23.21: Server dropped: start toohigh

server 172.25.23.21, port 123

stratum 16, precision 0, leap 00, trust 000

refid [172.25.23.21], delay 0.00000, dispersion 64.00000

transmitted 4, in filter 4

reference time: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000

originate timestamp: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000

transmit timestamp: de4e0e47.784f2a34 Sat, Mar 10 2018 15:56:55.469

filter delay: 0.00000 0.00000 0.00000 0.00000

0.00000 0.00000 0.00000 0.00000

filter offset: 0.000000 0.000000 0.000000 0.000000

0.000000 0.000000 0.000000 0.000000

delay 0.00000, dispersion 64.00000

offset 0.000000

10 Mar 15:56:56 ntpdate[13913]: no server suitable for synchronization found

对于上面正常的startnum的范围是[0--->15],出现上面情况的原因是因为NTP服务器无法和上层NTP服务器通信,导致Client的连接不正常;可以参考第一种错误类型进行配置; 这个是正常时间同步过程中的调式信息

[root@server21 ~]# ntpdate -d 172.25.23.250

10 Mar 16:13:18 ntpdate[1508]: ntpdate 4.2.6p5@1.2349-o Mon Jul 15 09:22:50 UTC 2013 (1)

Looking for host 172.25.23.250 and service ntp

host found : 172.25.23.250

transmit(172.25.23.250)

receive(172.25.23.250)

transmit(172.25.23.250)

receive(172.25.23.250)

transmit(172.25.23.250)

receive(172.25.23.250)

transmit(172.25.23.250)

receive(172.25.23.250)

server 172.25.23.250, port 123

stratum 4, precision -24, leap 00, trust 000

refid [172.25.23.250], delay 0.02571, dispersion 0.00000

transmitted 4, in filter 4

reference time: de4e1219.b90d7820 Sat, Mar 10 2018 16:13:13.722

originate timestamp: de4e121e.fa8ae002 Sat, Mar 10 2018 16:13:18.978

transmit timestamp: de4e121e.ea321e17 Sat, Mar 10 2018 16:13:18.914

filter delay: 0.02580 0.02571 0.02571 0.02571

0.00000 0.00000 0.00000 0.00000

filter offset: 0.063770 0.063791 0.063784 0.063784

0.000000 0.000000 0.000000 0.000000

delay 0.02571, dispersion 0.00000

offset 0.063791

10 Mar 16:13:18 ntpdate[1508]: adjust time server 172.25.23.250 offset 0.063791 sec

补充关于date命令的使用

[root@server21 ~]# date MMDDHHMMYYYY

MM:表示月

DD:表示天

HH:表示小时

MM:表示分钟

YYYY:表示年

上面这个命令主要用来查看(不使用任何选项)和修改时间;hwclock 关于硬件时间

-r :表示用于读取硬件时间

-w:表示将软件时间写入为硬件时间