基本概念:
0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互。
[from Wikipedia]
C/S合一
ntpd对下层client来说是service server,对于上层server来说它是client,也就是说新版的NTP服务程序已经不对服务端和客户端进行区分了,统一叫做ntpd。ntpd根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。所有的配置都在/etc/ntp.conf文件中。
NTP客户端同步间隔
NTP服务会间隔多长时间想时钟服务器请求一次时钟同步呢?默认最小时间间隔为64s,默认最大时间间隔是1024s(17分钟左右)。64s是比较合理的,默认间隔也是可调的(Note that most device drivers will not operate
properly if the poll interval is less than 64 s and that the broadcast server
and manycast client associations will also use the default, unless
overridden.)。
容忍误差范围
NTP服务并不是在任何情况下都会进行同步的。当时钟服务器时间和本地时间相差大于1000s时,NTP服务就会认为是人为调整了时钟或出现了硬件故障,例如CMOS电池损坏等。此时,NTP服务就会退出,需要人工(ntpdate …)进行时钟同步。
采用-g选项可以让ntpd忽略1000s或更大误差,设置时钟到server system time, 但是ntpd还是会因此退出。
层次(strata)
stratum根据上层server的层次而设定(+1)。
对于提供network time service
provider的主机来说,stratum的设定要尽可能准确。
而作为局域网的time service
provider,通常将stratum设置为10 (Stratum 10 is
conventional for unsynchronized local clocks; it is high enough
that nobody is likely to mistake it for a desirable clock to
synchronize with.),如下:
server
127.127.1.0
# local clock
fudge
127.127.1.0
stratum 10
#stratum设置为其它值也是可以的,其范围为0~15
设置与启动
文件路径:/etc/ntp.conf
# 1. 先处理权限方面的问题,包括放行上层服务器以及开放局域网用户来源:
restrict default kod nomodify
notrap nopeer noquery
<==拒绝
IPv4 的用户
restrict -6 default kod
nomodify notrap nopeer noquery
<==拒绝
IPv6 的用户
restrict
220.130.158.71
<==放行
tock.stdtime.gov.tw
进入本 NTP
的服务器
restrict
59.124.196.83
<==放行
tick.stdtime.gov.tw
进入本 NTP
的服务器
restrict
59.124.196.84
<==放行
time.stdtime.gov.tw
进入本 NTP
的服务器
restrict 127.0.0.1
<==底下两个是默认值,放行本机来源
restrict -6 ::1
restrict 192.168.100.0 mask
255.255.255.0 nomodify <==放行局域网用户来源,或者列出单独IP
# 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
server 220.130.158.71
prefer
<==以这部主机为最优先的server
server 59.124.196.83
server 59.124.196.84
# 3.默认的一个内部时钟数据,用在没有外部
NTP 服务器时,使用它为局域网用户提供服务:
# server
127.127.1.0
# local clock
# fudge
127.127.1.0 stratum 10
# 4.预设时间差异分析档案与暂不用到的
keys 等,不需要更动它:
driftfile
/var/lib/ntp/drift
keys
/etc/ntp/keys
文件路径:/etc/sysconfig/ntpd
OPTIONS="-u ntp:ntp -p
/var/run/ntpd.pid"
SYNC_HWCLOCK=yes
# 将他改成 yes
吧!这样 BIOS
的时间也会跟着改变的!
启动
/etc/init.d/ntpd start
或 /etc/init.d/ntpd
restart
查看端口使用情况
netstat -tlunp | grep
ntp
查看本机作为client更新time的过程
ntptrace
查看ntpd的运行信息
ntpstat
查看所有层次time service
provider的详细信息
ntpq –pn
Client单独用ntpdate更新时间
ntpdate
ntp_server_ip
其后可能需要hwclock -w
(clock
-w)写入BIOS timer
可能的问题
Ø
no server suitable
for synchronization found
原因:ntpd启动过程需要5分钟左右!(或者ntpd根本没有启动)
在ntp
server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization
found的错误。
那么如何知道何时ntp
server完成了和自身同步的过程呢?
在ntp
server上使用命令:
watch ntpq
出现画面:
注意LOCAL的这个就是与自身同步的ntp server,而reach这个值,在启动ntp server服务后,就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。
如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。
Ø
Server dropped:
strata too high”的错误,并且显示“stratum 16”
修改fudge
server的stratum为0~15
Ø
Server dropped: no
data
从客户端执行ntpdate –d
server_ip(debug模式)时有错误信息: Server dropped: no data.
出现这个问题的原因可能有2:
1).
检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误(版本差异),解决方法是把notrust去掉。
# ntpq -c version
2).
检查ntp
server的防火墙,可能是server的防火墙屏蔽了upd 123端口。可以用下面命令来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。
#service iptables stop