CentOS6.5系统搭建NTP服务器


先简单介绍几个名词

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的时间值而不会提供时区(因为它不知道你在哪里).所以当我们设置系统时间的时候,设置好时区是首先要做的工作

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

如果发现时区不正确,修改时区,详见附录1

2.检查 NTP 是否安装

# 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

ch.pool.ntp.org

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

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

01.# ntpdate -q 1.cn.pool.ntp.org
02.server 202.112.10.36, stratum 2, offset -1.883582, delay 0.18350
03.server 202.112.29.82, stratum 2, offset -1.860654, delay 0.23692
04.server 202.112.31.197, stratum 2, offset -1.812637, delay 0.13278
05.server 218.75.4.130, stratum 2, offset -1.837221, delay 0.04099
06.31 Oct 09:07:03 ntpdate[1625]: step time server 218.75.4.130 offset -1.837221 sec
07.# ntpdate -q 2.cn.pool.ntp.org
08.server 202.112.10.36, stratum 2, offset -1.834295, delay 0.07317
09.server 202.112.29.82, stratum 2, offset -1.810122, delay 0.12784
10.server 202.112.31.197, stratum 2, offset -1.838527, delay 0.07227
11.server 218.75.4.130, stratum 2, offset -1.833332, delay 0.04346
12.31 Oct 09:12:36 ntpdate[22558]: step time server 218.75.4.130 offset -1.833332 sec

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

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

4.手工校正当前时间

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

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

1.# ntpdate 2.cn.pool.ntp.org
2.31 Oct 09:14:18 ntpdate[22563]: step time server 218.75.4.130 offset -1.830097 sec
注意:如果NTPD服务已经运行了,这个命令使用不了,要先关闭NTPD服务,就可以了,

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

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

如果时间还是不对,建议用 date 命令手工调整一下,我在实验过程中没有遇到过.

置 NTP 的配置文件

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

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

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

在我的实验环境中,内网使用 192.168.80.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.80.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.80.0 mask 255.255.255.0 nomodify notrap这是我添加的,允许192.168.80.0/24 网段的主机来进行对时,但不允许客户端来修改,登录我的NTP服务器

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

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"

启动NTP服务

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

启动后的观察

1.确认端口监听启动

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

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

3.确认已进行时间同步

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

1.# ntpstat
2.synchronised to NTP server (202.112.31.197) at stratum 3
3.time correct to within 80 ms
4.polling server every 128 s

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

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

remote 远程主机的主机名或IP

*目前正在使用的上层NTP

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

refid 上级NTP的IP

st 就是stratum 上层NTP的层级

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

poll 下一次更新在几秒后

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

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

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


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

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

客户端设置

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

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

1.# vim /etc/crontab
2.*/3 * * * * root (/usr/sbin/ntpdate 192.168.80.3 && /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.80.3 && /sbin/hwclock -w) & >/dev/null)

2.启动NTP服务自动同步

如果内网服务器比较多

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

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

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

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

附1:

1.查看当前时区

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

2.修改 /etc/sysconfig/clock

将原来的时区改为

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

3.覆盖 /etc/localtime

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

4.查看修改后的时区

1.# date
2.Thu Oct 31 21:42:33 CST 2013
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值