Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2543063
  • 博文数量: 271
  • 博客积分: 6659
  • 博客等级: 准将
  • 技术积分: 3141
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-17 10:24
文章分类

全部博文(271)

文章存档

2016年(2)

2015年(12)

2014年(7)

2013年(19)

2012年(22)

2011年(81)

2010年(128)

分类: LINUX

2010-03-25 03:44:06

1、NTP 是什么?
计算机主机主要是以 BIOS 内部的时间为主要的时间依据,而偏偏这个时间可能因为 BIOS 内部芯片本身的问题,而导致 BIOS 时间与标准时间 (UTC) 有一点点的差异存在!所以,为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步 ( synchronize ) 的工作就显的很重要了!
 
2、使用 RPM 安装
[root@test root]# chkconfig --level 2345 ntpd off
[root@test root]# /etc/rc.d/init.d/ntpd stop
如此一来,您的 ntp 套件已经安装完毕,可以使用 ntp 的 client 功能去联机 Time server 进行网络校时了!至于 NTP Server 则需要继续的进行设定呢!
 
3、NTP 的套件结构
NTP 套件的结果主要分为两部份,一个是 NTP Server 的部分,一个则是 NTP Client 的部分,在这个章节里面,我们不谈比较困难的设定,主要介绍较为简易的设定而已喔!所以,您需要注意到的档案与指令有底下这几个:
 
4、与 NTP 及时区有关的几个设定档:
/etc/ntp.conf  这个是 NTP daemon 的主要设定档,依据不同的版本放置的目录可能会不同,不过档名都是一样的!使用 locate ntp.conf 搜寻一下您的系统有没有这个档案吧!这也是 NTP唯一的一个设定档案!

/usr/share/zoneinfo 这是个目录,这个目录是 Linux 本身提供的,而不是 NTP 所提供的。在这个目录下的档案其实是规定了各主要时区的时间设定档案,例如台湾地区的时区设定档案在 /usr/share/zoneinfo/Asia/Taipei 就是了!这个目录里面的档案与底下要谈的两个档案( clock 与 localtime )是有关系的喔!
 
/etc/sysconfig/clock 这个档案其实也不包含在 NTP 的 daemon 当中,因为这个是 linux 的主要时区设定档案啊!每次开机后,Linux 会自动的读取这个档案来设定自己系统所预设要显示的时间说!举个例子来说,在我们台湾地区的本地时间设定中,这个档案内应该会出现一行『ZONE="Asia/Taipei"』的字样,这表示我们的时间设定档案『要取用 /usr/share/zoneinfoe/Asia/Taipei 那个档案』的意思!

/etc/localtime 这个档案就是『本地端的时间设定档』啦!刚刚那个 clock 档案里面规定了使用的时间设定档 (ZONE) 为 /usr/share/zoneinfo/Asia/Taipei ,所以说,这就是本地端的时间了,此时, Linux 系统就会将 Taipei 那个档案复制一份成为 /etc/localtime ,所以未来我们的时间显示就会以 Taipei 那个时间设定档案为准。好了,如果现在我这部主机搬到日本东京去了,那么我应该如何调整时间呢?其实什么调整都不需要,因为我们的 localtime 主要是分析与 UTC 时间的时差来显示的格式,所以,您只要将 /etc/sysconfig/clock 里面的 ZONE 设定成为 Asia/Tokyo 并且将 /usr/share/zoneinfo/Asia/Tokyo 复制成为 /etc/localtime ,呵呵!什么设定都不需要更动,就能显示时间为日本东京的时间了!这样是否能够了解?
 
5、与 NTP 及时间有关的执行档:
􀂃 /bin/date  这个是 Linux 系统上面常见的日期与时间输出指令,用途很广喔!除了输出时间外,也可以修改时间。

􀂃 /sbin/hwclock 这是一个 root 才能执行的指令,因为 Linux 系统上面 BIOS 时间与 Linux 系统时间是分开的,所以使用 date 这个指令调整了时间之后,还需要使用 hwclock 才能将修改过后的时间写入 BIOS 当中!

􀂃 /usr/sbin/ntpd 这就是 NTP 的主要 daemon 档案啦!得要启动他才能提供 NTP 服务。注意,这个指令预设会参考 /etc/ntp.conf 里面的设定喔!

􀂃 /usr/sbin/ntpdate 这个就是 Client 端用来连接 NTP Server 的主要执行档啰!如果您没有要启用 NTP 而仅想要使用 NTP Client 功能的话,那么只会用到这个指令而已啦!

􀂃 /usr/sbin/ntptrace 这个指令可以用来追踪某部时间服务器的时间对应关系,这也是个很有用的指令喔!底下我们会介绍如何使用这支程序!
 
