CentOS 6.5 下搭建NTP服务器

参考网站:

http://www.iyunv.com/thread-64847-1-1.html

http://acooly.iteye.com/blog/1993484

1         检查系统时区是否正确

在中国,正确的时区应为 CST(Chinese Standard Time),也就是我们通常所说的北京时间.

那么中国当地的时间晚上8点的话,我们可以有下面两种表示方式

20:00 CST

12:00 UTC

因为中国处在UTC+8时区,依次类推,在UTC标准时间,就是12:00.

不管通过任何渠道我们想要同步系统的时间,通常提供方只会给出UTC+0的时间值而不会提供时区.所以当我们设置系统时间的时候,设置好时区是首先要做的工作

[root@xldwhj ~]# date

Fri Jul 22 22:11:21 CST 2016

如果发现时区不正确,修改时区,修改时区方法如下:

1.修改 /etc/sysconfig/clock

将原来的时区改为

[root@xldwhj ~]# vi /etc/sysconfig/clock

ZONE="America/New_York"

改为
ZONE="Asia/Shanghai"

2.覆盖 /etc/localtime

[root@xldwhj ~]# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@xldwhj ~]#cp: overwrite `/etc/localtime'? y

3.查看修改后的时区

[root@xldwhj ~]# date

Fri Jul 22 22:11:21 CST 2016

 

2         检查 NTP 是否安装

[root@xldwhj ~]# rpm -qa |grep ntp
[root@xldwhj ~]#ntp-4.2.4p8-3.el6.centos.x86_64
[root@xldwhj ~]#ntpdate-4.2.4p8-3.el6.centos.x86_64

如上所示就是正确安装了

如果未安装,则使用yum安装

[root@xldwhj ~]# yum -y install ntp

3         检查上层 NTP 服务器是否正常连通

直接使用国家对时服务器

1.cn.pool.ntp.org

2.cn.pool.ntp.org

3.ch.pool.ntp.org

ntp.api.bz 是一组NTP服务器集群,目前有6台服务器,位于上海电信。这项服务是 api.bz 继移动飞信免费短信发送接口之后的第二项免费 API 服务

测试两个时钟服务是否正常可用

[root@xldwhj ~]# ntpdate -q 1.cn.pool.ntp.org

server 202.112.29.82, stratum 2, offset 169580.733614, delay 0.05197

22 Jul 22:18:22 ntpdate[2671]: step time server 202.112.29.82 offset 169580.733614 sec

[root@xldwhj ~]# ntpdate -q 2.cn.pool.ntp.org

server 202.112.29.82, stratum 2, offset 169580.734524, delay 0.05310

22 Jul 22:20:09 ntpdate[2694]: step time server 202.112.29.82 offset 169580.734524 sec

如上所示,就是正常的,如果上层服务器不可用,一般如下显示

1.server 202.112.29.82, stratum 0, offset 0.000000, delay 0.00000
2. 22 Jul 22:20:09 ntpdate[2694]: no server suitable for synchronization found

4         手工校正当前时间

当系统时间与标准时间相差太大时,NTP通常无法正常同步时间,因此我们一般要手工先对当前时间进行调整

两个对时服务器都可以用,随便使用一个既可

[root@xldwhj ~]# ntpdate 2.cn.pool.ntp.org

24 Jul 21:27:47 ntpdate[2697]: step time server 202.112.29.82 offset 169580.736829 sec
注意:如果NTPD服务已经运行了,这个命令将无法使用,要先关闭NTPD服务,就可以使用,调整好后,用 date 命令检查一下,时间是否正确了

[root@xldwhj ~]# date

Sun Jul 24 21:27:56 CST 2016

5         NTP配置

如果时间还是不对,建议用 date 命令手工调整一下。

NTP 服务器是起一个承上启下的作用,寻找上层服务器获取正确的时间,同时为下层服务器提供网络对时服务,所以对它的设置,也主要由两方面组成.

5.1         指定上层对时服务器

加#号注释掉centos默认的对时服务器,如果主机可以访问外网,这一步最好做一下.

添加我们刚才测试的国家对时服务器.

prefer 表示优先使用
# vim /etc/ntp.conf

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

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

#server 0.centos.pool.ntp.org

#server 1.centos.pool.ntp.org

#server 2.centos.pool.ntp.org

server 1.cn.pool.ntp.org

server 2.cn.pool.ntp.org prefer

5.2         设置对内网主机提供NTP服务

在我的机子中,内网使用 192.168.178.0/24 网段


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

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

#restrict default kod nomodify notrap nopeer noquery

#restrict -6 default kod 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 -6 ::1

restrict 192.168.178.0

mask 255.255.255.0  

nomodify notrap

几点说明:

restrict default kod nomodify notrap nopeer noqueryNTP服务默认拒绝所有NTP连线

