NTP時間同步服務器客戶端、服務端的安裝以及配置文件的配置

     NTP(Network Time Protocol)服務器是用於局域網服務器時間同步使用的,可以保證局域網所有的服務器與時間服務器的時間保持一致,應用於對時間實時性要求高的、必須統一時間事件。

                                 服務端安裝


一、安裝ntp服務      

yum install ntp ntpdate -y

二、修改ntp.conf配置文件

cp /etc/ntp.conf /etc/ntp.conf.bak   #對ntp.conf文件進行備份

     修改ntp.conf 配置文件如下:

# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).


 #記錄時間差異的文件存放位置
driftfile /var/lib/ntp/drift

# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery  #我們對於默認的客戶端拒絕所有的操作
restrict -6 default kod nomodify notrap nopeer noquery
 
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
#開啓內部遞歸網絡接口lo 即允許本機地址一切的操作
restrict 127.0.0.1 

restrict -6 ::1

#我們允許局域網內所有客戶端連接到這臺服務器同步時間.
#但是拒絕讓他們修改服務器上的時間
# Hosts on local network are less restricted.
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap


service 192.168.75.132 
 # 這句也是手動增加的,指明局域網中作爲NTP服務器的IP

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 互聯網上的三個ntp服務器。server用來指定要同步的時間服務器的ip地址
# 如果局域網環境下,註釋掉下面默認的網絡服務器地址,改成自己的局域網時間服務器IP地址
# 其中後面perfer表示優先,常用的有iburst,burst)
server 0.rhel.pool.ntp.org    
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org 

#sebroadcast 192.168.1.255 autokey    # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. 
 #如果server指定的服務器無法使用,就使用本機時鐘作爲ntp服務時間來源
server  127.127.1.0     # local clock
 
fudge   127.127.1.0 stratum 10

# Enable public key cryptography.
#crypto
#設定包含的配置文件子文件
includefile /etc/ntp/crypto/pw

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
# 當進行祕鑰方式進行認證的時候需要的文件
keys /etc/ntp/keys

# Specify the key identifiers which are trusted.
#trustedkey 4 8 42

# Specify the key identifier to use with the ntpdc utility.
#requestkey 8

# Specify the key identifier to use with the ntpq utility.
#controlkey    
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats

     對配置文件中的restrict進行說明: 

下面詳細對restrict進行介紹,這是一個權限控制的語句。語法如下:

    ask [netmask_IP] [parameter]

如果後面不加任何參數 則表示允許對面ip或ip段的任何操作。

如:restrict 127.0.0.1

常用的參數有:

ignore: 拒絕所有類型的 NTP 聯機;

nomodify: 客戶端不能使用 ntpc 與 ntpq 這兩支程序來修改服務器的時間參數, 但客戶端仍可透過這部主機來進行網絡校時的;

noquery: 客戶端不能夠使用 ntpq, ntpc 等指令來查詢時間服務器,等於不提供 NTP 的網絡校時;

notrap: 不提供 trap 這個遠程事件登錄的功能。

notrust: 拒絕沒有認證的客戶端。

三、啓動NTP服務

service ntpd start

     爲了使服務可以在系統引導的時候自動啓動,執行 

service ntpd start chkconfig ntpd on

                                 客戶端配置


一、客戶端安裝ntp服務

yum install ntp ntpdate -y

二、配置ntp.conf文件 。主要是添加時間服務器的IP地址

配置文件未做特別指出則與服務器端配置一致

tinker panic 0 
(這行意思是設置客戶端與服務器端時間差距,默認最大爲1000,爲0爲不限制。)

server 192.168.245.128 prefer  minpoll 3 maxpoll 3  
(minpoll表示客戶端向服務器端同步的最少時間,單位爲2的次冪最小爲3,秒)

server 192.168.245.130 iburst  minpoll 3 maxpoll 3
(maxpoll表示客戶端向服務器端同步的最長時間,單位爲2的次冪最大爲10,秒)

三、手動同步時間

ntpdate 服務器IP或域名

                                 檢查連接是否成功


一、ntpq -p命令參數:此命令可以查看客戶端和服務器端連接情況

remote:NTP 主機的 IP 或主機名,左邊的符號含義:
    如果有『 * 』代表目前正在作用當中的上層 NTP
    如果是『 + 』代表也有連上線,而且可作爲下一個提供時間更新的候選者。
    如果是『 - 』 表示爲不合格的ntp服務器
refid:上一層 NTP 主機的地址
st:遠程服務器的層級別,0-16,0爲最高層,
when:幾秒鐘前曾經做過時間同步更新的動作;
poll:下一次更新在幾秒鐘之後;
reach:已經向上層 NTP 服務器要求更新的次數
delay:網絡傳輸過程當中延遲的時間,單位爲 10^(-6) 秒
offset:時間補償的結果,單位與 10^(-3) 秒
jitter:Linux 系統時間與 BIOS 硬件時間的差異時間, 單位爲 10^(-6) 秒。

二、date修改系統時間,然後重啓ntpd服務

date命令是顯示或設置系統時間與日期。
語法
    date(選項)(參數)
選項
    -d<字符串>:顯示字符串所指的日期與時間。字符串前後必須加上雙引號;
    -s<字符串>:根據字符串來設置日期與時間。字符串前後必須加上雙引號;
    -u:顯示GMT;
    --help:在線幫助;
    --version:顯示版本信息。
參數
    <+時間日期格式>:指定顯示時使用的日期時間格式。
日期格式字符串列表
    %H 小時,24小時制(00~23)
    %I 小時,12小時制(01~12)
    %k 小時,24小時制(0~23)
    %l 小時,12小時制(1~12)
    %M 分鐘(00~59)
    %p 顯示出AM或PM
    %r 顯示時間,12小時制(hh:mm:ss %p)
    %s 從1970年1月1日00:00:00到目前經歷的秒數
    %S 顯示秒(00~59)
    %T 顯示時間,24小時制(hh:mm:ss)
    %X 顯示時間的格式(%H:%M:%S)
    %Z 顯示時區,日期域(CST)
    %a 星期的簡稱(Sun~Sat)
    %A 星期的全稱(Sunday~Saturday)
    %h,%b 月的簡稱(Jan~Dec)
    %B 月的全稱(January~December)
    %c 日期和時間(Tue Nov 20 14:12:58 2012)
    %d 一個月的第幾天(01~31)
    %x,%D 日期(mm/dd/yy)
    %j 一年的第幾天(001~366)
    %m 月份(01~12)
    %w 一個星期的第幾天(0代表星期天)
    %W 一年的第幾個星期(00~53,星期一爲第一天)
    %y 年的最後兩個數字(1999則是99)
實例
    格式化輸出:
        date +"%Y-%m-%d"
        2009-12-07

                               連接失敗解決辦法


一、有防火牆阻斷了與server之間的通訊,即123端口是否正常開放;

    檢查ntp server的防火牆。可能是server的防火牆屏蔽了upd 123端口。可以用如下命令來關掉iptables服務後再嘗試從ntp客戶端的同步,如果成功,證明是防火牆的問題,需要更改iptables的設置。

service iptables stop

    在vi /etc/sysconfig/iptables添加如下規則:

-A INPUT -m state --state NEW -m udp -p udp --dport 123 -j ACCEPT 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章