[NTP] LinuxNTP服務器搭建部署與NTP時間同步配置

本篇blog主要介紹了[NTP] Linux 系統時區設置,NTP時間服務器部署及ntp.conf配置參數詳解,並通過案例介紹了系統時區配置及時間同步的多種方法,不同Client客戶端的相關配置,最後補充了ntp及ntpdate的相關聯繫及區別。


Linux系統時區設置


一、什麼是NTP

NTP是用來使計算機時間同步化的一種協議,全稱是Network Time Protocol。它可以在大規模的設備範圍內同步矯正時間到幾ms級別的精度,在網絡穩定的局域網內,精度甚至可以達到微秒級別。 今天NTP協議已經發展到V4版本,V1版本在1988年6月推出,主要RFC爲RFC-1059;今天我們大規模使用的V4版本的NTP,還沒有正式的RFC出版,但是簡單的協議內容,在RFC2030裏已有描述。

二、如何查看系統時區

1、使用date命令查看系統時區

[root@vm ~]# date   
Tue Oct 16 16:37:24 CST 2018     # CST代表了中國標準時間(+8區)

[root@vm ~]# date -R
Tue, 16 Oct 2018 16:37:27 +0800    # 內容詳細的顯示了+0800時區,即東八區中國標準時間

2、查看clock系統時區

[root@vm ~]# more /etc/sysconfig/clock     # 查看/etc/localtime 定義的系統時區
# The time zone of the system is defined by the contents of /etc/localtime.

ZONE="Asia/Shanghai"

3、通過zdump命令查看相應時區時間

[root@vm ~]# zdump Africa/Bissau
Africa/Bissau  Tue Oct 16 09:23:59 2018 GMT

[root@vm ~]# zdump Asia/Taiwan
Asia/Taiwan  Tue Oct 16 09:25:46 2018 Asia

三、如何設置系統時區

1、第一種方法:使用tzselect命令設置系統時區

使用tzselect命令獲取TZ值

[root@vm ~]# tzselect    # 使用tzselect命令,獲取TZ值
Please identify a location so that time zone rules can be set correctly.
Please select a continent or ocean.
 1) Africa
 2) Americas
 3) Antarctica
 4) Arctic Ocean
 5) Asia
 6) Atlantic Ocean
 7) Australia
 8) Europe
 9) Indian Ocean
10) Pacific Ocean
11) none - I want to specify the time zone using the Posix TZ format.
#? 5    # 選擇Asia
Please select a country.
 1) Afghanistan		  18) Israel		    35) Palestine
 2) Armenia		  19) Japan		    36) Philippines
 3) Azerbaijan		  20) Jordan		    37) Qatar
 4) Bahrain		  21) Kazakhstan	    38) Russia
 5) Bangladesh		  22) Korea (North)	    39) Saudi Arabia
 6) Bhutan		  23) Korea (South)	    40) Singapore
 7) Brunei		  24) Kuwait		    41) Sri Lanka
 8) Cambodia		  25) Kyrgyzstan	    42) Syria
 9) China		  26) Laos		    43) Taiwan
10) Cyprus		  27) Lebanon		    44) Tajikistan
11) East Timor		  28) Macau		    45) Thailand
12) Georgia		  29) Malaysia		    46) Turkmenistan
13) Hong Kong		  30) Mongolia		    47) United Arab Emirates
14) India		  31) Myanmar (Burma)	    48) Uzbekistan
15) Indonesia		  32) Nepal		    49) Vietnam
16) Iran		  33) Oman		    50) Yemen
17) Iraq		  34) Pakistan
#? 9    # 選擇China
Please select one of the following time zone regions.
1) Beijing Time
2) Xinjiang Time
#? 1    # 選擇Beijing Time

The following information has been given:

	China
	Beijing Time

Therefore TZ='Asia/Shanghai' will be used.
Local time is now:	Tue Oct 16 16:46:58 CST 2018.
Universal Time is now:	Tue Oct 16 08:46:58 UTC 2018.
Is the above information OK?
1) Yes
2) No
#? 1    # 確認TZ值TZ='Asia/Shanghai'

