NFS (Network File System) 作為 Linux/Unix 標準分布式檔案系統,廣泛用於共享存儲、家目錄、CI/CD 快取、容器持久化卷。但其「無狀態、依賴 RPC、對網路/權限敏感」的特性,導致故障類型繁雜。本文匯總 13 大類經典故障現象、根因定位邏輯、標準化解決命令與預防措施,構建 NFS 運維知識庫。
| # | 錯誤現象 | 核心根因 | 關鍵解決命令 |
|---|---|---|---|
| 1 | RPC: Rpcbind failure / Timed Out | 客戶端 /etc/hosts 解析錯誤 / 服務端過載 | 修正 hosts;檢查服務端負載 |
| 2 | NFS server not responding, still trying | 網路不通 / 服務端關機 / 防火牆阻斷 | ping、rpcinfo -p server、開放 111/2049/輸出埠 |
| 3 | 客戶端開機卡住 Setting default interface for multicast | /etc/fstab 使用 fg (前景) 掛載失敗 | 改 bg (背景) 或註釋掉,待服務就緒再手動掛載 |
| 4 | NFS: Service not responding | 運行等級非 3 / nfsd 進程不存在 | who -r、init 3、systemctl start nfs-server |
| 5 | RPC: Program not registered | 運行等級非 3 / mountd 未啟動 / /etc/exports 格式錯誤 | systemctl restart nfs-server、檢查 exports 語法 |
| 6 | Stale NFS file handle | 服務端共享目錄移動/重建,客戶端句柄失效 | 客戶端 umount → mount 重新掛載 |
| 7 | RPC: Unknown host | /etc/hosts 或 DNS 解析錯誤 | 修正主機名/IP 映射 |
| 8 | mount point does not exist | 客戶端掛載點目錄未建立 | mkdir -p /mount/point |
| 9 | No such file or directory | 服務端匯出路徑不存在 / 拼寫錯誤 | 檢查 /etc/exports 路徑、建立目錄 |
| 10 | No route to host | 防火牆 (iptables/firewalld/硬體) 阻斷 111/2049/動態埠 | 開放埠、檢查 TCP_Wrappers (/etc/hosts.allow/deny) |
| 11 | Not owner (Solaris 10 掛載低版本 NFS) | 協議版本不匹配 | 加掛載選項 -o vers=3 |
| 12 | RPC: Program not registered & retrying | 服務端 NFS 服務未啟動 | systemctl restart nfs-server (Linux) / /etc/init.d/nfs restart (Solaris) |
| 13 | can't contact portmapper: Connection refused | 服務端 rpcbind/portmap 未啟動 | systemctl start rpcbind (RHEL 6: portmap) |
systemctl status nfs-server rpcbind / rpcinfo -p localhost 驗證 nfs (100003)、mountd (100005)、nlockmgr (100021) 註冊。ping server、telnet server 111、telnet server 2049、rpcinfo -p server。cat /etc/exports → exportfs -v 驗證生效;客戶端 mount -t nfs server:/share /mnt 或 /etc/fstab。| 服務 | 埠 | 協議 | 說明 |
|---|---|---|---|
rpcbind (portmap) | 111 | TCP/UDP | RPC 端口映射器,必須最先啟動 |
nfsd | 2049 | TCP/UDP | NFS 核心協議 |
mountd | 動態 (預設) | TCP/UDP | 掛載請求處理,建議固定 mountd_port=20048 |
statd | 動態 | TCP/UDP | 鎖狀態通知,建議固定 statd_port=32803 |
lockd | 動態 (核心) | TCP/UDP | 檔案鎖管理,建議固定 lockd_port=32804 |
防火牆開放範例:
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=rpc-bind
firewall-cmd --permanent --add-service=mountd
firewall-cmd --reload
# 或 iptables
iptables -A INPUT -p tcp -m multiport --dports 111,2049,20048,32803,32804 -j ACCEPT
iptables -A INPUT -p udp -m multiport --dports 111,2049,20048,32803,32804 -j ACCEPT
# 格式: <匯出路徑> <客戶端規格>(選項,選項...)
/data/nfs 192.168.1.0/24(rw,sync,no_root_squash,no_subtree_check,fsid=0)
/data/www 10.0.0.0/8(ro,sync,root_squash,no_subtree_check)
/home *(rw,sync,no_root_squash,no_all_squash,sec=krb5p)
sync:同步寫入(資料安全優先),async 高效能但掉電丟數據風險。no_root_squash:允許客戶端 root 保持 root 權限(需信任環境)。no_subtree_check:禁用子樹檢查(大目錄匯出提升效能)。fsid=0:NFSv4 根匯出標識。sec=krb5p:Kerberos 隱私保護(企業級安全)。# /etc/fstab
server:/data/nfs /mnt/nfs nfs rw,hard,intr,noatime,rsize=1048576,wsize=1048576,tcp,vers=4.2,_netdev 0 0
hard:無限重試(資料一致性),soft 超時報錯(適用非關鍵數據)。intr:允許信號中斷掛起進程(避免 D 狀態不可殺)。rsize/wsize=1M:最大傳輸塊 (NFSv3/4 支援),提升吞吐。tcp:強制 TCP(穩定可靠),udp 低延遲但不可靠。vers=4.2:指定最高協議版本(支援孔洞打孔、複製、ACL)。_netdev:網路裝置就緒後掛載(systemd 依賴排序)。# /etc/sysconfig/nfs (RHEL) / /etc/default/nfs-kernel-server (Debian)
RPCNFSDCOUNT=160 # nfsd 執行緒數 (核心數 × 6-8)
RPCMOUNTDOPTS="--manage-gids -p 20048"
STATD_PORT=32803
LOCKD_TCPPORT=32804
LOCKD_UDPPORT=32804
# /etc/modprobe.d/lockd.conf
options lockd nlm_udpport=32804 nlm_tcpport=32804
# /etc/sysctl.d/nfs.conf
fs.nfs.nlm_tcpport = 32804
fs.nfs.nlm_udpport = 32804
| 工具 | 用途 | 關鍵命令 |
|---|---|---|
showmount | 查看服務端匯出/客戶端掛載 | showmount -e server、showmount -a server |
rpcinfo | RPC 服務註冊探測 | rpcinfo -p server、rpcinfo -t server nfs |
nfsstat | NFS 客戶端/服務端統計 | nfsstat -s (server)、nfsstat -c (client)、nfsstat -m (mount) |
mountstats | 掛載點詳細統計 | mountstats /mnt/nfs |
tcpdump | 封包級分析 | tcpdump -i eth0 port 2049 -nn -v |
iostat | 磁碟 IO 關聯 | iostat -xmt 1 |
/etc/hosts 雙向解析正確。/etc/exports 語法驗證 exportfs -v 無報錯。/etc/fstab 使用 hard,intr,_netdev、大塊傳輸、NFSv4.2。RPCNFSDCOUNT 按核心數 × 6-8 配置,固定輔助服務埠。no_root_squash 僅限信任子網。nfsstat -s 重傳率、mountstats 平均延遲、rpcinfo 服務存活。建立「故障現象 → 根因 → 標準命令 → 預防措施」的 NFS 知識庫,並納入自動化巡檢與變更管理流程,讓 NFS 共享存儲真正成為「配置一次、長期穩定、故障秒級定位」的基礎設施基石。