局域網ntp服務器配置

  1、不少分佈式軟件須要局域網中各節點時間同步,最粗暴的辦法是在全部節點上都運行ntpdate -u 互聯網服務器地址,這樣人工讓全部節點與外網服務器同步。若是節點重啓,須要在該節點上再次運行該命令。html

  2、此處要在局域網內搭建一個時間服務器,該服務器與外網授時中心同步時間,局域網其餘機器與該服務器保持時間同步。node

1.局域網各機器安裝ntp:yum install ntpcentos

2.各機器啓動ntp服務:service ntpd start服務器

3.各機器ntp服務自動啓動:chkconfig ntpd on網絡

4.時間服務器配置。假定局域網全部機器都是120網段,本時間服務器的ip爲192.168.120.201,機器名爲node01。修改/etc/ntp.conf文件。紅色字體爲相比於原始文件的修改之處。less

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

#添加此處。容許內網其餘機器同步。
restrict 192.168.120.0 mask 255.255.255.0 nomodify notrap


# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

#註釋如下四行
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

#添加此處
server 授時中心ip或域名 perfer  

#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

#添加以下一行。容許上層時間服務器主動修改本機時間 restrict 授時中心ip或域名 nomodify notrap noquery #添加以下兩行。外部時間服務器不可用時,以本地時間做爲時間服務 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

5.局域網客戶端配置。客戶端機器名爲node0二、node03等。分佈式

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

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

#註釋如下四行
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

#添加如下一行
server 192.168.120.201

#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

#添加以下一行。容許上層時間服務器主動修改本機時間
restrict 192.168.120.201 nomodify notrap noquery

# 外部時間服務器不可用時,以本地時間做爲時間服務
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

6.配置完成後,有幾個命令須要使用,來檢查配置是否成功,可在時間服務器和客戶端上都運行這些命令來進行檢查。ide

  • service ntpd status(start|stop|restart),查詢本地ntpd服務開啓狀態。修改配置文件後,需運行restart命令重啓服務,要始終保持該服務運行。
  • netstat -tlunp | grep ntp 查看相關服務網絡端口開啓狀況,該命令判斷本地是否開啓了相關端口用於時間同步,默認本地開放123端口。
[root@node01 etc]# netstat -tlunp | grep ntp
udp        0      0 192.168.120.201:123         0.0.0.0:*                               1381/ntpd           
udp        0      0 127.0.0.1:123               0.0.0.0:*                               1381/ntpd           
udp        0      0 0.0.0.0:123                 0.0.0.0:*                               1381/ntpd           
udp        0      0 fe80::20c:29ff:feea:119b:123 :::*                                    1381/ntpd           
udp        0      0 ::1:123                     :::*                                    1381/ntpd           
udp        0      0 :::123                      :::*                                    1381/ntpd 
  • ntpq -p 判斷ntp.conf文件中的互聯網服務器設置是否生效,注意觀察remote列值是否和配置相符。時間服務器地址在此處顯示時多是倒着寫的,如配置的是223.65.211.42,顯示的多是42.211.65.223。服務器和客戶端顯示分別是:
[root@node01 etc]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*42.211.65.223.s 10.1.0.0         3 u   61   64  177   35.315  -15.290   9.980
 LOCAL(0)        .LOCL.          10 l  136   64  374    0.000    0.000   0.000
[root@node02 ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*node01          223.65.211.42    4 u   16  128  377    0.167   48.143  30.332
 LOCAL(0)        .LOCL.          10 l  916   64    0    0.000    0.000   0.000

  remote爲本機設定的時間服務器地址,refid爲本機設定的時間服務器的上層服務器地址。oop

  • ntpstat 命令查看時間同步狀態,用該命令判斷是否最近與設定的時間服務器進行了同步。如,本配置中服務器和客戶端的顯示分別是:
[root@node01 etc]# ntpstat
synchronised to NTP server (223.65.211.42) at stratum 4 
   time correct to within 114 ms
   polling server every 256 s
[root@node02 ~]# ntpstat
synchronised to NTP server (192.168.120.201) at stratum 5 
   time correct to within 169 ms
   polling server every 256 s

7.特別注意,本地與互聯網的時間同步在全部設置均完成後,須要等待5-10分鐘才能正式生效,此時才能使用上述命令進行查詢檢測配置是否成功。字體

8.確保在授時中心有效性。

相關文章
相關標籤/搜索