歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> 服務管理——ntp

服務管理——ntp

日期:2017/3/1 14:44:19   编辑:關於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的時間服務器是整個系統的基礎。 時間同步在真實環境中使用較多,不同機器時間不一致,向同一台數據庫服務器寫數據會出問題。這僅僅只是一個應用。 在學習時間同步服務器之前,我們先了解下時間相關的命令: [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 [email protected]'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 &
Copyright © Linux教程網 All Rights Reserved