NTP
全稱(chēng)是Network Time Protocol
,也就是互聯(lián)網(wǎng)時(shí)間協(xié)議,說(shuō)到時(shí)間,就談?wù)剷r(shí)間的一些概念吧;
- 時(shí)區(qū)
-
按照常識(shí)來(lái)說(shuō),一天被劃分
24
小時(shí),近似球體的地球是360
度,所以使用經(jīng)緯度為坐標(biāo),將全球劃分為24個(gè)時(shí)區(qū),每個(gè)時(shí)區(qū)是15
度; - 東八區(qū)
-
格林尼治時(shí)間為世界標(biāo)準(zhǔn)時(shí)間,又因?yàn)?東半球(格林尼治以東)的時(shí)間比較早,中國(guó)的經(jīng)度在
120E
,是位于第八個(gè)時(shí)區(qū),這里的中國(guó)表示北京或者上海,因?yàn)闄M跨的時(shí)區(qū)不止一個(gè); - UTC
-
Coordinated Universal Time
表示協(xié)調(diào)通用時(shí)間,根據(jù)原子振蕩周期所計(jì)算的物理時(shí)鐘,這種計(jì)算方式對(duì)于時(shí)間的計(jì)算誤差時(shí)很小的; - GMT
-
Greenwich Mean Time
表示的是格林尼治時(shí)間,是根據(jù)地球的自轉(zhuǎn)周期計(jì)算的標(biāo)準(zhǔn)時(shí)間,由于各種不穩(wěn)定因素的影響,計(jì)時(shí)不是很理想; - 軟件時(shí)鐘
-
表示的是
LInux[這里指提Linux]
操作系統(tǒng)從1970/01/01
開(kāi)始計(jì)算的秒數(shù); - 硬件時(shí)鐘
-
主機(jī)硬件上面的時(shí)鐘,主要是
BIOS
內(nèi)部時(shí)間的記錄了; -
時(shí)間校對(duì)的幾個(gè)步驟
-
1.
server
端啟動(dòng)daemon
; -
2.
Client
向server
發(fā)送校對(duì)時(shí)間的報(bào)文; -
3.
NTP server
返回當(dāng)前的標(biāo)準(zhǔn)時(shí)間給Client
; -
4.
Client
根據(jù)返回的時(shí)間校對(duì)自己當(dāng)前的系統(tǒng)時(shí)間;
-
1.
-
關(guān)于
NTP
服務(wù)相關(guān)的配置文件
/etc/ntp.conf
//表示NTP服務(wù)的主要配置文件,也是為一個(gè)的配置文件
/usr/share/zoneinfo/
//表示對(duì)應(yīng)的各個(gè)時(shí)區(qū)的文件
/etc/sysconfig/clock
//表示用于設(shè)置時(shí)鐘和是否使用UTC的配置文件
/etc/localtime
//用于復(fù)制/usr/share/zoneinfo/里面對(duì)應(yīng)的,/etc/sysconfig/clock里面設(shè)定的時(shí)區(qū)文件,作為當(dāng)前系統(tǒng)使用的本地時(shí)間配置文件;
- 關(guān)于時(shí)間的幾個(gè)命令
/bin/date
//Linux軟件時(shí)鐘的修改和查看
/sbin/hwclock
//用于硬件時(shí)鐘的修改和顯示,如果修改了軟件時(shí)鐘需要使用這個(gè)方式同步到硬件時(shí)鐘上面;
/usr/sbin/ntpd
//NTP服務(wù)的主要程序,配置文件為/etc/ntp.conf
/usr/sbin/ntpdate
//用于實(shí)現(xiàn)Client時(shí)間的校正
- 關(guān)于/etc/ntp.conf配置文件
利用restrict來(lái)實(shí)現(xiàn)權(quán)限控制
restrict [IP] [mask] [netmask_IP] [patameter]
patameter支持的參數(shù):
igonre:拒絕所有類(lèi)型的`NTP`連接
nomodify:Client不能夠使用命令ntpc以及ntpdq來(lái)修改修改服務(wù)器的時(shí)間參數(shù),但是可以進(jìn)行網(wǎng)絡(luò)校時(shí);
noquery:表示不提供NTP網(wǎng)絡(luò)校時(shí);
notrap:表示不提供遠(yuǎn)程事件登陸的功能;
notrust:表示不接受沒(méi)有認(rèn)證的Clent;
如果沒(méi)有任何參數(shù),表示不接受任何的限制;
server [IP or hostname] [prefer]
用于設(shè)置上層提供NTP服務(wù)的服務(wù)器;
prefer:表示優(yōu)先使用
driftfile [可以被ntpd寫(xiě)入的進(jìn)程或者文件]
NTP Server計(jì)算時(shí)間是通過(guò)本機(jī)的時(shí)鐘振蕩來(lái)記錄的,這個(gè)時(shí)間和上層Time Server的時(shí)鐘振蕩頻率不一定是一致的,需要將這個(gè)振蕩差異記錄下來(lái);
-
NTP
服務(wù)的規(guī)劃rhel7.2
作為提供時(shí)間同步服務(wù)的主機(jī)172.25.23.250
rhel6.5
作為某個(gè)集群中的節(jié)點(diǎn),進(jìn)行時(shí)間同步,這里選擇server21.com
-
需要注意的幾點(diǎn):
-
1.自己配置的
NTP
是需要設(shè)置上層服務(wù)器來(lái)進(jìn)行時(shí)間同步的,172.25.23.250
主機(jī)通過(guò)另一塊網(wǎng)卡可以上網(wǎng),使用的上層主機(jī)是cn.ntp.org.cn
; -
2.
NTP
服務(wù)器和上層NTP
服務(wù)的時(shí)間不能夠相差太多; -
3.對(duì)于防火墻的設(shè)置,一定需要允許
123/UDP
端口; - 4.防火墻還需要放行時(shí)間同步請(qǐng)求的網(wǎng)段;
-
1.自己配置的
-
首先安裝
ntp
服務(wù)的軟件包,這里使用Yum
配置安裝
[root@my Desktop]# yum install ntp -y
[root@my Desktop]# rpm -q ntp
ntp-4.2.6p5-22.el7.x86_64
-
然后根據(jù)上面的提示修改配置文件
/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
- 然后啟動(dòng)或者重新啟動(dòng)服務(wù)
[root@my Desktop]# !systemc
systemctl start ntpd
-
確保端口已經(jīng)啟動(dòng)
-
使用命令
ntpstat
查看NTP
服務(wù)器的狀態(tài)
[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
來(lái)查看NTP
服務(wù)器時(shí)間同步的過(guò)程
[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
-
解釋上述幾個(gè)子段的作用
-
remote :表示
NTP
主機(jī)的IP
或者是主機(jī)名,最左邊的-
*
:表示目前正在作用當(dāng)中的上層NTP
服務(wù)器; -
+
:表示已經(jīng)連接成功,并且可以作為下一個(gè)提供時(shí)間更新的候選者;
-
-
refid
:表示參考的上一層NTP
主機(jī)的地址; -
st
:表示的含義是startnum
,一般范圍是從0-->15
; -
when
:表示幾秒前做過(guò)時(shí)間同步化更新操作; -
poll
:表示下一次更新在幾秒鐘之后; -
reach
:表示已經(jīng)向上一層服務(wù)器要求更新的次數(shù); -
delay
: 表示數(shù)據(jù)在傳輸過(guò)程中延遲的時(shí)間; -
offset
:表示時(shí)間補(bǔ)償?shù)慕Y(jié)果; -
jitter
:Linux
系統(tǒng)時(shí)間和BIOS
硬件時(shí)間的差異;
-
remote :表示
-
Client
僅僅需要使用簡(jiǎn)單的命令就可以完成時(shí)間和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
-
關(guān)于
NTP
服務(wù)器配置過(guò)程中出現(xiàn)額的幾個(gè)錯(cuò)誤,以及解決額的辦法 -
和上層
NTP
服務(wù)器不能夠正常通信;
[root@server21 ~]# ntpstat -p
unsynchronised
time server re-starting
polling server every 8 s
-
上面這個(gè)錯(cuò)誤出現(xiàn)的原因是沒(méi)有設(shè)置上層服務(wù)器,導(dǎo)致和上述
NTP
服務(wù)器的同步?jīng)]有辦法完成,或者是因?yàn)樯蠈臃?wù)器設(shè)置錯(cuò)誤; -
解決,這里使用的上層服務(wù)器是
cn.ntp.org.cn
, 可以使用聯(lián)網(wǎng)主機(jī)測(cè)試和上層時(shí)間同步服務(wù)器的連接是否正常
[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
- 出現(xiàn)的錯(cuò)誤
[root@server20 ~]# ntpdate 172.25.23.250
9 Mar 10:46:17 ntpdate[1129]: no server suitable for synchronization found
-
對(duì)于上述出現(xiàn)的錯(cuò)誤,使用
-d
選項(xiàng)來(lái)查看相信的出錯(cuò)信息
[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
-
上面出現(xiàn)的原因是
server droped
,也就是說(shuō)服務(wù)器丟棄了數(shù)據(jù),可能存在的原因-
1.
NTP
服務(wù)器啟動(dòng)不正常,也就是NTP
服務(wù)器啟動(dòng)過(guò)程中存在錯(cuò)誤,使用tail -f /var/log/messages
查看并且確定錯(cuò)誤的詳細(xì)信息;-
上面存在的原因可能有兩個(gè):1.
selinux
設(shè)置不正常,使用命令setence 0
; -
還有可能是因?yàn)?code>iptables沒(méi)有添加防火墻例外,也就是對(duì)于
123/UDP
的請(qǐng)求被拒絕;
-
上面存在的原因可能有兩個(gè):1.
-
1.
- 對(duì)于上面的錯(cuò)誤還有可能遇到一種情況
[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
-
對(duì)于上面正常的
startnum
的范圍是[0--->15]
,出現(xiàn)上面情況的原因是因?yàn)?code>NTP服務(wù)器無(wú)法和上層NTP
服務(wù)器通信,導(dǎo)致Client
的連接不正常; - 可以參考第一種錯(cuò)誤類(lèi)型進(jìn)行配置;
- 這個(gè)是正常時(shí)間同步過(guò)程中的調(diào)式信息
[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
-
補(bǔ)充關(guān)于
date
命令的使用
[root@server21 ~]# date MMDDHHMMYYYY
MM:表示月
DD:表示天
HH:表示小時(shí)
MM:表示分鐘
YYYY:表示年
- 上面這個(gè)命令主要用來(lái)查看(不使用任何選項(xiàng))和修改時(shí)間;
-
hwclock
關(guān)于硬件時(shí)間
-r :表示用于讀取硬件時(shí)間
-w:表示將軟件時(shí)間寫(xiě)入為硬件時(shí)間