1. 程式人生 > >區域網時間同步NTP服務架設

區域網時間同步NTP服務架設

本文主要介紹當局域網內伺服器不能連線外網,但是又必須保持各伺服器時間同步

1.選用區域網內的其中一臺機器做為ntp時間同步源伺服器,其他節點機器從源時間同步伺服器同步時間

例如我選用的機器為:172.16.19.156(機器名稱:centos-1)

2.時間源同步伺服器192.168.1.181安裝NTP服務

yum -y install ntp

安裝完成後配置/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.
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.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.pool.ntp.org




#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


# 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
#server 127.127.1.0

server 127.127.1.0
fudge  127.127.1.0 stratum 10

變更內容為顏色標識位,主要是註釋掉預設的時間同步伺服器,改用本機做為本機的時間同步伺服器

3.客戶端配置,在此也採用ntp進行時間同步

yum -y install  ntp ntpdate

配置/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.
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
#server 0.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.pool.ntp.org



#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


# 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

#新增時間同步源伺服器,在此設定172.16.19.156為時間源同步伺服器
server centos-1 iburst

4.啟動源時間同步伺服器和設定開機啟動

service ntpd start 

chkconfig ntpd on

5.啟動客戶端時間同步服務

service ntpd start 

chkconfig ntpd on

6.在源時間同步伺服器上執行如下命令,檢視是否正常採用本地時間同步


7.在客戶端執行如下命令,檢視是否正常從源時間伺服器獲取時間


8.剛啟動的時候,執行ntpq -p時可能會提示一些錯誤,如果網路正常的話,一般是因為剛開始啟動,還沒有開始時間同步.一般5分鐘後就會正常.如果有其他錯誤就需要具體分析了

ntp時間同步服務還是很容易架設的