1.NFS服務端安裝配置
①在服務器端安裝nfs-utils以及rpcbind包
[root@test_01 ~]# yum install -y nfs-utils rpcbind
②在客戶端安裝nfs-utils包
[root@test_02 ~]# yum install -y nfs-utils
③編輯配置文件/etc/exports,並在其中加入如下內容
配置文件內由空格分隔開成兩端內容,第一段表示需要共享出去的路徑
第二段表示客戶端縮在網段以及相關權限設置
/home/nfstestdir 192.168.231.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
NFS配置選項解釋:
rw/ro:讀寫/只讀
sync:同步模式,內存數據實時寫入磁盤
async:非同步模式,相隔固定時間將數據從內存寫入磁盤
no_root_squash :客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大
root_squash:與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶
all_squash:客戶端上所有用戶在使用NFS共享目錄時都被限定爲一個普通用戶
anonuid/anongid:和上面幾個選項搭配使用,定義被限定用戶的uid和gid
④保存配置後,創建/home/nfstestdir文件夾,並將權限設置爲777
/home/nfstestdir
⑤啓動nfs服務
[root@test_01 ~]# systemctl start nfs [root@test_01 ~]# ps aux |grep nfs root 11813 0.0 0.0 0 0 ? S< 19:12 0:00 nfsd4_callbacks] root 11819 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11820 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11821 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11822 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11823 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11824 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11825 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11826 0.0 0.0 0 0 ? S 19:12 0:00 nfsd] root 11830 0.0 0.0 112680 976 pts/0 S+ 19:12 0:00 grep --color=auto nfs
⑥如果需要nfs服務開機啓動,則需要執行如下命令
[root@test_01 ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
⑦在服務端查看掛載的目錄
[root@test_01 ~]# showmount -e 192.168.231.128 Export list for 192.168.231.128: /home/nfstestdir 192.168.231.0/24
⑧將nfs共享文件夾掛載至本地/mnt/目錄
[root@test_01 ~]# mount -t nfs 192.168.231.128:/home/nfstestdir/ /mnt [root@test_01 ~]# df -h 文件系統 容量 已用 可用 已用% 掛載點 /dev/sda3 28G 4.3G 24G 16% / devtmpfs 903M 0 903M 0% /dev tmpfs 912M 0 912M 0% /dev/shm tmpfs 912M 8.7M 904M 1% /run tmpfs 912M 0 912M 0% /sys/fs/cgroup /dev/sda1 197M 109M 88M 56% /boot tmpfs 183M 0 183M 0% /run/user/0 192.168.231.128:/home/nfstestdir 28G 4.3G 24G 16% /mnt
2.exportfs命令使用方法
exportfs命令用於管理NFS共享的文件系統列表。
選項:
-a 打開或取消所有目錄共享。
-o options,...指定一列共享選項,與 exports(5) 中講到的類似。
-i 忽略 /etc/exports 文件,從而只使用默認的和命令行指定的選項。
-r 重新共享所有目錄。它使 /var/lib/nfs/xtab 和 /etc/exports 同步。 它將 /etc/exports 中已刪除的條目從 /var/lib/nfs/xtab 中刪除,將內核共享表中任何不再有效的條目移除。
-u 取消一個或多個目錄的共享。
-f 在“新”模式下,刷新內核共享表之外的任何東西。 任何活動的客戶程序將在它們的下次請求中得到 mountd添加的新的共享條目。
-v 輸出詳細信息。當共享或者取消共享時,顯示在做什麼。 顯示當前共享列表的時候,同時顯示共享的選項。
例:
①編輯配置文件/etc/exports,加入新的共享文件夾
/home/nfstestdir 192.168.231.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) /tmp 192.168.231.129(rw,sync,no_root_squash,anonuid=1000,anogid=1000)
②在服務器端使用exportfs命令重新共享目錄
[root@test_01 ~]# exportfs -arv exporting 192.168.231.129:/tmp exporting 192.168.231.0/24:/home/nfstestdir
③在客戶端查看可用共享目錄
[root@test_02 ~]# showmount -e 192.168.231.128 Export list for 192.168.231.128: /home/nfstestdir 192.168.231.0/24 /tmp 192.168.231.129
※在NFS 4版本中有可能會出現掛載目錄下文件的屬主shu屬組爲nobody的情況。
針對這一狀況,在掛載目錄的時候,加上-o選項並使用參數?nfsvers=3來指定nfs版本爲3即可解決