NTP服务端搭建
young / / / 阅读量

准备

查看亚洲大部分时区和国家城市的时间文件位置

ls -F /usr/share/zoneinfo/Asia

查看当前某一时区的时间

[root@localhost ~]# zdump Hongkong
Hongkong Sat Sep 12 08:33:43 2015 HKT

设置系统时间的时区

[root@localhost ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@localhost ~]# date
Sat Sep 12 08:37:37 CST 2015

查看系统时间和硬件时间是否一致(RTC即系统停止还在计算时间一般写在BIOS里头)

# 系统时间查询
[root@localhost ~]# date
Sat Sep 12 08:37:37 CST 2015
# 硬件时间查询
[root@localhost ~]# hwclock -r
Sat 12 Sep 2015 08:43:26 AM CST -0.863373 seconds

设置系统时间和硬件时间与当前实际时间保持一致

# 将系统时间写入硬件时间
hwclock -w

安装

查看操作系统是否安装ntp服务组件,执行命令

# 显示下面2个包,代表已经安装ntp-server服务组件
[root@localhost ~]# rpm -qa|grep ntp
ntp-4.2.6p5-1.el6.centos.x86_64
fontpackages-filesystem-1.41-1.1.el6.noarch
ntpdate-4.2.6p5-1.el6.centos.x86_64

安装ntp服务组件包,执行命令

[root@localhost ~]# yum install ntp

配置

编辑配置文件/etc/ntp.conf文件,

# 找到restrict字段并在中间加上如下信息
restrict 192.168.0.0/16 mask 255.255.0.0 nomodify noquery
# 找到server 0.centos.pool.ntp.org iburst 并把相关的给注释了,添加如下
server 210.72.145.44
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# 如果第二部配置的server 210.72.145.44 无效时,则NTP服务器会根据这里的配置,把自己的时间做为NTP服务器的时间,即和自己同步。考虑到有的局域网里不可以访问外网,所有这里需要把这个配置项用上

启动

启动ntpd服务并添加开机启动

service ntpd start
chkconfig ntpd on

检查ntp server的防火墙。需要防火墙开放udp 123端口。

iptables INPUT -p udp -m udp –dport 123 -j ACCEPT

客户端配置

ntpdate 192.168.166.100
# LINUX做为客户端自动同步时间 如果想定时进行时间校准,可以使用crond服务来定时执行。
# 编辑 /etc/crontab 文件 加入下面一行: 192.168.0.1是NTP服务器的IP地址
30 8 * * * root /usr/sbin/ntpdate 192.168.166.100; /sbin/hwclock -w 
# 然后重启crond服务
service crond restart

错误问题集锦

当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:

Server dropped: Strata too high

在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
这是因为NTP server还没有和其自身或者它的server同步上。
以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0 fudge
127.127.1.0 stratum 8
在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
那么如何知道何时ntp server完成了和自身同步的过程呢?
在ntp server上使用命令:
watch ntpq -p
出现画面:
Every 2.0s: ntpq -p Thu Jul 10 02:28:32 2008
 remote refid st t when poll reach delay offset jitter
192.168.30.22 LOCAL(0) 8 u 22 64 1 2.113 179133. 0.001
LOCAL(0) LOCAL(0) 10 l 21 64 1 0.000 0.000 0.001
注意LOCAL的这个就是与自身同步的ntp server。
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。
如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。

Server dropped: no data

从客户端执行netdate –d时有错误信息如下:
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123
…..
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出现这个问题的原因可能有2:
1.检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。
使用以下命令检查ntp的版本:
ntpq -c version
下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant “Don’t trust this host/subnet for time”.
In 4.2 (and later) notrust means “Ignore all NTP packets that are not cryptographically authenticated.” This forces remote time servers to authenticate themselves to your (client) ntpd
解决:
把notrust去掉。
2.检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。
可以用命令
iptables INPUT -p udp -m udp –dport 123 -j ACCEPT
如果觉得麻烦就直接把防火墙停掉
service iptables stop
来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置
支付宝捐赠
请使用支付宝扫一扫进行捐赠
微信捐赠
请使用微信扫一扫进行赞赏
有 0 篇文章