NTP

一、概述
1.網絡時間協議(NTP)用於將計算機的時間與另一個參考時間源同步。
2.NTP的種類有很多,在RHEL7中默認使用的NTP服務器是chrony.x86_64,但是它並不好用,所以我們先使用命令yum remove chrony.x86_64 -y將它移除,然後再安裝我們要用的軟件包即可(安裝過程請往下看)
3.NTP Server所使用的端口是123

二、NTP服務器配置
[root@localhost ~]# yum install ntp -y 安裝NTP服務
[root@localhost ~]# vi /etc/ntp.conf 修改ntp配置文件,該文件中所有已#開頭的都是註釋信息

[root@localhost ~]# cat /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  本機爲NTP服務器,其他機器在訪問NTP服務器的時候的默認限制,也就是別人在查詢NTP服務器的時候做具有的權限是什麼;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    這裏後邊沒有寫nomodify、notrap 、nopeer、noquery這些權限,就表示本地具有所有的權限(這裏不用修改,保持默認)
restrict ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.157.0 mask 255.255.255.0 nomodify notrap  這條是我們自己加入的,對於192.168.157.0/24網段的客戶端,不允許修改、不允許陷阱操作,但是我們沒有加noquery權限,表示192.168.157.0/24這個網段的客戶端可以查詢

# 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 iburst  註釋掉
#server 1.rhel.pool.ntp.org iburst   註釋掉
#server 2.rhel.pool.ntp.org iburst  註釋掉
#server 3.rhel.pool.ntp.org iburst  註釋掉
server 127.127.1.0 liburst   手動加入
本機作爲一個NTP服務器,那麼本機的時間跟誰來同步呢?我們用server來指定,server用來指定NTP服務器的上層服務器,這裏指定的都是互聯網的地址,如果我想以本機的時間爲準,註釋掉默認的4條,然後加入server 127.127.1.0 liburst即可,127.127.1.0這個地址就是我們主板上晶體芯片的地址;liburst就是加速訪問。

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

# 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

我們對配置文件ntp.conf做的修改如下:
(1)加入restrict 192.168.157.0 mask 255.255.255.0 nomodify notrap配置
(2)註釋如下4行
server 0.rhel.pool.ntp.org iburst
server 1.rhel.pool.ntp.org iburst
server 2.rhel.pool.ntp.org iburst
server 3.rhel.pool.ntp.org iburst
(3)增加server 127.127.1.0 liburst,以本機時間爲準配置
其他的不用管,保存退出即可

[root@localhost ~]# systemctl restart ntpd 重啓NTP服務
[root@localhost ~]# systemctl enable ntpd 設置服務開機自啓
NTP服務器端配置到此結束

三、NTP客戶端配置
1.手動同步時間
[root@localhost ~]# date -s "2018-11-23 20:00:00" 爲了看到效果,我們先將NTP的客戶端改成一個不準的時間
[root@localhost ~]# ntpdate 192.168.157.129 根據NTP服務器192.168.157.129的時間來更新自己的時間
NTP
更新成功了,會告訴你跟新了多少秒,如上圖

2.自動同步時間
手動同步NTP服務器的時間,使用ntpdate命令即可,但我們不能總是手動輸入命令;我們可以配置一個daemon(後臺進程),讓NTP客戶端自動和NTP服務器進行同步。
[root@localhost yum.repos.d]# yum -y install system-config-date
[root@localhost ~]# system-config-date &
NTP
NTP
NTP
NTP

那麼我們怎麼知道,是不是同步了呢?使用如下命令
[root@localhost ~]# chronyc sources -v
NTP
chronyc的配置文件是/etc/目錄下的chrony.conf文件,上邊圖形界面的修改實際上就是修改的就是/etc/chrony.conf配置文件。

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