linu下NTP服务的配置

上传人:ta****u 文档编号:216216173 上传时间:2023-06-05 格式:DOCX 页数:15 大小:81.65KB
收藏 版权申诉 举报 下载
linu下NTP服务的配置_第1页
第1页 / 共15页
linu下NTP服务的配置_第2页
第2页 / 共15页
linu下NTP服务的配置_第3页
第3页 / 共15页
资源描述:

《linu下NTP服务的配置》由会员分享,可在线阅读,更多相关《linu下NTP服务的配置(15页珍藏版)》请在装配图网上搜索。

1、Network Time Protocol(NTP,网络时间协议)用于同步它所有客户端时钟的服务。NTP 服务器将本地系统的时钟与一个公共的NTP服务器同步然后作为时间主机提供服务,使 本地网络的所有客户端能同步时钟。同步时钟最大的好处就是相关系统上旦志文件中的数据,如果网络中使用中央日志主 机集中管理日志,得到的日志结果就更能反映真实情况。在同步了时钟的网络中,集中 式的性能监控、服务监控系统能实时的反应系统信息,系统管理员可以快速的检测和解 决系统错误。安装配置NTP服务下面将介绍NTP服务器的简单配置第一步,安装NTP服务一般的Linux发行版都会带ntp软件包,如果你的系统中还没有安装

2、,就使用rpm 命令安装此包,以下以centos系统为例配置一台时间服务器:查找当前系统是否已安装ntprootlocalhost # rpm -qa | grep ntp chkf on tpath-1.10.1-1.1ntp-422p1-8.el5.centos.1(这个就是已经安装的RPM包)如果没有安装,可用下例命令安装:rootlocalhost # rpm -ivh ntp-422p1-8.el5.centos.1.rpm第二步,配置NTP服务器NTP服务器配置如下:编辑配置文件/etc/ ntp.c onfrestrict default kod no modify no tra

3、p no peer no queryrestrict -6 default kod no modify no trap n opeer no queryrestrict 127.0.0.1restrict -6 :1restrict 192.16810 mask 2552552550 nomodify notrapserver 192 168 146225server 0.centos.pool.ntp.orgserver 1.ce ntos.pool. ntp.orgserver 2.centos.pool.ntp.orgserver 127.127.1.0# local clockfudg

4、e 127.127.1.0 stratum 10配置文件说明如下:第一行restrict、default定义默认访问规则,nomodify禁止远程主机修改本地服务器 配置,notrap拒绝特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询(这里的查 询是服务器本身状态查询)。restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap这句是手动增加的,意思是从192.168.1.1-192.168.1.254的服务器都可以使用我们 的NTP服务器来同步时间。server 192.168.146.225这句也是手动增加的,指明

5、局域网中作为NTP服务器的IP;配置文件的最后两行作用是当服务器与公用的时间服务器失去联系时以本地时间为 客户端提供时间服务。端口ntp使用udp协议,记得开放其123端口。启动NTPD为了使NTP服务可以在系统引导的时候自动启动,执行:#chkc on fig n tpd on启动ntpd:service ntpd startNTP客户端配置:在客户端手动执行“ntpdate服务器IP”来同步时间;另可以使用crond来定时同步时间:以root身份运行周期性任务:rootsupers un root# cron tab -e添加以下内容,每15分钟更新一下时间:15 * * * * ntpd

6、ate 服务器 IP此处的ntpdate命令包含在ntp软件包中,记得确认系统中是否已安装。第三步,检査时间服务器是否正确同步使用下面的命令检查时间服务器同步的状态:#ntpq -p一个可以证明同步有问题的证据是:所有远程服务器的jitter值是4000并且delay和 reach的值是0。可能的原因有:有防火墙阻断了与server之间的通讯,即123端口是否正常开放;此外每次重启NTP服务器之后大约要35分钟客户端才能与server建立正常的通 讯连接,否则你在客户端执行“ntpdate服务器ip”的时候将返回:27 Jun 10:20:17 ntpdate21920: no server

7、suitable for synchronization foundLinux下NTP服务器的配置Linux下的ntp软件不但能自动与互联网上的时钟保持同步,同时本身已经是一台 SNTP服务器了,可以供局域网内的电脑校对时间。服务配置如下:第一步安装软件包我用的是RPM包安装的,或者到http:/www.eecis.udel.edu/ntp/去下载xntp重 新编译一个新的。#rpm -qa | grep ntpnt p-4.2.0-7chkfo ntpat h-1.10.0T第二步 让LAN的时间服务器(第三级)与互联网上的时间服务器(第一或者第二级)同步修改/e tc/n tp.conf这