You can make this change permanent for yourself by appending the line
	TZ='Asia/Shanghai'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai

在全局配置中添加TZ變量

tzselect命令只告訴你選擇的時區的寫法,並不會自動生效。需在用戶變量/.profile、/~bash_profile或者全局變量/etc/profile中設置並導入獲取的TZ變量值。 

[root@vm ~]# vi /etc/profile    # 添加TZ變量環境變量 ,把獲取的TZ值加入到全局變量中
TZ='Asia/Shanghai'; export TZ
"/etc/profile" 79L, 1871C written

[root@vm ~]# source /etc/profile    # 使能TZ變量生效

[root@vm ~]# date -R    # 驗證時區
Tue, 16 Oct 2018 17:08:05 +0800

[root@vm ~]# more /etc/sysconfig/clock 
# The time zone of the system is defined by the contents of /etc/localtime.
# This file is only for evaluation by system-config-date, do not rely on its
# contents elsewhere.
ZONE="Asia/Shanghai"

2、第二種方法:複製相應的時區文件,替換系統默認時區文件或者創建鏈接文件,設置系統時區

複製時區文件,替換系統時區默認文件

[root@vm ~]# ls /usr/share/zoneinfo/    # 在/usr/share/zoneinfo/ 文件下有很多時區文件
Africa      Asia       Canada   Cuba   EST      GB       GMT-0      HST      iso3166.tab  Kwajalein    Mexico   NZ       Portugal    PST8PDT  Singapore  Universal  W-SU
America     Atlantic   CET      EET    EST5EDT  GB-Eire  GMT+0      Iceland  Israel       leapseconds  MST      NZ-CHAT  posix       right    Turkey     US         zone1970.tab
Antarctica  Australia  Chile    Egypt  Etc      GMT      Greenwich  Indian   Jamaica      Libya        MST7MDT  Pacific  posixrules  ROC      tzdata.zi  UTC        zone.tab
Arctic      Brazil     CST6CDT  Eire   Europe   GMT0     Hongkong   Iran     Japan        MET          Navajo   Poland   PRC         ROK      UCT        WET        Zulu

[root@vm ~]# rm -rf /etc/localtime     # 刪除系統默認時區文件

