ntp redhat

http://yuebaibai222.blog.51cto.com/2535988/1317928              

先简单介绍几个名词

Atomic Clock: 现在计算时间最准确的是使用 原子震荡周期 所计算的物理时钟(Atomic Clock),因此也被定义为标准时间(International Atomic Time)

UTC(coordinated Universal Time): 协和标准时间 就是利用 Atomic Clock 为基准定义出来的正确时间

硬件时钟: 硬件时钟是指嵌在主板上的特殊的电路, 它的存在就是平时我们关机之后还可以计算时间的原因

系统时钟: 就是操作系统的kernel所用来计算时间的时钟. 它从1970年1月1日00:00:00 UTC时间到目前为止秒数总和的值


搭建 NTP 服务之前的准备

1.检查系统时区是否正确

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

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

20:00 CST

12:00 UTC

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

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

1
2
# date
Thu Oct 31 11:22:45 EDT 2013

如果发现时区不正确,修改方法见本文 附1


2.检查 NTP 是否安装

1
2
3
# rpm -qa |grep ^ntp
ntp-4.2.4p8-3.el6.centos.x86_64
ntpdate-4.2.4p8-3.el6.centos.x86_64

如上所示就是正确安装了


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

直接使用国家对时服务器

1.cn.pool.ntp.org

2.cn.pool.ntp.org

为了防止出现上层对时服务器故障,无法正确提供时间同步,一般建议配置两个或以上时间同步服务器.下面分别测试两个时钟服务是否正常可用

1
2
3
4
5
6
7
8
9
10
11
12
# ntpdate -q 1.cn.pool.ntp.org
server 202.112.10.36, stratum 2, offset -1.883582, delay 0.18350
server 202.112.29.82, stratum 2, offset -1.860654, delay 0.23692
server 202.112.31.197, stratum 2, offset -1.812637, delay 0.13278
server 218.75.4.130, stratum 2, offset -1.837221, delay 0.04099
31 Oct 09:07:03 ntpdate[1625]: step time server 218.75.4.130 offset -1.837221 sec
# ntpdate -q 2.cn.pool.ntp.org
server 202.112.10.36, stratum 2, offset -1.834295, delay 0.07317
server 202.112.29.82, stratum 2, offset -1.810122, delay 0.12784
server 202.112.31.197, stratum 2, offset -1.838527, delay 0.07227
server 218.75.4.130, stratum 2, offset -1.833332, delay 0.04346
31 Oct 09:12:36 ntpdate[22558]: step time server 218.75.4.130 offset -1.833332 sec


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

1
2
server 10.32.196.60, stratum 0, offset 0.000000, delay 0.00000
26 Oct 15:01:17 ntpdate[23338]: no server suitable for synchronization found


4.手工校正当前时间

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

两个对时服务器都可以用,随便挑一个就行

1
2
# ntpdate 2.cn.pool.ntp.org
31 Oct 09:14:18 ntpdate[22563]: step time server 218.75.4.130 offset -1.830097 sec

调整好后,用 date 命令检查一下,时间是否正确了

1
2
# date
Thu Oct 31 09:14:50 CST 2013

如果时间还是不对,建议用 date 命令手工调整一下,我在实验过程中没有遇到过,但有的网友说会说现这种情况


设置 NTP 的配置文件

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

1.指定上层对时服务器

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

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

prefer 表示优先使用

1
2
3
4
5
6
7
8
# 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


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

在我的实验环境中,内网使用 192.168.80.0/24 网段

1
2
3
4
5
6
7
8
9
10
# 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.80.0 mask 255.255.255.0 nomodify notrap


几点说明:

restrict default kod nomodify notrap nopeer noquery

kod kod技术可以阻止 "Kiss of Death"包(一种DOS***)对服务器的破坏

nomodify 用户端不能更改NTP服务端的时间参数,但可以通过NTP服务端进行时间校对

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

nopeer 不与其它同一层的NTP服务器进行时间同步

noquery 不提供NTP服务


restrict 还可以使用的其它参数

