ntp时间服务器

一、NTP时间服务器:
使用环境:当一个计算机群体需要需要进行时间的严格同步,即可使用NTP服务器。
NTP通信协议原理:
1.首先主机启动NTP。
2.客户端会向NTP服务器发送调整时间的message。
3.然后NTP server会送出当前的标准时间给client
4.client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。
NTP这个deamon采用了123端口。(UDP)
二、ntp服务端安装:
1.检查是否已安装(如下表示已安装):
[root@mysql ~]# rpm -qa ntp
ntp-4.2.2p1-9.el5.centos.2.1
2.若未安装,可使用以下命令安装
[root@mysql ~]# yum -y install ntp
也可以通过光盘或rpm包直接安装
rpm -ivh ntp-4.2.2p1-9.el5.centos.2.1.rpm
三、配置文件
1./etc/ntp.conf
linux各版本虽然目录不同,但文件名相同。可以用which ntp.conf 或者locate ntp.conf来查找。这是NTP唯一的一个设置文件。
2./usr/share/zoneinfo/
这个里面规定了这个主要时区的时间设置文件。
3./etc/sysconfig/clock
这个文件是linux的主要时区设置文件,每次开机后linux会自动读取这个文件来设置系统所默认的显示时间,可以看看它里面到底设置了什么:
[root@mysql ~]# cat /etc/sysconfig/clock
ZONE="Asia/Shanghai"
UTC=true
ARC=false
4./etc/localtime
本地端时间配置文件。
5./bin/date
这个是时间的修改命令,除了输出时间,还可以修改时间。
6./sbin/hwclock
因为linux系统上面BIOS时间与linux系统时间是分开的,所以使用date这个指令调整了时间之后,还需要使用hwclock才能将修改过的时间写入BIOS中。
7./usr/sbin/ntpd
这是NTP的daemon文件,需要启动它才能提供NTP服务,这个命令会读取/etc/ntp.conf里面的设置。
8./usr/sbin/ntpdate
这是client用来连接NTP Server的主要执行文件,如果您不想启用NTP,只想启用NTP Client功能的话,可以只应用此命令。
9,/usr/sbin/ntptrace
可以用来追踪某台时间服务器的时间对应关系。
四、NTP 服务端的配置
1.修改 /etc/ntp.conf
"注释"以下配置:
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
"添加"以下配置:
#add by yongfu https://www.360docs.net/doc/ac12616668.html,
restrict default nomodify noquery notrap
restrict 192.168.1.0 mask 255.255.255.0 nomodify (允许内网通讯,修改192.168.1.0 为自己的内网网络)
server https://www.360docs.net/doc/ac12616668.html, prefer (prefer表示优先 注意把默认的server更改成这样)
server https://www.360docs.net/doc/ac12616668.html,
其他的保持默认不动。
用restrict控管权限
nomodify – 用户端不能更改ntp服务器的时间参数
noquery – 用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap – 不提供trap远端登陆
配置文件说明如下:
第一行restrict、default定义默认访问规则,nomodify禁止远程主

机修改本地服务器配置,notrap拒绝特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询(这里的查询是服务器本身状态查询)。
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
这句是手动增加的,意思是从192.168.1.1-192.168.1.254的服务器都可以使用我们的NTP服务器来同步时间。
server 192.168.146.225【时间服务器的ip】
这句也是手动增加的,指明局域网中作为NTP服务器的IP;
配置文件的最后两行作用是当服务器与公用的时间服务器失去联系时以本地时间为客户端提供时间服务。
2.将同步好的时间写到CMOS里
vi /etc/sysconfig/ntpd
SYNC_HWCLOCK=yes
3.为了使NTP服务能够在系统引导的时候自动启动,执行:
# chkconfig ntpd on
4.启动/关闭/重启NTP的命令是:
# /etc/init.d/ntpd start
# /etc/init.d/ntpd stop
# /etc/init.d/ntpd restart
5.切记每次修改了配置文档后都需要重新启动服务来使配置生效。
6.能够使用下面的命令来检查NTP服务是否启动,将得到一个进程ID号:
# pgrep ntpd
7.使用下面的命令检查时间服务器同步的状态:
# ntpq -p
8.用ntpstat 也能够查看一些同步状态:
[root@www ~]# netstat -nltpu|grep ntpd
udp 0 0 192.168.1.101:123 0.0.0.0:* 18799/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 18799/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 18799/ntpd
udp 0 0 fe80::21e:c9ff:feb1:ddd:123 :::* 18799/ntpd
udp 0 0 fe80::21e:c9ff:feb1:ddf:123 :::* 18799/ntpd
五、ntp客户端的安装及设置
1.安装ntp:
方法参考前面,只需要安装就可以了,不用修改任何配置。
2.添加crontab定时任务:
# crontab -e
加入一行:
10 */1 * * * /usr/sbin/ntpdate 192.168.1.101
这样每小时的第10分钟,linux系统都会自动向我们的新建NTP时间服务器192.168.1.101 进行时间的同步操作。
3.注意事项:
当ntpd服务重启时,客户端需要等待15分钟才能与NTP服务器顺利连接上,耐心等待。
如在101的机器上重启的ntpd服务:/etc/init.c/ntpd restart
当102的机器去连接101的ntp服务是会出现以下错误:
[root@mysql ~]# /usr/sbin/ntpdate 192.168.1.101
7 May 20:13:29 ntpdate[8072]: no server suitable for synchronization found
耐心等待几分钟即可



当用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和其自身保持同步,如果在/n

tp.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的设置。


相关文档
最新文档