[root@vm ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime    # 複製時區文件到默認路徑 或者

或者通過創建鏈接文件,/usr/share/zoneinfo/Asia/ShangHai文件鏈接到/etc/localtime文件

[root@vm ~]# rm -rf /etc/localtime 

[root@vm ~]# ln -sv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
`/etc/localtime' -> `/usr/share/zoneinfo/Asia/Shanghai'

通過實驗,在設置系統時區過程中,TZ變量優先級高於本地/etc/localtime文件優先級。也就是說,若TZ變量及/etc/localtime 都存在的情況下 ,TZ變量會優先生效 。若想要設置timezone(系統時區),故建議直接在全局變量配置文件中添加TZ變量即可,即使用第一種配置方式設置linux系統時區。


Linux NTP服務器部署安裝及配置


一、linux NTP服務器部署安裝

1、驗證服務器端NTP服務是否已安裝

[root@vm ~]# rpm -qa ntp*    
ntpdate-4.2.6p5-12.el6.centos.2.x86_64
ntp-4.2.6p5-12.el6.centos.2.x86_64

2、若系統爲安裝ntp服務 ,可通過rpm或yum進行安裝

[root@vm ~]# yum install ntp -y

[root@vm ~]# rpm -ql ntp   
/etc/ntp.conf    # ntp服務器的主配置文件
/etc/rc.d/init.d/ntpd    # 開機啓動ntpd腳本文件

3、國內穩定NTP時間同步服務器域名

times.aliyun.com

ntp.aliyun.com

cn.pool.ntp.org

0.cn.pool.ntp.org

1.cn.pool.ntp.org

4、使用ntpdate同步時間服務器

[root@vm ~]# ntpdate times.aliyun.com
17 Oct 10:05:27 ntpdate[26878]: step time server 120.25.115.19 offset 4.115593 sec

[root@vm ~]# ntpdate cn.pool.ntp.org
17 Oct 10:05:47 ntpdate[26908]: adjust time server 85.199.214.100 offset 0.037943 sec

[root@vm ~]# ntpdate 0.cn.pool.ntp.org
17 Oct 10:06:00 ntpdate[26915]: adjust time server 120.25.115.20 offset -0.003371 sec

二、linux NTP服務配置

1、NTP服務 ntp.conf 參數詳解

默認ntp.conf內容如下

[root@vm ~]# vi /etc/ntp.conf

#系統時間與BIOS事件的偏差記錄
driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery    # 拒絕所有IPv4的client連接此NTP服務器
restrict -6 default kod nomodify notrap nopeer noquery    # 拒絕所有IPv6的client連接此NTP服務器


restrict 127.0.0.1    # 放行本機localhost對NTP服務的訪問
restrict -6 ::1


# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap    # 放行192.168.1.0網段主機與NTP服務器進行時間同步

# 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

#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

各項參數詳解

利用restrict 來管理權限控制
Restrict [IP]  mask [netmask_IP] [parameter]
 
Parameter 的
  ignore :拒絕所有類型的NTP聯機。
  nomodify: 客戶端不能使用ntpc與ntpq這兩個程序來修改服務器的時間參數,但客戶端可透過這部主機來進行網絡校時;
  noquery:客戶端不能夠使用ntpc與ntpq等指令來查詢時間服務器,不提供NTP的網絡校時。
  notrap:不提供trap 這個運程事件登入的功能。
  notrust:拒絕沒有認證的客戶端。
  Kod:kod技術可以阻止“Kiss of Death “包對服務器的破壞。
  Nopeer:不與其他同一層的NTP服務器進行時間同步。

利用server 設定上層NTP服務器,格式如下:
server [IP or hostname] [prefer]

參數主要如下:
  perfer:表示優先級最高
  burst :當一個運程NTP服務器可用時,向它發送一系列的併發包進行檢測。
  iburst :當一個運程NTP服務器不可用時,向它發送一系列的併發包進行檢測。

2、配置NTPserver

調整NTP配置文件

[root@vm ~]# vi /etc/ntp.conf

# Hosts on local network are less restricted.

# 允許局域網內設備與這臺服務器進行同步時間.但是拒絕讓他們修改服務器上的時間
restrict 192.168.23.0 mask 255.255.255.0 nomodify notrap 
restrict 192.168.10.0 mask 255.255.255.0 nomodify  notrap
restrict 192.168.20.0 mask 255.255.255.0 nomodify  notrap
restrict 172.16.10.0 mask 255.255.255.0 nomodify  notrap
restrict 192.168.100.0 mask 255.255.255.0 nomodify  notrap

# 允許上層時間服務器修改本機時間
restrict times.aliyun.com nomodify
restrict ntp.aliyun.com  nomodify
restrict cn.pool.ntp.org nomodify 

# 定義要同步的時間服務器
server times.aliyun.com iburst prefer    # prefer表示爲優先,表示本機優先同步該服務器時間
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst

logfile /var/log/ntpstats/ntpd.log    # 定義ntp日誌目錄
pidfile  /var/run/ntp.pid    # 定義pid路徑

# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys

ntp服務啓動

[root@vm ~]# chkconfig ntpd on

[root@vm ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

[root@vm ~]# ss -tunlp | grep ntp
udp    UNCONN     0      0         192.168.23.100:123                   *:*      users:(("ntpd",24250,19))
udp    UNCONN     0      0              127.0.0.1:123                   *:*      users:(("ntpd",24250,18))
udp    UNCONN     0      0                      *:123                   *:*      users:(("ntpd",24250,16))
udp    UNCONN     0      0      fe80::92b1:1cff:fe10:3a44:123                  :::*      users:(("ntpd",24250,21))
udp    UNCONN     0      0                    ::1:123                  :::*      users:(("ntpd",24250,20))
udp    UNCONN     0      0                     :::123                  :::*      users:(("ntpd",24250,17))

3、實現clock時間與system時間同步,配置/etc/sysconfig/ntpd文件

ntp服務,默認只會同步系統時間。如果想要讓ntp同時同步硬件時間,可以設置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可以讓硬件時間與系統時間一起同步。

[root@vm ~]# vi /etc/sysconfig/ntpd      # 實現硬件時間與系統時間同步  
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes

4、查看ntp服務器同步狀態

[root@vm ~]# ntpstat     # 確認本地NTP與上層NTP服務器是否聯通
synchronised to NTP server (120.25.115.19) at stratum 10     #本NTP服務器層次爲10,已向120.25.115.19 NTP同步過
   time correct to within 33 ms    # 時間校正到相差33ms之內
   polling server every 256 s    # 每256秒會向上級NTP輪詢更新一次時間

   
[root@vm ~]# ntpq -p     # 查看本地NTP需進行同步的公網NTP服務器狀態
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*203.107.6.88    100.107.25.114   2 u  306 1024  377   15.067   -0.283   1.171
+120.25.115.19   10.137.53.7      2 u  979 1024  377   36.893   -0.671   1.219
+119.28.183.184  100.122.36.196   2 u 1036 1024  377   49.942    7.536  18.648

參數詳解

remote :本地主機所連接的上層NTP服務器,最左邊的符號如下:
    如果有[*] 代表目前正在使用當中的上層NTP服務器。
    如果有[+] 代表也有連上上層NTP服務器,可以作爲提高時間更新的候選NTP服務器
    如果有[-] 代表同步的該NTP服務器被認爲是不合格的NTP Server
    如果有[x] 代表同步的外網NTP服務器不可用
refid  :指的是給上層NTP服務器提供時間校對的服務器。 
St:上層NTP服務器的級別。
When: 上一次與上層NTP服務器進行時間校對的時間(單位:s)
Poll :本地主機與上層NTP服務器進行時間校對的週期(單位:s)
reach:已經向上層 NTP 服務器要求更新的次數 
delay:網絡傳輸過程當中延遲的時間,單位爲 10^(-6) 秒 
offset:時間補償的結果,單位爲10^(-6) 秒
jitter:Linux 系統時間與 BIOS 硬件時間的差異時間, 單位爲 10^(-6) 秒。


NTP client 與NTP服務器時間同步配置


一、linux 客戶端與ntp服務器時間同步部署

可通過兩種方法實現,第一種配置週期性任務計劃時間同步,第二種方法配置ntp服務 

1、配置週期性時間同步任務

[root@zabbix ~]# yum install crontabs -y

[root@zabbix ~]# chkconfig crond on

[root@zabbix ~]# service crond start    # 啓動crond服務

[root@zabbix ~]# vi /etc/crontab    # 配置週期性時間同步任務
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
  0  1  *  *  * root /usr/sbin/ntpdate 192.168.23.100 &> /dev/null ;/sbin/hwclock -w &> /dev/null  
  # 表示每天的1:00執行/usr/sbin/ntpdate 192.168.23.100 NTP服務器,同時同步硬件時鐘,但不顯示任何信息

2、linux客戶端配置ntp服務

[root@zabbix ~]# yum install ntp ntpdate -y

[root@zabbix ~]# more /etc/sysconfig/clock 
ZONE="Asia/Shanghai"

[root@zabbix ~]# date -R
Wed, 17 Oct 2018 13:53:34 +0800

[root@zabbix ~]# ntpdate 192.168.23.100
17 Oct 13:55:08 ntpdate[11772]: adjust time server 192.168.23.100 offset 0.463749 sec

[root@zabbix ~]# vi /etc/ntp.conf # 調整同步的時間服務器
server 192.168.23.100  prefer
restrict 192.168.23.100 nomodify notrap noqurey 

[root@zabbix ~]# vi /etc/sysconfig/ntpd      # 實現硬件時間與系統時間同步  
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
SYNC_HWCLOCK=yes

[root@zabbix ~]# service ntpd start
Starting ntpd:                                             [  OK  ]

[root@zabbix ~]# ntpq -p    # 查看時間同步狀態
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 bogon           120.25.115.19    3 u    3   64    1    0.651   35.932   0.000
 
[root@zabbix ~]# ntpstat 
synchronised to NTP server (192.168.23.100) at stratum 4   #本NTP服務器層次爲4,已向203.107.6.88 NTP同步過
   time correct to within 37 ms     # 時間校正到相差37ms之內
   polling server every 128 s    # 每128秒會向上級NTP輪詢更新一次時間

二、Windows Server客戶端與ntp服務器時間同步部署

1、進入日期與時間,選擇更改設置

    image.png

2、輸入ntp服務器地址,並點擊“立即更新”,確定即可。

    image.png

三、華爲網絡設備客戶端與ntp服務器時間同步部署

<HX>sys
Enter system view, return user view with Ctrl+Z.

[HX]ntp-service server disable     

[HX]ntp-service ipv6 server disable 	

[HX]clock timezone ShangHai add 08:00:00    # 定義時區+8區

[HX]ntp-service unicast-server 192.168.23.100    # 指向同步ntp的服務器地址

[HX]dis ntp-service status     # 查看本地ntp狀態,“synchronized”表示同步完成狀態
 clock status: synchronized 
 clock stratum: 4 
 reference clock ID: 192.168.23.100
 nominal frequency: 100.0000 Hz 
 actual frequency: 100.0000 Hz 
 clock precision: 2^17
 clock offset: 77.7719 ms 
 root delay: 42.63 ms 
 root dispersion: 81.05 ms 
 peer dispersion: 50.72 ms 
 reference time: 07:06:50.505 UTC Oct 17 2018(DF715E0A.817A56DE)
 synchronization state: clock synchronized

補充:

ntpd、ntpdate的區別,下面是網上關於ntpd與ntpdate區別的相關資料。如下所示所示:
使用之前得弄清楚一個問題,ntpd與ntpdate在更新時間時有什麼區別。ntpd不僅僅是時間同步服務器,它還可以做客戶端與標準時間服務器進行同步時間,而且是平滑同步,並非ntpdate立即同步,在生產環境中慎用ntpdate,也正如此兩者不可同時運行。
時鐘的躍變,對於某些程序會導致很嚴重的問題。許多應用程序依賴連續的時鐘,例如數據庫事務。ntpdate調整時間的方式就是我們所說的”躍變“:在獲得一個時間之後,ntpdate使用settimeofday(2)設置系統時間,這有幾個非常明顯的問題:
第一,這樣做不安全。ntpdate的設置依賴於ntp服務器的安全性,***者可以利用一些軟件設計上的缺陷,拿下ntp服務器並令與其同步的服務器執行某些消耗性的任務。由於ntpdate採用的方式是跳變,跟隨它的服務器無法知道是否發生了異常(時間不一樣的時候,唯一的辦法是以服務器爲準)。
第二,這樣做不精確。一旦ntp服務器宕機,跟隨它的服務器也就會無法同步時間。與此不同,ntpd不僅能夠校準計算機的時間,而且能夠校準計算機的時鐘。
第三,這樣做不夠優雅。由於是跳變,而不是使時間變快或變慢,依賴時序的程序會出錯(例如,如果ntpdate發現你的時間快了,則可能會經歷兩個相同的時刻,對某些應用而言,這是致命的)。因而,唯一一個可以令時間發生跳變的點,是計算機剛剛啓動,但還沒有啓動很多服務的那個時候。其餘的時候,理想的做法是使用ntpd來校準時鐘,而不是調整計算機時鐘上的時間。
NTPD 在和時間服務器的同步過程中,會把 BIOS 計時器的振盪頻率偏差——或者說 Local Clock 的自然漂移(drift)——記錄下來。這樣即使網絡有問題,本機仍然能維持一個相當精確的走時。


綜上,本篇博客講解了Linux 系統時區設置,NTP時間服務器部署及ntp.conf配置參數詳解,並通過案例介紹了系統時區配置及時間同步的多種方法,不同Client客戶端的相關配置,最後補充了ntp及ntpdate的相關聯繫及區別。

轉載請註明出處:https://blog.51cto.com/itwish 

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