打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ntp服务搭建与配置
ntp服务搭建与配置
2015-06-05 11:34 3529人阅读 评论(1)  举报
 分类:
linux(18) 
安装NTP Server
Linux系统内核提供了对NTP的支持,因此只需要再安装一个NTP Server的守护进程即可。
常用的ntpd由ntp.org提供。
查找当前系统是否已安装ntp
http://www.ntp.org/downloads.html [root@localhost ~]# rpm -qa | grep ntp
chkfontpath-1.10.1-1.1
ntp-4.2.2p1-8.el5.centos.1       (这个就是已经安装的RPM包)
如果没安装
下载安装ntpd的稳定版本。目前是ntp-4.2.6p3.
wget http://www.eecis.udel.edu/~ntp/ntp_spool/ntp4/ntp-4.2/ntp-4.2.6p3.tar.gz
tar zxf ntp-4.2.6p3.tar.gz
mkdir /app/ntp
cd ntp-4.2.6p3
./configure --prefix=/app/ntp --enable-all-clocks --enable-parse-clocks
make clean && make check && make && make intall
注意:/app/ntp为ntpd的安装后文件路径
配置NTP服务器
1.NTP server的主配置文件为/etc/ntp.conf 现对/etc/ntp.conf的各项进行说明
#配置前做好备份
cd /etc/
mv ntp.conf ntp.bak.conf
2.vi /etc/ntp.conf(以下是ntp.conf文件的内容)
#设置此服务器同上层服务器做时间同步的IP地址,空格 后加prefer意味着首选IP地址
#经试验,下面的几个时间服务器速度还不错  常见列表 http://www.pool.ntp.org/zone/cn
server 3.cn.pool.ntp.org
server 0.asia.pool.ntp.org
server 2.asia.pool.ntp.org
#记录上次的NTP server与上层NTP server联接所花费的时间
driftfile /data/ntp/drift
#设置默认策略为允许任何主机进行时间同步 #设置默认策略为允许任何主机进行时间同步 default定义默认访问规则,nomodify禁止远程主机修改本地服务器配置,notrap拒绝#特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询(这里的查询是服务器本身状态查询)。
restrict default nomodify
#设置允许访问此时间服务器的时间服务的IP地址
#根据自己实际情况配置
#restrict 192.168.10.2  # 指定某台机器时间同步
#restrict 192.168.0.0 mask 255.255.255.0 #允许192.168.0.0/254子网内主机时间同步
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap #允许任何主机跟进行时间同步
#指定阶层编号为10,降低其优先度。 当服务器与公用的时间服务器失去联系时以本地时间为客户端提供时间服务
server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10
#设置ntp日志的path
statsdir /data/ntp/log
#设置ntp日志文件
logfile /data/ntp/log/ntp.log
端口
ntp使用udp协议,记得开放其123端口。
启动NTPD
为了使NTP服务可以在系统引导的时候自动启动,执行:
#chkconfig ntpd on
启动ntpd:
service ntpd start
NTP客户端配置:
在客户端手动执行“ntpdate 服务器IP”来同步时间;
另可以使用crond来定时同步时间:
以root身份运行周期性任务:
[root@supersun root]# crontab -e
添加以下内容,每15分钟更新一下时间:
*/15空格*空格 *空格 *空格 *空格 ntpdate空格 服务器IP
此处的ntpdate命令包含在ntp软件包中,记得确认系统中是否已安装。
本人自己设置的ntp时钟同步
通过crontab -e命令将如下命令周期性执行
10 3 * * 3 /usr/sbin/ntpdate 219.233.237.2 && /sbin/hwclock -w
ps:每周三的3:10分 与时间服务器219.233.237.2同步时间,并将当前时间写入BIOS中
检查时间服务器是否正确同步
使用下面的命令检查时间服务器同步的状态:
#ntpq -p
一个可以证明同步有问题的证据是:所有远程服务器的jitter值是4000并且delay和reach的值是0。
可能的原因有:
有防火墙阻断了与server之间的通讯,即123端口是否正常开放;
此外每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接,否则你在客户端执行“ntpdate 服务器ip”的时候将返回:
27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found
当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个:
错误1.Server dropped: Strata too high
在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。
在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
这是因为NTP server还没有和其自身或者它的server同步上。
以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。
server 127.127.1.0 fudge
127.127.1.0 stratum 8
在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
那么如何知道何时ntp server完成了和自身同步的过程呢?
在ntp server上使用命令:
# watch ntpq -p
出现画面:
Every 2.0s: ntpq -p                                                                                                             Thu Jul 10 02:28:32 2008
remote           refid      st t when poll reach   delay   offset jitter
==============================================================================
192.168.30.22   LOCAL(0)         8 u   22   64    1    2.113 179133.   0.001
LOCAL(0)        LOCAL(0)        10 l   21   64    1    0.000   0.000  0.001
注意LOCAL的这个就是与自身同步的ntp server。
注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。
如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。
错误2.Server dropped: no data
从客户端执行netdate –d时有错误信息如下:
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
transmit(192.168.30.22)
192.168.30.22: Server dropped: no data
server 192.168.30.22, port 123
.....
28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出现这个问题的原因可能有2:
1。检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。
使用以下命令检查ntp的版本:
# ntpq -c version
下面是来自ntp官方网站的说明:
The behavior of notrust changed between versions 4.1 and 4.2.
In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".
In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd
解决:
把notrust去掉。
2。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。
可以用命令
#service iptables stop
来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
ntpd时钟同步服务
linux centos ntp 时间服务器配置
Linux(Unix)时钟同步ntpd服务配置方法
linux ntp时间同步
在局域网中搭建自己的NTP时间服务器
Linux 时间同步
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服