ignore 关闭所有NTP服务

notrust 拒绝没有通过认证的客户端


restrict 127.0.0.1默认对本机无限制

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


3.设置同步更新本地hwclock

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

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

添加SYNC_HWCLOCK="yes"


启动NTP服务

1
2
# service ntpd start
Starting ntpd:                                             [  OK  ]



启动后的观察

1.确认端口监听启动

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

1
2
3
4
5
6
7
# netstat -nlpu |grep ntpd
udp        0      0 192.168.80.3:123            0.0.0.0:*                               22816/ntpd   
udp        0      0 127.0.0.1:123               0.0.0.0:*                               22816/ntpd   
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               22816/ntpd   
udp        0      0 fe80::20c:29ff:fed4:315f:123 :::*                                    22816/ntpd   
udp        0      0 ::1:123                     :::*                                    22816/ntpd   
udp        0      0 :::123                      :::*                                    22816/ntpd

2.查看系统日志,确定启动过程无报错

1
2
3
4
5
6
7
8
9
10
11
12
# tail -n 20 -f /var/log/messages
Oct 31 10:56:29 CentOS1 ntpd[22815]: ntpd [email protected] Fri Feb 22 11:23:27 UTC 2013 (1)
Oct 31 10:56:29 CentOS1 ntpd[22816]: precision = 0.052 usec
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #0 wildcard, 0.0.0.0#123 Disabled
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #1 wildcard, ::#123 Disabled
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #2 lo, ::1#123 Enabled
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #3 eth0, fe80::20c:29ff:fed4:315f#123 Enabled
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #4 lo, 127.0.0.1#123 Enabled
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on interface #5 eth0, 192.168.80.3#123 Enabled
Oct 31 10:56:29 CentOS1 ntpd[22816]: Listening on routing socket on fd #22 for interface updates
Oct 31 10:56:29 CentOS1 ntpd[22816]: kernel time sync status 2040
Oct 31 10:56:29 CentOS1 ntpd[22816]: frequency initialized 23.140 PPM from /var/lib/ntp/drift


3.确认已进行时间同步

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

1
2
3
4
5
6
7
# ntpstat
synchronised to NTP server (202.112.31.197) at stratum 3
   time correct to within 80 ms
   polling server every 128 s
#进行时间校对的NTP服务器
#本地主机与上层NTP服务器的时间差
#下次同步时间


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

1
2
3
4
5
# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+dns1.synet.edu. 202.118.1.46     2 u   13   64   77  567.019  132.808  32.883
*dns2.synet.edu. 202.118.1.46     2 u   58   64   37  209.322  -47.016  36.082


remote 本地主机所连接的上层NTP服务器

*目前正在使用的上层NTP

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

refid 给上层NTP服务器提供时间校对的服务器

st 就是stratum 上层NTP的级别

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

poll 下一次更新在几秒后

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

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

offset 本地主机与上层NTP服务器的时间差,该值的绝对值越接近0,与上层服务器的时间就越接近

jitter 一个统计值,这个值的绝对值越小,本地主机的时间就越精确


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

1
2
3
4
5
# chkconfig --list ntpd
ntpd            0:off   1:off   2:off   3:off   4:off   5:off   6:off
# chkconfig ntpd on
# chkconfig --list ntpd
ntpd            0:off   1:off   2:on    3:on    4:on    5:on    6:off

客户端设置

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

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

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

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

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


2.启动NTP服务自动同步

如果内网服务器比较多

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

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

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


附1:

1.查看当前时区

1
2
# date
Thu Oct 31 09:37:09 EDT 2013

2.修改 /etc/sysconfig/clock

将原来的时区改为

1
2
3
4
# vi /etc/sysconfig/clock
ZONE="America/New_York"
#改为
ZONE="Asia/Shanghai"


3.覆盖 /etc/localtime

1
2
# cp -a /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite `/etc/localtime'? y

4.查看修改后的时区

1
2
# date
Thu Oct 31 21:42:33 CST 2013


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章