ntp時間服務器 時間同步


具體兩種模式
1.服務器數量比較少,可以直接與時間服務器同步
2.本地服務器較多,在本地自建時間同步服務器,
 
 
時間同步的兩個命令
ntpd :         校准時間,一點點的校准過來時間的,最終把時間慢慢的校正對。
                      ntpd服務可以在修正時間的同時,修正cpu tick
                      ntpd有一個自我保護設置: 如果本機與上源時間相差太大, ntpd不運行


ntpdate : 不會考慮其他程序是否會陣痛,直接調整時間,有可能會對程序造成影響
 
ntpdate [-nv] [ IP/hostname]
# ntpdate -u 192.168.0.2
# ntpdate -u 10.11.0.35
# ntpdate -u time.ntp.org
# ntpdate -u ntp.sjtu.edu.cn >>/var/log/ntp.log 2>&1;hwclock -w  
 
 注意:若不加上-u參數, 會出現以下提示:no server suitable for synchronization found
-u:指定使用無特權的端口發送數據包,可以越過防火牆與主機同步;
 
 
理想的做法是:
使用ntpd來校准時鍾,而不是調整計算機時鍾上的時間。
在計算機剛剛啟動,但還沒有啟動很多服務的那個時候可以使用ntpdate同步時間
 
 
 一、直接用命令與時間服務器進行同步
 
 實例:
使用ntpdate 進行同步: 

ntpdate -u x.x.x.x

這里的x.x.x.x可以是公網時間服務器IP地址,也可以是下面二中我們自建的ntp服務器地址。
可以加一個定時計划,定時同步
1. crontab -e  
2. */5 * * * * /usr/sbin/ntpdate -u x.x.x.x
3. /etc/init.d/crond restart 
 
2.這里提供幾個公網時間服務器地址
阿里雲時間服務器,授時信號來自GPS、北斗兩套衛星信號,並配備原子鍾守時,
以下7個域名提供服務,大家可以直接使用。
time1.aliyun.com
time2.aliyun.com
time3.aliyun.com
time4.aliyun.com
time5.aliyun.com
time6.aliyun.com
time7.aliyun.com

或者直接訪問這個地址  time.pool.aliyun.com

# ntpdate -u time.pool.aliyun.com
13 Nov 10:48:28 ntpdate[26527]: adjust time server 182.92.12.11 offset 0.000458 sec

centos也有幾個時間服務器(最好還是用國內的吧)
0.centos.pool.ntp.org
1.centos.pool.ntp.org
2.centos.pool.ntp.org



 
二、 自建ntpd 時間同步服務器

NTP通信協議原理

  1. 首先主機啟動NTP。
  2. 客戶端會向NTP服務器發送調整時間的message。
  3. 然后NTP server會送出當前的標准時間給client
  4. client接受來自server的時間后,會根據這個信息來調整自己的時間。這樣就實現了網絡對時。

NTP這個deamon采用了UDP 123端口。


遠程服務器的層級(stratum):
由於NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端.
所以服務器從高到低級別可以設定為1-16.
為了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器的



1.配置ntp服務器
安裝  
# yum install ntp
開機自啟動
# chkconfig ntpd on
 
修改配置文件:
 [root@test01 ~]# less /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 
    
默認拒絕任何操作 
 
任何ip4地址、ip6地址  不能修改、不能trap遠程登錄、不能嘗試對等、不能校對時間


restrict 控制相關權限。

語法為:  restrict [ 客戶端IP ]  mask  [ IP掩碼 ]  [參數]

其中IP地址也可以是default ,default 就是指所有的IP

參數有以下幾個:

nomodify:客戶端不能更改服務端的時間參數,但是客戶端可以通過服務端進行網絡校時。

notrust :拒絕沒有認證的用戶端

noquery :客戶端不能使用ntpq,ntpc來查詢ntp服務器,等於不提供校對時間服務

notrap :不提供trap遠程登錄功能,trap服務是一種遠程時間日志服務

拒絕為匹配的主機提供模式 6 控制消息陷阱服務。陷阱服務是 ntpdq 控制消息協議的子系統,用於遠程事件日志記錄程序。

nopeer :用於阻止主機嘗試與服務器對等

kod : 訪問違規時發送 KoD 包,向不安全的訪問者發送Kiss-Of-Death報文。

restrict -6 表示IPV6地址的權限設置。

 
如何控制客戶的范圍
# 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
 
本機與公共時間服務器同步選項
設定NTP主機來源(其中prefer表示優先主機)
# 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 host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]

