ntpd服務可以在修正時間的同時,修正cpu tick
ntpd有一個自我保護設置: 如果本機與上源時間相差太大, ntpd不運行
ntpdate : 不會考慮其他程序是否會陣痛,直接調整時間,有可能會對程序造成影響
# 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:指定使用無特權的端口發送數據包,可以越過防火牆與主機同步;
使用ntpd來校准時鍾,而不是調整計算機時鍾上的時間。
在計算機剛剛啟動,但還沒有啟動很多服務的那個時候可以使用ntpdate同步時間
ntpdate -u x.x.x.x
這里的x.x.x.x可以是公網時間服務器IP地址,也可以是下面二中我們自建的ntp服務器地址。
3. /etc/init.d/crond restart
以下7個域名提供服務,大家可以直接使用。
http://time2.aliyun.com
http://time3.aliyun.com
http://time4.aliyun.com
http://time5.aliyun.com
http://time6.aliyun.com
http://time7.aliyun.com
或者直接訪問這個地址 time.pool.aliyun.com
13 Nov 10:48:28 ntpdate[26527]: adjust time server 182.92.12.11 offset 0.000458 sec
0.centos.pool.ntp.org
1.centos.pool.ntp.org
2.centos.pool.ntp.org
NTP通信協議原理
- 首先主機啟動NTP。
- 客戶端會向NTP服務器發送調整時間的message。
- 然后NTP server會送出當前的標准時間給client
- client接受來自server的時間后,會根據這個信息來調整自己的時間。這樣就實現了網絡對時。
NTP這個deamon采用了UDP 123端口。
由於NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端.
所以服務器從高到低級別可以設定為1-16.
為了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器的
安裝
# yum install ntp
# chkconfig ntpd on
# 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).
# permit the source to query or modify the service on this system.
restrict -6 default kod nomodify notrap nopeer noquery
默認拒絕任何操作
任何ip4地址、ip6地址 不能修改、不能trap遠程登錄、不能嘗試對等、不能校對時間,
語法為: 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地址的權限設置。
# be tightened as well, but to do so would effect some of 這里最好收緊權限,但是會影響一些管理功能
# the administrative functions.
restrict 127.0.0.1
restrict ::1
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 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秒。
#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 !