NTP在Linux下有兩種時鍾同步方式:
直接同步(也稱跳躍同步)和平滑同步(也稱微調同步)。
直接同步
使用ntpdate命令進行同步,直接進行時間變更。
如果服務器上存在一個12點運行的任務,當前服務器時間是13點,但標准時間時11點,使用此命令可能會造成任務重復執行。因此使用ntpdate同步可能會引發風險,因此該命令也多用於配置時鍾同步服務時第一次同步時間時使用。
注意:如果NTP Client和NTP Server時間偏差太大,可能導致Client的ntpd進程退出。
平滑同步
使用ntpd進行時鍾同步,可以保證一個時間不經歷兩次,它每次同步時間的偏移量不會太陡,是慢慢來的,這正因為這樣,ntpd平滑同步可能耗費的時間比較長。剛開始可能時鍾不同步,多等待時間后慢慢就同步了。
如果配置成平滑同步 ,vi /etc/sysconfig/ntpd,在OPTIONS選項中增加"-x"參數:
SYNC_HWCLOCK=yes
OPTIONS="-g -x"
SYNC_HWCLOCK=yes
OPTIONS="-g -x"
快速同步/平滑同步的設置
Linux中ntpd的-x選項的說明
offset值 | 0~128ms | 128ms~600s | 600s~1000s | 1000s以上 |
有-x參數 | 微調 | 微調(0.5ms/s,600s需14天) | 跳躍 | 退出(加-g參數可忽略) |
無-x參數 | 微調 | 跳躍 | 跳躍 | 退出(加-g參數可忽略) |
標准的時鍾同步服務器
http://www.pool.ntp.org/zone/cn網站包含全球的標准時間同步服務,也包括對中國時間的同步,對應的URL為cn.pool.ntp.org,推薦的ntp配置文件中的格式:
server 3.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 3.asia.pool.ntp.org
NTP相關常用命令:
重啟ntpd服務
#
systemctl restart ntpd
直接進行時間同步變更
ntpdate命令用來設置調整本地日期和時間。它從指定的每個服務器獲得了一些樣本,並應用標准NTP時鍾過濾器和選擇算法來選擇最好的樣本。
* 如果它確定時鍾偏差超過0.5秒,它通過調用settimeofday子例程設置時鍾時間。在引導時間,這是一個首選的方法。* 如果它確定時鍾偏差小於0.5秒,它通過調用adjtime子例程和偏移量來調整時鍾時間。此方法傾向於用犧牲一些穩定性來保持漂移時鍾更加准確。當不是通過運行一個守護程序而是從cron命令有規則的運行ntpdate命令時,每一小時或兩小時執行一次可以保證足夠的走時精度,從而避免調整時鍾。
使用多個NTP Server可以大幅度改善ntpdate命令的可靠性與精度。盡管允許使用單一NTP Server,但還是建議您通過配置至少3~4個NTP Server以獲得更好的性能。
如果一個類似 xntpd 守護程序的 NTP 服務器守護程序正在同一主機上運行,命令將拒絕ntpdate 設置日期。
語法:
ntpdate [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server ...
-a keyid: 使用keyid來認證全部數據包。
-b: 通過調用settimeofday子例程來增加時鍾的時間。
-d: 指定調試方式。判斷ntpdate命令會產生什么結果(不產生實際的結果)。結果再現在屏幕上。這個標志使用無特權的端口。
-e delay: 指定延遲認證處理的時間秒數。
-k keyfile: 當不使用缺省值/etc/ntp.keys文件時,為包含密鑰的文件指定一個不同的名稱。
-o version: 當輪詢它的發出數據包時,指定使用的NTP版本實現。 Version的值可以是1,2,3。缺省值是3。
-p samples: 指定從每個服務器獲取的樣本的數目。 Samples的值:1~8,它的缺省值是4。
-s: 指定日志操作syslog設施的使用,而不是使用標准輸出。 當運行ntpdate命令和cron命令時,它是很有用的。
-t timeout: 指定等待響應的時間。給定timeout的值四舍五入為0.2秒的倍數。缺省值是1秒。
-u: 指定使用無特權的端口發送數據包。 當在一個對特權端口的輸入流量進行阻攔的防火牆后是很有益的, 並希望在防火牆之外和主機同步。防火牆是一個系統或者計算機,它控制從外網對專用網的訪問。
ntpdate [-46bBdqsuv] [-a key#] [-e delay] [-k file] [-p samples] [-o version#] [-t timeo] [-U username] server ...
-a keyid: 使用keyid來認證全部數據包。
-b: 通過調用settimeofday子例程來增加時鍾的時間。
-d: 指定調試方式。判斷ntpdate命令會產生什么結果(不產生實際的結果)。結果再現在屏幕上。這個標志使用無特權的端口。
-e delay: 指定延遲認證處理的時間秒數。
-k keyfile: 當不使用缺省值/etc/ntp.keys文件時,為包含密鑰的文件指定一個不同的名稱。
-o version: 當輪詢它的發出數據包時,指定使用的NTP版本實現。 Version的值可以是1,2,3。缺省值是3。
-p samples: 指定從每個服務器獲取的樣本的數目。 Samples的值:1~8,它的缺省值是4。
-s: 指定日志操作syslog設施的使用,而不是使用標准輸出。 當運行ntpdate命令和cron命令時,它是很有用的。
-t timeout: 指定等待響應的時間。給定timeout的值四舍五入為0.2秒的倍數。缺省值是1秒。
-u: 指定使用無特權的端口發送數據包。 當在一個對特權端口的輸入流量進行阻攔的防火牆后是很有益的, 並希望在防火牆之外和主機同步。防火牆是一個系統或者計算機,它控制從外網對專用網的訪問。
查看網絡中的NTP服務器
#
ntpq -p
[root@abc ~]# ntpq -p
[root@abc ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
ntpsrv01 .XFAC. 16 u - 1024 0 0.000 0.000 0.000
*LOCAL(0) .LOCL. 10 l 46 64 377 0.000 0.000 0.000
[root@abc ~]#
配置了LOCAL作為Server。這個說明當前使用了本地時鍾作為服務端同步。這樣時鍾可能和ntpsrv01的不一致。
[root@abc ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntpsrv01 101.201.72.121 4 u 32 64 17 0.161 0.119 0.093
[root@abc ~]#
未配置LOCAL作為Server。這個說明使用ntpsrv01作為服務端同步。時鍾和ntpsrv01的保持一致。
==============================================================================
ntpsrv01 .XFAC. 16 u - 1024 0 0.000 0.000 0.000
*LOCAL(0) .LOCL. 10 l 46 64 377 0.000 0.000 0.000
[root@abc ~]#
配置了LOCAL作為Server。這個說明當前使用了本地時鍾作為服務端同步。這樣時鍾可能和ntpsrv01的不一致。
[root@abc ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*ntpsrv01 101.201.72.121 4 u 32 64 17 0.161 0.119 0.093
[root@abc ~]#
未配置LOCAL作為Server。這個說明使用ntpsrv01作為服務端同步。時鍾和ntpsrv01的保持一致。
狀態說明:
*表示目前使用的NTP Server,這里選擇的本機;
st:即stratum階層,值越小表示ntp serve的精准度越高;
when:單位秒,幾秒前曾做過時間同步更新的操作;
poll表示,每隔多少毫秒與ntp server同步一次;
reach:已經向上層NTP服務器要求更新的次數;
delay:網絡傳輸過程鍾延遲的時間;
offset:時間補償的結果;
jitter:Linux系統時間與BIOS硬件時間的差異時間
注意:
1)NTP Server端重啟后,Client端需要等5分鍾再與其進行時間同步,否則會提示“no server suitable for synchronization found”錯誤。等待的時間可以通過命令 watch ntpq -p來監控。
2)注意reach這個值,在啟動NTP Server服務后,這個值就從0開始不斷增加,當增加到17的時候,從0到17是5次的變更,每一次是poll的值的秒數,是64秒*5=320秒的時間。如果之后從NTP Client同步NTP Server還失敗的話,用ntpdate –d來查詢詳細錯誤信息,再做判斷。
*表示目前使用的NTP Server,這里選擇的本機;
st:即stratum階層,值越小表示ntp serve的精准度越高;
when:單位秒,幾秒前曾做過時間同步更新的操作;
poll表示,每隔多少毫秒與ntp server同步一次;
reach:已經向上層NTP服務器要求更新的次數;
delay:網絡傳輸過程鍾延遲的時間;
offset:時間補償的結果;
jitter:Linux系統時間與BIOS硬件時間的差異時間
注意:
1)NTP Server端重啟后,Client端需要等5分鍾再與其進行時間同步,否則會提示“no server suitable for synchronization found”錯誤。等待的時間可以通過命令 watch ntpq -p來監控。
2)注意reach這個值,在啟動NTP Server服務后,這個值就從0開始不斷增加,當增加到17的時候,從0到17是5次的變更,每一次是poll的值的秒數,是64秒*5=320秒的時間。如果之后從NTP Client同步NTP Server還失敗的話,用ntpdate –d來查詢詳細錯誤信息,再做判斷。
ntpdate -d排查錯誤信息:
1)Server dropped: no data
檢查ntp的版本(ntpq -c version),如果ntp版本>=4.2,在restrict的定義中使用了notrust的話,會導致以上錯誤。需要刪除notrust。
2)檢查NTP Server的防火牆,是否屏蔽了UDP 123端口。
檢查ntp的版本(ntpq -c version),如果ntp版本>=4.2,在restrict的定義中使用了notrust的話,會導致以上錯誤。需要刪除notrust。
2)檢查NTP Server的防火牆,是否屏蔽了UDP 123端口。
查看同步狀態
如果是內網,一般ntpstat很快就可以同步上
#
ntpstat
ntpstat 命令查看時間同步狀態,這個一般需要5-10分鍾后才能成功連接和同步。所以,服務器啟動后需要稍等下。
[root@abc ~]# ntpstat
synchronised to NTP server (192.168.111.254) at stratum 5
time correct to within 54 ms
polling server every 64 s
[root@abc ~]#
如果ntp客戶端和服務端同步有問題,可使用下面命令查看詳細信息:
#
ntpdate –d serverIP
-d參數只是用於調試,顯示效果而已,不會真實的改變系統的時間
restrict控制相關權限
語法為: restrict IP地址 mask 子網掩碼 參數
其中IP地址也可以是default ,default就是指所有的IP
參數有以下幾個:
ignore :關閉所有的 NTP 聯機服務
nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。
notrust :客戶端除非通過認證,否則該客戶端來源將被視為不信任子網
noquery :不提供客戶端的時間查詢:用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器
notrap :不提供trap遠端登陸:拒絕為匹配的主機提供模式 6 控制消息陷阱服務。陷阱服務是 ntpdq 控制消息協議的子系統,用於遠程事件日志記錄程序。
nopeer :用於阻止主機嘗試與服務器對等,並允許欺詐性服務器控制時鍾
kod : 訪問違規時發送 KoD 包。
restrict -6 表示IPV6地址的權限設置。
局域網內的NTP同步配置
局域網內1台服務器作為NTP Server,2台服務器作為NTP Client與服務器進行時鍾同步:
IP 描述
192.168.111.254 ntpd Server,用於和外部公共ntpd同步標准時間,同時作為內網的Server
192.168.111.129 ntpd Client,用於與ntpd Server同步時間
192.168.111.130 ntpd Client,用於與ntpd Server同步時間
1.檢查ntp包是否已經安裝
#
rpm -q ntp
ntp-4.2.6p5-19.el7.centos.x86_64
如果沒有安裝,則需要先安裝並設置開機自動啟動ntpd服務
#
yum -y install ntp
#
systemctl enable ntpd
#
systemctl start ntpd
2.防火牆配置
由於NTP服務需要使用到UDP端口號123,所以當系統的防火牆(Iptables)啟動的情況下,必須開放UDP端口號123。
3.配置內網ntpd Server:192.168.111.254
1)配置前先使用命令同步時間,本機與外部時間服務器時間差距太大,讓ntpd不能正常同步:
ntpdate -u cn.pool.ntp.org
2)修改/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
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 允許內網其他機器同步時間,如果不添加該約束默認允許所有IP訪問本機同步服務
restrict 192.168.111.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 iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
# 配置和上游標准時間同步
server 101.201.72.121
# 中國國家授時中心
server 133.100.11.8 #日本[福岡大學]
server 3.cn.pool.ntp.org
server 1.asia.pool.ntp.org
server 3.asia.pool.ntp.org
# 配置允許上游時間服務器主動修改本機(內網ntp Server)的時間
restrict
101.201.72.121
nomodify notrap noquery
restrict 133.100.11.8 nomodify notrap noquery
restrict 3.cn.pool.ntp.org nomodify notrap noquery
restrict 1.asia.pool.ntp.org nomodify notrap noquery
restrict 3.asia.pool.ntp.org nomodify notrap noquery
# 確保localhost有足夠權限,使用沒有任何限制關鍵詞的語法。
# 外部時間服務器不可用時,以本地時間作為時間服務。
# 注意:這里不能改,必須使用127.127.1.0,否則會導致無法
#在ntp客戶端運行ntpdate serverIP,出現no server suitable for synchronization found的錯誤。
#在ntp客戶端用ntpdate –d serverIP查看,發現有“Server dropped: strata too high”的錯誤,並且顯示“stratum 16”。而正常情況下stratum這個值得范圍是“0~15”。
#這是因為NTP server還沒有和其自身或者它的server同步上。
#以下的定義是讓NTP Server和其自身保持同步,如果在ntp.conf中定義的server都不可用時,將使用local時間作為ntp服務提供給ntp客戶端。
#下面這個配置,建議NTP Client關閉,建議NTP Server打開。因為Client如果打開,可能導致NTP自動選擇合適的最近的NTP Server、也就有可能選擇了LOCAL作為Server進行同步,而不與遠程Server進行同步。
server 127.127.1.0 # local clock
fudge 127.127.1.0 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.
#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
修改完成后重啟ntpd服務:
systemctl restart ntpd
查看網絡中的NTP服務器,同時顯示客戶端和每個服務器的關系:
#
ntpq -p
查看時間同步狀態
#
ntpstat
synchronised to local net at stratum 11
time correct to within 7948 ms
polling server every 64 s
這個一般需要5-10分鍾后才能成功連接和同步。所以,服務器啟動后需要稍等下。
剛啟動的時候,執行ntpstat,會顯示unsynchronised:
#
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
配置內網ntpd Client:192.168.111.129、130
1)檢查ntp是否安裝,以及是否設置了自啟動,參考ntpd Server的ntp安裝檢查。
2)修改/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
# 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 iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#配置上游時間服務器為本地的ntpd Server服務器
server 192.168.111.254
# 配置允許上游時間服務器主動修改本機的時間
restrict 192.168.111.254 nomodify notrap noquery
#下面這個配置,建議NTP Client關閉,建議NTP Server打開。因為Client如果打開,可能導致NTP自動選擇合適的最近的NTP Server、也就有可能選擇了LOCAL作為Server進行同步,而不與遠程Server進行同步。
#server 127.127.1.0 # local clock
#fudge 127.127.1.0 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.
#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
先和本地ntpd Server同步一下
#
ntpdate -u 192.168.111.254
重啟ntpd服務
#
systemctl restart ntpd
查看狀態
#
ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*192.168.111.254 LOCAL(0) 11 u 24 64 1 1.626 5182468 0.000