一,用rpm檢查是否有nfs-utils的包已安裝
[root@localhost liuhongdi]# rpm -qa | grep nfs-utils nfs-utils-2.3.3-26.el8.x86_64
說明:架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,如果沒有安裝,在centos平台如何安裝nfs?
[root@localhost liuhongdi]# dnf install nfs-utils
三,如何啟動nfs?
[root@localhost liuhongdi]# systemctl start nfs-server
四,如何查看當前nfs服務所支持的nfs的版本?
[root@localhost liuhongdi]# cat /proc/fs/nfsd/versions -2 +3 +4 +4.1 +4.2
說明:可以看到在nfs4的大版本下nfs2已經不被支持了
五,配置要導出的文件系統:
1,創建兩個要共享的目錄:
[root@localhost data]# mkdir /data/nfs [root@localhost data]# cd /data/nfs [root@localhost nfs]# mkdir rw [root@localhost nfs]# mkdir ro
2,切換兩個目錄的owner:
[root@localhost nfs]# chown nginx.nginx * [root@localhost nfs]# ll 總用量 0 drwxr-xr-x 2 nginx nginx 6 2月 24 11:11 ro drwxr-xr-x 2 nginx nginx 6 2月 24 11:11 rw
說明:切換owner為我們要用來訪問此目錄的用戶,
因為使用nginx訪問,所以用戶是nginx
說明2:查看nginx用戶的id信息:
[root@localhost nfs]# cat /etc/passwd | grep nginx nginx:x:973:973:Nginx web server:/var/lib/nginx:/sbin/nologin
說明:973:973 分別是用戶的id和所屬組的id
3,補充說明:如何手動創建一個nfs用戶?
[root@localhost nfs]# useradd -u 1100 -s /sbin/nologin -M nfsuser [root@localhost nfs]# id nfsuser uid=1100(nfsuser) gid=1100(nfsuser) 組=1100(nfsuser)
說明:
-u : 指定用戶uid
-M: --no-create-home 不創建用戶home目錄
-s : --shell 指定用戶的shell
以上用法通過man命令可以了解到:
[root@localhost nfs]# man useradd
4,編輯配置文件:
[root@localhost srv]# vi /etc/exports
編輯的內容為:
/data/nfs/rw 192.168.3.162(rw,sync,all_squash,anonuid=973,anongid=973)
說明: /etc/exports是nfs默認的配置文件
說明:各項權限的說明:
rw:可讀寫
ro: 只讀
no_root_squash:對root用戶不壓制,如果客戶端以root用戶寫入,在服務端都映射為服務端的root用戶
root_squash: nfs服務:默認情況使用的是相反參數root_squash,
如果客戶端是用戶root操作,會被壓制成nobody用戶
all_squash: 不管客戶端的使用nfs的用戶是誰,都會壓制成nobody用戶
insecure: 允許從客戶端過來的非授權訪問
sync: 數據同步寫入到內存和硬盤
async: 數據先寫入內存,不直接寫入到硬盤
anonuid: 指定uid的值,此uid必須存在於/etc/passwd中
anongid:指定gid的值
5,配置完成后,如何使nfs配置生效?
說明:完成設置后,使用exportfs實用程序有選擇地導出目錄,而無需重新啟動NFS服務
[root@localhost srv]# exportfs -rv exporting 192.168.3.162:/data/nfs/rw
說明:exportfs的參數:
-r: Reexport all directories: 重新導出所有目錄
-v: verbose,輸出詳情
6,如何查看當前配置為nfs共享的目錄及其狀態?
[root@localhost srv]# exportfs -v /data/nfs/rw 192.168.3.162(sync,wdelay,hide,no_subtree_check,anonuid=973,anongid=973,sec=sys,rw,secure,root_squash,all_squash)
7,在本地做掛載測試:完整過程:
[root@localhost srv]# vi /etc/exports
說明:編輯內容:
/data/nfs/rw 192.168.3.162(rw,sync,all_squash,anonuid=973,anongid=973) /data/nfs/ro 192.168.3.172(ro,sync,all_squash,anonuid=973,anongid=973)
其中:192.168.3.172是本機ip,可以掛載到本地
[root@localhost srv]# exportfs -rv exporting 192.168.3.172:/data/nfs/ro
exporting 192.168.3.162:/data/nfs/rw
[root@localhost srv]# mount 192.168.3.172:/data/nfs/ro /mnt [root@localhost mnt]# df -hT 文件系統 類型 容量 已用 可用 已用% 掛載點 devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs tmpfs 1.9G 10M 1.9G 1% /run tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/mapper/cl-root xfs 50G 6.5G 44G 13% / /dev/mapper/cl-home xfs 26G 521M 25G 3% /home /dev/sda1 ext4 976M 264M 646M 29% /boot tmpfs tmpfs 376M 1.2M 375M 1% /run/user/42 tmpfs tmpfs 376M 4.6M 372M 2% /run/user/1000 /dev/sr0 iso9660 6.7G 6.7G 0 100% /run/media/liuhongdi/CentOS-8-BaseOS-x86_64 192.168.3.172:/data/nfs/ro nfs4 50G 6.5G 44G 13% /mnt
六,客戶端測試掛載nfs
1,我們測試用的客戶端是一台 fedora 30
[root@localhost liuhongdi]# cat /etc/redhat-release Fedora release 30 (Thirty)
2,查看服務端輸出的目錄:
[root@localhost liuhongdi]# showmount -e 192.168.3.172 Export list for 192.168.3.172: /data/nfs/ro 192.168.3.172 /data/nfs/rw 192.168.3.162
3,掛載到本地目錄:
[root@localhost liuhongdi]# mount -t nfs 192.168.3.172:/data/nfs/rw /mnt [root@localhost liuhongdi]# df -hT 文件系統 類型 容量 已用 可用 已用% 掛載點 devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm tmpfs tmpfs 2.0G 1.6M 2.0G 1% /run tmpfs tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup /dev/mapper/fedora_localhost--live-root ext4 50G 22G 25G 47% / tmpfs tmpfs 2.0G 4.0K 2.0G 1% /tmp /dev/mapper/fedora_localhost--live-home ext4 25G 3.3G 20G 15% /home /dev/sda1 ext4 976M 221M 688M 25% /boot tmpfs tmpfs 391M 40K 391M 1% /run/user/1000 192.168.3.172:/data/nfs/rw nfs4 50G 6.4G 44G 13% /mnt
4,使客戶端和服務端的用戶id保持一致?
先查看本地nginx用戶的id
[root@localhost mnt]# grep nginx /etc/passwd nginx:x:979:979:Nginx web server:/var/lib/nginx:/sbin/nologin
把客戶端的nginx用戶id修改為和服務端一致:
[root@localhost mnt]# usermod -u 973 nginx [root@localhost mnt]# groupmod -g 973 nginx [root@localhost mnt]# grep nginx /etc/passwd nginx:x:973:973:Nginx web server:/var/lib/nginx:/sbin/nologin
5,在客戶端測試寫入到nfs文件系統
[root@localhost mnt]# touch e.txt [root@localhost mnt]# ll 總用量 0 -rw-r--r-- 1 root root 0 2月 24 14:03 a.txt -rw-r--r-- 1 nginx nginx 0 2月 24 14:15 b.txt -rw-r--r-- 1 nginx nginx 0 2月 24 14:18 c.txt -rw-r--r-- 1 root root 0 2月 24 14:18 d.txt -rw-r--r-- 1 nginx nginx 0 2月 24 14:21 e.txt [root@localhost mnt]# sudo -u nginx more e.txt [root@localhost mnt]# sudo -u nginx vi e.txt [root@localhost mnt]# sudo -u nginx more e.txt ccceee
說明:可以寫入,以root寫入和以nginx用戶寫入,效果一樣,
文件的owner都是nginx
而且客戶端使用客戶端的nginx用戶也可以正常的讀寫所創建的文件
6,生產環境中使用nfs4要注意的地方:
nfs的客戶端往往不止一台,應該在搭建時把使用nfs的用戶的uid/gid設置一致
避免讀寫時出現沒有權限的錯誤
七,客戶端機器啟動后能自動加載nfs文件系統:
把mount命令:例如:mount -t nfs 192.168.3.172:/data/nfs/rw /mnt 寫入到 /etc/rc.local
說明:有很多資料建議寫入到/etc/fstab,
os啟動時一般會先加載文件系統后啟動網絡,
在沒有網絡時nfs是無法加載的,總是出錯,
所以還是建議放到/etc/rc.local中
八,如何查看nfs服務的版本?
1,客戶端查看nfs服務的版本:
[sysop@webserver2 ~]$ nfsstat -m
2,服務端查看 nfs的版本
[root@loadserver ~]# nfsstat -s
3,客戶端也可以用mount查看nfs服務的版本
[sysop@webserver2 ~]$ mount -v
4,如何查看nfsstat的幫助?
[root@loadserver ~]# nfsstat --help
九,查看本地centos的版本:
[root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)
