搭建内部时间同步服务器(NTP)

    当服务器多了,时间准确与否,一致与否是个大问题。虽然这个问题总是被忽略,但是统一一致的时间是很有必要的。下面我们通过NTP实现时间同步。

    NTP(Network Time Protocol 网络时间协议)是一个用于同步计算机时钟的网络协议。它可以使计算机与其他服务器或时钟源进行时间同步,进行高精度的时间校正。简而言之,NTP就是使一台或多台服务器(客户端)与时间服务器(服务端)之间进行时间同步(即客户端与服务端的时间同步),以保证时间的统一性。前采用的时间标准是世界协调时UTC(Universal Time Coordinated)。NTP的主要开发者是美国特拉华大学的David L. Mills教授。

一、NTP服务安装

wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.8p1.tar.gz

tar -zxvf ntp-4.2.8p1.tar.gz 
cd ntp-4.2.8p1
./configure --prefix=/usr/local/ntp --enable-all-clocks --enable-parse-clocks
make 
make install

二、配置/etc/ntp.conf

restrict用来设置访问权限

server用来设置上层时间服务器

driftfile用来设置保存漂移时间的文件

#记录上次的NTP server与上层NTP server联接所花费的时间
driftfile /etc/ntp/dift
broadcastdelay	0.008

includefile /etc/ntp/crypto/pw
keys    /etc/ntp/keys

#===restrict 权限控制===
#设置默认策略不允许任何主机进行时间同步,修改
restrict default nomodify notrap noquery
#设置允许访问此时间服务器的时间服务的IP地址,根据自己实际情况配置
restrict 127.0.0.1
restrict 192.168.36.0 mask 255.255.255.0 nomodify notrap #允许192.168.36网段进行时间同步

#====源服务器===
server 210.72.145.44 prefer   #指定上级更新时间服务器,优先使用这个地址
server 133.100.11.8 

#指定阶层编号为10,降低其优先度。让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0
fudge 127.127.1.1 stratum 10

#设置ntp日志的path
statsdir /var/log/ntp/
#设置ntp日志文件
logfile /var/log/ntp/ntp.log 

在配置文件/etc/ntp.conf中主要包括以下几个参数
1. restrict <IP 地址><子网掩码>|<网段> <子网掩码> [ ignore|nomodify|noquery|notrap|notrust|nokod ]: 指定可以进行NTP通信的IP地址或网段
1.1 ignore:关闭所有NTP服务
1.2 nomodify :表示客户端不能更改NTP服务器的时间参数,但可以通过NTP服务器进行时间校对
1.3 noquery: 不提供NTP服务
1.4 notrap:不提供trap远程事件登录的功能
1.5 notrust: 聚聚没有通过认证的客户端
1.6 kod:kod技术可以组织“Kiss of Death”包(一种DOS攻击)对服务器的破坏,使用此参数将开启该功能 
1.7 nopeer:不与其他同一层的NTP服务器进行时间同步
若果没有指定选项,那就表示指定的客户端在访问NTP服务器时没有任何限制
2. server [IP|FQDN] [prefer]:指定该NTP服务器上层NTP服务器,如果有多个上层NTP服务器,则参考prefer的优先级进行时间同步,prefer越小优先级越高
3. broadcast 网段 子网掩码:指定进行NTP时间广播的网段,在不指定此参数时NTP服务器会对所有能访问的网段广播

4.driftfile 文件名 #解决NTP服务器校准时间时的传送延迟
在与上级时间服务器联系时所花费的时间,记录在driftfile参数后面的文件内。
注意:  driftfile 后面接的文件需要使用完整的路径文件名,不能是链接文件,并且文件的权限需要设定成 ntpd守护进程可以写入。

三、启动服务

#添加为服务
 chkconfig --level 345 ntpd on
#启动
 service ntpd start
 或/etc/rc.d/init.d/ntpd start
#停止
 service ntpd stop
 或/etc/rc.d/init.d/ntpd stop

ntp 默认启动udp 123端口(netstat -ln|grep 123,netstat -tlunp | grep ntp),在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间咯。(因为启动服务器,端口会被服务端占用,就不能手动同步时间了)。ntpd启动后,客户机要等几分钟再与其进行时间同步,否则会提示“no server suitable for synchronization found”错误。

四、管理命令

1.查看本机和上层服务器的时间同步结果

watch ntpq -p

Every 2.0s: ntpq -p                                                                                                                            Sun Feb  8 04:26:33 2015

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 210.72.145.44   .INIT.          16 u    -   64    0    0.000    0.000   0.000
 133.100.11.8    133.100.53.51    2 u  261   64   20    0.000    0.000   0.001
*LOCAL(0)        .LOCL.           8 l   18   64  377    0.000    0.000   0.001

下面对部分参数进行说明:
remote: 本地主机所连接的上层NTP服务器
st:NTP服务器优先级
when:上次与NTP服务器同步的时间(单位:s)
poll:下次与NTP服务器同步的时间(单位:s)
delay:从本地发出时间同步命令给远程时间服务器开始,到整个时间同步完成所需时间(单位:10^-6s)
offset:本地主机与时间服务器的时间差(同步后将自动纠正)
jitter:一个offset的分布统计值,该值越小,表示时间越精确

2.查看同步日志(也可以查看/var/log/ntp/ntp.log)

tcpdump udp port 123

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
04:37:21.683586 IP slave-b.ntp > 133.100.11.8.ntp: NTPv4, Client, length 48
04:38:59.642869 IP 192.168.36.189.54681 > slave-b.ntp: NTPv4, Client, length 48
04:38:59.643906 IP slave-b.ntp > 192.168.36.189.54681: NTPv4, Server, length 48
04:38:59.644442 IP 192.168.36.189.54681 > slave-b.ntp: NTPv4, Client, length 48
04:38:59.644566 IP slave-b.ntp > 192.168.36.189.54681: NTPv4, Server, length 48
04:38:59.645039 IP 192.168.36.189.54681 > slave-b.ntp: NTPv4, Client, length 48
04:38:59.645149 IP slave-b.ntp > 192.168.36.189.54681: NTPv4, Server, length 48
04:38:59.645731 IP 192.168.36.189.54681 > slave-b.ntp: NTPv4, Client, length 48
04:38:59.645841 IP slave-b.ntp > 192.168.36.189.54681: NTPv4, Server, length 48
注:默认是每五分钟ntptime向NTP服务器查询一次

3.查看ntp服务器与上级服务器是否通讯

ntpstat

synchronised to local net at stratum 9 
   time correct to within 11 ms
   polling server every 512 s
注意:此命令出现上述synchronised结果比较慢。

五、客户端同

使用ntpdate工具进行同步。

1.LINUX客户端: (局域网内对时间有要求的机器)
 echo "0 */1 * * * root /usr/sbin/ntpdate 192.168.36.54;/sbin/hwclock -w">>/etc/crontab
 每小时同NTP server进行一次时钟同步,并写入本机BIOS
2.Solaris客户端:
 echo "15 5 * * * /usr/sbin/ntpdate 192.168.36.54">>/etc/crontab

六、问题处理

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

错误1.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来查询详细错误信息,再做判断。

错误2.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端口。
可以用命令service iptables stop 来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。
或iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT 开启路由访问策略

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值