配置ntp

 

1.鳥哥的Linux私房菜:第十五章、時間服務器: NTP 服務器(強烈建議看完)

2.http://www.crsay.com/wiki/wiki.php/server/centos/ntp-set

3.常見錯誤集錦:http://www.blogjava.net/spray/archive/2008/07/10/213964.html

2架構

Stratum 1/2 Internet NTP Server

Stratum 3 公司防火牆(防火牆帶有ntp功能,並且能夠連接外網)

Stratum 4 自己搭建ntp server(跟公司防火牆的ntp server對時,這樣的好處有兩個,一是降低了防火牆的壓力,二是降低耦合性,防止因爲防火牆的變更而帶來更多的改動)

Stratum 5 公司內的各臺機器,相當於是一個ntp client。

3安裝

3.1在線安裝

ntp在線安裝的方式很簡單,只需要執行以下命令即可幫你安裝好ntp以及所有的依賴包

sudo apt-get install ntp

3.2離線安裝

如果要離線安裝,那麼就需要下載ntp安裝包和依賴包。我們可以在一個有線環境下運行上面的在線安裝,然後到/var/cache/apt/archives這個目錄下拷貝完整的ntp安裝包和依賴包。當前我已經將這些安裝包放到網盤上面去了:http://pan.baidu.com/s/1dDzg95f

  對於下載的deb包,我們使用dpkg安裝包來安裝,先安裝依賴包,最後安裝程序包。具體安裝過程如下:

複製代碼

