NTP服務的搭建

NTP服務的搭建安全

  1. 1.     準備環境服務器

服務器系統爲redhat 6.4 兩塊網卡eth0(鏈接外網),eth1(鏈接內網ip192.168.1.1)。網絡

如今搭建一臺時間同步服務器,服務器和公網的NTP Server 時間同步,而後負責同步局域網的時間app

  1. 2.     安裝NTP軟件包socket

yum install ntp –yide

  1. 3.     編輯配置文件/etc/ntp.conf工具

1NTP服務的主要配置文件是/etc/ntp.conf。其中已經有了一些默認設置。以下圖所示:ui

wKiom1WmYynjNiKYAAE7nfRJNuo946.jpg

含義註釋:spa

  restrict default kod nomodify notrap nopeer noquery   限制其餘計算機查詢修改本機上的NTP服務,其中default表示全部IPrest

  restrict -6 default kod nomodify notrap nopeer noquery  應用於IPV6地址的主機
restrict 127.0.0.1
   開放本機內部接口,用於反饋;以便於在本地對NTP服務進行監控及配置

  restrict 192.168.1.0 mask 255.255.255.0 nomodifynotrap   開放這個網段,不能修改;即容許192.168.1.0/24網段的NTP客戶端均可以用本機的NTP服務器進行網絡校時,但不容許它們修改本機的NTP服務配置
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
這些是網絡中真正服務器,這裏的server參數用來指定上層的NTP服務器,默認指定internet上的時間服務器池中的時間服務器做爲上層NTP服務器

  配置使用網絡時間,主要是經過修改配置文件/etc/ntp.conf來實現的

ntp服務器默認監聽UTP 123端口

   2. 肯定本身的時區:

date命令能夠查看當前系統時間,中國的時區爲CST。使用tzselect命令進行配置(時區選擇,交換式配置方式

或者選擇Shanghai而後創建一個軟鏈接,以下所示:

ln -sf/usr/share/zoneinfo/posix/Asia/Shanghai /etc/localtime #localtime爲系統時區

或者編輯/etc/sysconfig/clock

 ZONE=」Asia/shanghai」

再cp /usr/share/zoneinfo/Asia/shanghai/etc/localtime

(3).利用restrict 來管理權限控制

   修改權限設置的語句格式爲:

restrict  IP地址或域名 mask子網掩碼][選項]

該語名用來設置其餘計算機對本機NTP服務的訪問權限,其中,IP地址或域名參數能夠是default,表示全部計算機

經常使用選項:

ignore:表示禁止全部的NTP請求包進入

nomodify:表示禁止其餘計算機更改本機NTP服務的設置,但能夠經過本NTP服務器進行網絡校時

notrust:表示禁止全部未經過認證的NTP包進入

noquery:表示禁止其餘計算機查詢本機NTP服務的狀態

notrap:不提供trap遠程登陸

nopeer:提供時間服務,但不作爲對等體

kod:向不安全的訪問者發送Kiss-Of-Death報文

若是沒有設置任何選項,就表示該計算機(或網段)沒有任何限制

設置容許對局域網192.168.1.0/24提供時間同步服務

restrict192.168.1.0 mask 255.255.255.0  nomodify  notrap

如圖

wKioL1WmZR2R-AMCAACIbf5FdGE934.jpg

 

配置選項 server(用來和公網時間同步)

做用:指定上層NTP服務器,以及一些鏈接選項

server [host] [key n] [version n][prefex] [mode n] [minpoll n] [maxpoll n] [iburst]

:

server 0.rhel.poll.ntp.org

server 1.rhel.poll.ntp.org

wKioL1WmZSPD1py3AACdOGAnpS0882.jpg

 

啓動ntp服務:service ntpd start

開機啓動服務:chkconfig ntpd on

容許ntp服務的端口123穿過防火牆

iptables-I INPUT -p udp --dport 123 -j ACCEPT

三.配置時間同步客戶端

3、配置時間同步客戶機:

方法1從「系統」à「管理」à菜單中,啓動「日期和時間」管理工具

wKioL1WmZSvBD-TTAADv8Xk_0OA209.jpg

方法2手工執行 ntpdate <ntp server ip  > 來同步

crontab –e

wKiom1WmY1rwhlTlAACFDgcxGY4817.jpg

ntpdate -d  <ntp server  ip >

wKioL1WmZTzybUOAAAIg1mysGYw654.jpg

或者利用crontab來執行

