欢迎光临
我们一直在努力

linux服务管理之ntp时间服务器配置

一、ntp相关知识

什么是时间同步服务器?

Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。

NTP提供准确时间,首先要有准确的时间来源,这一时间应该是国际标准时间UTC。 NTP获得UTC的时间来源可以是原子钟、天文台、卫星,也可以从Internet上获取。这样就有了准确而可靠的时间源。时间按NTP服务器的等级传播。按照离外部UTC 源的远近将所有服务器归入不同的Stratum(层)中。Stratum-1在顶层,有外部UTC接入,而Stratum-2则从Stratum-1获取时间,Stratum-3从Stratum-2获取时间,以此类推,但Stratum层的总数限制在15以内。所有这些服务器在逻辑上形成阶梯式的架构相互连接,而Stratum-1的时间服务器是整个系统的基础。

在linux中,时间同步在真实环境中使用较多,不同机器时间不一致,向同一台数据库服务器写数据会出问题。这仅仅只是一个应用。

先了解linux系统中与时间相关的的一些linux命令:

  1. #查看时间  
  2. [root@serv01 ~]# date  
  3. Wed Aug 7 17:47:44 CST 2013  
  4. [root@serv01 ~]# date -s ""  
  5.    
  6. #查看时区  
  7. [root@larrywen /]# cat /etc/sysconfig/clock  
  8. # The time zone of the system is defined bythe contents of /etc/localtime.  
  9. # This file is only for evaluation bysystem-config-date, do not rely on its  
  10. # contents elsewhere.  
  11. ZONE="Asia/Chongqing"  
  12.    
  13. #可以重新设置时区  
  14. [root@larrywen /]# system-config-date  
  15.    
  16. #查看不同地区的时区信息  
  17. [root@larrywen /]# ls /usr/share/zoneinfo/  
  18. Africa     Australia  Cuba     Etc     GMT0       Iceland      Japan     MST      Poland      right     Universal  Zulu  
  19. America    Brazil     EET      Europe  GMT-0      Indian       Kwajalein  MST7MDT Portugal    ROC        US  
  20. Antarctica Canada     Egypt    Factory GMT+0      Iran         Libya      Navajo  posix       ROK        UTC  
  21. Arctic     CET        Eire     GB      Greenwich  iso3166.tab  MET       NZ       posixrules  Singapore WET  
  22. Asia       Chile      EST      GB-Eire Hongkong   Israel       Mexico     NZ-CHAT PRC         Turkey     W-SU  
  23. Atlantic   CST6CDT    EST5EDT  GMT     HST        Jamaica      Mideast   Pacific  PST8PDT     UCT       zone.tab  
  24.    
  25. #设置时区不重要,重要的是不同机器之间的时间同步起来  
  26. [root@serv01 ~]# cat /etc/sysconfig/clock  
  27. ZONE="Asia/Chongqing"  
  28. [root@serv01 ~]# date  
  29. Wed Aug 7 09:52:31 CST 2013  
  30.    
  31. #修改时区  
  32. [root@serv01 ~]# date  
  33. Wed Aug 7 09:52:31 CST 2013  
  34. #修改成日本时区  
  35. [root@serv01 ~]# vim /etc/sysconfig/clock  
  36. [root@serv01 ~]# cat /etc/sysconfig/clock  
  37. ZONE="Asia/Tokyo"  
  38. #拷贝内容  
  39. [root@serv01 ~]# cp/usr/share/zoneinfo/Asia/Tokyo /etc/localtime  
  40. cp: overwrite `/etc/localtime'? Y  
  41. #查看时间,发现立即改变,不需要重启  
  42. [root@serv01 ~]# date  
  43. Wed Aug 7 10:55:44 JST 2013  
  44.    
  45. #使用工具修改时间 
  46. #安装工具  
  47. [root@serv01 ~]# yum install/usr/bin/system-config-date -y  
  48. [root@serv01 ~]# yum installsystem-config-date -y  
  49. #安装system-config-date的时候会把ntp服务器安装上  
  50.    
  51. #支持X Window的形式登录  
  52. [root@larrywen 0807]# ssh 192.168.1.11 -X  
  53. root@192.168.1.11's password:  
  54. Last login: Wed Aug  7 18:19:30 2013 from 192.168.1.1  
  55. /usr/bin/xauth:  creating new authority file /root/.Xauthority  
  56. #修改时间,改成重庆时区  
  57. [root@serv01 ~]# system-config-date  
  58. Gtk-Message: Failed to load module"pk-gtk-module": libpk-gtk-module.so: cannot open shared object file:No such file or directory  
  59. [root@serv01 ~]# cat /etc/localtime  
  60. [root@serv01 ~]# date  
  61. Wed Aug 7 10:03:22 CST 2013  

2)、硬件时钟和软件时钟
linux将时钟分为系统时钟(systemclock)和硬件(real time clock,简称rtc)时钟两种。
系统时间是指当前linux kernel中的时钟,也就是软件时钟。(脚本学堂 www.jbxue.com 整理)
而硬件时钟则是主板上由电池供电的那个主板硬件时钟,这个时钟可以在bios的“standard bios feture”项中进行设置。

  1. #同步硬件时钟和软件时钟  
  2. [root@serv01 ~]# clock –help  
  3. hwclock – query and set the hardware clock(RTC)  
  4.    
  5. Usage: hwclock [function] [options…]  
  6.    
  7. Functions:  
  8.   -s | –hctosys     set the system time from the hardware clock  
  9.   -w | –systohc      set the hardware clock to the currentsystem time  
  10.    
  11. #ntp:Network TimeProtocal   

二、ntp同步时间

  1. #第一步,安装ntp 
  2. [root@serv01 ~]# yum install ntp* -y  
  3.    
  4. #查询安装的RPM包  
  5. [root@serv01 ~]# rpm -qa|grep ntp  
  6. ntp-4.2.4p8-2.el6.x86_64  
  7. ntpdate-4.2.4p8-2.el6.x86_64  
  8. fontpackages-filesystem-1.41-1.1.el6.noarch  
  9. [root@serv01 ~]# chkconfig|grep ntp  
  10. ntpd                 0:off       1:off       2:off       3:off       4:off       5:off       6:off  
  11. ntpdate               0:off       1:off       2:off       3:off       4:off       5:off       6:off  
  12.    
  13. [root@serv01 ~]# ls /etc/ntp.conf  
  14. /etc/ntp.conf  
  15. [root@serv01 ~]# rpm -ql ntp  
  16.    
  17. <span style="color:#ff0000">#第二步,修改配置文件</span>  
  18. [root@serv01 ~]# vim /etc/ntp.conf  
  19. [root@serv01 ~]# man 5 ntp.conf  
  20.    
  21. [root@serv01 ~]# vim /etc/ntp.conf  
  22. [root@serv01 ~]# cat /etc/ntp.conf  
  23. #配置如下  
  24. restrict 192.168.1.0 mask 255.255.255.0nomodify notrap  
  25. #注释上一级时间同步服务器  
  26. #server 0.rhel.pool.ntp.org  
  27. #server 1.rhel.pool.ntp.org  
  28. #server 2.rhel.pool.ntp.org  
  29. #取消注释  
  30. server    127.127.1.0  # local clock  
  31. fudge     127.127.1.0stratum 10      
  32.    
  33. #第三步,启动服务  
  34. [root@serv01 ~]# /etc/init.d/ntpd start  
  35. Starting ntpd:                                            [  OK  ]  
  36. #重启后生效  
  37. [root@serv01 ~]# chkconfig ntpd on  
  38. [root@serv01 ~]# chkconfig|grep ntpd  
  39. ntpd                 0:off       1:off       2:on       3:on       4:on       5:on       6:off  
  40. ntpdate               0:off       1:off       2:off       3:off       4:off       5:off       6:off  
  41. [root@serv01 ~]#  
  42.    
  43. #serv02的配置  
  44. [root@serv02 ~]# vim /etc/ntp.conf  
  45. [root@serv02 ~]# cat /etc/ntp.conf  
  46. server 192.168.1.11  
  47. [root@serv02 ~]# vim /etc/ntp/step-tickers  
  48. [root@serv02 ~]# cat /etc/ntp/step-tickers  
  49. # List of servers used for initialsynchronization.  
  50. 192.168.1.11  
  51. [root@serv02 ~]# date  
  52. Wed Aug 7 18:30:18 CST 2013  
  53. [root@serv02 ~]# /etc/init.d/ntpdate start  
  54. ntpdate: Synchronizing with time server:                   [  OK  ]  
  55. [root@serv02 ~]# date  
  56. Wed Aug 7 10:29:39 CST 2013  

三、ssh双向等效性验证时间同步服务器

  1. #serv01  
  2. [root@serv01 ~]# ssh-keygen  
  3. [root@serv01 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.12  
  4. [root@serv01 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.11  
  5.    
  6. #serv02  
  7. [root@serv02 ~]# ssh-keygen  
  8. [root@serv02 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.11  
  9.    
  10. #可以查看同步的时间  
  11. [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date;  
  12. Wed Aug 7 10:34:50 CST 2013  
  13. Wed Aug 7 10:34:50 CST 2013  
  14.    
  15. #修改时间  
  16. [root@serv01 ~]# date -s"10:38:00"  
  17. Wed Aug 7 10:38:00 CST 2013  
  18.    
  19. #查看serv02的时间  
  20. [root@serv02 ~]# date  
  21. Wed Aug 7 10:35:42 CST 2013  
  22. #关闭服务  
  23. [root@serv02 ~]# /etc/init.d/ntpdate stop  
  24. #开启服务  
  25. [root@serv02 ~]# /etc/init.d/ntpdate start  
  26. ntpdate: Synchronizing with timeserver:                   [  OK  ]  
  27. #可以看到已经同步了  
  28. [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  
  29. Wed Aug 7 10:38:57 CST 2013  
  30. Wed Aug 7 10:38:57 CST 2013  
  31.    
  32. [root@serv02 ~]# ntpq -p 192.168.1.11  
  33.     remote           refid      st t when poll reach   delay  offset  jitter  
  34. ==============================================================================  
  35. *LOCAL(0)        .LOCL.          10 l  33   64  377   0.000    0.000   0.000  
  36.    
  37. #使用命令同步,并将同步命令做成死循环  
  38. [root@serv02 ~]# ntpdate 192.168.1.11  
  39.  7Aug 23:02:44 ntpdate[1342]: adjust time server 192.168.1.11 offset 0.000041 sec  
  40. #可以看到已经同步了  
  41. [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  
  42. Wed Aug 7 23:02:53 CST 2013  
  43. Wed Aug 7 23:02:53 CST 2013  
  44. [root@serv01 ~]# date -s "10:50"  
  45. Wed Aug 7 10:50:00 CST 2013  
  46.    
  47. [root@serv02 ~]# ntpdate 192.168.1.11  
  48.  7Aug 10:51:10 ntpdate[1372]: step time server 192.168.1.11 offset -43989.248450sec  
  49. [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  
  50. Wed Aug 7 10:51:15 CST 2013  
  51. Wed Aug 7 10:51:15 CST 2013  
  52.    
  53. #写成死循环  
  54. [root@serv02 ~]# while :; do ntpdate192.168.1.11; sleep 3; done  
  55.  7Aug 10:53:27 ntpdate[1380]: adjust time server 192.168.1.11 offset -0.000066sec  
  56.  7Aug 10:53:31 ntpdate[1382]: adjust time server 192.168.1.11 offset 0.000133 sec  
  57. [root@serv01 ~]# date -s "10:57"  
  58. Wed Aug 7 10:57:00 CST 2013  
  59. [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  
  60. Wed Aug 7 10:57:12 CST 2013  
  61. Wed Aug 7 10:57:13 CST 2013  
  62.    
  63. #可以放到后台执行  
  64. [root@serv02 ~]# while :; do ntpdate192.168.1.11; sleep 3; done > /dev/null 2>&1 &  
  65.    
  66. #写到配置文件,下次启动自动执行  
  67. [root@serv02 ~]# vim /etc/rc.local  
  68. [root@serv02 ~]# tail -n1 /etc/rc.local  
  69. while :; do ntpdate 192.168.1.11; sleep 3;done >/dev/null 2>&1 &  
  70.    
  71. #另外一种方式(不安全:在同一网段的任何人都可以使用)  
  72. serv01  
  73. [root@serv01 ~]# chkconfig xinetd on  
  74. [root@serv01 ~]# vim /etc/xinetd.d/  
  75. chargen-dgram   daytime-dgram   discard-dgram   echo-dgram      tcpmux-server   time-stream      
  76. chargen-stream  daytime-stream  discard-stream  echo-stream     time-dgram     
  77. #将disbale由yes改为no   
  78. [root@serv01 ~]# vim/etc/xinetd.d/time-stream  
  79. #将disbale由yes改为no   
  80. [root@serv01 ~]# vim/etc/xinetd.d/time-dgram  
  81. #也可以这样打开  
  82. [root@serv01 ~]# chkconfig time-stream on  
  83. [root@serv01 ~]# chkconfig time-dgram on  
  84.    
  85. #启动xinetd服务  
  86. [root@serv01 ~]# /etc/init.d/xinetd start  
  87. Starting xinetd:                                          [  OK  ]  
  88.    
  89. #安装openssh-clients  
  90. [root@serv01 ~]# yum installopenssh-clients -y  
  91. #制作公钥  
  92. [root@serv01 ~]# ssh-keygen  
  93. #拷贝到本机  
  94. [root@serv01 ~]# ssh-copy-id -i.ssh/id_rsa.pub 192.168.1.11  
  95. #拷贝到serv02  
  96. [root@serv01 ~]# ssh-copy-id -i.ssh/id_rsa.pub 192.168.1.12  
  97. #设置时间  
  98. [root@serv01 ~]# date -s"11:36:00"  
  99. Wed Aug 7 11:36:00 CST 2013  
  100. #安装rdate  
  101. [root@serv02 /]# yum install rdate -y  
  102. #同步时间  
  103. [root@serv02 /]# rdate -s 192.168.1.11  
  104. [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date;  
  105. Wed Aug 7 11:36:29 CST 2013  
  106. Wed Aug 7 11:36:28 CST 2013  
  107.    
  108. #可以写死循环  
  109. [root@serv02 ~]# while :; do rdate -s192.168.1.11; sleep 3; done >/dev/null 2>&1 &^C  
  110. [root@serv02 ~]# vi /etc/rc.local  
  111. [root@serv02 ~]# tail -n1 /etc/rc.local  
  112. while :; do rdate -s 192.168.1.11; sleep 3;done >/dev/null 2>&1 & 

以上就是linux下ntp服务的安装与配置方法,希望对大家有所帮助。

 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:英协网 » linux服务管理之ntp时间服务器配置

分享到: 生成海报
avatar

热门文章

  • 评论 抢沙发

    • QQ号
    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址

    登录

    忘记密码 ?

    切换登录

    注册

    我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活