NFS簡介(轉)

post0發表於2007-08-11
NFS簡介(轉)[@more@]

大家好 :

        下面是小弟寫的一些介紹 NFS 的文章, 由於想參與網路管理的工作學長指派這一個主題給我報告, 由於本身對這方面並不太懂, 只是找一些書籍隨便拼拼湊湊, 不知道有沒有寫到重點,或是一些要點沒有提到,因此post出來希望熟悉NFS 的高手們能花一點時間看看我的文章  然後mail給我,指出錯誤的地方,或者直接reply也可以, 謝謝 !

        E-mail : u3430854@sparc20.ncu.edu.tw

===========================================================================

    ◎ What is NFS ?

         Network        File        System    

          NFS 是由SUN公司發展, 並於1984年推出, NFS是一個RPC service ,

     它使我們能夠達到檔案的共享, 它的設計是為了在不同的系統間使用, 所

     以它的通訊協定設計與主機及作業系統無關.當使用者想用遠端檔案時只

     要用"mount"就可把remote檔案系統掛接在自己的檔案系統之下,使得遠端

     的檔案使用上和local機器的檔案沒兩樣.

           machine  A                        machine  B

                /                               /

      bin     etc     usr               bin     etc      usr

                      man                          man     share     local

     假如我們在機器A上, 要把機器B上的 /usr/man 掛接到machine A 的

     /usr/man只要下

         mount  machine_name:/usr/man  /usr/home

     就可mount過來.而我們不只是可以mount目錄,就是一個檔也是可以的.在

     掛接之後我們只能對檔案做reading (or writing) 的動作,而不能在

     remote machie上把此檔或目錄move,delete掉 , 但須注意的是如我們

     mount /usr 後 , 不能再mount /usr底下的目錄, 否則會發生錯誤

   □ Servers & Clients

          NFS就是促使Servers上的檔案能被其他的機器mount,而達到資源共享,

      享用這些檔案的機器就可稱為Client,一個client可以從server上mount一

      個檔或是一個層次的目錄(file hierarchies) . 然而事實上任何一臺機器

      都可以做NFS server or NFS client ,甚至同時為NFS server and NFS

      client 也可以.

   □ Server's Exporting & Client's Mounting

          NFS server 所export 出來的檔案或目錄都記錄在 /etc/exports 這

      一個檔中,當我們啟動NFS server 時 在 /etc/rc.local 的這一個script

      會自動的啟動 exportfs 這一個程式 , 搜尋 /etc/exports 這一個檔是否

      存在, 並且賦予正確的許可權給所有export出去的 file hierarchies .

          但須注意的是,只有server所export出去的路徑,NFS client才能夠mount

     , 同樣的當啟動client時 , 系統會自動去mount所有server export的路徑,

      而mount到的所有路徑都會記錄在 /etc/fstab 下 , 類似如下的fstab檔

    /dev/sd0a  /      4.2 rw 1 1

    /dev/sd0h  /tmp   4.2 rw 1 3

    /dev/sd0g  /usr   4.2 rw 1 2

    /dev/fd0   /pcfs  pcfs rw,noauto 0 0

    sparc20:/swap /swap nfs rw,intr,bg,soft 0 0

    sparc17:/home /home nfs rw,intr,bg,soft 0 0

    sparc17:/home3 /home3 nfs rw,intr,bg,soft 0 0

    sparc14:/home4 /home4 nfs rw,intr,bg,soft 0 0

    sparc20:/home2 /home2 nfs rw,intr,bg,soft 0 0

    sparc20:/var/spool/mail /var/spool/mail nfs rw,intr,bg,soft 0 0

    rs970:/home1 /home1 nfs rw,intr,bg,soft 0 0

   ★ Noted :

         當client mount 到一個路徑,絕對不是說copy server上的這一個路徑

     到local的機器上,我們可以用 cd 進入這一個mount到的路徑,就如同是使用

     local directory一樣

   □ Setting Up a NFS Server

         1. 定義機器為 NFS file server

         2. 劃分server's disk ,定義哪一些partitions 是要提供出來作為

            client 所共享的file system

         3. 在 Client Form 上定義每一臺client 的引數

         4. 寫出 /etc/exports  (一般系統都有一個default exports)

         5. 重新boot NFS server or 用指令 exportfs -a 輸出所有的

            directories 並且用 nfsd 8 & 啟動 nfsd守護程式,常駐在背景

    ※ ps. 一些細節

         1. 檢查 /etc/exports 輸出路徑的許可權,確定只有root能修改,

            all user只能read

         2. 用exportfs 去增加或刪除directories

            exportfs -o access=engineering,ro=dancer /usr

            exportfs -u /usr

         3. 假如你的機器沒有NIS(YP server)的服務,當更改資料時記得修改

               /etc/passwd

               /etc/group

               /etc/hosts

               /etc/ethers

         4. 為你自己的network 設定security

    exportfs的語法

      /usr/etc/exportfs   [ -avu ]   [ -o option ]  [ directory ]

          -a     : 把 /etc/exports 中所有路徑export出去

          -u     : 把 export出去的路徑卸下 , 如 exportfs -u /usr

          -o option :  如 exportfs -o ro /usr ,所有人對/usr 都為read only

                       option 還有 root = hostname , access = client

                                   access = netgroup

    For example :

              exportfs -a  把exports中的路徑全部export出去

              exportfs -o access=engineering:other  /usr

                /usr 這路徑export後只有engineering and other 這兩個

                group 能夠 read & write

              exportfs -o access=oak,ro=dancer  /usr

                設定dancer 這臺client 對 /usr 為read only ,且只有

                oak這一個 group 能做read

           /etc/exports  檔的□例

    ●  syntax : directory          -option[,option]

                                     (設定兩個group能rw)

      /usr                     -access=engineering:accounting

      /home                    -access=engineering:accounting

      /var/spool/mail                -access=engineering:accounting

      /export/exec/sun3              -access=engineering:accounting

      /export/exec/sun3.sunos.4.1    -access=engineering:accounting

      /export/exec/kvm/sun3.sunos.4.1  -access=engineering:accounting

      /export/root/birch          -access=birch,root=birch

      /export/swap/birch          -access=birch,root=birch

      /export/root/oak          -access=oak,root=oak

      /export/swap/oak          -access=oak,root=oak

      /export/root/willow         -access=willow,root=willow

      /export/swap/willow         -access=willow,root=willow

      /export/root/pine           -accsee=pine,root=pine

      /export/swap/pine           -accsee=pine,root=pine

   (access=client , root=hostname 如此只有這一臺client的superuser有權力rw)

   □ Setting Up a NFS Client

        1. 宣告機器為沒有磁碟機或沒有資料的Client,在使用SunInstall之前

        2. 編輯好 /etc/fstab 這一個檔,確定要mount的路徑都在fstab中

        3. 依照fstab所設的內容,在Client上設定好Mount points

           (mount_points 就是用mkdir 設exports所輸出的路徑)

        4. 確定我們所要mount的路徑,都有出現在 /etc/exports 中

        5. 可以啟動mount去連結server上的directories   ( mount -a )

        /etc/fstab   檔的□例

    ● syntax  filesystem  directory  type  options  freq  pass

        oak:/export/root/boomer  /  nfs  rw  0  0

                                            ^^^

                                      因為檔案在server上,not on client

                                      所以client的設定為0

        oak:/export/exec/sun3           /usr  nfs  ro  0  0

        oak:/export/exec/kvm/sun3       /usr/kvm nfs ro  0  0

        oak:/usr/share                  /usr/share  nfs  ro  0  0

        oak:/home/oak           /home/oak  nfs  rw,bg  0  0

    § mount 的語法

   ● syntax : mount -t type [-rv] -o [option] server:pathname /mount_point

   MOUNT :

           mount -a     把/etc/fstab 中所列的路徑全部掛上

           mount -o ro,soft,bg dancer:/usr/local /usr/local/dancer

                  把dancer server 的/usr/local mount 到 client的

                  /usr/local/dancer 並且是read only

          -t type : 你所要mount的型別, 如 nfs or 4.2

              -r  : 所mount的路徑定為read only

              -v  : mount過程的每一個動作,都有messages 傳回到螢幕上

             hard : 重複要求,直到server回應為止,但如server一直不回應

                   the server may be down !

             soft : 當client的請求得不到回應,retry one time 後 傳回

                   error message

             bg   : 當第一次請求不成功,第二次的mount將放到背景執行

             fg   : retries mount 都一直在提示符號下進行

            intr  : 當正在進行 NFS 請求時,允許用鍵盤中斷

      mount 成功時的message

                  NFS server hostname ok

       mount fail

                NFS server hostname not responding, still trying

         . . .hostname server not reponding:  RPC: Timed out

    § UNMOUNT :

            umount mount_point

            umount -a      卸下所有已經mount上的路徑

