1. 程式人生 > >Linux服務管理之NTP服務器配置

Linux服務管理之NTP服務器配置

ntpd ntpserver ntpq

目標環境,3CentOS 6.6,一臺作為NTPD服務與外部公共NTP服務同步時間,同時作為內網的NTPD服務器,其他機器與這臺服務做時間同步。

技術分享

1、NTP時間同步方式選擇

NTP同步方式在linux下一般兩種:使用ntpdate命令直接同步和使用NTPD服務平滑同步。有什麽區別呢,簡單說下,免得時間長了,概念又模糊。

現有一臺設備,系統時間是13:00 , 真實的當前時間(在空中,也許衛星上,這裏假設是在準備同步的上級目標NTP服務器)是:12:30 。如果我們使用ntpdate同步(ntpdate-u 目標NTP服務器IP),操作系統的時間立即更新為12:30,假如,我們的系統有一個定時應用,是在每天12:40運行,那麽實際今天這個的任務已經運行過了(當前時間是13:00嘛),現在被ntpdate修改為12:30,那麽意味作10分鐘後,又會執行一次任務,這就糟糕了,這個任務只能執行一次的嘛!!我想你(其實是我)已經懂了ntpdate時間同步的隱患,當然這個例子有些極端,但的確是有風險的,生產環境我不打算這麽幹,還是穩妥點好。所以解決該問題的辦法就是時間平滑更改,不會讓一個時間點在一天內經歷兩次,這就是NTPD服務方式平滑同步時間,它每次同步時間的偏移量不會太陡,是慢慢來的(問:怎麽來,沒有細究,只曉得一次一點的同步,完全同步好需要較長時間,所以一般開啟NTPD服務同步前先用ntpdate先手動同步一次)。

2、安裝配置

CentOS 6.6已經自帶了NTPD服務,一般默認已安裝,如果沒有安裝,先檢查下,然後配置好yum倉庫,yum方式安裝下就OK,具體如下:

[[email protected] ~]# rpm -qa ntp

ntp-4.2.6p5-1.el6.centos.x86_64 // 這表示已安裝了,如果沒有安裝,這是空白。


如果沒有安裝,我們就安裝一下:

# yum install ntp


按上面的安裝方式在內網每臺服務器上都安裝好NTP軟件包。

完成後,都需要配置NTP服務為自啟動

# chkconfig ntpd on

# chkconfig --list ntpd

ntpd 0:關閉1:關閉2:啟用3:啟用4:啟用5:啟用6:關閉

在配置前,先使用ntpdate手動同步下時間,免得本機與外部時間服務器時間差距太大,讓ntpd不能正常同步。

[[email protected] ~]# ntpdate -u 172.16.0.1

1Jan 19:50:32 ntpdate[2088]: step time server 172.16.0.1 offset 29061.965240sec



配置內網NTP-Server(172.16.31.10)

下面主要是配置內網的NPTD服務器(172.16.31.10),就在/etc/ntp.conf文件,配置好了就OK。先上配置文件再說,紅色部分是我的修改,其他的是默認。

[[email protected] ~]# cat /etc/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.

#ntpd 需要有 loopback 的參與,而默認是拒絕所有,將 loopback 放行就好了

restrict 127.0.0.1 #開放本機的任何訪問

restrict -6 ::1


# 允許內網其他機器同步時間

# Hosts on local network are less restricted.

restrict 172.16.31.0 mask 255.255.0.0 nomodify notrap


# Use public servers from the pool.ntp.org project.

# Please consider joining the pool (http://www.pool.ntp.org/join.html).

# 中國這邊最活躍的時間服務器 : http://www.pool.ntp.org/zone/cn

#server 210.72.145.44 perfer # 中國國家受時中心

#server 202.112.10.36 # 1.cn.pool.ntp.org

#server 59.124.196.83 # 0.asia.pool.ntp.org

但是我這裏上層有時間服務器,我就使用上面的吧!!!

server 172.16.0.1


#broadcast 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


# 允許上層時間服務器主動修改本機時間

#allow update time by the upper server

restrict 172.16.0.1 nomodify notrap noquery


# 外部時間服務器不可用時,以本地時間作為時間服務

# Undisciplined Local Clock. This is a fake driver intended for backup

# # and when no outside source of synchronized time is available.

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 8


# Enable writing of statistics records.

#statistics clockstats cryptostats loopstats peerstats


配置文件修改完成,保存退出,啟動服務。

[[email protected] ~]# service ntpd start

Starting ntpd: [ OK ]


[[email protected] ~]# date

Thu Jan 1 20:00:25 CST 2015

啟動後,一般需要5-10分鐘左右的時候才能與外部時間服務器開始同步時間。可以通過命令查詢NTPD服務情況。

