Linux的時間設定與同步(NTP)
阿新 • • 發佈:2018-12-31
在oracle10g做rac的時候,儘量要保持2個節點的時間一樣,liunx的NTP服務是可以滿足這個需求的。今天我就用一個非系統工程師的理解來說明下如何配置NTP同步。原理我就不說了,我也是一知半解的,就是懂點配置。
客戶端的配置檔案
說道配置檔案,NTP的配置檔案就在/etc/ntp.conf
(如果看客您只想看怎麼配置 搜尋crontab 在本頁面,即進入正文)
[[email protected] ~]# cat /etc/ntp.conf # 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 # 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 -6 ::1 # 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.rhel.pool.ntp.org server 1.rhel.pool.ntp.org server 2.rhel.pool.ntp.org #broadcast 192.168.1.255 key 42 # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 key 42 # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 key 42 # manycast client # 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 # Drift file. Put this in a directory which the daemon can write to. # No symbolic links allowed, either, since the daemon updates the file # by creating a temporary in the same directory and then rename()'ing # it to the file. driftfile /var/lib/ntp/drift # 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
這個檔案常用的沒有幾個屬性,我們把註釋的部分都去掉看看
具體引數怎麼使用大家可以參考臺灣鳥哥的網站:http://linux.vbird.org/linux_server/0440ntp.php[[email protected] ~]# cat /etc/ntp.conf |grep -v ^# #許可權部分限制 restrict default kod nomodify notrap nopeer noquery <==拒絕 IPv4 的使用者 restrict -6 default kod nomodify notrap nopeer noquery <==拒絕 IPv6 的使用者 restrict 127.0.0.1 <==底下兩個是預設值,放行本機來源 restrict -6 ::1 #設定伺服器地址 server 0.rhel.pool.ntp.org server 1.rhel.pool.ntp.org server 2.rhel.pool.ntp.org server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 #時間差異設定和KEY 不需要改動一般情況下 driftfile /var/lib/ntp/drift keys /etc/ntp/keys
常用的檢視ntp的命令如下:
[[email protected] ~]# /etc/init.d/ntpd start #啟動ntp服務
Starting ntpd: [ OK ]
[[email protected] ~]#
[[email protected] ~]# chkconfig ntpd on #開機啟動
[[email protected] ~]#
[[email protected] ~]# netstat -tlunp | grep ntp #檢視端口占用
udp 0 0 10.10.27.11:123 0.0.0.0:* 4996/ntpd
udp 0 0 127.0.0.1:123 0.0.0.0:* 4996/ntpd
udp 0 0 0.0.0.0:123 0.0.0.0:* 4996/ntpd
udp 0 0 ::1:123 :::* 4996/ntpd
udp 0 0 fe80::20c:29ff:fe90:123 :::* 4996/ntpd
udp 0 0 :::123 :::* 4996/ntpd
[ [email protected] ~]#
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ntpstat #檢視同步情況
unsynchronised
time server re-starting
polling server every 64 s
[[email protected] ~]#
[[email protected] ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*LOCAL(0) .LOCL. 10 l 24 64 37 0.000 0.000 0.001
說了這麼多原理,還是給大家一個例子吧,下面是一個服務端的配置,
[[email protected] etc]# cat ntp.conf
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
restrict 10.10.27.13 #可以訪問的主機IP,也可以設定整個網段,看最上面的全檔案內容
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
Broadcastdelay 0.008
[[email protected] etc]#
客戶端的配置檔案
[[email protected] ~]# cat /etc/ntp.conf |grep -v ^#
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server 10.10.27.11 prefer
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
Broadcastdelay 0.008 #從字面意思來看 是 廣播延時時間。不明白乾嗎用的,原檔案沒有說明有這個引數
[[email protected] ~]#
這樣配置以後,大家就可以啟動服務了,在兩個節點伺服器(由於ntpd的server/client之間的時間誤差不允許超過1000秒,因此你得先手動進行時間同步,然後再設定與啟動服務。)
[[email protected] ~]# /etc/init.d/ntpd start #啟動ntp服務
Starting ntpd: [ OK ]
[[email protected] ~]#
[[email protected] ~]# chkconfig ntpd on #開機啟動
[[email protected] ~]#
[[email protected] ~]# netstat -tlunp | grep ntp #檢視端口占用
在我修改服務端的時間後,我發現客戶端是不能馬上同步的,在我看了很多的帖子以後,我才發現,聰明的大師就配置了一個ntp服務端,其他節點同步時間的時候是用ntpdate的,這個命令可以馬上同步時間。可以把這個命令加在客戶端的crontab
下面給大家也演示下crontab的方式,這個很簡單,我推薦這個方法
伺服器端:直接新增下面的最後一行 對了10.10.27.11是服務端IP
[[email protected] ~]# cat /etc/ntp.conf |grep -v ^#
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict -6 ::1
server 0.rhel.pool.ntp.org
server 1.rhel.pool.ntp.org
server 2.rhel.pool.ntp.org
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
keys /etc/ntp/keys
restrict 10.10.27.0 mask 255.255.255.0 nomodify #直接編輯檔案 新增這行 沒事閒著不要加 notrap
[[email protected] ~]#
服務端配置好了以後,記得啟動
[[email protected] ~]# /etc/init.d/ntpd start #啟動ntp服務
Starting ntpd: [ OK ]
[[email protected] ~]#
客戶端就有點麻煩了
[[email protected] ~]# ll ntpdate_to_11.sh #新建一個sh檔案
-rwxrwxrwx 1 root root 47 Jun 12 11:47 ntpdate_to_11.sh
[[email protected] ~]#
[[email protected] ~]# cat ntpdate_to_11.sh #檔案內容如下
while :; do ntpdate 10.10.27.11; sleep 1; done
[[email protected] ~]#
[[email protected] ~]# pwd
/root
[[email protected] ~]#
[[email protected] ~]# crontab -e
在VI介面輸入“* * * * * /usr/sbin/ntpdate 10.10.27.11 >> /root/ntpdate_to_rac1.log 2>&1” #記得儲存啊
[[email protected] ~]# crontab -l #檢視下是否新增成功
* * * * * /usr/sbin/ntpdate 10.10.27.11 >> /root/ntpdate_to_rac1.log 2>&1
所有同步的過程記錄在日誌ntpdate_to_rac1.log裡面了。
一開始的時候會報
ntpdate_to_11.sh ntpdate_to_rac1.log
[[email protected] ~]# cat ntpdate_to_rac1.log
12 Jun 11:51:05 ntpdate[4520]: no server suitable for synchronization found
12 Jun 11:52:05 ntpdate[4532]: no server suitable for synchronization found
12 Jun 11:53:01 ntpdate[4547]: no server suitable for synchronization found
12 Jun 11:54:01 ntpdate[4559]: no server suitable for synchronization found
12 Jun 11:55:02 ntpdate[4570]: no server suitable for synchronization found
12 Jun 11:56:01 ntpdate[4583]: no server suitable for synchronization found
12 Jun 11:57:01 ntpdate[4595]: no server suitable for synchronization found
12 Jun 11:58:01 ntpdate[4607]: no server suitable for synchronization found
12 Jun 11:59:01 ntpdate[4620]: no server suitable for synchronization found
12 Jun 12:00:01 ntpdate[4631]: no server suitable for synchronization found
12 Jun 12:01:01 ntpdate[4642]: no server suitable for synchronization found
12 Jun 12:02:01 ntpdate[4654]: adjust time server 10.10.27.11 offset 0.077524 sec
12 Jun 12:03:01 ntpdate[4662]: adjust time server 10.10.27.11 offset 0.017492 sec
12 Jun 12:04:01 ntpdate[4670]: adjust time server 10.10.27.11 offset -0.008772 sec
12 Jun 12:05:01 ntpdate[4679]: adjust time server 10.10.27.11 offset 0.004367 sec
12 Jun 12:06:01 ntpdate[4688]: adjust time server 10.10.27.11 offset -0.002139 sec
上面錯誤是伺服器端的ntp還沒有啟動呢,這個過程,大概在15分以內。大家注意下就好了。上面的同步週期是1分鐘,大家可以根據自己的喜好來修改。