6、主要设定档 /etc/ntp.conf 说明
[root@localhost ~]# cat ntp.conf
# 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 这个参数来设定,主要的语法为:
# restrict IP mask netmask_IP parameter
# 其中 IP 可以是软件地址,也可以是 default ,default 就类似 0.0.0.0 咯!
# 至于 paramter 则有:
# ignore : 关闭所有的 NTP 联机服务
# nomodify: 表示 Client 端不能更改 Server 端的时间参数,不过,
# Client  端仍然可以透过 Server 端来进行网络校时。
# notrust : 该 Client 除非通过认证,否则该 Client 来源将被视为不信任网域
# noquery : 不提供 Client 端的时间查询 # 如果 paramter 完全没有设定,那就表示该 IP (或网域) 『没有任何限制!』
# 在我们这个例子当中,因为拒绝所有,仅开放特定的IP段 , # 并且让 127.0.0.1 以及本机 IP 192.168.1.1 可以不受限制,所以:
restrict default ignore  # 关闭所有的 NTP 要求封包
restrict 127.0.0.1   # 开启内部递归网络接口 lo
#restrict -6 ::1  
restrict 211.150.7.1  # 主机本身的 IP 也同时开启!
restrict 192.168.1.1  # 主机本身的 IP 也同时开启!
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 针对另一个 IP 开放让他可以更新时间! 
restrict 192.168.1.0 mask 255.255.255.0  nomodify notrap
restrict 61.55.1.0 mask 255.255.255.0  nomodify notrap
restrict 124.115.1.0 mask 255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool ().
# 上层主机我们选择 time-b.nist.gov prefer ,要设定上层主机主要以 server这个参数来设定,语法为:
# server [IP|FQDN] [prefer]
server time-b.nist.gov prefer   # 后面加上 perfer 的话,那表示我们的 NTP 主机主要以该部主机来作为时间校正的对应
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
#broadcast 192.168.1.255 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 key 42  # manycast client
# 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
# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
#为了解决更新时间封包的传送延迟动作,使用 driftfile 来规定我们的主机,在与 Time Server 沟通时所花费的时间,可以记录在 driftfile
#我们的 NTP server 与  time-b.nist.gov prefer 联机时所花费的时间会记录在 /var/lib/ntp/drift 档案内
driftfile /var/lib/ntp/drift
# 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
 
7、NTP 的启动与观察
[root@test root]# /etc/rc.d/init.d/ntpd start
[root@test root]# netstat -unl | grep 123
udp 0 0 192.168.1.1:123 0.0.0.0:*
udp 0 0 127.0.0.1:123 0.0.0.0:*
udp 0 0 0.0.0.0:123 0.0.0.0:*
# 请注意喔,NTP 使用的是 UDP 的封包!而且 port number 为 123,
# 此外,我有两个接口,以及对外提供服务,所以自然就会有三个!
[root@test root]# ntptrace 192.168.1.1
192.168.1.1: stratum 3, offset 0.000056, synch distance 0.65865
210.59.157.10: stratum 2, offset -0.228265, synch distance 0.22488
ntp0.usno.navy.mil: stratum 1, offset -0.250685, synch distance 0.00038,
# 我们也可以利用 ntptrace 来追踪一下到底我们的主机有没有正确的先经过
# 上层 Time Server 的校时了呢?如上所示,我们的主机 192.168.1.1
# 是第三层的时间服务器 (stratum 3),与目前本机的时间误差(offset)
# 以及若要同步更新时(synch distance)的时间损耗,同时,也会将这个第三层
# 对哪一部第二层主机进行校时也列出来!有时候您会发现这样的错误讯息:
192.168.1.1: stratum 16, offset 0.000048, synch distance 0.00087
0.0.0.0: *Not Synchronized*
# 这表示我们的主机尚未与 Internet 的上层 Time Server 进行校时,
# 最可能发生这样的错误在 ntp.conf 里面的 restrict 设定了!
# 此外,当启动 ntpd 后,您至少需要等待 5 分钟左右,这段时间
# 我们的 NTP Server 会不断的与上层时间服务器联系,如果尚未联系成功,
# 那么我们的 NTP 主机就会暂时无法让 Client 端来进行更新喔!
# 所以如果未能更新,不要太紧张,先等待一阵子再说吧!
如果 ntptrace 可以成功的话,那就表示您的主机 OK 啦!
 
8、linux时间
目前 Linux 系统上面有两个时间喔,一个是 Linux 系统,另一个则是 BIOS 时间(真正的硬件记录的时间)!我们可以使用 date 这个指令来手动修正目前主机的时间,不过, date 这个指令仅修正
Linux 时间而已,我们还需要以 hwclock 这个指令来将 BIOS 时间也更新才行!

[root@test root]# hwclock [-rw]
-r:检视目前的 BIOS 时间
-w:将目前 Linux 的时间写入 BIOS 当中!

[root@test root]# date ; hwclock -r
Thu Aug 21 10:01:46 CST 2003 Thu 21 Aug 2003 09:57:52 AM CST 0.647923 seconds
# 你可以看到, date 与 hwclock -r 所显示的时间是『不一致的』!
# 这就是因为 Linux 时间与 BIOS 时间不一致所导致的一个问题!
# 我们需要以 hwclock -w 来将 Linux 时间写入 BIOS 喔!
[root@test root]# hwclock -w

9、在 Linux 系统自动网络校时?
[root@test root]# ntpdate 192.168.1.1
 
!若要每日进行时间校正,可以写入 cron
[root@test root]# vi /etc/crontab
# 加入这一行: 10 5 * * * root /usr/sbin/ntpdate 192.168.0.2 ; /sbin/hwclock -w
使用 cron 之后,每天 5:10 Linux 系统就会自动的进行网络校时啰!相当的简易吧!
 
11、安全相关方面
/sbin/iptables -A INPUT -p UDP -i eth0 -s 192.168.0.0/24  --dport 123 -j ACCEPT

12、另外附上修改过后的/etc/ntp.conf文件
 
# 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 default ignore
restrict 127.0.0.1
#restrict -6 ::1
restrict 211.150.1.1
restrict 192.168.1.1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.1.0 mask 255.255.255.0  nomodify notrap
restrict 61.55.1.0 mask 255.255.255.0  nomodify notrap
restrict 124.115.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool ().
server time-b.nist.gov prefer
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
#broadcast 192.168.1.255 key 42         # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 key 42             # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 key 42  # manycast client
# 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
# Drift file.  Put this in a directory which the daemon can write to.
# No symbolic links allowed, either, since the daemon updates the file
# by creating a temporary in the same directory and then rename()'ing
# it to the file.
driftfile /var/lib/ntp/drift
# 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
 
 
参考资源:鸟哥的Linux私房菜
阅读(6661) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~