时间的重要性不言而喻,对于一些应用,如kerberos, oracle rac,需要同步系统时钟,而计算机的是以BIOS内部的时间为依据,由于BIOS内部芯片本身的问题,有时会出现BIOS时间与UTC时间得差异,而为了避免主机时间长期运行产生的时间偏差,ntp就应遇而生。本文主要总结ntp的相关配置和使用。 1 utc 在计算时间的时候,最准确的计算应该是使用"原子震荡周期"所计算的物理时钟( Atomic Clock, 也被称为原子钟 ),这也被定义为标准时间(International Atomic Time)。而我们常常看见的 UTC 即 Coordinated Universal Time (协和标准时间)就是利用原子钟 为基准所定义出来的正确时间。这个 UTC 标准时间以 GMT 这个时区为主!所以本地时间与 UTC 时间的时差就是本地时间与 GMT 时间的时差。 2 NTP协议 网络时间协议(Network Time Protocol )是一种通过因特网服务于计算机时钟的时间同步协议。它提供了一种同步时间机制,能在庞大而复杂多样的因特网中用光速调整时间分配。它使用的是可返回时间设计,分布式子网内的时间服务器,能自我组织操作、分层管理配置,经过有线或无线方式同步子网内的逻辑时钟达到国家标准时间。此外,通过本地路由选择运算法则及时间后台程序,服务器可以重新分配标准时间。 NTP 的设计带来了三种产品——时钟偏移、时间延迟及差量,它们都与指定参考时钟相关联。时钟偏移表示调整本地时钟与参考时钟相一致而产生的偏差数;时间延迟表示在指定时间内发送消息到达参考时钟的延时时间;差量表示了相对于参考时钟本地时钟的最大偏差错误。因为大多数主机时间服务器通过其它对等时间服务器达到同步,所以这三种产品中的每一种都有两个组成部分:其一是由对等决定的部分,这部分是相对于原始标准时间的参考来源而言;其二是由主机衡量的部分,这部分是相对于对等而言。每一部分在协议中都是独立维持的,从而可以使错误控制和子网本身的管理操作变得容易。它们不仅提供了偏移和延迟的精密测量,而且提供了明确的最大错误范围,这样用户接口不但可以决定时间,而且可以决定时间的准确度。 NTP 源于时间协议和 ICMP 时间标志消息,但其设计更强调精确度和健壮性两个方面,即使是在有多路网关、延迟差量及不可靠网络上使用时。当前使用的最新版是 NTPv3 ,它与以前的版本兼容. 3 NTP服务器的安装和设置 3.1 检查需要的包 [root@rhel ~]# rpm -qa |grep ntp --ntp的主要软件,包括配置文件和执行文件等。 ntp-4.2.4p8-2.el6.i686 ntpdate-4.2.4p8-2.el6.i686 fontpackages-filesystem-1.41-1.1.el6.noarch [root@rhel ~]# rpm -qa|grep tzdata -- time zone data ,主要提供各个时区对应的显示格式。 tzdata-java-2010l-1.el6.noarch tzdata-2010l-1.el6.noarch [root@rhel ~]# 3.2配置文件 (1) /etc/ntp.conf ntp daemon的主要配置文件,不同版本可能放置的目录不同,RedHat 6 是放在/etc目录下,可以通过locate 查找。 root@rhel ~]# locate ntp.conf /etc/.ntp.conf.swp /etc/ntp.conf /usr/share/man/man5/ntp.conf.5.gz /usr/share/system-config-date/ntp.conf.templat (2) /etc/sysconfig/clock 每次开机会读取 该文件中包含的配置文件中默认色显示时间。会将ZONE对应的文件另存为/etc/localtime,可以从后面两个文件的内容看出。 [root@rhel Asia]# less /etc/sysconfig/clock # The time zone of the system is defined by the contents of /etc/localtime. # This file is only for evaluation by system-config-date, do not rely on its # contents elsewhere. ZOnE="Asia/Shanghai" (END) (3) /usr/share/zoneinfo目录 linux本身提供,www.linuxidc.com规定了各个主要时区的时间设置文件,如shanghai. root@rhel Asia]# pwd /usr/share/zoneinfo/Asia [root@rhel Asia]# ls -al Shanghai -rw-r--r--. 3 root root 405 Aug 31 2010 Shanghai [root@rhel Asia]# strings Shanghai TZif2 p ~h !Iap"^J #)Cp$Gg TZif2 !Iap #)Cp CST-8 (4)[root@rhel Asia]# strings /etc/localtime TZif2 p ~h !Iap"^J #)Cp$Gg TZif2 !Iap #)Cp CST-8 (5)/bin/date (6)/sbin/hwclock 因为biso时间和系统时间是分开的,用date调整了系统时间后,通过该命令将修改后的时间写入bios. [root@rhel Asia]# whereishwclock hwclock: /sbin/hwclock /usr/sbin/hwclock /usr/share/man/man8/hwclock.8.gz [root@rhel Asia]# hwclock Sat 17 Sep 2011 06:50:23 PM CST -0.229942 seconds (7)/usr/sbin/ntpd 服务器daemon (8)/usr/sbin/ntpdate client 端用于连接ntp server. [root@rhel Asia]# ntpdate 0.pool.ntp.org 17 Sep 19:20:32 ntpdate[16649]: step time server 115.181.34.4 offset 6404.317378 sec (9)/usr/sbin/ntptrace 3.3/etc/ntp.conf配置文件
[root@rhel ~]# less /etc/ntp.conf # For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# 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
# Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.rhel.pool.ntp.org iburst server 2.rhel.pool.ntp.org iburst
# Undisciplined Local Clock. This is a fake driver intended for backup # and when no outside source of synchronized time is available. #server 127.127.1.0 # local clock #fudge 127.127.1.0 stratum 10
# Enable public key cryptography. #crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys
# Specify the key identifiers which are trusted. #trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility. #requestkey 8
# Specify the key identifier to use with the ntpq utility. #controlkey 8
# Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats server 0.pool.ntp.org iburst (END) 说明: driftfile /var/lib/ntp/drift #由于ntp server本身的时间是依据bios芯片震动周期频率计算的,与上层ntp server不完全一致,两者频率的误差记录在该文件中。 ps:必须是完整的路径;不能是链接文件,需要设置ntp有写入权限;数值单位为百万分之一秒(ppm). [root@rhel ntp]# less drift -395.982