root@BJCGNMON01:/var/cache/apt/archives# dpkg  -i  libopts25_1%3a5.12-0.1ubuntu1_amd64.deb 
Selecting previously unselected package libopts25.
(Reading database ... 51276 files and directories currently installed.)
Unpacking libopts25 (from libopts25_1%3a5.12-0.1ubuntu1_amd64.deb) ...
Setting up libopts25 (1:5.12-0.1ubuntu1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

root@BJCGNMON01:/var/cache/apt/archives# dpkg  -i  ntp_1%3a4.2.6.p3+dfsg-1ubuntu3.1_amd64.deb 
(Reading database ... 51324 files and directories currently installed.)
Preparing to replace ntp 1:4.2.6.p3+dfsg-1ubuntu3.1 (using ntp_1%3a4.2.6.p3+dfsg-1ubuntu3.1_amd64.deb) ...
Unpacking replacement ntp ...
Setting up ntp (1:4.2.6.p3+dfsg-1ubuntu3.1) ...
 * Starting NTP server ntpd
   ...done.
Processing triggers for ureadahead ...
ureadahead will be reprofiled on next reboot
Processing triggers for man-db ...

複製代碼

  當然還有更加簡單的方法,將下載的deb包拷貝到/var/cache/apt/archives目錄下,然後在執行一下命令同樣可以安裝。

sudo apt-get install ntp

安裝完畢以後我們可以查看服務是否啓動,執行以下命令:

複製代碼

enadmin@ubuntu-server:~/ftp/ntp_ubuntu$ service --status-all
 [ ? ]  acpid
 [ ? ]  apache
 [ + ]  apache2
 [ - ]  apparmor
 [ ? ]  apport
 [ ? ]  atd
 [ - ]  bootlogd
 [ ? ]  console-setup
 [ ? ]  cron
 [ ? ]  dbus
 [ ? ]  dmesg
 [ ? ]  dns-clean
 [ ? ]  friendly-recovery
 [ - ]  grub-common
 [ ? ]  hostname
 [ ? ]  hwclock
 [ ? ]  hwclock-save
 [ ? ]  irqbalance
 [ ? ]  killprocs
 [ ? ]  module-init-tools
 [ ? ]  mysql
 [ + ]  nagios
 [ + ]  nagios-bak
 [ ? ]  network-interface
 [ ? ]  network-interface-container
 [ ? ]  network-interface-security
 [ ? ]  networking
 [ + ]  ntp
 [ ? ]  ondemand

複製代碼

可以看到ntp服務已經啓動([+]表示已經啓動。)

4配置文件/etc/ntp.conf解析

sudo vim /etc/ntp.conf

這個文件是配置ntp最重要的一個文件,也是ntp配置過程中的唯一難點,之前一直不知道這個配置問價當中配置選項的含義,所以一直無從下手。

4.1配置上層server

利用 server 關鍵字設定上層 NTP 服務器,上層 NTP 服務器的設定方式爲:

server [IP or hostname] [prefer]

在 server 後端可以接 IP 或主機名,個人比較喜歡使用 IP 來設定, perfer 表示『優先使用』的服務器。

4.2利用 restrict 來管理權限控制

在 ntp.conf 檔案內可以利用『 restrict 』來控管權限,這個參數的設定方式爲:

restrict [IP] mask [netmask_IP] [parameter]

其中 parameter 的參數主要有底下這些:

  • ignore: 拒絕所有類型的 NTP 聯機;
  • nomodify: 客戶端不能使用 ntpc 與 ntpq 這兩支程序來修改服務器的時間參數, 但客戶端仍可透過這部主機來進行網絡校時的;
  • noquery: 客戶端不能夠使用 ntpq, ntpc 等指令來查詢時間服務器,等於不提供 NTP 的網絡校時;
  • notrap: 不提供 trap 這個遠程事件登錄 (remote event logging) 的功能。
  • notrust: 拒絕沒有認證的客戶端。

如果你沒有在 parameter 的地方加上任何參數的話,這表示『該 IP 或網段不受任何限制』。一般來說,我們可以先關閉 NTP 的權限,然後再一個一個的啓用允許登入的網段。

ntp server配置

ntp server的需求:

  1. 我的上層 NTP 服務器共只有防火牆的ntp server:10.138.16.251。
  2. 不對Internet 提供服務,僅允許來自內部網域10.138.0.0/255.255.0.0的查詢而已;
  3. 偵測一些 BIOS 時鐘與 Linux 系統時間的差異並寫入 /var/lib/ntp/drift 檔案當中。

複製代碼

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift #草稿文件

#日誌文件
statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

#上層ntp server
server 10.138.16.251 prefer 
#讓NTP Server和其自身保持同步,如果在/etc/ntp.conf中定義的server都不可用時,將使用local時間作爲ntp服務提供給ntp客戶端
server 127.127.1.0
fudge 127.127.1.0 stratum 5

#不允許來自公網上ipv4和ipv6客戶端的訪問
restrict -4 default kod notrap nomodify nopeer noquery 
restrict -6 default kod notrap nomodify nopeer noquery

#運行上層ntp server的所有權限
restrict 10.138.16.251

#允許這個網段的對時請求
restrict 10.138.0.0 mask 255.255.0.0 nomodify 

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

複製代碼

使用 ntpd漸進式更新時間

ntp又是服務器,又是客戶端,只需要通過配置即可。上面我們將10.138.16.232配置成了ntp服務器,下面這臺服務器我們以10.138.16.232作爲ntp服務器配置client。

複製代碼

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable

# Specify one or more NTP servers.

# Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board
# on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for
# more information.
#server 0.ubuntu.pool.ntp.org
#server 1.ubuntu.pool.ntp.org
#server 2.ubuntu.pool.ntp.org
#server 3.ubuntu.pool.ntp.org

# Use Ubuntu's ntp server as a fallback.
#server ntp.ubuntu.com

server 10.138.16.251 prefer
server 127.127.1.0
fudge 127.127.1.0 stratum 10



# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

restrict 10.138.16.251
restrict 10.138.0.0 mask 255.255.0.0 nomodify


# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient

複製代碼

使用ntpdate更新時間

 在客戶端只需要運行一下命令接口進行對時操作

sudo ntpdate 10.138.16.232

ntpdate命令是要在ntp進程退出的情況下執行,如果執行上述命令出錯,那麼請先關閉ntp進程,具體操作日下所示:

複製代碼

root@BJCGNMON01:/etc/init.d# ntpdate 10.138.16.232
27 Feb 14:59:32 ntpdate[18546]: the NTP socket is in use, exiting
root@BJCGNMON01:/etc/init.d# service ntp stop
 * Stopping NTP server ntpd
   ...done.
root@BJCGNMON01:/etc/init.d# ntpdate 10.138.16.232
27 Feb 14:58:13 ntpdate[18557]: step time server 10.138.16.232 offset -98.402560 sec

複製代碼

如果是windows操作系統,按照參考文獻1進行修改。

 

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