1、NFS 介紹
NFS 是 Network FileSystem 的縮寫,顧名思義就是網(wǎng)絡(luò)文件存儲(chǔ)系統(tǒng),它最早是由 Sun 公司發(fā)展出來(lái)的,也是 FreeBSD 支持的文件系統(tǒng)中的一個(gè),它允許網(wǎng)絡(luò)中的計(jì)算機(jī)之間通過(guò) TCP/IP 網(wǎng)絡(luò)共享資源。通過(guò) NFS,我們本地 NFS 的客戶端應(yīng)用可以透明地讀寫位于服務(wù)端 NFS 服務(wù)器上的文件,就像訪問本地文件一樣方便。簡(jiǎn)單的理解,NFS 就是可以透過(guò)網(wǎng)絡(luò),讓不同的主機(jī)、不同的操作系統(tǒng)可以共享存儲(chǔ)的服務(wù)。 NFS 在文件傳送或信息傳送過(guò)程中依賴于 RPC(Remote Procedure
Call) 協(xié)議,即遠(yuǎn)程過(guò)程調(diào)用, NFS 的各項(xiàng)功能都必須要向 RPC 來(lái)注冊(cè),如此一來(lái) RPC 才能了解 NFS 這個(gè)服務(wù)的各項(xiàng)功能 Port、PID、NFS 在服務(wù)器所監(jiān)聽的 IP 等,而客戶端才能夠透過(guò) RPC 的詢問找到正確對(duì)應(yīng)的端口,所以,NFS 必須要有 RPC存在時(shí)才能成功的提供服務(wù),簡(jiǎn)單的理解二者關(guān)系:NFS是 一個(gè)文件存儲(chǔ)系統(tǒng),而 RPC 是負(fù)責(zé)信息的傳輸。
2、環(huán)境、軟件準(zhǔn)備
本次搭建的環(huán)境,是在CentOS7.6 X64系統(tǒng)上來(lái)執(zhí)行操作,以下是安裝的軟件及版本:
[if !supportLists]·???????[endif]rpcbind: 0.2.0-38.el7.x86_64
[if !supportLists]·???????[endif]nfs-utils: 1.3.0-0.54.el7.x86_64
3、NFS 服務(wù)安裝
通過(guò)上邊簡(jiǎn)要的介紹,我們知道 NFS 服務(wù)需要依賴 RPC 服務(wù),所以這里 NFS 服務(wù)端需要安裝 rpcbind 和 nfs-utils,客戶端只需要安裝 nfs-utils 即可,由于我選用的為 CentOS 系統(tǒng),所以可以使用 yum 快速的安裝。首先,確認(rèn)下服務(wù)端系統(tǒng)是否已安裝 NFS。
$ rpm -qa nfs-utilsrpcbind
[if !supportLists]·???????[endif]nfs-utils-1.3.0-0.54.el7.x86_64
[if !supportLists]·???????[endif]rpcbind-0.2.0-38.el7.x86_64
注意:這里表示已經(jīng)安裝過(guò),若為空,則說(shuō)明未安裝。參照以下命令安裝:
[if !supportLists]·???????[endif]服務(wù)端 $ yum install -y nfs-utils rpcbind
[if !supportLists]·???????[endif]客戶端 $ yum install -y nfs-utils
4、NFS 配置及使用
我們?cè)诜?wù)端創(chuàng)建一個(gè)共享目錄/data/share ,作為客戶端掛載的遠(yuǎn)端入口,然后設(shè)置權(quán)限。
$ mkdir -p /data/share
$ chmod 666 /data/share
修改 NFS 配置文件 /etc/exports ,添加共享參數(shù)配置。
$ vim /etc/exports
data/share 192.168.1.0/24(rw,sync,insecure, no_root_squash)
此處,我配置了將 /data/share 文件目錄設(shè)置為允許 IP 為該 192.168.1.0/24區(qū)間的客戶端掛載,如果客戶端 IP 不在該區(qū)間也想要掛載的話,可以設(shè)置 IP區(qū)間更大或者設(shè)置為 * 即允許所有客戶端掛載,例如:/home*(ro,sync,insecure,no_root_squash) 設(shè)置 /home 目錄允許所有客戶端只讀掛載。說(shuō)明一下,這里配置后邊有很多參數(shù),每個(gè)參數(shù)有不同的含義,具體可以參考下邊:
參數(shù)說(shuō)明
ro 只讀訪問
rw
讀寫訪問
sync
所有數(shù)據(jù)在請(qǐng)求時(shí)寫入共享
async nfs
在寫入數(shù)據(jù)前可以響應(yīng)請(qǐng)求
secure nfs
通過(guò) 1024 以下的安全 TCP/IP 端口發(fā)送
insecure nfs
通過(guò) 1024 以上的端口發(fā)送
wdelay
如果多個(gè)用戶要寫入 nfs 目錄,則歸組寫入(默認(rèn))
no_wdelay
如果多個(gè)用戶要寫入 nfs 目錄,則立即寫入,當(dāng)使用 async時(shí),無(wú)需此設(shè)置
hide
在 nfs 共享目錄中不共享其子目錄
no_hide
共享 nfs 目錄的子目錄
subtree_check
如果共享 /usr/bin 之類的子目錄時(shí),強(qiáng)制 nfs 檢查父目錄的權(quán)限(默認(rèn))
no_subtree_check
不檢查父目錄權(quán)限
all_squash
共享文件的 UID 和 GID 映射匿名用戶 anonymous,適合公用目錄
no_all_squash
保留共享文件的 UID 和 GID(默認(rèn))
root_squash root
用戶的所有請(qǐng)求映射成如 anonymous 用戶一樣的權(quán)限(默認(rèn))
no_root_squash root
用戶具有根目錄的完全管理訪問權(quán)限、
nonuid=xxx
指定 nfs 服務(wù)器 /etc/passwd 文件中匿名用戶的
UIDanongid=xxx
指定 nfs 服務(wù)器 /etc/passwd 文件中匿名用戶的GID
接下來(lái),我們先啟動(dòng) RPC 服務(wù) $ service rpcbind start
或者使用如下命令亦可
$ /bin/systemctlstart rpcbind.service
查看 NFS 服務(wù)項(xiàng) rpc 服務(wù)器注冊(cè)的端口列表
$ rpcinfo -plocalhost
* ? program vers proto ? port service
* ? 100000 ? 4 ? tcp ? 111 portmapper
* ? 100000 ? 3 ? tcp ? 111 portmapper
* ? 100000 ? 2 ? tcp ? 111 portmapper
* ? 100000 ? 4 ? udp ? 111 portmapper
* ? 100000 ? 3 ? udp ? 111 portmapper
* ? 100000 ? 2 ? udp ? 111 portmapper
此時(shí)我們還沒有啟動(dòng) NFS 服務(wù),只監(jiān)聽了 111 端口,接著我們來(lái)啟動(dòng) NFS 服務(wù),再來(lái)看下注冊(cè)的端口列表。
啟動(dòng) NFS 服務(wù) $ service nfs start 或者使用如下命令亦可 /bin/systemctl start nfs.service
啟動(dòng) NFS 服務(wù)后 rpc 服務(wù)已經(jīng)啟用了對(duì) NFS 的端口映射列表
rpcinfo -plocalhost
* ? program vers proto ? port service
* ? 100000 ? 4 ? tcp ? 111 portmapper
* ? 100000 ? 3 ? tcp ? 111 portmapper
* ? 100000 ? 2 ? tcp ? 111 portmapper
* ? 100000 ? 4 ? udp ? 111 portmapper
* ? 100000 ? 3 ? udp ? 111 portmapper
* ? 100000 ? 2 ? udp ? 111 portmapper
* ? 100024 ? 1 ? udp 33745 status
* ? 100024 ? 1 ? tcp 36980 status
* ? 100005 ? 1 ? udp 20048 mountd
* ? 100005 ? 1 ? tcp 20048 mountd
* ? 100005 ? 2 ? udp 20048 mountd
* ? 100005 ? 2 ? tcp 20048 mountd
* ? 100005 ? 3 ? udp 20048 mountd
* ? 100005 ? 3 ? tcp 20048 mountd
* ? 100003 ? 3 ? tcp ? 2049 nfs
* ? 100003 ? 4 ? tcp ? 2049 nfs
* ? 100227 ? 3 ? tcp ? 2049 nfs_acl
* ? 100003 ? 3 ? udp ? 2049 nfs
* ? 100003 ? 4 ? udp ? 2049 nfs
* ? 100227 ? 3 ? udp ? 2049 nfs_acl
* ? 100021 ? 1 ? udp 38960 nlockmgr
* ? 100021 ? 3 ? udp 38960 nlockmgr
* ? 100021 ? 4 ? udp 38960 nlockmgr
* ? 100021 ? 1 ? tcp 38362 nlockmgr
* ? 100021 ? 3 ? tcp 38362 nlockmgr
* ? 100021 ? 4 ? tcp 38362 nlockmgr
我們發(fā)現(xiàn),啟動(dòng)了 NFS 服務(wù)后,rpc 注冊(cè)的端口列表明顯增多。現(xiàn)在在服務(wù)端看下是否正確加載了設(shè)置的 /etc/exports 配置。 $ showmount -e localhost
[if !supportLists]·???????[endif]Export list for localhost:
[if !supportLists]·???????[endif]/data/share 192.168.1.0/24 5、NFS 測(cè)試 在另一臺(tái) Linux 虛擬機(jī)上測(cè)試一下,是否能夠正確掛載吧。首先,我們可以在 客戶端查看下 NFS 服務(wù)端 設(shè)置可共享的目錄信息。 showmount -e
192.168.1.5
[if !supportLists]·???????[endif]Export list for 192.168.1.5:
[if !supportLists]·???????[endif]/data/share 192.168.1.0/24 在客戶端創(chuàng)建掛在目錄/share $ mkdir -p /share 掛載遠(yuǎn)端目錄到本地 /share 目錄。 $ mount 192.168.1.5:/data/share /share $ df -h | grep 192.168.1.5
[if !supportLists]·???????[endif]Filesystem Size Used Avail Use% Mounted on
[if !supportLists]·???????[endif]192.168.1.5:/data/share 2.0T 33M 2.0T 1% /share 可以看到,可以正確將遠(yuǎn)端 NFS 目錄掛載到本地。注意:掛載點(diǎn) /share 目錄 必須已經(jīng)存在,而且目錄中沒有文件或子目錄。 在 NFS 服務(wù)端 /data/share 目錄下創(chuàng)建一個(gè)文件,看下客戶端是否能夠正確讀 取并修改。
服務(wù)端寫入
$ echo "This is NFS server." > /data/share/nfs.txt $ll /data/share/total 4 -rw-r--r-- 1 root root 20 Sep 26 14:52 nfs.txt
客戶端讀取
$ ll /share/` total4 -rw-r--r-- 1 root root 20 Sep 26 14:52 nfs.txt `$ cat /share/nfs.txt
[if !supportLists]·???????[endif]This is NFS server.
客戶端寫入
$ echo "Thisis NFS client." >> /share/nfs.txt
服務(wù)端讀取
$ cat /data/share/nfs.txt
[if !supportLists]·???????[endif]This is NFS server.
[if !supportLists]·???????[endif]This is NFS client.
都是木有問題的,這是因?yàn)樯线呍O(shè)置了 NFS 遠(yuǎn)端目錄權(quán)限為 rw 擁有讀寫權(quán)限,如果設(shè)置為 ro,那么客戶端只能讀取,不能寫入。這里提一下,NFS 默認(rèn)使用用 UDP 協(xié)議來(lái)進(jìn)行掛載,為了提高 NFS 的穩(wěn)定性,可以使用 TCP 協(xié)議掛載,那么客戶端掛載命令可使用如下命令:
$ mount192.168.1.5:/data/share /share -o proto=tcp -o nolock
如果客戶端要卸載 NFS 掛載的話,使用如下命令即可。若提示設(shè)備忙,先執(zhí)行
fuser -km /share /
然后再執(zhí)行umount
$ umount /share