8、是NTP的主要配置文件,里面设置了你用来同步时间的时间服务器的域名或者IP 地址,下面是到 互联网同步时间的最基本的配置:首先定义我们喜欢的时间服务器:server nt p.research.govserver ot her nt p.research.gov接下来,我们设置上面两台服务器的访问权限,在这个例子中我们不允许它们修改 或者查询我们配置在Linux上的NTP服务器res trie t nt p.research.gov mask 255.255.255.255 nomodify not rap noqueryres trie t ot her nt p.research.gov

9、mask 255.255.255.255 nomodify not rap noquery掩码255.255.255.255是用来 限制远程NTP服务器的掩码地址。接下来设置允许访问我们时间服务器的客户机地址,通常这些服务器都应该位于我 们自己局域网内。请注意,配置中noquery已经去掉了:res trie t 192.168.0.0 mask 255.255.255.0 not rus t nomodify not rap在上例中,掩码地址扩展为255,因此从192.168.0.1-192.168.0.254的服 务器都 可以使用我们的NTP服务器来同步时间。最后,也是最重要的是默认的限制

10、配置要从你配置文件中删除,否则它将覆盖你所 有的配置选项,你将发现如果不删除该配置,你的时间服务器将只能和自己通讯。如果 ntp.conf中有以下一行,请将它注释:#restriet default ignore保存你的配置文件,然后对每个你在nt p.conf里配置的时间服务器执行2编查询命 令:#nt pda te nt p.research.gov27 Jun 10:12:01 ntpdate25475: adjust time server ntp.research.gov offset -0.127154 sec#nt pda te nt p.research.gov27 Jun 1

11、0:12:06 ntpdate25478: adjust time server ntp.research.gov offset 0.010008 sec第三步启动NTP进程为了使NTP服务可以在系统引导的时候自动启动,执行:#chkconfig ntpd on启动/关闭/重启NTP/查看状态的命令是:#/etc/init .d/ntpd start#/etc/init .d/ntpd stop#/etc/init .d/ntpd restart#/etc/init .d/ntpd status切记每次修改了配置文件后都需要重新启动服务来使配置生效。可以使用下面的命 令来检查NTP服务 是否启

12、动,你应该可以得到一个进程ID号:#pgrep ntpd 第四步 检査时间服务器是否正确同步使用下面的命令检查时间服务器同步的状态:#ntpq -p一个可以证明同步问题的证据是所有远程服务器的jitter值是4000并且delay和reach的值是0。可能的原因有:配置文件中的restrict default ignore没有被注释有防火墙阻断了与server之间的通讯此外每次重启NTP服务器之后大约要3 5分钟客户端才能与server建立正常的通 讯连接,否则你执行nt pda te ip的时候将返回:27 Jun 10:20:17 nt pda te21920: no server sui

13、t able for synchroniza tion found第五步客户端与ntp服务器同步时间在客户端安装NTP,安装过程同NTP在服务器端。客户端开启ntp服务#service ntpd start与ntp服务器同步#ntpdate (ntp服务器地址)接下来编辑/et c/n tp.confnt p.conf# #server 127.127.1.0 # local clockfudge 127.127.1.0 stratum 10server stdti me.gov.hk # A stratum 1 server at server.orgserver 192.168.x.y #

14、x.y为你前面所装机器在局域网里的IPdriftfile /etc/ntp/driftbroadcastdelay 0.008authenticate nokeys /etc/ntp/keysres trie t 192.168.X.0 mask 255.255.255.0 not rus t nomodify not rap/x.0 为你 所在局域网段res trie t 127.0.0.1restrict 192.168.x.y #x.y为你前面所装机器在局域网里的IP#restriet default ignore# #同时配置#/sbin/service ntpd start /启动

15、ntpd 参数可为 res tart start stop#/sbin/chkconfig -add ntpd#/sbin/chkconfig -level 234 nt pd on /配置在开机时运行如何检查?#netstat -unl | grep 123 /查看 123 端口#ndptrace 192.168.x.y /看校对时间过程,出现offset即为正常 否则为time out# ntpq -p如果出现jitter的值为4000则是防火墙或者网络问题正常为remote refid st t when poll reach delay offset jitter*clock.nc.f

16、ukuok .GPS. 1 u 43 64 37 19.067 -6.884 10.339+clock. tl.fukuok .GPS. 1 u 36 64 35 19.670 -3.259 2.341 L0CAL(0) L0CAL(0) 5 l 45 64 37 0.000 0.000 0.001几点注意:1虽然ntp溢出问题较少,但建议配置大型网罗的时候,不要装在重要数据库服务 器或者Web主机上(Ntp是root权限)2在遇到问题之前,先看看ntp自带的文档。3防火墙问题的话,送一句配置$TMP -t filter -A INPUT -p udp -destination-port 12

