对多个linux服务器,时间保持一致是很必要的。根据精确度要求,应该有相应的时间间隔进行时间同步。如果不进行时间同步,时间久了就会差别很大,遇到问题时定位就很困难。因为多台设备的配合,log之间可能有前因后果,时间是同步事件的先后的重要依据。
一般来说,对一个机房内的设备,可以设置一台时间服务器,由它定期从一个标准的时间服务器上获取时间。其他的服务器可以通过内网的连接从这台服务器进行同步。这样不仅时间会一致,而且照顾到一些没有公网的设备。
本文测试系统:
[root@test ~]# cat /etc/*release
CentOS release 5.2 (Final)
[root@test ~]# rpm -qf /usr/sbin/ntpd
ntp-4.2.2p1-8.el5.centos.1在安装时应该确定ntp包已经安装。
启动服务器
如果ntpd已经安装,则可以直接启动:
[root@test ~]# service ntpd start
Starting ntpd: [ OK ]
同时,也需要检查一下配置文件,centos缺省都配置好了。
[root@test ~]# vi /etc/ntp.conf
server 0.centos.pool.ntp.org
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
检查一下时间服务器是否可用:
[root@test ~]# ping 0.centos.pool.ntp.org
PING 0.centos.pool.ntp.org (74.88.39.232) 56(84) bytes of data. 64 bytes from ool-4a5827e8.dyn.optonline.net (74.88.39.232): icmp_seq=1 ttl=54 time=251 ms 如果不可用,则确定一下网络是否能连接到外网。检查一下域名解析是否配置。[root@test ~]# cat /etc/resolv.conf
nameserver 8.8.8.8编辑配置文件
vi /etc/ntp.conf 首先定义服务器 server pool.ntp.org restrict default nomodify notrap noquery restrict 192.168.0.0 mask 255.255.255.0 notrust nomodify notrap #从192.168.0.1-192.168.0.254的服务器都可以使用我们的NTP服务器来同步时间。 注释掉以下一行 #restrict default ignore①、第一种配置:允许任何IP的客户机都可以进行时间同步
将“restrict default kod nomodify notrap nopeer noquery”这行修改成: restrict default nomodify 配置文件示例:/etc/ntp.conf ②、第二种配置:只允许192.168.18.***网段的客户机进行时间同步 在restrict default nomodify notrap noquery(表示默认拒绝所有IP的时间同步)之后增加一行: restrict 192.168.18.0 mask 255.255.255.0 nomodify
设置ntpd自启动
[root@test ~]# find /etc/rc.d/ -name "*ntpd"
/etc/rc.d/rc6.d/K74ntpd /etc/rc.d/init.d/ntpd /etc/rc.d/rc3.d/K74ntpd /etc/rc.d/rc4.d/K74ntpd /etc/rc.d/rc5.d/K74ntpd /etc/rc.d/rc2.d/K74ntpd /etc/rc.d/rc1.d/K74ntpd /etc/rc.d/rc0.d/K74ntpd [root@test ~]# /sbin/chkconfig --level 345 ntpd on [root@test ~]# !find find /etc/rc.d/ -name "*ntpd" /etc/rc.d/rc6.d/K74ntpd /etc/rc.d/init.d/ntpd /etc/rc.d/rc3.d/S58ntpd /etc/rc.d/rc4.d/S58ntpd /etc/rc.d/rc5.d/S58ntpd /etc/rc.d/rc2.d/K74ntpd /etc/rc.d/rc1.d/K74ntpd /etc/rc.d/rc0.d/K74ntpd 说明在3,4,5三个级别已经可以自启动。
检查防火墙
[root@test ~]# iptables -L
对比较严格的防火墙,应该对ntp端口123进行配置:
[root@test ~]# iptables -A INPUT -p udp --dport 123 -j ACCEPT
[root@test ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT udp -- anywhere anywhere udp dpt:ntp
客户端配置
客户端采用ntpdate来更新,配置在crontab中。根据需要决定频率。在每一台需要同步时间的设备上设置crontab
[root@test1 ~]# crontab -e
00 00 * * * /usr/sbin/ntpdate 192.168.12.31
192.168.12.31是test服务器的内网地址。
crontab 设置的是每天0点同步时间。
为了保证时间服务器可用,将命令先在命令行下执行一下。
[root@test1 ~]# ntpdate 192.168.12.31
30 Mar 17:45:24 ntpdate[16495]: step time server 192.168.12.31 offset 0.694312 sec [root@test1 ~]# date Tue Mar 30 17:45:37 CST 2010说明同步时间成功。
配置时间同步客户机
vi /var/spool/cron/root 增加一行,在每天的5点13分、9点13分、14点13分、19点13分与时间同步服务器进行同步 13 5,9,14,19 * * * /usr/sbin/ntpdate 192.168.18.2 备注:如果客户机没有ntpdate,可以下载ntpdate.tar.gz到/usr/sbin/目录,然后解压: wget http://blog.s135.com/p_w_upload/200708/ntdate.tar.gz cd /usr/sbin/ tar zxvf ntpdate.tar.gz