kod 当server的服务超过限制,server不是直接丢弃服务要求,而是传送一个信号,让client转向要求其它server服务,或等一段时间再要求server服务

nomodify 用户端不能更改NTP服务端的时间参数

notrap 不提供trap 这个远程事件登录功能

nopeer 避免其他人使用此ntpd作为time server

noquery 用户端不能使用ntpq,ntpc等指令来查询时间

restrict 127.0.0.1默认对本机无限制

restrict 192.168.178.0 mask 255.255.255.0 nomodify notrap这是我添加的,允许192.168.178.0/24 网段的主机来进行对时,但不允许客户端来修改,登录我的NTP服务器

#允许任何IP的客户机都可以进行时间同步
restrict default nomodify

5.3         设置同步更新本地hwclock

在Linux下系统时间在开机的时候会和硬件时间同步(synchronization),之后也就各自独立运行了那么既然两个时钟独自运行,那么时间久了必然就会产生误差,而NTP默认只更新系统时间,因此我们需要设置硬件时钟进行同步调整

1.# vim /etc/sysconfig/ntpd
2.# Drop root to id 'ntp:ntp' by default.
3.OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
4.SYNC_HWCLOCK="yes"

添加SYNC_HWCLOCK="yes"

1.启动NTP服务

[root@xldwhj ~]# service ntpd start

Starting ntpd:                                             [  OK  ]

启动后的观察

2.确认端口监听启动

ntpd启动 123 端口 通过UDP协议对外提供服务

[root@xldwhj ~]# netstat -nlpu | grep ntpd

udp        0      0 192.168.178.2:123           0.0.0.0:*                               2812/ntpd          

udp        0      0 192.168.254.128:123         0.0.0.0:*                               2812/ntpd          

udp        0      0 127.0.0.1:123               0.0.0.0:*                               2812/ntpd          

udp        0      0 0.0.0.0:123                 0.0.0.0:*                               2812/ntpd          

udp        0      0 fe80::20c:29ff:feef:76b2:123 :::*                                    2812/ntpd          

udp        0      0 fe80::20c:29ff:feef:76a8:123 :::*                                    2812/ntpd          

udp        0      0 ::1:123                     :::*                                    2812/ntpd          

udp        0      0 :::123                      :::*                                    2812/ntpd

3.确认已进行时间同步

该命令执行,通常要等10到15分钟,才会显示同步成功

[root@xldwhj ~]# ntpstat

4.查看与上层服务器连接情况

 [root@xldwhj ~]# ntpq -p 

 remote远程主机的主机名或IP

+已连线,可提供时间更新的候补服务器

*目前正在使用的上层NTP

refid 上级NTP的IP

st 就是stratum 上层NTP的层级

when 几秒钟前曾做过时间同步更新

poll 下一次更新在几秒后

reach 已经向上层服务器要求更新的次数

delay 网络传输过程中的延迟时间

jitter linux 系统时间与bios硬件时钟之间的差异

5.确认无问题后,将服务设为开机启动

[root@xldwhj ~]# chkconfig --list ntpd

ntpd              0:off       1:off       2:off       3:off       4:off       5:off       6:off

[root@xldwhj ~]# chkconfig ntpd on

[root@xldwhj ~]# chkconfig --list ntpd

ntpd              0:off       1:off       2:on 3:on 4:on 5:on 6:off

5.4         客户端测试

客户端到时钟服务器同步时间,有两种常见的用法:

首先可以测试同步:ntpdate 服务器ip地址

1.如果主机比较少,可以通过 cron 来同步时间

1.# vim /etc/crontab
2.*/3 * * * * root (/usr/sbin/ntpdate 192.168.178.2 && /sbin/hwclock -w) & >/dev/null

为了尽快看到执行效果,所以设置为每三分钟同步一次,实际生产中不用这么频繁,每天同步一次就差不多了

1.# tail -f /var/log/cron
2.Oct 31 11:37:01 CentOS2 crond[1522]: (*system*) RELOAD (/etc/crontab)
3.Oct 31 11:39:01 CentOS2 CROND[1688]: (root) CMD ((/usr/sbin/ntpdate 192.168.178.3 && /sbin/hwclock -w) & >/dev/null)

2.启动NTP服务自动同步(未检测)

如果内网服务器比较多

可以通过架设NTP来实现时间同步

因为该NTP只需向上层来同步时间,而不需对下层提供对时服务,就比较简单了

只要重复上面NTP架设步骤,去除restrict 步骤即可

注意:

1 ntp服务启动后5到10分钟才会进行同步,所以别着急
2 当你的时间设置和NTP服务器的时间相差很大的时候,NTP同步会失败

5.5         客户端常见错误解决方法

错误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

注意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的设置。

posted @ 2016-10-17 17:53  xldwhj  阅读(2847)  评论(0编辑  收藏  举报