由於此前發布項目應用時,需要對兩台文件服務器進行文件掛載,所以才實際第一次接觸到這個名詞,但由於一直以來自己沒有真正的去操作過,只是停留在一些理論層次,所以今天記錄一下這個實現過程,以備后用。
使用設備:Centos7 虛擬機兩台,一台作為服務端,一台作為客戶端。
安裝SNF服務
1、由於在進行文件掛載時需要使用SNF服務(讓不同的機器、不同的操作系統、可以彼此分享個別文件),所以我們需要進行檢查自己的虛擬機是否開啟或安裝此服務。
service nfs status
服務開始狀態,如圖:
若結果顯示 **nfs: unrecognizedservice ** 說明服務沒有開始。
2、檢查是否安裝SNF
rpm -qa | grep nfs
rpm -qa | grep rpcbind
結果如圖:
若沒有安裝,使用 yum -y install nfs-utils rpcbind 進行安裝。
注:此步驟限於centos6.X之前的系統,若版本為6.x之后則可跳過。
檢查是否安裝portmap
若顯示nfs: unrecognizedservice 則需要安裝portmap,使用yum install portmap
服務端配置
在NFS服務端上創建共享目錄/sharefile/data並設置權限
mkdir -p /sharefile/data
chmod 666 /sharefile/data/
修改export文件,增加共享目錄。
vim /etc/exports
#服務端需要共享的目錄
/sharefile/data 192.168.142.*(ro,sync,no_root_squash)
注:/sharefile/data是指服務端要共享的目錄 192.168.142.* 客戶端的ip(允許訪問的ip)
檢查防火牆是否關閉
systemctl status firewalld
若active(running) 標識防火牆打開,使用 systemctl stop firewalld 關閉防火牆
啟動SNF服務
service nfs restart
service rpcbind start
showmount -e 192.168.142.9
輸出結果:
Export list for 192.168.142.9:
/sharefile/data 192.168.142.*
客戶端
1、檢查SNF服務是否安裝,並啟動服務。
service nfs restart
service rpcbind start
2、創建文件路徑
mkdir -p /testshare/data/
3、執行:
mount -t nfs 192.168.142.9:/sharefile/data /testshare/data/
ip為服務端IP,/sharefile/data服務端共享文件路徑 /testshare/data/ 客戶端目錄
4、查看掛載目錄
df -h
輸出結果:
文件系統 容量 已用 可用 已用% 掛載點
192.168.142.9:/sharefile/data 36G 6.0G 30G 17% /testshare/data
注:
1、取消掛載
umount /sharefile/data /testshare/data/
/testshare/data/ 目錄必須存在
2、機器重啟后可執行 mount -t nfs 192.168.142.9:/sharefile/data /testshare/data/ 重新掛載
3、修改/etc/rc.local設置自動掛載
vim /etc/rc.local
#添加掛載命令
mount -t nfs 192.168.142.9:/sharefile/data /testshare/data/