NFS (Network File System) 為 Sun 公司基於 TCP/IP、RPC (Remote Procedure Call)、XDR (External Data Representation) 開發,支援異構作業系統間檔案共享。
NFS 伺服器:共享本地資源(執行 mountd、nfsd 守護程序)
NFS 客戶端:存取共享資源(執行 statd、lockd 守護程序)
實務中單一主機常同時扮演雙重角色,互相共享存取。
| 角色 | 守護程序 | 關鍵檔案 | 管理指令 |
|---|---|---|---|
| 伺服器 | mountd, nfsd | /etc/dfs/dfstab, /etc/dfs/sharetab, /etc/rmtab, /etc/rc3.d/S15nfs.server | share, unshare, shareall, unshareall |
| 客戶端 | statd, lockd | /etc/vfstab, /etc/mnttab | mount, umount, mountall, umountall |
mount 請求 → 伺服器 mountdmountd 驗證後回傳資源資訊(磁碟裝置號、inode 號等唯一編碼)/etc/mnttabmountd 在 /etc/rmtab 記錄:remote hostname: local filenamemountd 重啟或收到卸載請求時更新 /etc/rmtabshare -F nfs -o rw=client1:client2,ro=@192.168.1.0/24:host3,root=host4 -d "描述" /path/to/resource
rw:讀寫(預設)rw=client1:client2:指定主機讀寫ro:唯讀ro=@network/host:指定網段/主機唯讀root=host:授予指定主機 root 權限(優先級低於伺服器本地權限)取消共享:unshare /path/to/resource
/etc/dfs/dfstab,每行一條 share 指令/etc/init.d/nfs.server start 或進入 run level 3 自動生效shareall(共享所有 dfstab 資源)、unsharealldfshares:顯示可掛載資源(資源、伺服器、權限、傳輸埠)dfmounts:顯示資源被利用狀況(資源、目錄、客戶端)nfsstat:完整 NFS 狀態統計showmount -e server_host # 查看可掛載資源
showmount -a # 顯示已使用資源與來源
showmount -d # 顯示已遠端掛載資源
mkdir /local/mount/point
mount server:/remote/path /local/mount/point # 預設 rw
mount -o ro server:/remote/path /local/mount/point # 唯讀
umount server:/remote/path /local/mount/point
編輯 /etc/vfstab:
server:/remote/path - /local/mount/point nfs - yes soft,bg
soft:掛載失敗報錯不掛起bg:背景掛載,不阻塞開機流程mountall:掛載所有 vfstab 中 mount at boot=yes 資源mountall -r:僅影響遠端檔案mountall -l:僅影響本地檔案umountall:卸載所有已掛載資源(排除 /、/proc 等關鍵檔案系統)share 設定(本地唯讀則 root 選項也無法寫入)/etc/rmtab 記錄非權威,僅供參考追蹤automount (autofs) 實現按需掛載