服務管理——ntp

來源:互聯網
上載者:User

服務管理——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的時間伺服器是整個系統的基礎。 時間同步在真實環境中使用較多,不同機器時間不一致,向同一台資料庫伺服器寫資料會出問題。這僅僅只是一個應用。 在學習時間同步伺服器之前,我們先瞭解下時間相關的命令:[plain] #查看時間  [root@serv01 ~]# date  Wed Aug 7 17:47:44 CST 2013  [root@serv01 ~]# date -s ""     #查看時區  [root@larrywen /]# cat /etc/sysconfig/clock  # The time zone of the system is defined bythe contents of /etc/localtime.  # This file is only for evaluation bysystem-config-date, do not rely on its  # contents elsewhere.  ZONE="Asia/Chongqing"     #可以重新設定時區  [root@larrywen /]# system-config-date     #查看不同地區的時區資訊  [root@larrywen /]# ls /usr/share/zoneinfo/  Africa     Australia  Cuba     Etc     GMT0       Iceland      Japan     MST      Poland      right     Universal  Zulu  America    Brazil     EET      Europe  GMT-0      Indian       Kwajalein  MST7MDT Portugal    ROC        US  Antarctica Canada     Egypt    Factory GMT+0      Iran         Libya      Navajo  posix       ROK        UTC  Arctic     CET        Eire     GB      Greenwich  iso3166.tab  MET       NZ       posixrules  Singapore WET  Asia       Chile      EST      GB-Eire Hongkong   Israel       Mexico     NZ-CHAT PRC         Turkey     W-SU  Atlantic   CST6CDT    EST5EDT  GMT     HST        Jamaica      Mideast   Pacific  PST8PDT     UCT       zone.tab     #設定時區不重要,重要的是不同機器之間的時間同步起來  [root@serv01 ~]# cat /etc/sysconfig/clock  ZONE="Asia/Chongqing"  [root@serv01 ~]# date  Wed Aug 7 09:52:31 CST 2013     #修改時區  [root@serv01 ~]# date  Wed Aug 7 09:52:31 CST 2013  #修改成日本時區  [root@serv01 ~]# vim /etc/sysconfig/clock  [root@serv01 ~]# cat /etc/sysconfig/clock  ZONE="Asia/Tokyo"  #拷貝內容  [root@serv01 ~]# cp/usr/share/zoneinfo/Asia/Tokyo /etc/localtime  cp: overwrite `/etc/localtime'? Y  #查看時間,發現立即改變,不需要重啟  [root@serv01 ~]# date  Wed Aug 7 10:55:44 JST 2013     #使用工具修改時間  #安裝工具  [root@serv01 ~]# yum install/usr/bin/system-config-date -y  [root@serv01 ~]# yum installsystem-config-date -y  #安裝system-config-date的時候會把ntp伺服器安裝上     #支援X Window的形式登入  [root@larrywen 0807]# ssh 192.168.1.11 -X  root@192.168.1.11's password:  Last login: Wed Aug  7 18:19:30 2013 from 192.168.1.1  /usr/bin/xauth:  creating new authority file /root/.Xauthority  #修改時間,改成重慶時區  [root@serv01 ~]# system-config-date  Gtk-Message: Failed to load module"pk-gtk-module": libpk-gtk-module.so: cannot open shared object file:No such file or directory  [root@serv01 ~]# cat /etc/localtime  [root@serv01 ~]# date  Wed Aug 7 10:03:22 CST 2013   硬體時鐘和軟體時鐘Linux將時鐘分為系統時鐘(SystemClock)和硬體(Real Time Clock,簡稱RTC)時鐘兩種。系統時間是指當前Linux Kernel中的時鐘,也就是軟體時鐘。而硬體時鐘則是主板上由電池供電的那個主板硬體時鐘,這個時鐘可以在BIOS的“Standard BIOS Feture”項中進行設定。[plain] #同步硬體時鐘和軟體時鐘  [root@serv01 ~]# clock --help  hwclock - query and set the hardware clock(RTC)     Usage: hwclock [function] [options...]     Functions:    -s | --hctosys     set the system time from the hardware clock    -w | --systohc      set the hardware clock to the currentsystem time     #ntp:Network TimeProtocal   二 ntp同步時間 [plain] <span style="color:#ff0000">#第一步,安裝ntp</span>  [root@serv01 ~]# yum install ntp* -y     #查詢安裝的RPM包  [root@serv01 ~]# rpm -qa|grep ntp  ntp-4.2.4p8-2.el6.x86_64  ntpdate-4.2.4p8-2.el6.x86_64  fontpackages-filesystem-1.41-1.1.el6.noarch  [root@serv01 ~]# chkconfig|grep ntp  ntpd                 0:off       1:off       2:off       3:off       4:off       5:off       6:off  ntpdate               0:off       1:off       2:off       3:off       4:off       5:off       6:off     [root@serv01 ~]# ls /etc/ntp.conf  /etc/ntp.conf  [root@serv01 ~]# rpm -ql ntp     <span style="color:#ff0000">#第二步,修改設定檔</span>  [root@serv01 ~]# vim /etc/ntp.conf  [root@serv01 ~]# man 5 ntp.conf     [root@serv01 ~]# vim /etc/ntp.conf  [root@serv01 ~]# cat /etc/ntp.conf  #配置如下  restrict 192.168.1.0 mask 255.255.255.0nomodify notrap  #注釋上一級時間同步伺服器  #server 0.rhel.pool.ntp.org  #server 1.rhel.pool.ntp.org  #server 2.rhel.pool.ntp.org  #取消注釋  server    127.127.1.0  # local clock  fudge     127.127.1.0stratum 10        <span style="color:#ff0000">#第三步,啟動服務</span>  [root@serv01 ~]# /etc/init.d/ntpd start  Starting ntpd:                                            [  OK  ]  #重啟後生效  [root@serv01 ~]# chkconfig ntpd on  [root@serv01 ~]# chkconfig|grep ntpd  ntpd                 0:off       1:off       2:on       3:on       4:on       5:on       6:off  ntpdate               0:off       1:off       2:off       3:off       4:off       5:off       6:off  [root@serv01 ~]#     #serv02的配置  [root@serv02 ~]# vim /etc/ntp.conf  [root@serv02 ~]# cat /etc/ntp.conf  server 192.168.1.11  [root@serv02 ~]# vim /etc/ntp/step-tickers  [root@serv02 ~]# cat /etc/ntp/step-tickers  # List of servers used for initialsynchronization.  192.168.1.11  [root@serv02 ~]# date  Wed Aug 7 18:30:18 CST 2013  [root@serv02 ~]# /etc/init.d/ntpdate start  ntpdate: Synchronizing with time server:                   [  OK  ]  [root@serv02 ~]# date  Wed Aug 7 10:29:39 CST 2013   三 ssh雙向等效性驗證時間同步伺服器[plain] #serv01  [root@serv01 ~]# ssh-keygen  [root@serv01 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.12  [root@serv01 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.11     #serv02  [root@serv02 ~]# ssh-keygen  [root@serv02 ~]# ssh-copy-id -i~/.ssh/id_rsa.pub 192.168.1.11     #可以查看同步的時間  [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date;  Wed Aug 7 10:34:50 CST 2013  Wed Aug 7 10:34:50 CST 2013     #修改時間  [root@serv01 ~]# date -s"10:38:00"  Wed Aug 7 10:38:00 CST 2013     #查看serv02的時間  [root@serv02 ~]# date  Wed Aug 7 10:35:42 CST 2013  #關閉服務  [root@serv02 ~]# /etc/init.d/ntpdate stop  #開啟服務  [root@serv02 ~]# /etc/init.d/ntpdate start  ntpdate: Synchronizing with timeserver:                   [  OK  ]  #可以看到已經同步了  [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  Wed Aug 7 10:38:57 CST 2013  Wed Aug 7 10:38:57 CST 2013     [root@serv02 ~]# ntpq -p 192.168.1.11      remote           refid      st t when poll reach   delay  offset  jitter  ==============================================================================  *LOCAL(0)        .LOCL.          10 l  33   64  377   0.000    0.000   0.000     #使用命令同步,並將同步命令做成死迴圈  [root@serv02 ~]# ntpdate 192.168.1.11   7Aug 23:02:44 ntpdate[1342]: adjust time server 192.168.1.11 offset 0.000041 sec  #可以看到已經同步了  [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  Wed Aug 7 23:02:53 CST 2013  Wed Aug 7 23:02:53 CST 2013  [root@serv01 ~]# date -s "10:50"  Wed Aug 7 10:50:00 CST 2013     [root@serv02 ~]# ntpdate 192.168.1.11   7Aug 10:51:10 ntpdate[1372]: step time server 192.168.1.11 offset -43989.248450sec  [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  Wed Aug 7 10:51:15 CST 2013  Wed Aug 7 10:51:15 CST 2013     #寫成死迴圈  [root@serv02 ~]# while :; do ntpdate192.168.1.11; sleep 3; done   7Aug 10:53:27 ntpdate[1380]: adjust time server 192.168.1.11 offset -0.000066sec   7Aug 10:53:31 ntpdate[1382]: adjust time server 192.168.1.11 offset 0.000133 sec  [root@serv01 ~]# date -s "10:57"  Wed Aug 7 10:57:00 CST 2013  [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date  Wed Aug 7 10:57:12 CST 2013  Wed Aug 7 10:57:13 CST 2013     #可以放到後台執行  [root@serv02 ~]# while :; do ntpdate192.168.1.11; sleep 3; done > /dev/null 2>&1 &     #寫到設定檔,下次啟動自動執行  [root@serv02 ~]# vim /etc/rc.local  [root@serv02 ~]# tail -n1 /etc/rc.local  while :; do ntpdate 192.168.1.11; sleep 3;done >/dev/null 2>&1 &     #另外一種方式(不安全:在同一網段的任何人都可以使用)  serv01  [root@serv01 ~]# chkconfig xinetd on  [root@serv01 ~]# vim /etc/xinetd.d/  chargen-dgram   daytime-dgram   discard-dgram   echo-dgram      tcpmux-server   time-stream      chargen-stream  daytime-stream  discard-stream  echo-stream     time-dgram     #將disbale由yes改為no   [root@serv01 ~]# vim/etc/xinetd.d/time-stream  #將disbale由yes改為no   [root@serv01 ~]# vim/etc/xinetd.d/time-dgram  #也可以這樣開啟  [root@serv01 ~]# chkconfig time-stream on  [root@serv01 ~]# chkconfig time-dgram on     #啟動xinetd服務  [root@serv01 ~]# /etc/init.d/xinetd start  Starting xinetd:                                          [  OK  ]     #安裝openssh-clients  [root@serv01 ~]# yum installopenssh-clients -y  #製作公開金鑰  [root@serv01 ~]# ssh-keygen  #拷貝到本機  [root@serv01 ~]# ssh-copy-id -i.ssh/id_rsa.pub 192.168.1.11  #拷貝到serv02  [root@serv01 ~]# ssh-copy-id -i.ssh/id_rsa.pub 192.168.1.12  #設定時間  [root@serv01 ~]# date -s"11:36:00"  Wed Aug 7 11:36:00 CST 2013  #安裝rdate  [root@serv02 /]# yum install rdate -y  #同步時間  [root@serv02 /]# rdate -s 192.168.1.11  [root@serv01 ~]# ssh 192.168.1.11 date; ssh192.168.1.12 date;  Wed Aug 7 11:36:29 CST 2013  Wed Aug 7 11:36:28 CST 2013     #可以寫死迴圈  [root@serv02 ~]# while :; do rdate -s192.168.1.11; sleep 3; done >/dev/null 2>&1 &^C  [root@serv02 ~]# vi /etc/rc.local  [root@serv02 ~]# tail -n1 /etc/rc.local  while :; do rdate -s 192.168.1.11; sleep 3;done >/dev/null 2>&1 &  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.