本篇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
time1.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 driftfile /var/lib/ntp/drift #系統時間與BIOS事件的偏差記錄 restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery # 限制所有的client連接此NTP服務器進行同步、查詢等相關操作 restrict 127.0.0.1 restrict -6 ::1 # 本機localhost對NTP服務擁有全部的權限 # 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 #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 子網掩碼 參數 其中IP地址可是單個主機、網段或者是default ,default 就是指所有的IP 參數有以下幾個: ignore :關閉所有的 NTP 聯機服務 kod : 訪問違規時發送 KoD 包。 nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。 notrust :客戶端除非通過認證,否則該客戶端來源將被視爲不信任子網 noquery :不提供客戶端的時間查詢:用戶端不能使用ntpq,ntpc等命令來查詢ntp服務器 notrap :不提供trap遠端登陸:拒絕爲匹配的主機提供模式 6 控制消息陷阱服務。陷阱服務是 ntpdq 控制消息協議的子系統,用於遠程事件日誌記錄程序。 nopeer :用於阻止主機嘗試與服務器對等,並允許欺詐性服務器控制時鐘 restrict -6 表示IPV6地址的權限設置。 fudge 127.127.1.0 stratum 0 # 這行是時間服務器的層次。設爲0則爲頂級,如果要向別的NTP服務器更新時間,請不要把它設爲0 restrict default ignore # 設置默認策略爲允許任何主機進行時間同步 restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 允許192.168.1.1-192.168.1.255主機與NTP服務器進行時間同步,但不允許更改服務器端參數;同時NTP服務器拒絕爲匹配的主機提供trap遠程登錄及日誌記錄。 logfile /var/log/ntpstats/ntpd.log # 定義ntp日誌目錄
2、配置NTPserver
調整NTP配置文件
[root@vm ~]# vi /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 #系統時間與BIOS事件的偏差記錄 # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery # 限制所有的client連接此NTP服務器進行同步、查詢等相關操作 # 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 # 本機localhost對NTP服務擁有全部的權限 restrict -6 ::1 # Hosts on local network are less restricted. restrict 192.168.23.0 mask 255.255.255.0 nomodify # 允許局域網內client連接到這臺服務器同步時間.但是拒絕讓他們修改服務器上的時間 restrict 192.168.10.0 mask 255.255.255.0 nomodify restrict 192.168.20.0 mask 255.255.255.0 nomodify restrict 172.16.10.0 mask 255.255.255.0 nomodify restrict 192.168.100.0 mask 255.255.255.0 nomodify # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server times.aliyun.com prefer # 表示本機NTP優先進行時間同步的服務器 server time1.aliyun.com iburst server cn.pool.ntp.org iburst server 0.cn.pool.ntp.org iburst server 1.cn.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 fudge 127.127.1.0 stratum 3 # 設置本機NTP等級爲3 ,允許其他客戶端同步該ntp服務器,也能實現該ntp服務器與公網ntp服務器同步 logfile /var/log/ntpstats/ntpd.log # 定義ntp日誌目錄 # 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、配置/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 3 #本NTP服務器層次爲3,已向120.25.115.19 NTP同步過 time correct to within 33 ms # 時間校正到相差33ms之內 polling server every 256 s # 每256秒會向上級NTP輪詢更新一次時間 [root@vm ~]# watch ntpq -p # 查看本地NTP需進行同步的公網NTP服務器狀態 remote refid st t when poll reach delay offset jitter ============================================================================== *120.25.115.19 10.137.53.7 2 u 131 128 377 36.747 -7.176 1.091 +203.107.6.88 100.107.25.114 2 u 106 128 377 14.224 -6.557 1.017 +119.28.206.193 100.122.36.196 2 u 127 128 377 49.294 -5.359 1.101 -amy.chl.la 213.251.128.249 2 u 6 128 235 378.766 -24.676 54.061 x5.103.139.163 .GPS. 1 u 100 128 257 265.566 -44.008 11.612 +120.25.115.20 10.137.53.7 2 u 45 128 377 38.392 -6.198 4.650
參數詳解
remote:進行時間同步的上層NTP的ip或主機名 “*” 表示時間同步的主NTP Server; “+”表示時間同步的輔助NTP Server,和帶有“*”號的服務器一起爲本地NTP提供同步服務,當*號服務器不可用時它就可以接管; “-”表示同步的NTP服務器被clustering algorithm認爲是不合格的NTP Server; “x”表示同步的外網NTP服務器不可用 refid:爲外網NTP服務器提供時間同步服務的上一層ntp主機地址 st :stratum階層 when :多少秒前曾經同步過時間 poll:本地機和遠程服務器多少時間進行一次同步(單位爲秒) reach: 已經向上層ntp服務器要求更新的次數 delay: 網絡延遲 offset: 時間補償 jitter:系統時間與bios時間差
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 server time1.aliyun.com iburst [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 203.107.6.88 10.165.84.13 2 u - 64 1 17.134 31.774 1.699 [root@zabbix ~]# ntpstat synchronised to NTP server (203.107.6.88) at stratum 3 #本NTP服務器層次爲3,已向203.107.6.88 NTP同步過 time correct to within 981 ms # 時間校正到相差981ms之內 polling server every 64 s # 每64秒會向上級NTP輪詢更新一次時間
二、Windows Server客戶端與ntp服務器時間同步部署
1、進入日期與時間,選擇更改設置
2、輸入ntp服務器地址,並點擊“立即更新”,確定即可。
三、華爲網絡設備客戶端與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 client的配置等案例。
轉載請註明出處:http://blog.51cto.com/itwish