|
源代码网推荐源代码网整理以下一.TimeServer的搭建(在此我们使用ntp:NetworkTimeProtocol) TimeServer即时间服务器。我们经常会发现服务器上的时间不正确,这样会影响到我们的应用,有时甚至会带来一些不良后果。比如影响我们的备份,影响我们的数据库的更新等。比较常见的解决方法是与公网上的时间服务器同步(只用使用crontab执行0002***/usr/sbin/ntpdate195.13.1.153就行了,后面的195.13.1.153就是公网上可以用的时间服务器之一),但这样做计算机必须能连接公网,这样也许会有一些安全问题。并且如果你的计算机在内网不能直接连接公网的时间服务器的话,那就……今天我们就来讲一讲搭建我们自己的时间服务器的做法。 方法一:时间服务器与公网上的时间服务器同步,其它机器与自己的时间服务器同步。缺点:时间服务器还要暴露在公网上。 步骤: 1.检查是否有相应的包 代码: #rpm-qa|grepntp ntp-4.1.1a-9 chkfontpath-1.9.6-3
如果没有ntp这个包,则从光盘上装上。执行下面命令安装NTP的RPM包: #rpm-ivhntp-4.1.1a-9.i386.rpm(我使用的是redhat8.0,请根据你的实际情况安装对应的包)
2.修改配置文件 /etc/ntp.conf是ntp的主要配置文件,里面设置了你用来同步时间的时间服务器的域名或者IP地址,下面是到互联网同步时间的最基本的配置: 首先我们来定义自己喜欢的时间服务器:(可用的时间服务器,参看http://chinaunix.net/jh/5/100591.html,或者参看:http://www.eecis.udel.edu/~mills/ntp/servers.html) 代码: server195.13.1.153 server194.137.39.67
接下来,我们设置上面两台服务器的访问权限,在这个例子中我们不允许它们修改或者查询我们配置在Linux上的NTP服务器。 代码: restrict195.13.1.153mask255.255.255.255nomodifynotrapnoquery restrict194.137.39.67 mask255.255.255.255nomodifynotrapnoquery
说明:掩码255.255.255.255是用来限制远程NTP服务器的掩码地址。 然后设置允许访问的内网机器。请注意,配置中noquery已经去掉了: 代码: restrict192.168.1.0mask255.255.255.0notrustnomodifynotrap
在此例中,掩码地址扩展为255,因此从192.168.1.1-192.168.1.254的计算机都可以使用我们的NTP服务器来同步时间。如果你想限制的更严格一点,你可以修改你的掩码。
最后,也是最重要的是默认的限制配置要从你配置文件中删除,否则它将覆盖你所有的配置选项,你会发现如果不删除该配置,你的时间服务器将只能和自己通讯。如果ntp.conf中有以下一行,请将它注释: 代码: #restrictdefaultignore
3.检查可用性 a.保存你的配置文件,然后对每个你在ntp.conf里配置的时间服务器执一下查询命令,确保这些上游时间服务器可用。 代码: #ntpdate195.13.1.153 27Jun10:12:01ntpdate[25475]:adjusttimeserver133.100.11.8offset-0.127154sec #ntpdate194.137.39.67 27Jun10:12:06ntpdate[25478]:adjusttimeserver133.100.9.2offset0.010008sec
b.执行下列命令 #ntpq-p …………输出略
一个可以证明同步问题的证据是所有远程服务器的jitter值是4000并且delay和reach的值是0。
代码: remote refid sttwhenpollreach delay offset jitter
==========================================================
LOCAL(0)LOCAL(0)10l- 6470.0000.000 0.008
* 0.0.0.016u- 6400.0000.0004000.00
4.设置自启动 为了使NTP服务可以在系统引导的时候自动启动,执行:
代码: #chkconfigntpdon
启动/关闭/重启NTP的命令是: 代码:
#/etc/init.d/ntpdstart #/etc/init.d/ntpdstop #/etc/init.d/ntpdrestart
5.客户端的设置 a.linux客户端 以root身份登录,执行crontab-e输入0002***/usr/sbin/ntpdate192.168.1.1(换成你的TimeServer的ip) 这样就会在每天的凌晨两点自动与TimeServer同步时间。 b.windowsXp客户端 双击右下角的时间,出现“日期和时间属性”的窗口,选择Internet时间,在服务器一栏中输入你的TimeServer的ip,点击"立即更新",过几秒钟将能看到更新成功的提示。然后勾选“自动与Internet时间服务器同步”。点击确定。 方法二:时间服务器与自己的硬件时钟同步,其它机器与时间服务器同步。缺点:如果TimeServer的硬件时钟不准确,则所有的时间将不准确。优点:更安全,没有暴露在公网上的机器。 unix类系统的时钟都有两种,一种是硬件时钟,一种是系统时钟。在此不在详述。步骤如下: 1.校准Timeserver的硬件时钟(可以直接在bios中设置),或者用hwclock命令来校对,例如:hwclock--set--date="6/16/0411:14:05" 2.设置系统时间和硬件时钟同步: 输入:hwclock--hctosys. 3.修改配置文件 vi/etc/ntp.conf,我的ntp.conf如下 代码: #Prohibitgeneralaccesstothisservice. #restrictdefaultignore
#Permitallaccessovertheloopbackinterface. Thiscould #betightenedaswell,buttodosowouldeffectsomeof #theadministrativefunctions. restrict127.0.0.1
#--CLIENTNETWORK------- #Permitsystemsonthisnetworktosynchronizewiththis #timeservice. Donotpermitthosesystemstomodifythe #configurationofthisservice. Also,donotusethose #systemsaspeersforsynchronization. #restrict192.168.1.0mask255.255.255.0notrustnomodifynotrap
#---OURTIMESERVERS----- #orremovethedefaultrestrictline #Permittimesynchronizationwithourtimesource,butdonot #permitthesourcetoqueryormodifytheserviceonthissystem.
#restrictmytrustedtimeserveripmask255.255.255.255nomodifynotrapnoquery
#---NTPMULTICASTCLIENT--- #multicastclient#listenondefault224.0.1.1 #restrict224.0.1.1mask255.255.255.255notrustnomodifynotrap #restrict192.168.1.0mask255.255.255.0notrustnomodifynotrap restrict192.168.1.0mask255.255.255.0notrustnomodifynotrap
#---GENERALCONFIGURATION--- # #UndisciplinedLocalClock.Thisisafakedriverintendedforbackup #andwhennooutsidesourceofsynchronizedtimeisavailable.The #defaultstratumisusually3,butinthiscaseweelecttousestratum #0.Sincetheserverlinedoesnothavethepreferkeyword,thisdriver #isneverusedforsynchronization,unlessnootherother #synchronizationsourceisavailable.Incasethelocalhostis #controlledbysomeexternalsource,suchasanexternaloscillatoror #anotherprotocol,thepreferkeywordwouldcausethelocalhostto #disregardallothersynchronizationsources,unlessthekernel #modificationsareinuseanddeclareanunsynchronizedcondition # #server127.127.1.0#localclock server127.127.1.0prefer fudge 127.127.1.0stratum10
# #Driftfile. Putthisinadirectorywhichthedaemoncanwriteto. #Nosymboliclinksallowed,either,sincethedaemonupdatesthefile #bycreatingatemporaryinthesamedirectoryandthenrename()"ing #ittothefile. # driftfile/etc/ntp/drift broadcastdelay 0.008
# #Authenticationdelay. Ifyouuse,orplantousesomeday,the #authenticationfacilityyoushouldmaketheprogramsintheauth_stuff #directoryandfigureoutwhatthisnumbershouldbeonyourmachine. # authenticateyes
# #Keysfile. Ifyouwanttodiddleyourserveratruntime,makea #keysfile(mode600forsure)anddefinethekeynumbertobe #usedformakingrequests. # #PLEASEDONOTUSETHEDEFAULTVALUESHERE.Pickyourown,orremote #systemsmightbeabletoresetyourclockatwill.Notealsothat #ntpdisstartedwitha-Aflag,disablingauthentication,that #willhavetoberemovedaswell. # keys/etc/ntp/keys
其它设置和方法一相同, 源代码网供稿. |