1. 程式人生 > 其它 >linux NFS網路儲存最詳細講解

linux NFS網路儲存最詳細講解

目錄

NFS網路儲存

# 定義: 
NFS 就是 Network FileSystem 的縮寫,最早之前是由 Sun 這家公司所發展出來的 (注1)。 它最大的功能就是可以透過網路,讓不同的機器、不同的作業系統、可以彼此分享個別的檔案 (share files)。所以,你也可以簡單的將他看做是一個檔案伺服器 (file server) 呢!這個 NFS 伺服器可以讓你的 PC 來將網路遠端的 NFS 伺服器分享的目錄,掛載到本地端的機器當中, 在本地端的機器看起來,那個遠端主機的目錄就好像是自己的一個磁碟分割槽槽一樣 (partition)!使用上面相當的便利!
就如同上面的圖示一般,當我們的 NFS 伺服器設定好了分享出來的 /home/sharefile 這個目錄後,其他的 NFS 客戶端就可以將這個目錄掛載到自己系統上面的某個掛載點 (掛載點可以自定義),例如前面圖示中的 NFS client 1 與 NFS client 2 掛載的目錄就不相同。我只要在 NFS client 1 系統中進入 /home/data/sharefile 內,就可以看到 NFS 伺服器系統內的 /home/sharefile 目錄下的所有資料了 (當然,許可權要足夠啊!^_^)!這個 /home/data/sharefile 就好像 NFS client 1 自己機器裡面的一個 partition 喔!只要許可權對了,那麼你可以使用 cp, cd, mv, rm... 等等磁碟或檔案相關的指令!

NFS應用

1.使用者訪問NFS客戶端,將請求轉化為函式
2.NFS通過TCP/IP連線伺服器
3.NFS服務端接受請求,會先呼叫portmap程序進行埠對映
4.Rpc.nfsd程序用於判斷NFS客戶端能否連線服務端
5.Rpc.mount程序用於判斷客戶端對服務端的操作許可權
6.如果通過許可權驗證,可以對服務端進行操作,修改或讀取。

NFS實踐

服務端

1、安裝NFS和rpcbind
[root@nfs ~]# yum install nfs-utils rpcbind -y

2、建立掛載點
[root@nfs ~]# mkdir /web/nfs{1..9}

3、配置掛載點
[root@nfs ~]# vim /etc/exports
格式:
[掛載點] [可以訪問的IP]([許可權])
/web/nfs1  172.16.1.0/20(rw,sync,all_squash)

4、關閉selinux和防火牆
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld

5、啟動Nfs和rpcbind服務
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl start rpcbind

6、檢查服務端是否正常
[root@nfs ~]# showmount -e [服務端的地址,預設是本機地址]

[root@nfs ~]# showmount -e
Export list for nfs:
/web/nfsv1 172.16.1.0/20
[root@nfs ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web/nfsv1 172.16.1.0/20

[root@nfs ~]# cat /var/lib/nfs/etab

7、給掛載點授權
[root@nfs ~]# chown -R nfsnobody.nfsnobody /web

客戶端

1、安裝NFS
[root@web01 opt]# yum install -y nfs-utils

2、建立目錄
[root@web01 opt]# mkdir /opt/nfs/

3、掛載NFS
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/

4、測試NFS檔案同步功能
如圖:在web01 建立檔案 ,同時web02也同樣建立了檔案。

這樣就成功啦

NFS配置詳解

nfs共享引數 引數作用
rw 讀寫許可權 (常用)
ro 只讀許可權 (不常用)
root_squash 當NFS客戶端以root管理員訪問時,對映為NFS伺服器的匿名使用者 (不常用)
no_root_squash 當NFS客戶端以root管理員訪問時,對映為NFS伺服器的root管理員 (不常用)
all_squash 無論NFS客戶端使用什麼賬戶訪問,均對映為NFS伺服器的匿名使用者 (常用)
no_all_squash 無論NFS客戶端使用什麼賬戶訪問,都不進行壓縮 (不常用)
sync 同時將資料寫入到記憶體與硬碟中,保證不丟失資料 (常用)
async 優先將資料儲存到記憶體,然後再寫入硬碟;這樣效率更高,但可能會丟失資料 (不常用)
anonuid 配置all_squash使用,指定NFS的使用者UID,必須存在系統 (常用)
anongid 配置all_squash使用,指定NFS的使用者GID,必須存在系統 (常用)
1、控制讀寫
rw、ro

2、控制檔案許可權
root_squash
no_root_squash
all_squash
no_all_squash

3、控制寫模式
sync
async

4、控制使用者
anonuid
anongid

應用專案:

#  搭建考試系統。
搭建web服務。

1、安裝web軟體
[root@web01 opt]# yum install httpd php php-devel -y

2、將程式碼放置於網站的根目錄
[root@web01 opt]# cd /var/www/html/

# 上傳程式碼
這裡已經提前寫過了 直接copy過來 使用lrzsz或者使用xftp (檔案丟到最後)

3、授權
[root@web01 html]# chown -R www.www /var/www/html

4、關閉selinux和防火牆
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld

5、修改web軟體的使用者
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www

6、啟動web軟體
[root@web01 html]# systemctl start httpd

7、測試

	1、上傳檔案
	
	2、訪問 驗證結果
    http://172.16.1.7/upload/1_linux.jpg

實現檔案共享

1、修改NFS配置檔案
[root@nfs nfs1]# vim /etc/exports
/web/upload  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)

2、建立掛載點
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload

3、重啟NFS
[root@nfs nfs1]# systemctl restart nfs-server rpcbind

4、客戶端安裝NFS軟體
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y

5、掛載
[root@web01 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web02 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web03 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload

6、測試
用web2上傳,web3檢視

這裡就實現到了共享。