NFS與AutoNFS實例
NFS概述:
NFS,是Network File System的簡寫,即網絡文件系統。網絡文件系統是FreeBSD支持的文件系統中的一種,也被稱為NFS. NFS允許一個系統在網絡上與他人共享目錄和文件。通過使用NFS,用戶和程序可以像訪問本地文件一樣訪問遠端系統上的文件。
安裝NFS(在sishen_63上面)
[[email protected]_63 ~]# yum install -y nfs-utils
啟動NFS
[[email protected]_63 ~]# service nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
查看端口
[[email protected]_63 ~]# netstat -antup | grep 2049
tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN -
tcp 0 0 :::2049 :::* LISTEN -
udp 0 0 0.0.0.0:2049 0.0.0.0:* -
udp 0 0 :::2049 :::* -
設置開機自動起的話使用chkconfig nfs on這條命令。
設置測試目錄
[[email protected]_63 ~]# vim /etc/exports
/tmp *(rw)
[[email protected]_63 ~]# service nfs restart
客戶端查看(在sishen_64上)
測試客戶端訪問權限
[[email protected]_64 opt]# touch sishen_64.txt 註意看紅色字體
[[email protected]_64 opt]# ll
total 70636
drwx------ 2 root root 4096 Sep 12 18:07 keyring-GhLJxt
drwx------ 2 gdm gdm 4096 Sep 12 18:07 orbit-gdm
drwx------ 2 root root 4096 Sep 12 18:11 orbit-root
drwx------ 2 root root 4096 Sep 12 18:07 pulse-GF9xIQtH3K7r
drwx------ 2 gdm gdm 4096 Sep 12 18:08 pulse-KrvunjvNzMZC
-rw-r--r-- 1 nfsnobody nfsnobody 0 Sep 12 20:11 sishen_64.txt
-rw-r--r-- 1 root root 10795 Sep 12 18:06 vgauthsvclog.txt.0
drwx------ 2 root root 4096 Sep 12 15:47 virtual-root.GbNb3n
drwx------ 2 root root 4096 Sep 12 18:07 virtual-root.GMsPxq
drwx------ 2 root root 4096 Sep 12 15:04 virtual-root.moiJHi
drwxrwxrwt 2 root root 4096 Jun 19 2016 VMwareDnD
drwx------ 2 root root 4096 Sep 12 18:07 vmware-root
-r--r--r-- 1 root root 72270857 Jun 20 2016 VMwareTools-10.0.5-3228253.tar.gz
其他測試
創建共享目錄
[[email protected]_63 ~]# mkdir -p /share/{aa,bb,cc,dd,ee,ff}
[[email protected]_63 ~]# ll /share/
total 24
drwxr-xr-x 2 root root 4096 Sep 12 22:58 aa
drwxr-xr-x 2 root root 4096 Sep 12 22:58 bb
drwxr-xr-x 2 root root 4096 Sep 12 22:58 cc
drwxr-xr-x 2 root root 4096 Sep 12 22:58 dd
drwxr-xr-x 2 root root 4096 Sep 12 22:58 ee
drwxr-xr-x 2 root root 4096 Sep 12 23:00 ff
修改配置文件
/tmp *(rw)
/share/aa *(rw,no_root_squash)
/share/bb 192.168.1.0/24(rw,sync)
/share/cc 192.168.1.64(ro)
/share/dd *.baidu.com(rw,all_squash,anonuid=500,anongid=500)
/share/ee 192.168.2.0/24(async) 192.168.3.0/24(rw)
/share/ff *(rw,root_squash)
sync/async 數據同步寫入硬盤/不同步寫入在內存中緩存
root_squash 壓制root,如果用root登錄,使其身份自動切換成nfsnobody
no_root_squash 不壓制root ,如果是root登錄nfs,身份就是root
更改/share目錄權限
[[email protected]_63 ~]# ll -d /share/
drwxr-xr-x 8 root root 4096 Sep 12 23:00 /share/
[[email protected]_63 ~]# chmod 777 -R /share/
[[email protected]_63 ~]# ll -d /share/
drwxrwxrwx 8 root root 4096 Sep 12 23:00 /share/
重啟nfs服務
[[email protected]_63 ~]# service nfs restart
客戶端驗證
[[email protected]_64 ~]# showmount -e 192.168.1.63
Export list for 192.168.1.63:
/share/ff *
/share/aa *
/tmp *
/share/dd *.baidu.com
/share/ee 192.168.3.0/24,192.168.2.0/24
/share/cc 192.168.1.64
/share/bb 192.168.1.0/24
被拒絕了,沒有權限
去服務端更改配置文件,然後重啟nfs服務後再來測試
修改內容
將 /share/dd *.baidu.com(rw,all_squash,anonuid=500,anongid=500)
改為 /share/dd *(rw,all_squash,anonuid=500,anongid=500)
掛載成功!
測試文件權限
掛載其他目錄,ee目錄由於服務端限制,所以掛載會出錯。
查看掛載目錄
添加普通用戶
使用root和sishen用戶登錄,創建並對比文件權限
因為aa目錄不壓制root用戶,所以aa.txt的所有者還是root,但是註意普通用戶是nobody。
切換到cc目錄下,使用sishen用戶創建文件失敗,因為該目錄為只讀
切換到root用戶去創建,仍然失敗,因為該目錄權限對所有用戶為只讀
切換到ff目錄下,分別使用root和sishen用戶創建文件,並查看文件權限
因為ff目錄壓制root,所以root身份映射成nfsnobody。
配置自動掛載
需要在客戶端編輯/etc/fstab文件,內容格式為:
192.168.1.63:/share/aa /share/aa nfs defaults 0 0
保存退出,重啟客戶端操作系統驗證。
AutoNFS
[[email protected]_63 ~]# yum install -y autofs
客戶端創建測試共享測試目錄
[[email protected]_64 ~]# mkdir /tmp/{a,root_squash,no_root_squash,all_squash}
服務端修改配置文件,大約在第7行左右,/misc下面添加如下內容
[[email protected]_63 ~]# vim /etc/auto.master
/tmp/a /etc/auto.nfs --timeout=60
/tmp/all_squash /etc/auto.nfs --time=60
/tmp/root_squash /etc/auto.nfs --time=60
/tmp/no_root_squash /etc/auto.nfs --time=60
註意:紅色字體是掛載到本地時目錄的名字
[[email protected]_63 ~]# vim /etc/auto.nfs
/tmp/a -fstype=nfs 192.168.1.64:/tmp/a
root_squash -fstype=nfs 192.168.1.64:/tmp/root_squash
no_root_squash -fstype=nfs 192.168.1.64:/tmp/no_root_squash
all_squash -fstype=nfs 192.168.1.64:/tmp/all_squash
註意:紅色字體名字自己定義即可,沒有特殊要求
服務端重啟autofs服務
[[email protected]_63 ~]# service autofs restart
Stopping automount: [ OK ]
Starting automount: [ OK ]
首次訪問共享目錄,需要手動切進去,之後就不需要了,如下
[[email protected]_63 ~]# cd /tmp/a
[[email protected]_63 a]# touch sishen_63.test #創建文件失敗,因為沒有給權限
touch: cannot touch `sishen_63.test‘: Permission denied
[[email protected]_63 ~]# cd /tmp/all_squash
[[email protected]_63 all_squash]# cd ..
[[email protected]_63 tmp]# cd root_squash
為了驗證共享目錄是sishen_64上的,我們可以停掉autofs服務,查看。
[[email protected]_63 tmp]# service autofs stop
Stopping automount: [ OK ]
[[email protected]_63 tmp]# cd a
-bash: cd: a: No such file or directory
[[email protected]_63 tmp]# cd root_squash
-bash: cd: root_squash: No such file or directory
由此可見,以上4個目錄均是sishen_64上的。
到此,搞一小段,之後會做出其他相應的服務。歡迎各位讀者大佬批評指正~~~
NFS與AutoNFS實例