天天看點

NFS 網絡系統配置及自動挂載

NFS服務簡介  

NFS是Network File System的縮寫,即網絡檔案系統。NFS是由Sun開發并發展起來的一項用于在不同機器,不同作業系統之間通過網絡互相分享各自的檔案。NFS server也可以看作是一個FILE SERVER,用于在UNIX類系統之間共享檔案,可以輕松的挂載(mount)到一個目錄上,操作起來就像本地檔案一樣的友善。

一、nfs-server(192.168.2.1)

1、檢查是否安裝

[root@host ~]# rpm -qa |grep nfs     //檢視是否安裝了nfs

nfs-utils-lib-1.0.8-7.6.el5

nfs-utils-1.0.9-42.el5              //nfs主程式,已安裝

2、NFS配置檔案設定

若要共享目錄或檔案,需要在共享清單中指明該檔案/etc/exports  (文法參照 man  exports)

#vim   /etc/exports

/home/nfs 192.168.2.0/24(rw,sync)    (192.168.2.0網段的使用者對/home/nfs NFS卷具有讀寫)

/media/cdrom 192.168.2.*(ro)        (192.168.2.0網段的使用者對/media/cdromNFS卷具有讀)

注:/etc/exports,配置檔案有三部分: 輸出目錄,用戶端,參數選項

a、輸出目錄是指NFS系統中需要共享給客戶機使用的目錄;

用戶端是指網絡中可以通路這個NFS輸出目錄的計算機

b、用戶端常用的指定方式

指定ip位址的主機 192.168.0.200

指定子網中的所有主機 192.168.0.0/24

指定域名的主機 a.liusuping.com

指定域中的所有主機 *.liusuping.com

所有主機 *

c、選項:

選項用來設定輸出目錄的通路權限、使用者映射等。NFS主要有3類選項:

通路權限選項

設定輸出目錄隻讀 ro

設定輸出目錄讀寫 rw

使用者映射選項

all_squash 将遠端通路的所有普通使用者及所屬組都映射為匿名使用者或使用者組(nfsnobody);

no_all_squash 與all_squash取反(預設設定);

root_squash 将root使用者及所屬組都映射為匿名使用者或使用者組(預設設定);

no_root_squash 與rootsquash取反;

anonuid=xxx 将遠端通路的所有使用者都映射為匿名使用者,并指定該使用者為本地使用者(UID=xxx);

anongid=xxx 将遠端通路的所有使用者組都映射為匿名用 戶組賬戶,并指定該匿名使用者組賬戶為本地使用者組賬戶(GID=xxx);

其它選項

secure 限制用戶端隻能從小于1024的tcp/ip端口連接配接nfs伺服器(預設設定);

insecure 允許用戶端從大于1024的tcp/ip端口連接配接伺服器;

sync 将資料同步寫入記憶體緩沖區與磁盤中,效率低,但可以保證資料的一緻性;

async 将資料先儲存在記憶體緩沖區中,必要時才寫入磁盤;

wdelay 檢查是否有相關的寫操作,如果有則将這些寫操作 一起執行,這樣可以提高效率(預設設定);

no_wdelay 若有寫操作則立即執行,應與sync配合使用;

subtree 若輸出目錄是一個子目錄,則nfs伺服器将檢查其父目錄的權限(預設設定);

no_subtree 即使輸出目錄是一個子目錄,nfs伺服器也不檢查其父目錄的權限,這樣可以提高效率

3、啟動nfs服務

# service nfs start

啟動 NFS 服務:                                            [确定]

關掉 NFS 配額:                                            [确定]

啟動 NFS 守護程序:                                        [确定]

啟動 NFS mountd:                                          [确定]

4、檢視自己的rpc程序,與NFS相關的是否正常開啟,如下表明已經正常開啟:

   #rpcinfo -p  192.168.2.1 (本機ip,)

    100011    1   udp    821  rquotad

    .........

100003    2   udp   2049  nfs

........

    100021    1   tcp  34647  nlockmgr

     .......

    100005    1   udp    852  mountd

      ........

5、導出共享清單

[root@host ~]# exportfs -rv

exporting 192.168.2.0/24:/home/nfs

exporting 192.168.2.*:/media/cdrom

二、nfs用戶端(192.168.2.20/24)

1、檢視自己的rpc程序是否開啟

[root@client ~]# rpcinfo -p

   程式 版本 協定   端口

100000    2   tcp    111  portmapper

100024    1   udp    794  status

100021    1   udp  58859  nlockmgr

2、檢視nsf-server的exports檔案是否可以通路:

[root@client ~]# showmount -e 192.168.2.1

Export list for 192.168.2.1:

/media/cdrom 192.168.2.*

/home/nfs    192.168.2.0/24

3、建立挂載點,手動挂載

#mkdir  /tmp/abc

#mkdir  /tmp/cdrom

# mount 192.168.2.1:/home/nfs  /tmp/abc

# mount 192.168.2.1:/media/cdrom  /tmp/cdrom

4、設定開機自動挂載

#vim /etc/fstab

192.168.2.1:/home/nfs   /tmp/abc                nfs     defaults,soft,intr      0 0