查看服務連接和監聽

[[email protected] ~]# netstat -tunlp |grep ntp

udp 0 0 172.16.31.10:123 0.0.0.0:* 2128/ntpd

udp 0 0 127.0.0.1:123 0.0.0.0:* 2128/ntpd

udp 0 0 0.0.0.0:123 0.0.0.0:* 2128/ntpd

udp 0 0 fe80::a00:27ff:fedb:35e6:123 :::* 2128/ntpd

udp 0 0 ::1:123 :::* 2128/ntpd

udp 0 0 :::123 :::* 2128/ntpd

連接和監聽已正確,采用UDP方式

ntpq -p 查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關系

[[email protected] ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

===================================================================

server.magelinu LOCAL(0) 11 u 5 64 3 0.696 30.409 0.325

*LOCAL(0) .LOCL. 10 l 5 64 3 0.000 0.000 0.000

技術分享


ntpstat 命令查看時間同步狀態,這個一般需要5-10分鐘後才能成功連接和同步。所以,服務器啟動後需要稍等下。

剛啟動的時候,一般是:

[[email protected] ~]# ntpstat

unsynchronised

time server re-starting

polling server every

64 s


連接並同步後:

[[email protected] ~]# ntpstat

synchronised to NTP server

(202.112.10.36) at stratum 3

time correct to within

275 ms

polling server every

256 s


OK,內網的NTPD服務已經配置完成,如果所有正常後,開始配置內網的其他設備與這臺服務器作為時間同步服務。

配置內網NTP-Clients

內網其他設備作為NTP的客戶端配置,相對就比較簡單,而且所有設備的配置都相同。

首先需要安裝NTPD服務,然後配置為自啟動(與NTP-Server完全一樣)。然後找其中一臺配置/etc/ntp.conf文件,配置完成驗證通過後,拷貝到其他客戶端機器,直接使用即可。


# yum install ntp

# chkconfig ntp on


[[email protected] ~]# cat /etc/ntp.conf |grep -v ^#

driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquer

restrict 127.0.0.1

restrict -6 ::1

# 配置時間服務器為本地的時間服務器

server 172.16.31.10

# 允許上層時間服務器主動修改本機時間

restrict 172.16.31.10 nomodify notrap noquery

# 外部時間服務器不可用時,以本地時間作為時間服務

server 127.127.1.0 # local clock

fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

為了簡單,這裏只列出了配置項,註釋全部清理了。

OK,保存退出,請求服務器前,請先使用ntpdate手動同步下時間


[[email protected] ~]# ntpdate -u 172.16.31.10

1Jan 20:09:18 ntpdate[1855]: step time server 172.16.31.10 offset 29062.354752sec



這裏有可能出現同步失敗,一般情況下原因都是本地的NTPD服務器還沒有正常啟動起來,一般需要幾分鐘時間後才能開始同步。

錯誤判斷請參考後面的錯誤處理。

[[email protected] ~]# service ntpd start

Starting ntpd: [ OK ]


啟動後,查看同步情況

[[email protected] ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

===================================================================

172.16.31.10 172.16.0.1 12 u 5 64 1 0.380 -0.124 0.000

*LOCAL(0) .LOCL. 10 l 4 64 1 0.000 0.000 0.000

[[email protected] ~]# ntpstat

synchronised to local net at stratum 11

time correct to within 7948 ms

polling server every 64 s


因為是內網,一般ntpstat很快就可以同步上。

OK,本機客戶端配置完成後,使用SCP拷貝/etc/ntp.conf到其他需要同步的客戶端機器,啟動NTPD服務即可。

其他客戶端機器上操作配置如下:

第三臺機器同樣配置。

[[email protected] ~]# scp /etc/ntp.conf [email protected]:/etc/ntp.conf

[[email protected] ~]# chkconfig ntpd on

[[email protected] ~]# service ntpd start

Starting ntpd: [ OK ]

[[email protected] ~]# ntpq -p

remote refid st t when poll reach delay offset jitter

==============================================================================

172.16.31.10 172.16.0.1 12 u 5 64 1 1.561 2906213 0.000

*LOCAL(0) .LOCL. 10 l 4 64 1 0.000 0.000 0.000

[[email protected] ~]# date

Thu Jan 1 12:09:13 CST 2015

[[email protected] ~]# ntpdate -u 172.16.31.10

1 Jan 20:13:49 ntpdate[1815]: step time server 172.16.31.10 offset 29062.129201 sec

[[email protected] ~]# date

Thu Jan 1 20:13:53 CST 2015

至此,ntp服務器就配置完成!!!


本文出自 “眼眸刻著你的微笑” 博客,請務必保留此出處http://dengaosky.blog.51cto.com/9215128/1964901

Linux服務管理之NTP服務器配置