配置ntpd提供网络授时服务,然而,在服务器间有协


在服务器单机运行的时候,时钟准不准并不是太重要。然而,在服务器间有协作越密切,就越来越显示出时钟的重要性了。举几个简单的例子:

在Linux进行服务器文件备份的时候,备份服务器通过文件修改时间从主服务器上拉取修改的文件列表。这时,两台服务器时间不同步,就会造成列表内容过多,网络带宽浪费。

在做Oracle RAC集群的时候,如果两台服务器时间不同步则会造成主服务器已失效,备份服务器还没到切换的时间。这种时间的断层使得RAC几乎不起作用。

所以,虽然不重要,但是有必要在局域网中设置一台标准的授时服务器。Linux系统中使用ntpd软件包通过ntp协议实现网络授时。

理论描述

ntpdate命令在客户端运行,想服务器段ntpd提交时间校准请求。ntpd通过ntp协议回应客户端的请求。服务器在udp 123端口监听。

操作步骤

安装ntpd并校准服务器时间

yum install ntpd ntpdate -y
ntpdate 210.72.145.44

配置ntpd

配置服务器文件为/etc/ntp.conf。这里只显示有修改的部分。

driftfile /var/lib/ntp/drift
# 只允许从时间服务器同步时间,不允许trap,modify等请求
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
##本地允许所有操作
restrict 127.0.0.1
restrict -6 ::1
##本地网络中设置较少限制
restrict 10.0.0.0 mask 255.0.0.0 nomodify notrap
restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap
restrict 172.16.0.0 mask 255.15.0.0 nomodify notrap
##上述设置为设置客户端的权限。
# Use public servers from the pool.ntp.org project.
server 210.72.145.44 perfer
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 0.asia.pool.ntp.org
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 0.CentOS.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org
##设置本地服务器向那些服务器看齐
server  127.127.1.0     # local clock
##在时间服务器没网络连接的话,设置本地时钟。
fudge   127.127.1.0 stratum 10
##设置本地始终的权值

启动ntpd服务

service ntpd start
chkconfig ntpd on

配置防火墙

添加如下记录到防火墙配置文件,并重启防火墙

-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j accept'
service iptables restart

查看服务器工作状态

ntpstat output detial

ntpstat用来查看ntp状态,ntpq -p用来输出当前服务器从那些主机同步时间。

客户端配置

客户端使用ntpdate命令从服务器端同步时间。

ntpdate time.honliv.com

可将上述命令添加到crontab中,记得使用ntpdate的绝对路径。

变通应用

对于想医院内外环境,可以设置ntpd为本地服务器。这样所有服务器想这一天时间服务器同步时间。不用考虑是否真的和标准时间一致。设置时删除其他server 然后将

fudge   127.127.1.0 stratum 10

改成

fudge   127.127.1.0 stratum 1

即可。对于Oracle RAC环境,搭建者往往使用其中一台节点作为ntpd服务器。这点作者不敢苟同。从可靠性上讲,万一有故障的是ntpd这台呢。另一方面,RAC提供数据库服务肯定要和其他服务器联通。既然联通,使用全局ntpd服务器不挺好的。当然,全局没有就不说了。

结语

本文档讲述了ntpd的安装和配置,以及后期维护的方法。

http://www.xiyang-liu.com/2013/06/configure-ntpd-to-offer-ntp-service-in-linux/

相关内容