• slider image 168
:::
條列式新聞
新聞載入中,請稍後...

3-2-3-5 NFS & Automount

 

  1. Server 端 設定
    1. 安裝 nfs server
      # apt-get install nfs-kernel-server nfs-common
    2. 修改 設定檔,限制單一 ip 存取,且只能唯讀,只有匿名權限
      # pico /etc/exports
      /ftp    163.23.xxx.xx/32(ro,all_squash)
    3. 啟動 nfs server
      # service nfs-kernel-server start
  2. Client 連結
    1. 在 /etc/fstab 寫入掛載
      # pico /etc/fstab
      192.168.5.3:/ftp    /ftp   nfs

 


 

參考原文出處:http://www.l-penguin.idv.tw/article/nfs-automount.htm

NFS & Automount

項目選擇


NFS (Network FileSystem)

在 Unix/Linux 的世界中,用來做資料交換存取最常用的方法,莫過於 NFS (Network FileSystem) 機制了,它最早是由 Sun 所提出的,而後來在 Unix/Linux 上一直被廣泛的使用。

使用 NFS,它最大的特色就是使用 RPC (Remote Procedure Call) 做 Port 對應,而做 Port 對應的工作,是使用 pormap 來做的,也就是說,一旦決定要使用 NFS 來使 Unix/Linux 之間的檔案交換,那麼就需要先啟動 portmap 來使 RPC 控制,如此 NFS 才能使用。

NFS 分享設定

首先,在做 NFS 分享前,要先編好設定檔 /etc/exports,內容非常簡單,我們就來看看以下實做吧:

[root@ldap-dns /]# vi /etc/exports
====================================
/misc 192.168.1.0/24(ro)
====================================
[root@ldap-dns /]#

好了,這樣設定完,只要是 192.168.1.0/24 的機器都可以來掛載,但是只能讀取 (Read Only) 不可寫入。

當然,如果你要開於其它權限的話,可以參考下面的參數:

  • ro
    這個分享目錄只能唯讀。
  • rw
    這個分享目錄可以讀取和寫入。
  • root_squash
    Client 若是以 root 存取,那麼會把 root 的權限改為和 anonymous 一樣。
  • no_root_squash
    Client 若是以 root 存取,那麼就不限制權限。
  • all_squash
    無若 Client 是否為 root,其權限都只跟 anonymous 一樣。

啟動 NFS 服務

首先,一定要記得啟動 NFS 之前一定要先把 portmap 啟動才行,否則會出錯:

[root@ldap-dns /]# service portmap start
Starting portmap: [ OK ]
[root@ldap-dns /]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS daemon: [ OK ]
Starting NFS mountd: [ OK ]
[root@ldap-dns /]#

Only for SuSE

若你的系統是使用 SuSE,那麼因為系統沒有 service 指令,所以無法使用 service 來啟動 portmap,但啟動方法也很簡單:

suse:/etc/init.d # /etc/init.d/portmap start
Starting RPC portmap daemon done
suse:/etc/init.d /etc/init.d/nfsserver start
Starting kernel based NFS server done
suse:/etc/init.d #

啟動之後,可以使用 showmount 看看有分享那些目錄:

[root@ldap-dns /]# showmount -e
Export list for ldap-dns.l-penguin.idv.tw:
/misc 192.168.1.0/24
[root@ldap-dns /]#[root@ldap-dns /]# showmount -e
Export list for ldap-dns.l-penguin.idv.tw:
/misc 192.168.1.0/24
[root@ldap-dns /]#

你可以使用 rpcinfo 這個工具來看看有那些服務使用 rpc 資源:

[root@ldap-dns misc]# rpcinfo -p 192.168.1.211
program vers proto port
100000 2 tcp 111 portmapper
100000 2 udp 111 portmapper
100011 1 udp 828 rquotad
100011 2 udp 828 rquotad
100011 1 tcp 831 rquotad
100011 2 tcp 831 rquotad
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100021 1 udp 1028 nlockmgr
100021 3 udp 1028 nlockmgr
100021 4 udp 1028 nlockmgr
100021 1 tcp 1037 nlockmgr
100021 3 tcp 1037 nlockmgr
100021 4 tcp 1037 nlockmgr
100005 1 udp 844 mountd
100005 1 tcp 847 mountd
100005 2 udp 844 mountd
100005 2 tcp 847 mountd
100005 3 udp 844 mountd
100005 3 tcp 847 mountd
[root@ldap-dns misc]#

Client 存取設定

若要使用網路上分享出來的 NFS 資源的話,使用 mount 指令就可以幫我們解決了,但是還有一個最重要的前提是,千萬不要忘了先把 portmap 啟動,如此才能正確的掛載:

[root@mailgw ~]# service portmap start
Starting portmap: [ OK ]
[root@mailgw ~]# mount -t nfs -o ro,soft,intr,timeo=10 192.168.1.211:/misc /mnt
[root@mailgw ~]#

上面的 option 選項裡,記得要下 "soft,intr,timeo" 來指定掛載逾時和軟性掛載,指定軟性掛載的好處是,在 NFS Server 沒有回應或關機時,避免 Client 會完全停掉而無法無應,此時就算是關機也無法解決。