17、3 -j ACCEPT 也就是123 udp in全部接受。如果子网IP仍然提示4000错误,可以把res trie t 192.168.0.0 mask 255.255.255.0 not rus t nomodify not rap改为res trie t 192.168.0.0 mask 255.255.255.0 nomodify对于权限参数的说明可以参考台湾鸟哥的文章的说明:rootroot# vi /etc/ntp.conf# 1.關於權限設定部分# 權限的設定主要以restrict這個參數來設定,主要的語法為:restriet IP mask netmask_IP paramet

18、er# 其中IP可以是軟體位址,也可以是default , default就類似0.0.0.0咯!# 至於param ter則有:# ignore:關閉所有的NTP連線服務# nomodify:表示Client端不能更改Server端的時間參數,不過,# Client端仍然可以透過Server端來進行網路校時。# notrust:該Client除非通過認證,否則該Client來源將被視為不信任網域# noquery :不提供Client端的時間查詢# 如果param ter完全沒有設定,那就表示該IP (或網域)沒有任何限制!# 在我們這個例子當中,因為拒絕所有,僅開放192.168.0.0/

19、24,# 並且讓127.0.0.1以及本機IP 192.168.0.2可以不受限制,所以: restrict default ignore #關閉所有的NTP要求封包res trict 127.0.0.1#開啟內部遞迴網路介面lorestrict 192.168.0.2#主機本身的IP也同時開啟!res trict 192.168.100.20mask 255.255.255.255 nomodify#針對另一個IP開放讓他可以更新時間!res trict 192.168.0.0mask 255.255.255.0 nomodify#在網域裡面的client可以進行網路校時,但不會影響Serv

20、er !# 2.上層主機的設定# 上層主機我們選擇time.stdtime.gov. tw,要設定上層主機主要以server# 這個參數來設定,語法為:# server IP|FQDN prefer# Server後面接的就是我們上層Time Server囉!而如果Server參數# 後面加上perfer的話,那表示我們的NTP主機主要以該部主機來作為# 時間校正的對應。另外,為了解決更新時間封包的傳送延遲動作,# 所以可以使用driftfile來規定我們的主機# 在與Time Server溝通時所花費的時間,可以記錄在driftfile# 後面接的檔案內,例如下面的範例中,我們的NTP se

21、rver與# time.stdtime.gov. tw連線時所花費的時間會記錄在/etc/ntp/drift檔案內#先輸入第二層主機的IPserver 210.59.157.10 preferserver 210.59.157.30 prefer server 202.39.157.155 prefer#第一層的主機就列為參考用!server 210.59.157.40server 210.59.157.41server 202.39.157.151#當然要讓Server可以進入我們的NTP主機啦!權限要開放啊!res trict 210.59.157.10restrict 210.59.15

22、7.30restrict 202.39.157.155restrict 210.59.157.40res trie t 210.59.157.41restrict 202.39.157.151driftfile /etc/ntp/drift最后附上我的修改过后的/etc/ntp.conf文件。参考了以下几篇文章:在RedHat9下配置时间服务器时间同步NTP服务器的配置 原创linux NTP配置 簡易 NTP 伺服器設定# Prohibit general access to this service.#restriet default ignoreres trie t66.187.224.

23、4mask255.255.255.255nomodifynot rapnoqueryres trie t66.187.224.4mask255.255.255.255nomodifynot rapnoqueryres trie t66.187.224.4mask255.255.255.255nomodifynot rapnoqueryres trie t66.187.233.4mask255.255.255.255nomodifynot rapnoquery# Permit all access over the loopback interface. This could# be tight

24、ened as well, but to do so would effect some of# the administrative functions.res trie t 127.0.0.1# CLIENT NETWORK # Permit systems on this network to synchronize with this# time service. Do not permit those systems to modify the# configuration of this service. Also, do not use those# systems as pee

25、rs for synchronization.res trie t 192.168.28.0 mask 255.255.255.0 nomodify# OUR TIMESERVERS# or remove the default restriet line# Permit time synchronization with our time source, but do not# permit the source to query or modify the service on this system.# restriet mytrustedtimeserverip mask 255.25

26、5.255.255 nomodify notrap noquery# server mytrustedtimeserverip# NTP MULTICASTCLIENT#mu lti cas tclien t# lis ten on defau lt 224.0.1.1# res trie t 224.0.1.1 mask 255.255.255.255 not rus t nomodify not rap# res trie t 192.168.1.0 mask 255.255.255.0 not rus t nomodify not rap# GENERAL CONFIGURATION#

27、Undisciplined Local Clock. This is a fake driver intended for backup# and when no outside source of synchronized time is available. The# default stratum is usually 3, but in this case we elect to use stratum# 0. Since the server line does not have the prefer keyword, this driver# is never used for s

