ubuntu ntp 时间同步 搭建 使用
2014-02-25 20:08
246 查看
简介
网络时间协议(NTP)是用来帮助Linux系统时钟与准确的时间源同步的协议.允许让网上所有站点同步时间.分为两类:*第1类: NTP站点使用的原子钟计时.
*第2类: NTP站点和较小的准确时间源.( NTP sites with slightly less accurate time sources)
这个做法好处是至少有一个服务器在您的网络上成为本地时间服务器,为其他设备服务。(This makes the correlation of system events on different systems much easie)这使得在不同的系统上的系统事件相关性对比变得容易。它也降低了由于NTP的通讯需要的互联网带宽使用量,并减少为每个NTP的客户端配置防火墙规则.而且并非所有服务器将有因特网接入.在这种情况下,您需要一个中央服务器处理所有访问.
查询可用的第1类和第2类服务器清单,清请访问http://www.ntp.org/
[编辑]
下载和安装NTP包
最版RedHat和Fedora Linux软件产品都可以用RPM的格式.从RPMS中下载并安装并不难.如果您需要复修,参考第6章"安装Linux软件"("Installing Linux Software")有所有的细节。当寻找该文件,记住NTP RPMs的文件名通常用NTP加版本号,如:NTP - 4.1.2 - 5.i386.rpm
[编辑]/etc/ntp.conf
文件
/etc/ntp.conf 文件是Linux NTP的主要配置文件,你可以设置NTP服务器的ip地址.下面是建立互联网基础的NTP服务器的步骤1) 首先指定有兴趣的服务器:
server otherntp.server.org # A stratum 1 server at server.org server ntp.research.gov # A stratum 2 server at research.gov
2)限制你允许的这些服务器的访问类型,在这个例子中的服务器是不容许修改运行时配置或查询您的Linux NTP服务器:
restrict otherntp.server.org mask 255.255.255.255 nomodify notrap noquery restrict ntp.research.gov mask 255.255.255.255 nomodify notrap noquery
The mask 255.255.255.255 statement is really a subnet mask limiting access to the single IP address of the remote NTP servers.
3)如果此服务器也是要提供时间给其他计算机,如PC,其他Linux服务器和网络设备,那么您必须界定网络从这个服务器接受NTP同步的请求。你要修改restric语句,去掉noquery关键字,让网络查询您的NTP服务器。语法是:
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
在这种情况下,mask包括所有255个在本地网络内可能的IP地址.
4) 还要确保localhost(这个常用的IP地址用来指Linux服务器本身)有足够权限.使用没有任何限制关键词的语法:
restrict 127.0.0.1
5) 保存文件并重新启动NTP,这些设置才会生效.现在,您可以用相似的方式设定您的网络上其他的Linux主机与这个新的主NTP服务器同步.
[编辑]怎样开始NTP
当你修改了配置信息,你必须重新启动NTP进程以取得及时的效果.开始配置NTP,用下面的命令:
[root@bigboy tmp]# chkconfig ntpd on
启动,停止,重启NTP,用下面的命令:
[root@bigboy tmp]# service ntp start [root@bigboy tmp]# service ntp stop [root@bigboy tmp]# service ntp restart
测试和解决NTP问题
在配置和启动NTP后,你应该测试以确定NTP是否在工作,参考下面的指引去检测NTP的工作.
[编辑]检查NTP运行
使用下面的命令检查NTP服务是否运行[root@bigboy tmp]# pgrep ntpd
这时你应该能得到一个进程ID.
[编辑]
同步初始化
如果本地主机的时间和主时间服务器的差距很大,你的NTP的进程可能会终止,并且在/var/log/messages 留下错误信息.你应该在同步服务器时间之前,运行ntpdate -u命令预先得道时间.不过ntpdate命令不会一直运行,你仍然需要运行ntpd后台进程去自动同步时间.下面的范例中,服务器最初的时间是在午夜,然后同步时间到am 8:03.
先检查自己的服务器的时间
[root@smallfry tmp]# date Thu Aug 12 00:00:00 PDT 2004 [root@smallfry tmp]#
ntpdate 命令在smallfry机器上运行了3次去同步192.168.1.100服务器的时间,不过它必须在ntpd进程停止的时候运行,所以你要先停止ntpd,运行ntpdate,然后再运行ntpd.
[root@smallfry tmp]# service ntpd stop [root@smallfry tmp]# ntpdate -u 192.168.1.100 Looking for host 192.168.1.100 and service ntp host found : bigboy.my-site.com 12 Aug 08:03:38 ntpdate[2472]: step time server 192.168.1.100 offset 28993.084943 sec [root@smallfry tmp]# ntpdate -u 192.168.1.100 Looking for host 192.168.1.100 and service ntp host found : bigboy.my-site.com 12 Aug 08:03:40 ntpdate[2472]: step time server 192.168.1.100 offset 2.467652 sec [root@smallfry tmp]# ntpdate -u 192.168.1.100 Looking for host 192.168.1.100 and service ntp host found : bigboy.my-site.com 12 Aug 08:03:42 ntpdate[2472]: step time server 192.168.1.100 offset 0.084943 sec [root@smallfry tmp]# service ntpd start [root@smallfry tmp]#
现在时间改正过来了.
[root@smallfry tmp]# date Thu Aug 12 08:03:45 PDT 2004 [root@smallfry tmp]#
[编辑]
检定NTP 同步
使用ntpq命令查看与您同步的服务器.它提供你一份时间服务器配置清单,包括延误值(delay),偏差值(offset)和抖动值(jitter).为了能正确同步,延迟值和偏移值应该不为零,抖动值(jitter)应小于100 .[root@bigboy tmp]# ntpq -p
这是命令输出的结果:
remote refid st t when poll reach delay offset jitter ============================================================================== -jj.cs.umb.edu gandalf.sigmaso 3 u 95 1024 377 31.681 -18.549 1.572 milo.mcs.anl.go ntp0.mcs.anl.go 2 u 818 1024 125 41.993 -15.264 1.392 -mailer1.psc.edu ntp1.usno.navy. 2 u 972 1024 377 38.206 19.589 28.028 -dr-zaius.cs.wis ben.cs.wisc.edu 2 u 502 1024 357 55.098 3.979 0.333 +taylor.cs.wisc. ben.cs.wisc.edu 2 u 454 1024 347 54.127 3.379 0.047 -ntp0.cis.strath harris.cc.strat 3 u 507 1024 377 115.274 -5.025 1.642 *clock.via.net .GPS. 1 u 426 1024 377 107.424 -3.018 2.534 ntp1.conectiv.c 0.0.0.0 16 u - 1024 0 0.000 0.000 4000.00
[编辑]
Linux NTP 客户端无法同步
下表的迹象表明你没有得到适当的同步,因为所有远程服务器的jitter值为4000,delay和reach值为0remote refid st t when poll reach delay offset jitter ============================================================================= LOCAL(0) LOCAL(0) 10 l - 64 7 0.000 0.000 0.008 ntp-cup.externa 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00 snvl-smtp1.trim 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00 nist1.aol-ca.tr 0.0.0.0 16 u - 64 0 0.000 0.000 4000.00
可能的原因:
如果您使用的DNS名称做为NTP服务器时旧版本的的NTP包会工作不正常.在这样的情况下,您要使用的实际IP地址代替.
防火墙阻止访问NTP服务器.这可能是NTP服务器和它的时间源之间有防火墙软件如iptables的限制.
在NTP客户端的restrict字段里面有notrust nomodify notrap的关键字.Fedora Core 2的某些版本中除非NTP客户端的restrict字段里面删除notrust nomodify notrap的关键字,否则不能执行NTP同步.
在下面例子中,restrict字段只是客户端网络的定义而没有任何关键字:
#-- CLIENT NETWORK ------- #restrict 172.16.1.0 mask 255.255.255.0 notrust nomodify notrap restrict 172.16.1.0 mask 255.255.255.0
[编辑]
=================example
Server 192.168.126.1
Client 192.168.126.2
服务器:
修改配置文件添加这一行vim /etc/ntp.conf
restrict 192.168.126.0 mask 255.255.255.0 nomodify notrap
启动服务
/etc/init.d/ntpd start
防火墙需要开123端口或者关闭防火墙
-A INPUT -m state --state NEW -m tcp -p tcp --dport 123 -jACCEPT
客户端:
1. data -s "xxxx--xx-xx xx:xx:xx" 修改客户端时间
2.与服务端同步时间执行以下命令
手工执行或用crontab来执行
手工:ntpdate 192.168.126.1
crontab –e
0 21 * * * ntpdate 192.168.126.1 >> /root/ntpdate.log 2>&1
3. 查看本地时间是否被改正
date
相关文章推荐
- EF里的继承映射关系TPH、TPT和TPC的讲解以及一些具体的例子
- Yii中缓存依赖的处理
- 利用Busybox移植Yaffs2根文件系统
- PHP中获取当前页面的完整URL
- php操作mysql数据库
- 源码安装LAMP,以Module方式使用PHP并使用xcache 2.0.0加速PHP
- PHP性能优化工具–xhprof安装
- PHP 登录DEMO
- php中变量传递的几种办法
- ftp 不要用 sun ftpclient
- PHP中Get()和Post()用法详解
- php-fpm ---error
- php 函数小结
- php处理cnd 创建11.bat批处理文件
- PHP5 session 详解【经典】
- php正则表达式匹配中文
- PHP字符编码绕过漏洞总结
- php 二维|数组|合并 多个一维数组合并成二维数组
- php @符号的使用
- php代码结尾不要添加结尾标记