CentOS7中使用NTP進行時間同步

來源:互聯網
上載者:User

CentOS7中使用NTP進行時間同步
1. NTP時鐘同步方式說明

NTP在linux下有兩種時鐘同步方式,分別為直接同步處理和平滑同步:
直接同步處理
使用ntpdate命令進行同步,直接進行時間變更。如果伺服器上存在一個12點啟動並執行任務,當前伺服器時間是13點,但標準時間時11點,使用此命令可能會造成任務重複執行。因此使用ntpdate同步可能會引發風險,因此該命令也多用於配置時鐘同步服務時第一次同步時間時使用。
平滑同步
使用ntpd進行時鐘同步,可以保證一個時間不經曆兩次,它每次同步時間的位移量不會太陡,是慢慢來的,這正因為這樣,ntpd平滑同步可能耗費的時間比較長。

標準時鐘同步服務
http://www.pool.ntp.org/zone/cn網站包含全球的標準時間同步服務,也包括對中國時間的同步,對應的URL為cn.pool.ntp.org,在其中也描述了ntp設定檔中的建議寫法:
server 1.cn.pool.ntp.org
server 3.asia.pool.ntp.org
server 2.asia.pool.ntp.org

2. 環境情況

準備四台電腦,分別為:

IP 用途
192.168.11.212 ntpd伺服器,用於與外部公用ntpd同步標準時間
172.16.248.129 ntpd用戶端,用於與ntpd同步時間
172.16.248.130 ntpd用戶端,用於與ntpd同步時間
172,16,248.131 ntpd用戶端,用於與ntpd同步時間
3. 檢查服務是否安裝

使用rpm檢查ntp包是否安裝

[root@localhost kevin]# rpm -q ntpntp-4.2.6p5-19.el7.centos.3.x86_64

如果已經安裝則略過此步,否則使用yum進行安裝,並設定系統開機自動啟動並啟動服務

[root@localhost kevin]# yum -y install ntp[root@localhost kevin]# systemctl enable ntpd[root@localhost kevin]# systemctl start ntpd
4. 設定ntp伺服器: 192.168.11.212

配置前先使用命令:ntpdate -u cn.pool.ntp.org,同步伺服器

修改/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 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.1restrict ::1# Hosts on local network are less restricted.#restrict 192.168.1.0 mask 255.255.255.0 nomodify notraprestrict 172.16.248.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 iburstserver 2.cn.pool.ntp.orgserver 1.asia.pool.ntp.orgserver 2.asia.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# 允許上層時間伺服器主動修改本機時間restrict 2.cn.pool.ntp.org nomodify notrap noqueryrestrict 1.asia.pool.ntp.org nomodify notrap noqueryrestrict 2.asia.pool.ntp.org nomodify notrap noqueryserver 127.0.0.1 # local clockfudge 127.0.0.1 stratum 10# Enable public key cryptography.#cryptoincludefile /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# Disable the monitoring facility to prevent amplification attacks using ntpdc# monlist command when default restrict does not include the noquery flag. See# CVE-2013-5211 for more details.# Note: Monitoring will not be disabled with the limited restriction flag.disable monitor

修改完成後重啟ntpd服務systemctl restart ntpd

使用ntpq -p 查看網路中的NTP伺服器,同時顯示用戶端和每個伺服器的關係

使用ntpstat 命令查看時間同步狀態,這個一般需要5-10分鐘後才能成功串連和同步。所以,伺服器啟動後需要稍等下:
剛啟動的時候,一般是:

# ntpstat unsynchronised  time server re-starting   polling server every 64 s

串連並同步後:

# ntpstat synchronised to NTP server (202.112.10.36) at stratum 3   time correct to within 275 ms   polling server every 256 s
5. 設定ntp用戶端: 172.16.248.129|130|131

安裝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 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.1restrict ::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 iburstserver 172.16.248.1restrict 172.16.248.1 nomodify notrap noqueryserver 127.0.0.1fudge 127.0.0.1 stratum 10#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# Enable public key cryptography.#cryptoincludefile /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# Disable the monitoring facility to prevent amplification attacks using ntpdc# monlist command when default restrict does not include the noquery flag. See# CVE-2013-5211 for more details.# Note: Monitoring will not be disabled with the limited restriction flag.disable monitor

重啟ntpd服務

#systemctl restart ntpd

啟動後,查看同步情況

# ntpq -p# ntpstat 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.