其中host是上層NTP服務器的IP地址或域名,隨后所跟的參數解釋如下所示:

◆ key: 表示所有發往服務器的報文包含有秘鑰加密的認證信息,n是32位的整數,表示秘鑰號。

◆ version: 表示發往上層服務器的報文使用的版本號,n默認是3,可以是1或者2。

◆ prefer: 優先使用。

◆ mode: 指定數據報文mode字段的值。

◆ minpoll: 指定與查詢該服務器的最小時間間隔為2的n次方秒,n默認為6,范圍為4-14。

◆ maxpoll:  指定與查詢該服務器的最大時間間隔為2的n次方秒,n默認為10,范圍為4-14。

◆ iburst: 當初始同步請求時,采用突發方式接連發送8個報文,時間間隔為2秒。


#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

當外部時間不可用時,以本地(本機)時間作為服務時間
本地時間以 127.127.1.0 表示  級別為10

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available.
#server 127.127.1.0     # local clock
#fudge  127.127.1.0 stratum 10

下面是一些加密認證的配置
# 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


一個簡單實例如下:
restrict default kod nomodify notrap nopeer noquery
拒絕所有ip4 的請求
# restrict -6 default kod nomodify notrap nopeer noquery  #針對ipv6設置
 
# 允許本地所有操作
restrict 127.0.0.1
#restrict -6 ::1
 
# 允許的局域網絡段或單獨ip 某些權限
restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify motrap
restrict 192.168.1.123 mask 255.255.255.255 nomodify motrap
 
# 使用上層的internet ntp服務器
restrict cn.pool.ntp.org
restrict 1.cn.poo.ntp.org
restrict 0.asia.pool.ntp.org
restrict 3.asia.pool.ntp.org
server cn.pool.ntp.org prefer
server 1.cn.poo.ntp.org
server 0.asia.pool.ntp.org
server 3.asia.pool.ntp.org
 
# 如果無法與上層ntp server通信以本地時間為標准時間
server   127.127.1.0    # local clock
fudge    127.127.1.0 stratum 10
 
# 計算本ntp server 與上層ntpserver的頻率誤差
driftfile /var/lib/ntp/drift
 
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys /etc/ntp/keys
 
#日志文件
logfile /var/log/ntp .log


配置/etc/sysconfig/ntpd文件

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

#允許BIOS與系統時間同步,也可以通過hwclock -w 命令

SYNC_HWCLOCK=yes

 

啟動ntpd 服務
# service ntpd start

 


# 查看ntp的端口,應該看到123端口
#netstat -unlnp

 


 

 

# 查看ntp服務器有無和上層連通
#ntpstat
 
synchronised to NTP server () at stratum 2
time correct to within 74 ms
polling server every 128 s
# 注意:此命令出現上述synchronised結果比較慢,我的用了大概5分鍾。

剛啟動的時候,一般是:

 

# ntpstat 

unsynchronised

  time server re-starting

   polling server every 64 s

連接並同步后:

synchronised to NTP server (202.112.10.36) at stratum 3 

   time correct to within 275 ms

   polling server every 256 s


 

ntptrace -n 127.0.0.1
 
127.0.0.1: stratum 3, offset -0.001095, synch distance 0.532610
116.193.83.174: timed out, nothing received

 



查看ntp服務器與上層ntp服務器的狀態:ntpq

 

#ntpq -p
 
# 其中:

# remote - 本機和上層ntp的ip或主機名,“+”有連線可做候選,“*”正在使用的

# refid - 更上一層的ntp地址

# st - stratum的 級別

 
# when   - 多少秒前曾經同步過時間
# poll   - 下次更新在多少秒后
# reach  - 已經向上層ntp服務器要求更新的次數
# delay  - 網絡延遲
# offset - 時間補償
# jitter - 系統時間與bios時間差

 

# ntpq -p
     remote           refid                  st t when poll reach   delay   offset  jitter
==============================================================================
*time4.aliyun.co 10.137.38.86     2 u  111  128  377   27.480   -5.995   1.852




2.配置NTP客戶端:
在所有客戶端上vim /etc/ntp.conf,添加:
server x.x.x.x 

x.x.x.x 為上面配置的ntp服務器地址


注意:當server與client之間的時間誤差過大時(可能是1000秒),處於對修改時間可能對系統和應用帶來不可預知的問題,NTP將停止時間同步!
所以如果發現NTP啟動之后時間並不進行同步時,應該考慮到可能是時間差過大引起的,
此時需要先手動進行時間同步 ntpdate 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM