前言
ntp(Network Time protocol)是網絡時間協議,是用來使網絡中的各個計算機時間同步的一種協議。
ntp配置
1)ntp是由ntp軟件提供,如果沒有可以使用yum進行安裝
2)ntp配置文件介紹:/etc/ntp.conf
利用restrict來管理權限控制;語法格式:restrict IP mask MASK [parameter]
常用的parameter有以下幾類:
ignore:拒絕所類型的ntp連接
nomodify:客戶端只能通過此服務器進行時間同步;不能使用ntpc或ntpq這兩種方式來修改時間;
noquery:不提供網絡時間校驗功能
notrap:不支持遠程登錄的時間戳
notust:拒絕認證不通過的客戶端
注意:如果parameter沒有任何參數,表示來接受自任何網絡的時間同步請求
利用server定義上層ntp服務器;語法格式 server SERVER_IP [prefer];prefer表示首先使用此服務器地址
利用driftfile記錄時間差異;默認記錄位置是/var/lib/ntp/drift;
注意:若果要修改保存路徑,要確保ntpd用戶對目標文件有寫入權限
利用key提高安全性;語法格式: key key_file
3)配置實例:
# vim /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 kod nomodify notrap nopeer noquery \\拒絕所有ipv4用戶 restrict -6 default kod nomodify notrap nopeer noquery \\拒絕所有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 202.120.2.101 \\放行來自202.120.2.101地址進入本服務器 restrict 127.0.0.1 \\放行本機來源 restrict -6 ::1 \\放行本機來源 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap restrict 172.16.2.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 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 202.120.2.101 prefer \\定義此服務器為首先使用的時間服務器 server s1b.time.edu.cn \\定義備用的時間服務器 server s1c.time.edu.cn \\定義備用的時間服務器 #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
4)啟動ntpd服務;查看監聽端口
# service ntpd start #netstat -upln Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 172.16.2.13:123 0.0.0.0:* 11303/ntpd udp 0 0 0.0.0.0:123 0.0.0.0:* 11303/ntpd
5)等待大約15分鐘左右,使用ntpstat查看,自己定義的ntp服務器已經開始與網絡時間服務器進行同步
# ntpstat synchronised to NTP server (202.120.2.101) at stratum 4 time correct to within 176 ms polling server every 64 s
6) 使用ntpq -p可以查看ntp服務器狀態
# ntpq -p remote refid st t when poll reach delay offset jitter ================================================================================================== *dns.sjtu.edu.cn 202.118.1.130 3 u 60 64 275 34.939 -5.211 4.329 ntpa.nic.edu.cn .STEP. 16 u - 1024 0 0.000 0.000 0.000 202.112.7.150 .STEP. 16 u - 1024 0 0.000 0.000 0.000
remote: ntp服務器的上層服務器;如果前面有*代表是正在使用的ntp服務器,+代表ntp服務端已連接,可做為備用ntp服務器
refid:上層ntp服務器的校準服務器
st:NTP上層服務器的級別
when: 多少秒之前與上層服務器同步過時間
poll:多長時間之后與上層服務器再次進程同步時間
reach:已經向上層ntp服務器同步的次數
delay:時延;單位 10^(-3)秒
offset:時間補償的結果;單位與 10^(-3)秒
jitter:linux系統時間與BIOS硬件的差異時間;單位與10^(-3)秒
詳情請參考:http://linux.vbird.org/linux_server/0440ntp.php
原創文章,作者:馬行空,如若轉載,請注明出處:http://www.www58058.com/5569
學習了,謝謝分享!