快捷搜索:   nginx

centos5 下的NTP服务

网络世界中,时间的同步 时是很重要的,有的时候出现奇怪的问题,往往可能就是时间同步问题导致的,而全球共分24个时区,所以就需要ntp器进行同步各地区的时间。计算机上 还有个硬件时间,也就是cmos时钟,在windows系统中,系统启动的时候直接从coms时钟中读取时间,关闭的时候往cmos中写入,从而保持两者 的一致性。linux系统里是通过hwclock命令来读取和设置硬件时钟的,下面就来看看centos5下的ntp吧!

[root@centos5 ~]# hwclock       查看硬件时钟
2009年07月04日 星期六 19时18分11秒 -0.609827 seconds


[root@centos5 ~]# hwclock --utc 设置硬件时钟为格林威治时间
2009年07月05日 星期日 03时19分26秒 -0.561311 seconds

[root@centos5 ~]# cat /etc/sysconfig/clock 查看系统的硬件时钟和时区配置文件
ZONE="Asia/Shanghai"
UTC=true
ARC=false

[root@centos5 ~]# ls -l /usr/share/zoneinfo/Asia/Shanghai
-rw-r--r-- 2 root root 405 2008-03-11 /usr/share/zoneinfo/Asia/Shanghai

[root@centos5 ~]# date -R   查看系统所在时区
Sun, 01 Feb 2009 01:05:53 +0800

把系统时间写入硬件时钟
[root@centos5 ~]# date -s "2009-07-04 19:04:00"
2009年 07月 04日 星期六 19:04:00 CST
[root@centos5 ~]# date
2009年 07月 04日 星期六 19:04:18 CST

[root@centos5 ~]# hwclock
2009年07月05日 星期日 19时44分32秒 -0.685570 seconds
[root@centos5 ~]# hwclock --systohc
[root@centos5 ~]# hwclock
2009年07月04日 星期六 19时04分56秒 -0.653825 seconds

把硬件时钟写入系统时钟

[root@centos5 ~]# hwclock
2009年07月03日 星期五 19时04分30秒 -0.298749 seconds

[root@centos5 ~]# date -s "2009-07-04 19:04:00"
2009年 07月 04日 星期六 19:04:00 CST
[root@centos5 ~]# hwclock --hctosys
[root@centos5 ~]# date
2009年 07月 03日 星期五 19:05:02 CST

编译方式安装ntp服务
,从该主页上下载源码包,编译并安装
[root@centos5 ~]# cd /usr/local/src/
[root@centos5 src]# tar -zxvf ntp-4.2.4p7.tar.gz
[root@centos5 src]# cd ntp-4.2.4p7
[root@centos5 ntp-4.2.4p7]# ./configure --prefix=/usr/local/ntp
[root@centos5 ntp-4.2.4p7]# make && make install
[root@centos5 /]# cd /usr/local/
[root@centos5 local]# ls
bin etc games include lib libexec ntp sbin share src
[root@centos5 local]# ls ntp/
bin lib man
[root@centos5 ntp]# cd ./bin/
[root@centos5 bin]# ls
ntpd     ntpdc       ntpq     ntptrace sntp
ntpdate ntp-keygen ntptime ntp-wait tickadj

[root@centos5 bin]# ./ntpdate ch.pool.ntp.org 试与ch.pool.ntp.org这台公网ntp服务器同步下时间
4 Jul 20:08:47 ntpdate[28663]: step time server 134.34.3.18 offset 88900.032745                                                                    

sec ,其中offset是双方的时间差

编辑配置文件并启动服务
[root@centos5 bin]#cd ..
[root@centos5 ntp]# ls
bin lib man
[root@centos5 ntp]# mkdir etc
[root@centos5 ntp]# ls
bin etc lib man
[root@centos5 ntp]# vi etc/ntpd.conf
server ch.pool.ntp.org
[root@centos5 ntp]#./bin/netpd -c /usr/local/ntp/ntpd.conf
[root@centos5 ntp]# netstat -nupl |grep 123
udp        0      0 192.168.0.200:123           0.0.0.0:*                               28697/ntpd
udp        0      0 127.0.0.1:123               0.0.0.0:*                               28697/ntpd
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               28697/ntpd
udp        0      0 fe80::20c:29ff:fefd:123     :::*                                    28697/ntpd
udp        0      0 ::1:123                     :::*                                    28697/ntpd
udp        0      0 :::123                      :::*                                    28697/ntpd

[root@centos5 /]# tail -f /var/log/messages
Jul 4 20:21:08 centos5 ntpd[28696]: ntpd Fri Jul 3 11:11:43 UTC 2009 (1)
Jul 4 20:21:08 centos5 ntpd[28697]: precision = 1.000 usec
Jul 4 20:21:08 centos5 ntpd[28697]: Listening on interface #0 wildcard, 0.0.0.0#123 Disabled
Jul 4 20:21:08 centos5 ntpd[28697]: Listening on interface #1 wildcard, ::#123 Disabled
Jul 4 20:21:08 centos5 ntpd[28697]: Listening on interface #2 lo, ::1#123 Enabled
Jul 4 20:21:08 centos5 ntpd[28697]: Listening on interface #3 eth0, fe80::20c:29ff:fefd:caa0#123 Enabled
Jul 4 20:21:08 centos5 ntpd[28697]: Listening on interface #4 lo, 127.0.0.1#123 Enabled
Jul 4 20:21:08 centos5 ntpd[28697]: Listening on interface #5 eth0, 192.168.0.200#123 Enabled
Jul 4 20:21:08 centos5 ntpd[28697]: kernel time sync status 0040

[root@centos5 /]# cd /usr/local/ntp/etc/
[root@centos5 etc]# vi ntpd.conf
server ch.pool.ntp.org
server 127.127.1.0      当网络不可用时,使用本地时间作为参考
restrict default ignore 拒绝所有时间同步请求和其他访问
restrict 127.0.0.1      允许本机和192.168.0.0/24位的主机进行时间同步
restrict 192.168.0.0 mask 255.255.255.0
restrict ch.pool.ntp.org noquery 指定上级ntp服务器可以访问本ntp服务器,但加上noquery参数,即不允许对方查询自己的服务状态

[root@centos5 /]# ps -ef |grep ntpd
root     28697     1 0 20:21 ?        00:00:00 ./bin/ntpd -c /usr/local/ntp/etc/ntpd.conf
root     28731 5928 0 20:36 pts/0    00:00:00 grep ntpd
[root@centos5 /]# kill -9 28697
[root@centos5 /]# ps -ef |grep ntpd
root     28733 5928 0 20:36 pts/0    00:00:00 grep ntpd

[root@centos5 /]# /usr/local/ntp/bin/ntpd -c /usr/local/ntp/etc/ntpd.conf
[root@centos5 /]# netstat -nupl |grep 123
udp        0      0 192.168.0.200:123           0.0.0.0:*                               28743/ntpd
udp        0      0 127.0.0.1:123               0.0.0.0:*                               28743/ntpd
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               28743/ntpd
udp        0      0 fe80::20c:29ff:fefd:123     :::*                                    28743/ntpd
udp        0      0 ::1:123                     :::*                                    28743/ntpd
udp        0      0 :::123                      :::*                                    28743/ntpd

顶(0)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论