當然,你可以使用 mount 指令看看目前有那些目錄被掛載起來:

[root@mailgw mnt]# mount
192.168.1.211:/misc on /mnt type nfs (ro,soft,intr,timeo=10,addr=192.168.1.211)
[root@mailgw mnt]#

上面只截取 NFS 掛載部份。

現在,就來看看 /mnt 裡,是否能看到 192.168.1.211 分享出來的 /misc 目錄:

[root@mailgw mnt]# ls -l /mnt
total 8
-rw-r--r-- 1 root root 0 Aug 17 22:38 example
drwxr-xr-x 2 root root 4096 Aug 17 22:39 java
drwxr-xr-x 2 root root 4096 Aug 17 22:39 tomcat
[root@mailgw mnt]#

當然,可想而知,你是不可以在上面建立檔案的,因為在 NFS Server 掛載的選項就是只能唯讀呀!

Automount

使用了 NFS 之後,我想大家應該會知道,若大家連線越多,那麼對 Server 的資源也就浩用越大,若是 Client 能設定成只在需要的時候才自動掛上必要的目錄,而在一段時間之後若沒有再使用就卸載,如此對於 Server 的使用也能相對的減少資源使用。

autofs 可以用來自動掛載資料,也就是我們這次的重點,而 autofs 是一個 daemon,一旦啟動後它會駐列在系統中,他會先載入設定檔然後監視指定的目錄,一旦使用者要存取所指定的目錄時就會開始自動掛載起來。以下的範列,我將示範在主機裡分享 /home 目錄可供 Client 掛載,而在 Server 的設定,完全就跟往前一樣設定好分享條件,然後啟動 NFS 就可以了。所以我們要調整的是 Client 的部份。

/etc/auto.master

auto.master 主要是設定需要監控那一個目錄,然後再指定其設定檔:

[root@mailgw ~]# vi /etc/auto.master
=====================================
/home /etc/auto.home --timeout=30
=====================================

上面的意思是,autofs 要去監控 /home 目錄,而設定檔在 /etc/auto.home 這個檔案裡,而 timeout 時間是 30 秒。

Only for SuSE

 

若你的系統是使用 SuSE,那麼請改為下面的設定:

[root@mailgw ~]# vi /etc/auto.master
=====================================
/home /etc/auto.home "--timeout 30"
=====================================

/etc/auto.home

auto.home 是由 auto.master 設定而來的,裡面設定了 /home 目錄下的掛載來源和參數:

[root@mailgw ~]# vi /etc/auto.home
=====================================
* -rw,soft,intr 192.168.1.211:/home/&
=====================================

上面的意思是,/home 目錄下的任何子目錄,其子目錄都可以讀寫,並且是軟性掛載,而來源是 192.168.1.211 的 /home 目錄。"&" 的意思是,對應到 "*" 的值,若是要進入 /home/steven 時,那麼就會自動把 192.168.1.211 的 /home/steven 掛進來。

啟動 autofs

要啟動 autofs 之前,因為我們的來源是使用 NFS,所以 portmap 是一定要先開的,再來啟動 autofs。

[root@mailgw ~]# service portmap start
Starting portmap: [ OK ]
[root@mailgw ~]# service autofs start
Starting automount: [ OK ]
[root@mailgw ~]#
Only for SuSE

 

若你的系統是使用 SuSE Linux,設定方法都是一樣的,而啟動方法則請參考下面的範例:

suse:/var/log # /etc/init.d/autofs start
Starting service automounter/usr/sbin/automount: done
suse:/var/log #

現在再來看看目前的 mount 狀態:

[root@mailgw ~]# mount
/dev/sda1 on / type ext3 (rw)
none on /proc type proc (rw)
none on /sys type sysfs (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
none on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
automount(pid2774) on /home type autofs (rw,fd=4,pgrp=2774,minproto=2,maxproto=4)
[root@mailgw ~]#

上面可以看到 automount 正在監控 /home 目錄,而且為我們還沒有進入 /home 目錄裡的任何資料夾,所以會沒有其它的 mount 資訊。

現在可以試著進入 /home/steven 這個資料夾裡:

[root@mailgw ~]# cd /home/steven
[root@mailgw steven]#

接著馬上來看看 mount 的變化:

[root@mailgw steven]# mount
/dev/sda1 on / type ext3 (rw)
none on /proc type proc (rw)
none on /sys type sysfs (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
none on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
automount(pid2774) on /home type autofs (rw,fd=4,pgrp=2774,minproto=2,maxproto=4)
192.168.1.211:/home/steven on /home/steven type nfs (rw,soft,intr,addr=192.168.1 .211)
[root@mailgw steven]#

看到了吧,最後一行正顯示著 192.168.1.211 的 /home/steven 正被我們掛載。

08/21/2005



:::
展開 | 闔起

文章類別

書籍目錄

展開 | 闔起

線上使用者

27人線上 (25人在瀏覽線上書籍)

會員: 0

訪客: 27

更多…