28、ynchronization, unless no other other# synchronization source is available. In case the local host is# eontrolled by some external source, such as an external oscillator or# another protocol, the prefer keyword would cause the local host to# disregard all other synchronization sources, unless the ke

29、rnel# modifications are in use and declare an unsynchronized condition.#server 66.187.224.4fudge 127.127.1.0 stratum 10# Drift file. Put this in a directory which the daemon can write to.# No symbolic links allowed, either, since the daemon updates the file# by creating a temporary in the same direc

30、tory and then rename()ing# it to the file.#driftfile /var/lib/ntp/driftbroadcastdelay 0.008# Authentication delay. If you use, or plan to use someday, the# authentication facility you should make the programs in the auth_stuff# directory and figure out what this number should be on your machine.#aut

31、henticate yes# Keys file. If you want to diddle your server at run time, make a# keys file (mode 600 for sure) and define the key number to be# used for making requests.# PLEASE DO NOT USE THE DEFAULT VALUES HERE. Pick your own, or remote# systems might be able to reset your clock at will. Note also

32、 that# ntpd is started with a -A flag, disabling authentication, that# will have to be removed as well.#keys/etc/ntp/keysLinux 配置 NTP 服务器2012-06-14 11:11:41| 分类:Linux字号 订阅1验证Server上的NTP版本 roottestdb # rpm -q ntp n tp-4.2.2p1-9.el5_4.1如果没有安装,需要在安装光盘的Server目录下找到ntp-*.rpm并安装2修改有关权限的设置roottestdb # vim /

33、etc/ ntp.conf常用选项:ignore:禁止所有的NTP请求包进入nomodify:禁止其他计算机更改本机NTP服务的设置,但可以通过NTP服务器进行网 络校时notrust:禁止所有未通过认证的NTP包进入 noquery:禁止其他计算机查询本机NTP服务的状态我这里的设置:restrict default no modify no trap no queryrestrict 127.0.0.1restrict 192.168.1.0 mask 255.255.255.0 nomodify notraprestrict 192.168.0.0 mask 255.255.255.0

34、nomodify notrapserver 0.asia.pool. ntp.orgserver 1.asia.pool. ntp.orgserver 2.asia.pool. ntp.orgserver 3.asia.pool. ntp.orgserver 127.127.1.0fudge 127.127.1.0 stratum 103启动NTProottestdb # /etc/ in it.d/ntpd start4停止NTProottestdb # /etc/ in it.d/ntpd stop5重启NTProottestdb # /etc/ in it.d/ntpd restart6

35、设置NTP随系统启动自动加载roottestdb # chkc on fig n tpd on7检查NTP服务同步状态roottestdb # n tpq -p8客户端时间同步命令ntpdate 192.168.0.18 (此IP地址为配置的本地NTP服务器的IP地址)当用 ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的 错误的信息有以下2个:错误 1.Server dropped: Strata too high在 ntp 客户端运行 ntpdate serverIP,出现 no server suita

36、ble for synchronization found 的错误。在 ntp 客户端用 ntpdate -d serverIP 查看,发现有Server dropped: strata too high”的 错误,并且显示stratum 16”。而正常情况下stratum这个值得范围是015”。这是因为NTP server还没有和其自身或者它的server同步上。以下的定义是让NTP Server和其自身保持同步,如果在/etc/ntp.conf中定义的server 都不可用时,将使用local时间作为ntp服务提供给ntp客户端。server 127.127.1.0fudge 127.12

37、7.1.0 stratum 8在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一 个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产 生 no server suitable for synchronization found 的错误。那么如何知道何时ntp server完成了和自身同步的过程呢?在ntp server上使用命令:# watch n tpq -p出现画面:rootRHEL5 etc# ntpq -premote refid st t when poll reach delay offset j

38、itter*clock1.redhat.c .CDMA.1 u 45 64 277 375.752 -4.900 3.338注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到 17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的 时间。如果之后从ntp客户端同步ntp server还失败的话,用ntpdate -d来查询详细错误信息, 再做判断。错误 2.the NTP socket is in use, exiting rootRHEL5 etc# n tpdate 192.168.2.22614 Jun 11:08

39、:19 ntpdate3504: the NTP socket is in use, exiting在NTP服务器上不能私用ntpdate命令,可以使用ntpq -p查看同步状态错误 3.Server dropped: no data从客户端执行netdate -d时有错误信息如下:28 Jul 17:42:24 ntpdate14148: no server suitable for synchronization found 出现这个问题的原因可能有2:1检査ntp的版本,如果你使用的是ntp4.2 (包括42)之后的版本,在restrict的定 义中使用了 notrust的话,会导致以上错误。2检査ntp server的防火墙。可能是server的防火墙屏蔽了 upd 123端口。可以用命令#service iptables stop来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!