0 21 * * * /usr/sbin/ntpdate 192.168.0.1 ;/sbin/hwclock -w天天晚上9點進行同步

其中hwclock  –whwclocksystohc命令修正系統硬件時間,這樣系統重啓後,才能顯示出正確的時間

附:

4、內網服務器時間同步錯誤
1
noserver suitable for synchronization found
[root@client ~]# ntpdate 192.168.1.8
27 Oct 11:00:02 ntpdate[5299]: no server suitable for synchronization found

解決辦法:安裝ntpserver
修改/etc/ntp.conf,添加以下信息:
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
若是想在互聯網作ntpserver,取消noquery就能夠了,若是隻是給內部提供,加一行ip限制就行:
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap nopeer
而後重啓ntpd服務:
[root@client ~]# service ntpd restart
最後等ntp第一次去他的上級ntpserver同步,能夠查看:
[root@appserver ~]# ntpq -p
remote       refid      st t when poll reach  delay   offset  jitter
======================================================================
114.80.81.13    66.220.9.122     2u   22   64    1   37.802 -292.77   0.001
122.226.192.4   127.67.113.92    2 u  21   64    1   38.103 -298.15   0.001
61.153.197.226 .INIT.          16u    -   64    0   0.000    0.000   0.000
LOCAL(0)       .LOCL.          10 l  21   64    1    0.000   0.000   0.001

不然用客戶端同步,會報錯:
[root@client ~]# ntpdate -d 192.168.1.8
27 Oct 11:00:44 ntpdate[5307]: ntpdate Thu May 13 14:38:23 UTC 2010 (1)
Looking for host 192.168.1.8 and service ntp
host found : 192.168.1.8
transmit(192.168.1.8)
transmit(192.168.1.8)
transmit(192.168.1.8)
transmit(192.168.1.8)
transmit(192.168.1.8)
192.168.1.8: Server dropped: no data
server 192.168.1.8, port 123
stratum 0, precision 0, leap 00, trust 000
refid [192.168.1.8], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time:    00000000.00000000  Thu, Feb  7 203614:28:16.000
originate timestamp: 00000000.00000000  Thu, Feb  7 2036 14:28:16.000
transmit timestamp:  d25347df.73e86849  Thu, Oct 27 2011 11:00:47.452
filter delay:  0.00000  0.00000  0.00000  0.00000 
         0.00000  0.00000 0.00000  0.00000 
filter offset: 0.000000 0.000000 0.000000 0.000000
         0.000000 0.000000 0.0000000.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
27 Oct 11:00:48 ntpdate[5307]: no server suitable for synchronization found

2theNTP socket is in use, exiting
[root@client ~]# ntpdate 192.168.1.8
 2 Dec 15:20:42 ntpdate[2748]: the NTP socket is in use, exiting
 
解決辦法:在客戶端服務器上沒中止ntpd服務
[root@client ~]# /etc/init.d/ntpd stop
關閉 ntpd  [肯定]

如下的定義是讓NTP Server和其自身保持同步,若是在/ntp.conf中定義的server都不可用時,將使用local時間做爲ntp服務提供給ntp客戶端。

server 127.127.1.0

fudge 127.127.1.0  stratum 10

wKioL1WmZUrzLHbAAABkkLuIg90872.jpg

ntp server上從新啓動ntp服務後,ntp server自身或者與其server的同步的須要一個時間段,這個過程多是5分鐘,在這個時間以內在客戶端運行ntpdate命令時會產生no serversuitable for synchronization found的錯誤。

那麼如何知道什麼時候ntp server完成了和自身同步的過程呢?

ntp server服務器端上使用命令:

# watch  ntpq  -p

出現畫面:

wKioL1WmZVGA2oziAACmL_mqgfE922.jpg

注意LOCAL的這個就是與自身同步的ntp server

注意reach這個值,在啓動ntp server服務後,這個值就從0開始不斷增長,當增長到17的時候,從0175次的變動,每一次是poll的值的秒數,是64*5=320秒的時間。

ntp client上使用命令:查看時間同步狀態

wKiom1WmY36RWaYdAACVCDRG-xQ406.jpg

ntp命令

#/usr/sbin/ntpdate -u 192.168.0.1校對時間
#/usr/sbin/ntpdate -q
192.168.0.1查詢不更新
#hwclock --systohc
將機器硬件時鐘同步爲系統時鐘

相關文章
相關標籤/搜索