
NTP(Network Time Protocol,網絡時間協定)是用來使網絡中的各個計算機時間同步的一種協定。它的用途是把計算機的時鐘同步到世界協調時UTC,其精度在區域網路内可達0.1ms,在網際網路上絕大多數的地方其精度可以達到1-50ms。
NTP伺服器就是利用NTP協定提供時間同步服務的。
系統版本(這裡已7.2的系統為例)
1 [root@test ~]# cat /etc/redhat-release
2 CentOS Linux release 7.2.1511 (Core)
[root@test ~]# rpm -qa ntp
ntpdate-4.2.6p5-22.el7.centos.x86_64
如果沒有安裝就yum install ntp -y進行安裝
備份配置檔案
[root@test ~]# cp /etc/ntp.conf{,.bak}
[root@test ~]# ll /etc/ntp.conf*
-rw-r--r--. 1 root root 1778 Jan 11 2017 /etc/ntp.conf
-rw-r--r-- 1 root root 1778 Oct 23 23:10 /etc/ntp.conf.bak
精簡化配置檔案
[root@test ~]# egrep -v "^$|#" /etc/ntp.conf.bak >/etc/ntp.conf
修改配置檔案(紅色字型表示修改的内容)
1 # For more information about this file, see the man pages
2 # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
3
4 driftfile /var/lib/ntp/drift
5
6 # Permit time synchronization with our time source, but do not
7 # permit the source to query or modify the service on this system.
8 #restrict default kod nomodify notrap nopeer noquery #控制nto伺服器的權限,預設為最低,注釋掉
9 restrict default nomodify
10
11 # Permit all access over the loopback interface. This could
12 # be tightened as well, but to do so would effect some of
13 # the administrative functions.
14 restrict 127.0.0.1
15 restrict -6 ::116
17 # Hosts on local network are less restricted.
18 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
19 # 允許内網其他機器同步時間
20 restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap
21
22 # Use public servers from the pool.ntp.org project.
23 # Please consider joining the pool (http://www.pool.ntp.org/join.html).24 #server 0.centos.pool.ntp.org iburst
25 #server 1.centos.pool.ntp.org iburst
26 #server 2.centos.pool.ntp.org iburst
27 #server 3.centos.pool.ntp.org iburst
28
29 # 定義使用的上遊 ntp伺服器,将原來的注釋
30 server time1.aliyun.com
31 server ntp1.aliyun.com
32
33 #broadcast 192.168.1.255 autokey # broadcast server
34 #broadcastclient # broadcast client
35 #broadcast 224.0.1.1 autokey # multicast server
36 #multicastclient 224.0.1.1 # multicast client
37 #manycastserver 239.255.254.254 # manycast server
38 #manycastclient 239.255.254.254 autokey # manycast client
39
40 # 允許上層時間伺服器主動修改本機時間
41 restrict time1.aliyun.com nomodify notrap noquery
42 restrict ntp1.aliyun.com nomodify notrap noquery
43
44 # 外部時間伺服器不可用時,以本地時間作為時間服務
45 server 127.127.1.0
46 fudge 127.127.1.0 stratum 1047
48 # Enable public key cryptography.
49 #crypto
50
51 includefile /etc/ntp/crypto/pw
52
53 # Key file containing the keys and key identifiers used when operating
54 # with symmetric key cryptography.
55 keys /etc/ntp/keys
56
57 # Specify the key identifiers which are trusted.
58 #trustedkey 4 8 4259
60 # Specify the key identifier to use with the ntpdc utility.
61 #requestkey 862
63 # Specify the key identifier to use with the ntpq utility.
64 #controlkey 865
66 # Enable writing of statistics records.
67 #statistics clockstats cryptostats loopstats peerstats
注意:如果有同步時間的定時任務要将其登出,否則會沖突
[root@test ~]# systemctl start ntpd
顯示節點清單
[root@test ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
time5.aliyun.co 10.137.38.86 2 u 28 64 3 5.092 6241215 185.623
132.163.96.4 .INIT. 16 u - 64 0 0.000 0.000 0.000
[root@m01 ~]# ntpstat
unsynchronised #出現一下情況,需要等待一定時間即可同步
time server re-starting
polling server every 8 s
用戶端推薦使用ntpdate同步時間,如開啟ntp服務不安全
CentOS7預設沒有這個指令,需要安裝:
yum install ntpdate -y
同步内網時間伺服器:
ntpdate 172.16.1.61
說明:客戶機要等幾分鐘再與新啟動的ntp伺服器進行時間同步,否則會提示‘’no server suitable for synchronization found錯誤‘’
配置chrony時間伺服器:
1.停止ntpd服務,避免占用123端口
systemctl stop ntpd.service
2.安裝chrony服務
yum -y install chrony
3.備份配置檔案
cp /etc/chrony.conf{,.ori}
4.編輯配置檔案
vim /etc/chrony.conf
#注釋3-6行,在下面加入新的上遊伺服器
2# Please consider joining the pool (http://www.pool.ntp.org/join.html).
3#server 0.centos.pool.ntp.org iburst
4#server 1.centos.pool.ntp.org iburst
5#server 2.centos.pool.ntp.org iburst
6#server 3.centos.pool.ntp.org iburst
server ntp2.aliyun.com
#注釋25-26行,在下面加入允許同步的網段
# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 172.16.1.0/24
5.對比檢查一下配置檔案
diff /etc/chrony.conf{,.ori}
6.啟動chrony服務
systemctl start chronyd
小夥伴們可以關注我的微信公衆号:linux運維菜鳥之旅
關注“中國電信天津網廳”公衆号,首次綁定可免費領2G流量,為你的學習提供流量!