一、簡介
1. 定義
NFS (Network File System),最初由 Sun Microsystems 於1984年開發的分布式系統協議,允許客戶端上的用戶通過網絡訪問文件,其方式與訪問本地存儲的方式類似。基於 Open Network Computing Remote Procedure Call (ONC RPC) 協議,NFS 是通過 Request for Comments(RFC) 定義的開放標准,允許任何人實現該協議。
2. 版本和變化
Version | RFC | Date | Variations |
---|---|---|---|
NFSv2 | RFC 1094 | March 1989 | UDP,無狀態;32位,僅允許讀取文件的前2GB |
NFSv3 | RFC 1813 | June 1995 | 支持TCP;64位,突破2GB;異步寫入;在許多響應報文中額外增加文件屬性 |
NFSv4 | RFC 3010 | December 2000 | 集成了對文件鎖定和掛載協議的支持;增加了對強安全性(及其協商)、復合操作、客戶端緩存和國際化的支持 |
NFS 4.1 | RFC 5661 | January 2010 | 會話、目錄委托、並行NFS (pNFS) |
NFS 4.2 | RFC 7862 | November 2016 |
3. 部署說明
本文描述如何在 CentOS 7 上安裝 NFS,並在 Linux 和 Windows 下使用 NFS 客戶端進行連接。
hostname | ip | role | 描述 |
---|---|---|---|
nfs-server | 192.168.0.135 | server | Linux nfs server |
nfs-client | 192.168.0.136 | client | Linux client |
Windows | 192.168.0.120 | client | Windows client |
二、服務端
1. 關閉防火牆
# systemctl stop firewalld
# systemctl disable firewalld
2. 安裝 nfs
# yum -y install nfs-utils
3. 配置說明
通過文件 /etc/exports
來對 NFS 進行配置。
It follows the following syntax rules:
- Comments start with the hash mark (#).
- Blank lines are ignored by default.
- Each host’s options must be placed in parentheses directly after the host identifier, without any spaces separating the host and the first parenthesis.
- Each exported file system should be on its own individual line.
- A list of authorized hosts needs to be separated by space characters.
- Long lines can be wrapped with a backslash ().
NFS export default options are:
ro
: The exported file system is read-only and remote hosts cannot make any changes to the files shared on the file system. To allow hosts to make both reads and writes, specify therw
option instead.sync
: Aith this option, NFS server does not reply to requests before changes made by previous requests are written to disk. To enable asynchronous writes instead, specify the optionasync
.root_squash
: This prevents root users that connect remotely from having root privileges. Instead, the NFS server will assign them the user ID nfsnobody. This effectively “squashes” the power of the remote root user to the lowest local user, preventing possible unauthorized writes on the remote server. To disable root squashing, specifyno_root_squash
.- To squash every remote user (including root), use
all_squash
. To specify the user and group IDs that the NFS server should assign to remote users from a particular host, use theanonuid
andanongid
options.wdelay
: This reduces disk write overhead by delaying writing to the disk if it suspects another write request is imminent. This can be disabled usingno_wdelay
, when default sync is on.subtree_check
: This option enables subtree checking. It can be disabled usingno_subtree_check
.
4. 配置共享目錄
(1)修改 exports
這里將 /mnt/data 作為共享目錄,開放讀寫權限
# vi /etc/exports
/mnt/data 192.168.0.0/24(rw,no_root_squash)
注:這里的共享目錄可以使用 Ceph 塊設備掛載的文件夾,關於如何使用塊設備,請參考 塊設備快速入門
(2)使配置生效
# exportfs -r
(3)查看 exports
# exportfs -v
/mnt/data 192.168.0.0/24(rw,sync,wdelay,hide,no_subtree_check,sec=sys,secure,no_root_squash,no_all_squash)
5. 啟動服務
(1)啟動 rpcbind
//開機啟動
# systemctl enable rpcbind
//啟動
# systemctl start rpcbind
//重啟
# systemctl restart rpcbind
(2)啟動 nfs-server
//開機啟動
# systemctl enable nfs-server
//啟動
# systemctl start nfs-server
//重啟
# systemctl restart nfs-server
6. 確認啟動成功
# rpcinfo -p
# exportfs
/mnt/data 192.168.0.0/24
三、Linux 客戶端
1. 安裝 nfs
# yum -y install nfs-utils
2. 啟動 rpcbind
//開機啟動
# systemctl enable rpcbind
//啟動
# systemctl start rpcbind
//重啟
# systemctl restart rpcbind
3. 掛載
創建目錄
# mkdir /data
掛載 nfs,-o 指定版本
# mount -t nfs -o vers=3 192.168.0.135:/mnt/data /data
or
# mount -t nfs -o vers=4 192.168.0.135:/mnt/data /data
查看掛載結果
# df -hT | grep /data
192.168.0.135:/mnt/data nfs 17G 985M 17G 6% /data
or
192.168.0.135:/mnt/data nfs4 17G 985M 17G 6% /data
# dh -h
文件系統 容量 已用 可用 已用% 掛載點
192.168.0.135:/mnt/data 17G 982M 17G 6% /data
4. 自動掛載
磁盤被手動掛載之后,需要把掛載信息寫入
/etc/fstab
這個文件中,否則下次開機啟動時仍然需要重新掛載。
例如對於 NFSv3,修改 /etc/fstab
192.168.0.135:/mnt/data /data nfs defaults,vers=3 0 0
or
192.168.0.135:/mnt/data /data nfs vers=3,proto=tcp,hard,intr,rsize=32768,wsize=32768,noatime 0 0
執行掛載命令
# mount -a
查看掛載結果
# df -hT | grep /data
192.168.0.135:/mnt/data nfs 17G 985M 17G 6% /data
5. 解掛
# umount /data
四、Windows 客戶端
1. 打開NFS服務
(1)Windows 功能 - 啟用或關閉 Windows 功能
(2)通過命令提示符顯示 NFS 服務器
showmount -e 192.168.0.135
/mnt/data 192.168.0.0/24
2. 掛載
方法1:映射網絡驅動器
方法2:通過命令掛載
mount 192.168.0.135:/mnt/data Z:
3. 查看
方式1:打開我的點腦,就可以在網絡位置看到 Z:盤了
方式2:通過命令查看
mount
本地 遠程 屬性
-------------------------------------------------------------------------------
Z: \\192.168.0.135\mnt\data UID=-2, GID=-2
rsize=262144, wsize=262144
mount=soft, timeout=3.2
retry=1, locking=yes
fileaccess=755, lang=GB2312-80
casesensitive=no
sec=sys
4. 修改權限
對掛載盤進行寫操作時,提示權限不足!
如果出現這種情況,解決辦法:
(1)在運行中輸入regedit,打開注冊表編輯器。
(2)進入 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default 條目。
(3)選擇新建 QWORD,新建 AnonymousUid,AnonymousGid 兩個值,值為0。
(4)重啟 NFS 服務 或 電腦。
5. 解掛
umount Z:
參考鏈接
https://wiki.archlinux.org/index.php/NFS
https://en.wikipedia.org/wiki/Network_File_System
https://computingforgeeks.com/configure-nfsv3-and-nfsv4-on-centos-7/