192.168.2.1:/media/cdrom   /tmp/cdrom           nfs     defaults,soft,intr      0 0

 注:(soft、intr等參數可檢視man nfs)

 -ro,soft,intr:  -ro:挂載時的權限,soft:如果有I/O錯誤時,會告知系統,中繼挂載;intr:挂載時有大量逾時時,中繼挂載,并告知系統。

<a href="http://thenubbyadmin.com/2013/04/10/solving-nfs-mounts-at-boot-time/" target="_blank">http://thenubbyadmin.com/2013/04/10/solving-nfs-mounts-at-boot-time/</a>

_netdev

The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).

This is awesome, and exactly what we want. So you modify your entry in fstab to look like this:

10.10.10.1:/vol1/fs1 /data nfs defaults,_netdev 0 0

You’ve been bitten by NFS mounting in the past so you throw this in your test environment and reboot immediately. After the system comes up you notice a problem. Your NFS volumes are still unmounted. You see, there’s a bit of a hitch. Automounter followed the same procedure that it did before, except this time it didn’t even attempt to mount /data. The _netdev option doesn’t tell the system to mount the filesystem when network comes up, it says don’t attempt to mount it at all if the network isn’t up. There is still a missing piece to the puzzle. If you look at your init scripts there is a service called netfs. If you read the script you can see in the chkconfig header this description:

# description: Mounts and unmounts all Network File System (NFS),

# CIFS (Lan Manager/Windows), and NCP (NetWare) mount points.

This is exactly what you need. It is a service whose sole purpose is to read your /etc/fstab and mount network filesystems. All you have to do is enable it

chkconfig netfs on

and watch the magic happen. Now your mount boot process should look something like this:

Automounter reads /etc/fstab

Ignores /data since it has _netdev option set

Mounts all other filesystems

Finishes mount jobs and allows system to continue booting

Network comes up

Service netfs started

netfs reads /etc/fstab and finds an nfs filesystem

netfs mounts /data

What’s funny is that while I was researching this problem I never stumbled across netfs as a service. I had even gone so far as to start planning out my own custom init script that would do exactly this, except specifically for my mount points instead of generalizing. It’s nice to see that I was on the right track, but even better that the tools already existed.

#umount /tmp/cdrom    /tmp/abc

#mount -a

5、這種方式的挂載需要消耗大量的資源來維持連接配接,可以使用自動挂載當切換到該目錄時進行挂載,退出時,取消挂載

a、安裝autofs軟體包

[root@client tmp]# rpm -qa|grep autofs

autofs-5.0.1-0.rc2.131.el5

b、編輯autofs的配置檔案/etc/auto.master

#vim /etc/auto.master

/tmp /etc/nfs.misc   --timeout=08

:wq

c、編輯要求的産生/etc/nfs.misc

#vim /etc/nfs.misc

abc     -fstype=nfs   192.168.2.1:/home/nfs

cdrom   -fstype=nfs   192.168.2.1:/media/cdrom

d、啟動autofs服務

#service autofs  start

#rm -rf /tmp/abc

#rm -rf /tmp/cdrom

e、測試

<a href="http://blog.51cto.com/attachment/201212/165824435.jpg" target="_blank"></a>

注:abc、cdrom : 本地目錄,我們不需要建立它,用于挂載遠端共享檔案192.168.2.1:/home/nfs,在我們想要使用遠端共享檔案/home/nfs時,隻要在本地目錄/tmp下鍵入cd abc 系統就會自動挂載到遠端共享目錄:192.168.2.10:/home/nfs,我們就可以正常使用裡面的共享檔案了 ,在到達逾時時間時就會自動解除安裝。

f、以rw自動挂載/home/nfs/ NFS卷到/tmp/abc目錄下,卻發現無法建立目錄,因為nfs-server的本地目錄還沒有寫權限,

[root@client abc]# touch nfs.txt

touch: 無法觸碰 &amp;ldquo;nfs.txt&amp;rdquo;: 權限不夠

g、nfs-server服務端修改/home/nfs的本地權限

[root@host home]# chmod o+wt nfs/

就可以正常建立了,

[root@client abc]# dir

hello-word.txtnfs.txt  nft-test

實驗結束。

注:NFS用戶端與NFS伺服器在連接配接上之後,為了保證其正常的連接配接,NFS用戶端與NFS伺服器之間要不斷的發送資料包,來宣告自己還在與NFS伺服器進行着連接配接,但是,如果一個NFS伺服器上有許多的用戶端一直連接配接的話,NFS伺服器會承受很大的帶寬壓力,這對NFS伺服器的正常使用會造成很大的影響,是以,為了避免這種情況的發生,人們就想到了一種方法,讓NFS用戶端在擷取資料時與NFS伺服器進行連接配接,而在沒有擷取資料的時間内,自動的斷開與NFS伺服器之間的連接配接,但隻要用戶端發送擷取資料的請求進,用戶端就與伺服器自動連接配接上,自動挂載的方法對用戶端主機與伺服器主機都有很大的好處。配置方法是在NFS用戶端方面進行配置的

本文轉自 劉園  51CTO部落格,原文連結:http://blog.51cto.com/colynn/1095401

繼續閱讀