nfs共享
實驗環境:
服務端:192.168.36.158
客戶端:192.168.36.159
iptables and selinux disabled
一.nfs(Net File System)簡介
NFS分爲服務器和客戶機兩部分,每個主機都有自己的內核級服務:外部數據表示(XDR,eXternal Data Representation)、遠程過程調用(RPC,Remote Procedure Call)、I/O監控程序和鎖監控程序。每個主機還有自己的用戶級服務。內核級服務和用戶級服務都依賴於主機的功能:NFS客戶機或者是NFS服務器。當然,還要依賴於每個主機使用的不同功能的配置文件(如果是服務器,則用的是/etc/exports配置文件,如果是客戶機,則用的是/etc/fstab配置文件)。如果一臺主機既是服務器又是客戶機,那麼它需要運行兩個部分的服務。
在服務器端,portmap、 mountd、 nfsd三個監控程序將在後臺運行。portmap監控程序用來註冊基於rpc的服務。當一個RPC的監控程序啓動的時候,它告訴portmap監控程序它在哪一個端口進行偵聽,並且它在進行什麼樣的RPC服務。當一個客戶機向服務器提出一個RPC請求,那麼它就會和portmap監控程序取得聯繫以確定RPC消息應該發往的端口號。而Mountd監控程序的功能是來讀取服務器端的/etc/exportfs文件並且創建一個將服務器的本地文件系統導出的主機和網絡列表,因而客戶機的掛接(mount)請求都被定位到mountd監控程序(daemon)。當驗證了服務器確實具有掛接所請求的文件系統的權限以後,mountd爲請求的掛接點返回一個文件句柄。而nfsd監控程序則被服務器用來處理客戶機端發過來的請求,由於服務器需要同時處理多個客戶機的請求,所以在缺省情況下,在Linux當中將會自動啓動八個nfsd線程。當然,如果NFS服務器特別忙的時候,系統有可能根據實際情況啓動三十個線程。
二.共享資源
/etc/exports參數詳解
rw 可讀寫的權限
ro 只讀的權限
no_root_squash 登入到NFS主機的用戶如果是ROOT用戶,他就擁有ROOT的權限
root_squash 在登入NFS主機使用目錄的使用者如果是root時,那麼這個使用者的權限將被壓縮成爲匿名使用者,通常他的UID與GID都會變成nobody那個身份
all_squash 不管登陸NFS主機的用戶是什麼都會被重新設定爲nobody
anonuid 將登入NFS主機的用戶都設定成指定的userid,此ID必須存在於/etc/passwd中
anongid 同anonuid,但是變成groupID就是了
sync 資料同步寫入存儲器中
async 資料會先暫時存放在內存中,不會直接寫入硬盤
insecure 允許從這臺機器過來的非授權訪問
exportfs[-aruv] 參數
a 全部掛載(或卸載)/etc/exports檔案內的設定
r 重新掛載/etc/exports 裏面的設定,也同步的更新/etc/exports和/var/lib/nfs/xtab裏面的內容
u 卸載某一目錄
v 在export的時候,將分享的目錄顯示到熒屏上
#exportfs -rv 重新export一次
#exportfs -au 全部卸載
服務端
# yum install -y nfs nfs-utils rpcbind
# /etc/init.d/rpcbind start
# /etc/init.d/nfs start
# mkdir /test
# cd /test/
# touch file{1..3}
# vim /etc/exports
/test 192.168.36.159(rw)
# exportfs -rv
# showmount -e localhost
Export list for localhost:
/test 192.168.36.159
客戶端
# yum install -y nfs nfs-utils rpcbind
# showmount -e 192.168.36.158
Export list for 192.168.36.158:
/test 192.168.36.159
# mount 192.168.36.158:/test /mnt/
# df
192.168.36.158:/test 18208256 2638592 14638080 16% /mnt
cd /mnt/
# ls
file1 file2 file3
# rm -f file1
rm: cannot remove `file1': Permission denied
# touch file4
touch: cannot touch `file4': Permission denied
服務端
# ls -ld .
drwxr-xr-x. 2 root root 4096 Aug 19 04:30 .
# chmod 1777 .
# ls -ld .
drwxrwxrwt. 2 root root 4096 Aug 19 04:30 .
客戶端
# touch file4
# ll
total 0
-rw-r--r--. 1 root root 0 Aug 19 04:30 file1
-rw-r--r--. 1 root root 0 Aug 19 04:30 file2
-rw-r--r--. 1 root root 0 Aug 19 04:30 file3
-rw-r--r--. 1 nfsnobody nfsnobody 0 Aug 19 04:37 file4
# rm -f file4
服務端
/test 192.168.36.159(rw,no_root_squash)
# exportfs -rv
exporting 192.168.36.159:/test
客戶端
在掛載目錄mnt下
# touch file4
# ll
total 0
-rw-r--r--. 1 root root 0 Aug 19 04:30 file1
-rw-r--r--. 1 root root 0 Aug 19 04:30 file2
-rw-r--r--. 1 root root 0 Aug 19 04:30 file3
-rw-r--r--. 1 root root 0 Aug 19 04:52 file4
二.多個共享資源
服務端
根目錄下創建目錄/public和/public2
[root@test1 public]# mkdir /public2
[root@test1 public]# cd ../public2
[root@test1 public2]# touch file{2..5}
/public 192.168.36.159(rw,no_root_squash)
/public2 192.168.36.159(ro,sync)
[root@test1 ~]# exportfs -rv
exporting 192.168.36.159:/public2
exporting 192.168.36.159:/public
客戶端
[root@test1 test]# showmount -e 192.168.36.158
Export list for 192.168.36.158:
/public2 192.168.36.159
/public 192.168.36.159
[root@test1 ~]# umount /mnt/
[root@test1 ~]# mount 192.168.36.158:/ /mnt/
192.168.36.158:/ 18208256 2780544 14496128 17% /mnt
[root@test1 ~]# cd /mnt/
[root@test1 mnt]# ls
public public2
[root@test1 mnt]# cd public
[root@test1 public]# ls
test test2
[root@test1 public2]# touch file1
touch: cannot touch `file1': Read-only file system
[root@test1 public2]# rm -f file2
rm: cannot remove `file2': Read-only file system