1 NFS服務器安裝與配置
1.1 環境信息
操作系統:centos7
內核版本:3.10.0-327.el7.x86_64
1.2 NFS安裝與配置
關閉selinux功能:
[root@10-12-35-251 ~]# setenforce 0
查看selinux狀態:
[root@10-12-35-251 ~]# sestatus
SELinux status: disabled
服務器NFS軟件包安裝:
[root@10-12-35-251 ~]# yum install -y nfs-utils
配置export配置文件:
[root@10-12-35-251 ~]# cat /etc/exports
/data/ *(rw,sync,fsid=0)
注:*代表任何IP可以訪問並掛載;rw表示可讀寫;sync表示同步寫,fsid=0表示將/data找個目錄包裝成根目錄
新建/data共享目錄:
[root@10-12-35-251 ~]# mkdir /data
1.3 啓動NFS服務
先爲rpcbind和nfs做開機啓動:
[root@10-12-35-251 ~]#systemctl enable rpcbind.service
[root@10-12-35-251 ~]#systemctl enable nfs-server.service
然後分別啓動rpcbind和nfs服務:
[root@10-12-35-251 ~]#systemctl start rpcbind.service
[root@10-12-35-251 ~]#systemctl start nfs-server.service
確認NFS服務器啓動成功:
[root@10-12-35-251 ~]#rpcinfo -p
檢查 NFS 服務器是否開放掛載我們想共享的目錄 /data:
[root@10-12-35-251 ~]#exportfs
/data <world>
2 客戶端進行NFS目錄掛載
啓動rpcbind服務:
[root@localhost ~]# systemctl start rpcbind.service
檢查共享目錄是否存在:
[root@localhost ~]# showmount -e 10.12.35.251
Export list for 10.12.35.251:
/data *
掛載NFS共享目錄:
[root@localhost ~]# mount -t nfs 10.12.35.251:/data//mnt
查看客戶端mount信息:
10.12.35.251:/data/ on /mnt type nfs(rw,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=10.12.35.251,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=10.12.35.251)
3 NFS客戶端文件寫入測試
3.1 客戶端root用戶寫測試
[root@localhost ~]# cd /mnt
[root@localhost mnt]# touch test2
touch: cannot touch test2Permissiondenied
報錯關鍵字:Permission denied
出現這個錯誤是由於NFS服務器沒有開放目錄權限導致。
查看NFS目錄權限:
[root@10-12-35-251 ~]# ll /data/
total 26688
-rw-r--r-- 1 root root 27323419 Mar 2115:03 elasticsearch-1.7.1.noarch.rpm
-rw-r--r-- 1 root root 30 Mar 21 15:19 test
drwxr-xr-x 2 root root 6 Mar 21 15:03 test1
注:可以看到此目錄只有root用戶才能寫入;
對NFS目錄賦予777權限:
[root@10-12-35-251 ~]# chmod 777 -R/data
[root@10-12-35-251 ~]# ll /data/
total 26688
-rwxrwxrwx 1 root root 27323419 Mar 2115:03 elasticsearch-1.7.1.noarch.rpm
-rwxrwxrwx 1 root root 30 Mar 21 15:19 test
drwxrwxrwx 2 root root 6 Mar 21 15:03 test1
在客戶端即可進行文件寫入:
[root@localhost mnt]# touch test2
[root@localhost mnt]# echo ooooo>> test2
[root@localhost mnt]# cat test2
ooooo
查看目錄的文件內容:
[root@localhost mnt]# ll
total 26692
-rwxrwxrwx 1 root root 27323419 Mar 21 15:03 elasticsearch-1.7.1.noarch.rpm
-rwxrwxrwx 1 root root 30 Mar 21 15:19 test
drwxrwxrwx 2 root root 6 Mar 21 15:03 test1
-rw-r--r-- 1nfsnobody nfsnobody 6 Mar 21 15:33test2
注:用root 用戶建立的文件test2,變成了nfsnobody 用戶。
NFS服務有很多默認的參數,通過NFS服務器的/var/lib/nfs/etab文件查看/data目錄完整權限設定。
[root@10-12-35-251 ~]# cat/var/lib/nfs/etab
/data *(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,fsid=0,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
默認就有sync,wdelay,hide 等等,no_root_squash 是讓root保持權限,root_squash 是把root映射成nobody,no_all_squash不讓所有用戶保持在掛載目錄中的權限。所以,root建立的文件所有者是nfsnobody。
3.2 客戶端使用普通用戶測試
[root@localhost mnt]# groupadd test
[root@localhost mnt]# useradd test -gtest
[root@localhost mnt]# su test
[test@localhost mnt]$ ls
elasticsearch-1.7.1.noarch.rpm test test1 test2
創建文件
[test@localhost mnt]$ touch testfile
寫入內容
[test@localhost mnt]$ echo"thisistestfileintheNFS" >>testfile
查看文件內容
[test@localhost mnt]$ cat testfile
thisistestfileintheNFS
查看文件權限
[test@localhost mnt]$ ll
total 26696
-rwxrwxrwx 1 root root 27323419 Mar 21 15:03 elasticsearch-1.7.1.noarch.rpm
-rwxrwxrwx 1 root root 30 Mar 21 15:19 test
drwxrwxrwx 2 root root 6 Mar 21 15:03 test1
-rw-r--r-- 1 nfsnobody nfsnobody 6 Mar 21 15:33 test2
-rw-rw-r-- 1 test test 23 Mar 21 15:40 testfile
在客戶端使用root用戶對testfile寫入內容:
[root@localhost mnt]# echo 'sdfdd'>> testfile
-bash: testfile: Permission denied
權限不夠,普通用戶寫入文件時文件權限爲用戶名與組,這也就保證了服務器的安全性。
3.3 針對客戶端root用戶權限的解決方法
連接到NFS服務器,查看NFS目錄的配置信息:
[root@10-12-35-251 ~]# exportfs -v
/data <world>(rw,wdelay,root_squash,no_subtree_check,fsid=0,sec=sys,rw,secure,root_squash,no_all_squash)
修改服務器端export配置文件:
[root@10-12-35-251 ~]# cat /etc/exports
/data/ *(rw,sync,no_root_squash,fsid=0)
注:添加no_root_squash參數,表明此時客戶端root用戶的身份等同於NFS server上面的root用戶;
再次載入服務器端NFS配置:
[root@10-12-35-251 ~]# exportfs -rv
exporting *:/data
查看現有NFS目錄權限配置:
[root@10-12-35-251 ~]# exportfs -v
/data <world>(rw,wdelay,no_root_squash,no_subtree_check,fsid=0,sec=sys,rw,secure,no_root_squash,no_all_squash)
在客戶端使用root用戶創建文件:
[root@localhost mnt]# echothisisrootuserfile >> rootfile
查看root用戶創建的文件權限:
[root@localhost mnt]# ll
total 26700
-rwxrwxrwx 1 root root 27323419 Mar 21 15:03 elasticsearch-1.7.1.noarch.rpm
-rw-r--r-- 1root root 19 Mar 21 16:02 rootfile
-rwxrwxrwx 1 root root 30 Mar 21 15:19 test
drwxrwxrwx 2 root root 6 Mar 21 15:03 test1
-rw-r--r-- 1 nfsnobody nfsnobody 6 Mar 21 15:33 test2
-rw-rw-r-- 1 test test 23 Mar 21 15:40 testfile
可以看到客戶端root用戶創建的文件已經升級爲NFS服務器端的root用戶權限。
4 擴展閱讀
4.1 關於NFS權限擴展閱讀
1. 客戶端連接時候,對普通用戶的檢查
a. 如果明確設定了普通用戶被壓縮的身份,那麼此時客戶端用戶的身份轉換爲指定用戶;
b. 如果NFS server上面有同名用戶,那麼此時客戶端登錄賬戶的身份轉換爲NFS server上面的同名用戶;
c. 如果沒有明確指定,也沒有同名用戶,那麼此時用戶身份被壓縮成nfsnobody;
2. 客戶端連接的時候,對root的檢查
a. 如果設置no_root_squash,那麼此時root用戶的身份被壓縮爲NFS server上面的root;
b. 如果設置了all_squash、anonuid、anongid,此時root 身份被壓縮爲指定用戶;
c. 如果沒有明確指定,此時root用戶被壓縮爲nfsnobody;
d. 如果同時指定no_root_squash與all_squash 用戶將被壓縮爲 nfsnobody,如果設置了anonuid、anongid將被壓縮到所指定的用戶與組;
4.2 NFS相關命令擴展
1、exportfs
如果我們在啓動了NFS之後又修改了/etc/exports,是不是還要重新啓動nfs呢?這個時候我們就可以用exportfs 命令來使改動立刻生效,該命令格式如下:
# exportfs [-aruv]
-a 全部掛載或卸載 /etc/exports中的內容
-r 重新讀取/etc/exports 中的信息,並同步更新/etc/exports、/var/lib/nfs/xtab
-u 卸載單一目錄(和-a一起使用爲卸載所有/etc/exports文件中的目錄)
-v 在export的時候,將詳細的信息輸出到屏幕上。
具體例子:
# exportfs -au 卸載所有共享目錄
# exportfs -rv 重新共享所有目錄並輸出詳細信息
2、nfsstat
查看NFS的運行狀態,對於調整NFS的運行有很大幫助。
3、rpcinfo
查看rpc執行信息,可以用於檢測rpc運行情況的工具,利用rpcinfo -p 可以查看出RPC開啓的端口所提供的程序有哪些。
4、showmount
-a 顯示已經於客戶端連接上的目錄信息
-e IP或者hostname 顯示此IP地址分享出來的目錄
5、netstat
可以查看出nfs服務開啓的端口,其中nfs 開啓的是2049,portmap 開啓的是111,其餘則是rpc開啓的。
最後注意兩點,雖然通過權限設置可以讓普通用戶訪問,但是掛載的時候默認情況下只有root可以去掛載,普通用戶可以執行sudo。
NFS server 關機的時候一點要確保NFS服務關閉,沒有客戶端處於連接狀態!通過showmount -a 可以查看,如果有的話用kill killall pkill 來結束,(-9 強制結束)