NFS(Network File System)讓 Linux 系統間透明共享文件,嵌入式開發、集群存儲、容器持久化的常用方案。本文覆蓋服務端/客戶端配置、參數詳解、驗證測試、自動掛載與故障排查。
# 檢查是否已安裝 yum list installed | grep nfs # 未安裝則 yum install -y nfs-utils rpcbind
mkdir -p /share chown nfsnobody:nfsnobody /share # 可選:設置默認所有者
/share *(sync,rw,no_root_squash)
| 參數 | 含義 |
|---|---|
/share | 共享路徑 |
* | 允許所有客戶端(可限制 IP/網段/域名) |
sync | 同步寫入內存與磁盤,數據安全優先 |
rw | 讀寫權限 |
no_root_squash | 客戶端 root 保持 root 權限(生產環境慎用,建議 all_squash+anonuid/anongid) |
192.168.1.100192.168.1.0/24 或 192.168.1.0/255.255.255.0nfs.example.com*.example.comro:只讀all_squash:所有用戶映射為匿名用戶root_squash(默認):客戶端 root 映射為匿名anonuid=65534,anongid=65534:指定匿名用戶 UID/GIDsecure(默認):限制客戶端端口 <1024insecure:允許高端口(macOS/Windows 客戶端常需)async:異步寫入,性能優但掉電丟數據風險no_subtree_check:禁用子目錄權限檢查,提升性能exportfs -ra # 重新導出所有 systemctl restart nfs-server rpcbind systemctl enable nfs-server rpcbind
exportfs -v # 輸出示例: # /share(sync,wdelay,no_root_squash,no_subtree_check,sec=sys,rw,secure,no_all_squash)
systemctl start rpcbind systemctl enable rpcbind
showmount -e 192.168.1.100 # 服務端 IP
mkdir -p /mnt/nfs mount -t nfs 192.168.1.100:/share /mnt/nfs # 或掛載本機回環測試 mount -t nfs localhost:/share /mnt/nfs
umount /mnt/nfs
192.168.1.100:/share /mnt/nfs nfs defaults,_netdev 0 0
_netdev:標識網絡設備,啟動時等待網絡就緒defaults:rw,suid,dev,exec,auto,nouser,asyncmount | grep nfs 查看當前 NFS 掛載nfsstat -c 客戶端 RPC 統計rpcinfo -p server_ip 檢查 RPC 服務註冊mountd 動態端口(可通過 /etc/sysconfig/nfs 固定)all_squash,anonuid=1000,anongid=1000 映射到專用服務用戶。sync,非關鍵可用 async 提升性能。autofs 實現按需掛載,減少長連接開銷。nfsstat 關鍵指標:retrans、badcalls、timeout,及早發現網絡/性能問題。