==========================================================================

   ◎ How NFS Work ?

        當我們啟動 NFS file server 時,/etc/rc.local 會自動啟動exportfs這

    一個程式,指定可以export的檔案或目錄,而我們所能mount的也只能是其所指定

    的目錄.

   □  NFS 架設在 XDR/RPC的協定之上

     XDR : (eXternal Data Representation)  外部資料表示法

      XDR(eXternal Data Representation) 提供一種方法把資料從一種格式轉換

      成另一種標準資料格式表示法,確保在不同的電腦,作業系統及電腦語言中,所

      有資料代表的意義都是相同的

     RPC : (Remote Procedure Calls) 遠端程式呼叫

      RPC(Remote Procedure Calls) 遠端程式呼叫, 請求遠端電腦給予服務. 委

      託器(client)就會透過網路傳送RPC到遠端電腦,請求服務.

      (一般 local machine : client     remote machine : server )

   □ NFS 如何運用 RPC 傳送資料

                客戶端process               主服務端process

                 ┌————┐               ┌—————┐

                 │ 客戶端 │               │ 主服務站 │

                 │routines│               │ routines │

                 └—┬——┘               └┬————┘

     本地程式呼叫    │  《                   │      《

                 (1) │  │ (10)          (6) │      │ (5)

                     》  │                   》      │

                 ┌———┴┐               ┌————┴┐

                 │ 客戶端 │               │ 主服務端 │

                 │  stub  │               │   stub   │

                 └—┬——┘               └┬————┘

     系統呼叫        │  《                   │      《

                (2)  │  │ (9)           (7) │      │ (4)

                     》  │                   》      │

                 ┌———┴┐       (8)     ┌————┴┐

                 │network │ <——————┤ network  │

                 │routines├——————> │ routines │

                 └————┘       (3)     └—————┘

               本地系統核心     網路通訊      遠端系統核心

     (1) client 送出訊息,請求服務

     (2) client stub (客戶株) 把client 送出的引數轉換成XDR---標準資料

         格式並用系統呼叫(system call) 把訊息送到網路上

     (3) 訊息經過網路送達遠端主機系統

     (4) 遠端主機將接受到的訊息傳給server stub (服務站株)

     (5) 把XDR形式的資料,轉換成符合主機端的格式,取出client發出的服務

         請求引數,送給server

     (6) -- (10) 則是逆向而行 , server 送出服務給 client

   □ rc.local 啟動守護程式

      一個NFS server 要 inet , portmap , nfs , mount 此四個守護程式,保

   持在背景執行的狀態下才能運作. (if running NIS must add ypbind daemon)

   ◆  當啟動 NFS file server 時,

      the   /etc/rc.local script   會做如下的動作

          1. 執行exportfs , 讀取server's /etc/exports 告訴kernel

             所要輸出的file hierarchies 和 存取的許可權

              ( exportfs -a )

          2. 啟動 rpc.mountd daemon 和 nfsd daemon (通常是 8 個)

               ( rpc.mountd  -n      nfsd 8 &   echo  -n 'nfsd' )

    ◆  當啟動 NFS client 時

       rc.local  會做如下的動作

          1. 啟動 boid daemons 處理讀寫的程式

                (biod 8   echo -n ' biod' )

          2. 執行 mount -vat nfs 讀取client's /etc/fstab 並且 mount 所

             有屬於 NFS-type 的files

   □ NFS daemons (守護程式) 的功用

    nfsd, biod, rpc.mountd, inetd, portmap  都可在/usr/etc 下找到

    nfsd :   依client 對檔案系統的需求情況, 而啟動

                  " file system request daemon "

             應付client 的需求,而一般file system request daemon 的數目

             是 " 8 ", 這也就是我們在rc.local 中寫 " nfsd 8 & "的原因了

    biod :   此指令是在NFS client上用的 , 用來啟動

                 " asynchronous block I/O daemon"

             用來建立buffer cache , 處理在client上的讀寫

    mountd : mountd 是一臺 RPC server ,啟動rpc.mountd daemon後 它會讀取

            /etc/xtab 檢視哪一臺client正在mount 哪一個file system,並回

            應client 所要mount 的路徑

            (mountd處理的過程可用 showmount 來看)

    inetd : inetd (Internet services daemon) , 當系統啟動時rc.local

            會啟動inetd 讀取 inetd.conf 這一個 configuration-file ,

            讀取網路上所有 server's address , 連結啟動inetd.conf中所

            有的server , 當client 請求服務時, inetd 就會為clinet 啟動

            相關的server daemon 負責任務, 如user 用 telnet 時 , 則

            inetd 啟動telnetd 迎合user telnet 的需求, 其餘像 ftp ,

            finger , rlogin 之類的應用程式 , inetd 也都會啟動相對應的

            daemons, ftpd, fingerd, rloingd

    portmap :  portmap 是一臺server , 主要功能 轉換 TCP/IP 通訊協定的

               port number 變成 RPC program number , 因為這樣clinet才能

               做RPC calls

               一般 RPC server 是被inetd 所啟動的, 所以portmap 必須

               在inetd之前啟動, 否則無法做 RPC call

   □ NFS 的網路安全的

          NFS 使server上的檔案能被client所取用, 乍看之下好像server 上

      的檔案任何人都可取用沒什麼保護性, 其實不是如此的. 一開始server

      要 exportfs 之前在 /etc/exporrc 中就已經設定了檔案的使用許可權, 像

          /usr/src -access=engineering:accounting

      就是限定只有 rcgineering 和 accounting 這兩臺 client 才能 mount

      /usr/src

          /usr/src  -access=oak,root=oak

      這是說只有 oak 這臺 cilent 能 mount這一個路徑 且只有 oak client's

      superuser 才能行使 read & write 的權力

         另外管理者為了維護 exported 和 mounted 的安全, 一定要建

     立公共(public)和私人(secret)的 key(password), 然而這些安全性

     問題是要建立在 NIS (network infomation system) 上的, 有一個

     /etc/publickey檔 ,裡面記錄了public and secret keys , 而這些key

     是依照 machine_name 和 user_name ,以16 進位碼錶示出來的, 管理

     者可在有NIS的機器上用

         newkey -u username     給予user login 權力

         newkey -h hostname     造出login 此機器時所需的password

         在NFS剛安裝時  user 是 "nobody" 任何人都可以進入, 所以管理者

     一定要做修改, 以保護資料的安全.

     當使用者為 nobody 時 publickey 的內容

         netname   user's public key    : user's secret key

          nobody  3d91f44568fbbefada5a7:7675cd9b8753b5db09dabf12

        在管理者給予user權力之後, user 就可使用 chkey 修改自己的

     secret key 創造自己的帳號路口,

        willow% chkey

        Generating new key for username

        password  : 

        Sending key change request to server ...

        Done.

        willow%

         所設定的password 是使用者位於 NIS 中的加密鍵 , 當我們login 時

     NIS 就會將此密碼解開,(因為publickey中是以16進位碼記錄) , 存放到

     keyserv 里加以保管, 再將加密鍵傳給client , 當client 發出請求時 ,

     此鍵會附在每個NFS 請求上一起送到 NFS server 上, 當加密鍵和server上

     所儲存的keyserv核對無錯後, 請求就會被接受

    □ When fail to  mount server

         1. 用 rpcinfo -p server_name 去檢視此server是否存在

         2. 用 rpcinfo -u server_name mount 檢視mountd daemon 是否有

            在server上執行

         3. 假如server 都沒問題,檢查 server 和 client 之間的

            Ethernet connetions

         4. 在client 上 用 ps ax 看portmap and several biod daemons

            running or not !

            (rpcinfo : 用來對 RPC server 做 RPC 呼叫,回傳遠端程式呼叫

                       過程中的訊息)

       一些mount 失敗的錯誤訊息

       1.  /etc/mtab :  No such file or directory

               mtab 這一個路徑或是檔案必須存在,在mount之前

       2.  mount : ... Block device required

               遠端的機器名稱可能打錯了

       3.  mount: ... not found in /etc/fstab

               fstab 一定要存在,client 上 /etc下

       4.  ... not in hosts database

               /etc/hosts 沒有這一個 hosts database , 或是

               NIS 的daemon, ypbind 沒有在執行

       5.  Must be root to use mount

                一般都只有 root 才能mount ,所以mount之前先成

                為superuser

       6.  Stale NFS file handle

                當我們已經mount 上的file or directory  ,在server

                上突然被remove or unexport ,就會出現此訊息 () 進

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-945195/,如需轉載,請註明出處,否則將追究法律責任。