NTP
全稱是Network Time Protocol
,也就是互聯網時間協議,說到時間,就談談時間的一些概念吧;
- 時區
-
按照常識來說,一天被劃分
24
小時,近似球體的地球是360
度,所以使用經緯度為坐標,將全球劃分為24個時區,每個時區是15
度; - 東八區
-
格林尼治時間為世界標準時間,又因為,東半球(格林尼治以東)的時間比較早,中國的經度在
120E
,是位于第八個時區,這里的中國表示北京或者上海,因為橫跨的時區不止一個; - UTC
-
Coordinated Universal Time
表示協調通用時間,根據原子振蕩周期所計算的物理時鐘,這種計算方式對于時間的計算誤差時很小的; - GMT
-
Greenwich Mean Time
表示的是格林尼治時間,是根據地球的自轉周期計算的標準時間,由于各種不穩定因素的影響,計時不是很理想; - 軟件時鐘
-
表示的是
LInux[這里指提Linux]
操作系統從1970/01/01
開始計算的秒數; - 硬件時鐘
-
主機硬件上面的時鐘,主要是
BIOS
內部時間的記錄了; -
時間校對的幾個步驟
-
1.
server
端啟動daemon
; -
2.
Client
向server
發送校對時間的報文; -
3.
NTP server
返回當前的標準時間給Client
; -
4.
Client
根據返回的時間校對自己當前的系統時間;
-
1.
-
關于
NTP
服務相關的配置文件
/etc/ntp.conf
//表示NTP服務的主要配置文件,也是為一個的配置文件
/usr/share/zoneinfo/
//表示對應的各個時區的文件
/etc/sysconfig/clock
//表示用于設置時鐘和是否使用UTC的配置文件
/etc/localtime
//用于復制/usr/share/zoneinfo/里面對應的,/etc/sysconfig/clock里面設定的時區文件,作為當前系統使用的本地時間配置文件;
- 關于時間的幾個命令
/bin/date
//Linux軟件時鐘的修改和查看
/sbin/hwclock
//用于硬件時鐘的修改和顯示,如果修改了軟件時鐘需要使用這個方式同步到硬件時鐘上面;
/usr/sbin/ntpd
//NTP服務的主要程序,配置文件為/etc/ntp.conf
/usr/sbin/ntpdate
//用于實現Client時間的校正
- 關于/etc/ntp.conf配置文件
利用restrict來實現權限控制
restrict [IP] [mask] [netmask_IP] [patameter]
patameter支持的參數:
igonre:拒絕所有類型的`NTP`連接
nomodify:Client不能夠使用命令ntpc以及ntpdq來修改修改服務器的時間參數,但是可以進行網絡校時;
noquery:表示不提供NTP網絡校時;
notrap:表示不提供遠程事件登陸的功能;
notrust:表示不接受沒有認證的Clent;
如果沒有任何參數,表示不接受任何的限制;
server [IP or hostname] [prefer]
用于設置上層提供NTP服務的服務器;
prefer:表示優先使用
driftfile [可以被ntpd寫入的進程或者文件]
NTP Server計算時間是通過本機的時鐘振蕩來記錄的,這個時間和上層Time Server的時鐘振蕩頻率不一定是一致的,需要將這個振蕩差異記錄下來;
-
NTP
服務的規劃rhel7.2
作為提供時間同步服務的主機172.25.23.250
rhel6.5
作為某個集群中的節點,進行時間同步,這里選擇server21.com
-
需要注意的幾點:
-
1.自己配置的
NTP
是需要設置上層服務器來進行時間同步的,172.25.23.250
主機通過另一塊網卡可以上網,使用的上層主機是cn.ntp.org.cn
; -
2.
NTP
服務器和上層NTP
服務的時間不能夠相差太多; -
3.對于防火墻的設置,一定需要允許
123/UDP
端口; - 4.防火墻還需要放行時間同步請求的網段;
-
1.自己配置的
-
首先安裝
ntp
服務的軟件包,這里使用Yum
配置安裝
[root@my Desktop]# yum install ntp -y
[root@my Desktop]# rpm -q ntp
ntp-4.2.6p5-22.el7.x86_64
-
然后根據上面的提示修改配置文件
/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.1
restrict ::1
restrict cn.ntp.org.cn
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 172.25.23.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 iburst
#server 1.rhel.pool.ntp.org iburst
#server 2.rhel.pool.ntp.org iburst
#server 3.rhel.pool.ntp.org iburst
#
server cn.ntp.org.cn prefer
#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
- 然后啟動或者重新啟動服務
[root@my Desktop]# !systemc
systemctl start ntpd
-
確保端口已經啟動
-
使用命令
ntpstat
查看NTP
服務器的狀態
[root@my Desktop]# ntpstat
synchronised to NTP server (202.108.6.95) at stratum 3
time correct to within 106 ms
polling server every 64 s
-
或者使用
ntpq -p
來查看NTP
服務器時間同步的過程
[root@my Desktop]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*xk-6-95-a8.bta. 10.69.2.34 2 u 35 64 377 28.381 -30.163 17.439
-
解釋上述幾個子段的作用
-
remote :表示
NTP
主機的IP
或者是主機名,最左邊的-
*
:表示目前正在作用當中的上層NTP
服務器; -
+
:表示已經連接成功,并且可以作為下一個提供時間更新的候選者;
-
-
refid
:表示參考的上一層NTP
主機的地址; -
st
:表示的含義是startnum
,一般范圍是從0-->15
; -
when
:表示幾秒前做過時間同步化更新操作; -
poll
:表示下一次更新在幾秒鐘之后; -
reach
:表示已經向上一層服務器要求更新的次數; -
delay
: 表示數據在傳輸過程中延遲的時間; -
offset
:表示時間補償的結果; -
jitter
:Linux
系統時間和BIOS
硬件時間的差異;
-
remote :表示
-
Client
僅僅需要使用簡單的命令就可以完成時間和NTP
的同步操作
[root@server20 ~]# ntpdate 172.25.23.250
10 Mar 15:49:08 ntpdate[13911]: adjust time server 172.25.23.250 offset -0.049370 sec
-
關于
NTP
服務器配置過程中出現額的幾個錯誤,以及解決額的辦法 -
和上層
NTP
服務器不能夠正常通信;
[root@server21 ~]# ntpstat -p
unsynchronised
time server re-starting
polling server every 8 s
-
上面這個錯誤出現的原因是沒有設置上層服務器,導致和上述
NTP
服務器的同步沒有辦法完成,或者是因為上層服務器設置錯誤; -
解決,這里使用的上層服務器是
cn.ntp.org.cn
, 可以使用聯網主機測試和上層時間同步服務器的連接是否正常
[root@my Desktop]# ntpdate cn.ntp.org.cn
10 Mar 15:55:08 ntpdate[8952]: adjust time server 202.112.29.82 offset -0.003693 sec
- 出現的錯誤
[root@server20 ~]# ntpdate 172.25.23.250
9 Mar 10:46:17 ntpdate[1129]: no server suitable for synchronization found
-
對于上述出現的錯誤,使用
-d
選項來查看相信的出錯信息
[root@server20 ~]# ntpdate -d 172.25.23.21
10 Mar 15:56:52 ntpdate[13913]: ntpdate 4.2.6p5@1.2349-o Mon Jul 15 09:22:50 UTC 2013 (1)
Looking for host 172.25.23.21 and service ntp
host found : server21.com
transmit(172.25.23.21)
transmit(172.25.23.21)
transmit(172.25.23.21)
transmit(172.25.23.21)
transmit(172.25.23.21)
172.25.23.21: Server dropped: no data
server 172.25.23.21, port 123
stratum 0, precision 0, leap 00, trust 000
refid [172.25.23.21], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000
originate timestamp: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000
transmit timestamp: de4e0e47.784f2a34 Sat, Mar 10 2018 15:56:55.469
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
10 Mar 15:56:56 ntpdate[13913]: no server suitable for synchronization found
-
上面出現的原因是
server droped
,也就是說服務器丟棄了數據,可能存在的原因-
1.
NTP
服務器啟動不正常,也就是NTP
服務器啟動過程中存在錯誤,使用tail -f /var/log/messages
查看并且確定錯誤的詳細信息;-
上面存在的原因可能有兩個:1.
selinux
設置不正常,使用命令setence 0
; -
還有可能是因為
iptables
沒有添加防火墻例外,也就是對于123/UDP
的請求被拒絕;
-
上面存在的原因可能有兩個:1.
-
1.
- 對于上面的錯誤還有可能遇到一種情況
[root@server20 ~]# ntpdate -d 172.25.23.21
10 Mar 15:56:52 ntpdate[13913]: ntpdate 4.2.6p5@1.2349-o Mon Jul 15 09:22:50 UTC 2013 (1)
Looking for host 172.25.23.21 and service ntp
host found : server21.com
transmit(172.25.23.21)
transmit(172.25.23.21)
transmit(172.25.23.21)
transmit(172.25.23.21)
transmit(172.25.23.21)
172.25.23.21: Server dropped: start toohigh
server 172.25.23.21, port 123
stratum 16, precision 0, leap 00, trust 000
refid [172.25.23.21], delay 0.00000, dispersion 64.00000
transmitted 4, in filter 4
reference time: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000
originate timestamp: 00000000.00000000 Mon, Jan 1 1900 8:05:57.000
transmit timestamp: de4e0e47.784f2a34 Sat, Mar 10 2018 15:56:55.469
filter delay: 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000
filter offset: 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000
delay 0.00000, dispersion 64.00000
offset 0.000000
10 Mar 15:56:56 ntpdate[13913]: no server suitable for synchronization found
-
對于上面正常的
startnum
的范圍是[0--->15]
,出現上面情況的原因是因為NTP
服務器無法和上層NTP
服務器通信,導致Client
的連接不正常; - 可以參考第一種錯誤類型進行配置;
- 這個是正常時間同步過程中的調式信息
[root@server21 ~]# ntpdate -d 172.25.23.250
10 Mar 16:13:18 ntpdate[1508]: ntpdate 4.2.6p5@1.2349-o Mon Jul 15 09:22:50 UTC 2013 (1)
Looking for host 172.25.23.250 and service ntp
host found : 172.25.23.250
transmit(172.25.23.250)
receive(172.25.23.250)
transmit(172.25.23.250)
receive(172.25.23.250)
transmit(172.25.23.250)
receive(172.25.23.250)
transmit(172.25.23.250)
receive(172.25.23.250)
server 172.25.23.250, port 123
stratum 4, precision -24, leap 00, trust 000
refid [172.25.23.250], delay 0.02571, dispersion 0.00000
transmitted 4, in filter 4
reference time: de4e1219.b90d7820 Sat, Mar 10 2018 16:13:13.722
originate timestamp: de4e121e.fa8ae002 Sat, Mar 10 2018 16:13:18.978
transmit timestamp: de4e121e.ea321e17 Sat, Mar 10 2018 16:13:18.914
filter delay: 0.02580 0.02571 0.02571 0.02571
0.00000 0.00000 0.00000 0.00000
filter offset: 0.063770 0.063791 0.063784 0.063784
0.000000 0.000000 0.000000 0.000000
delay 0.02571, dispersion 0.00000
offset 0.063791
10 Mar 16:13:18 ntpdate[1508]: adjust time server 172.25.23.250 offset 0.063791 sec
-
補充關于
date
命令的使用
[root@server21 ~]# date MMDDHHMMYYYY
MM:表示月
DD:表示天
HH:表示小時
MM:表示分鐘
YYYY:表示年
- 上面這個命令主要用來查看(不使用任何選項)和修改時間;
-
hwclock
關于硬件時間
-r :表示用于讀取硬件時間
-w:表示將軟件時間寫入為硬件時間