五十四、NFS介紹、NFS服務端安裝配置、NFS配置選項
一、NFS介紹
NFS是Network File System的縮寫。
NFS最早由sun公司開發,分2,3,4三個版本,2和3由sun起草開發,4.0開始Netapp公司參與並主導開發,最新為4.1版本。
NFS數據傳輸基於RPC協議,RPC為Remote Procedure Call的簡寫。
NFS應用場景是:A,B,C三臺機器上需要保證被訪問到的文件是一樣的,A共享數據出來,B和C分別掛載A共享的數據目錄,從而B和C訪問到的數據和A上的一致。
NFS原理:
RPC服務是由rpcbind所實現的,centos5及之前叫protmap,centos6及之後叫rpcbind,是一個東西,原理就是啟動了這個rpcbind服務,實現了RPC協議的通信;
總結:NFS服務需要通過RPC協議進行通信。
二、NFS服務端安裝配置
準備兩臺機器,一臺服務端,一臺客戶端。
服務端上安裝兩個包:nfs-utils和rpcbind;
客戶端上安裝一個包:nfs-utils。
# yum install -y nfs-utils rpcbind
//這裏不管寫不寫rpcbind,這個nfs-utils都會給你安裝上這個包。
當epel這個安裝很慢的時候,把這個文件改一個名字即可:
# cd /etc/yum.repos.d/
# mv epel.repo epel.repo.1
# vim /etc/exports //加入如下內容
/home/nfstestdir 192.168.93.2/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
這裏指定共享的目錄,共享的機器的IP或IP段。
保存配置文件後,執行如下準備操作
# mkdir /home/nfstestdir
# chmod 777 /home/nfstestdir 給777權限是為了方便做實驗
# netstat -lntp //可以看到111端口已經被監聽了,這個就是rpcbind服務,安裝完rpcbind以後這個就會自動的啟動。
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
# systemctl start rpcbind
# systemctl start nfs
# ps aux |grep nfs
root 5745 0.0 0.0 0 0 ? S< 21:33 0:00 [nfsd4_callbacks]
root 5751 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5752 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5753 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5754 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5755 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5756 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5757 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5758 0.0 0.0 0 0 ? S 21:33 0:00 [nfsd]
root 5763 0.0 0.0 112676 984 pts/1 S+ 21:33 0:00 grep --color=auto nfs
# ps aux |grep rpcbind 當nfs啟動以後,rpcbind會自動啟動一些相關的服務
rpc 5694 0.1 0.0 64956 1404 ? Ss 21:31 0:00 rpcbind
rpcuser 5717 0.0 0.0 42376 1744 ? Ss 21:33 0:00 /usr/sbin/rpc.statd
root 5718 0.0 0.0 0 0 ? S< 21:33 0:00 [rpciod]
root 5734 0.0 0.0 42564 944 ? Ss 21:33 0:00 /usr/sbin/rpc.mountd
root 5735 0.0 0.0 43816 540 ? Ss 21:33 0:00 /usr/sbin/rpc.idmapd
root 5765 0.0 0.0 112680 980 pts/1 S+ 21:35 0:00 grep --color=auto rpc
# systemctl enable rpcbind
# systemctl enable nfs 開機啟動,在服務端執行。
三、NFS配置選項
/home/nfstestdir 192.168.93.2/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
rw 讀寫
ro 只讀
sync 同步模式,內存數據實時寫入磁盤,可以很快寫入磁盤,相應的會降低磁盤效率。
async 非同步模式,不實時寫入磁盤,每隔一段時間會把內存的東西寫到磁盤裏去,這樣的好處是能保證磁盤的效率,缺點是萬一斷電了,數據就會丟一部分。
no_root_squash 客戶端掛載NFS共享目錄後,root用戶不受約束,權限很大
root_squash 與上面選項相對,客戶端上的root用戶收到約束,被限定成某個普通用戶
all_squash 客戶端上所有用戶在使用NFS共享目錄時都被限定為一個普通用戶
anonuid/anongid 和上面幾個選項搭配使用,定義被限定用戶的uid和gid
客戶端掛載
# yum install -y nfs-utils
# showmount -e 192.168.93.130 //該ip為NFS服務端ip
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
//報錯,不能和93.130的111端口通信,原因有2個:一,是93.130的111端口是否打開;二,防火墻導致。
# systemctl stop firewalld 建議:關掉防火墻,服務端和客戶端都關。
# getenforce 檢查Selinux,也要關,服務端和客戶端都要關。
Disabled
# showmount -e 192.168.93.130 通信OK
Export list for 192.168.93.130:
/home/nfstestdir 192.168.93.2/24 看到了93.130這臺機器共享目錄和共享的機器給了這個IP段。
# mount -t nfs 192.168.93.130:/home/nfstestdir /mnt 把它掛載起來到/mnt
# df -h
# touch /mnt/aminglinux.txt 客戶端/mnt下touch個文件,再到服務端看,就有
# ls -l /mnt/aminglinux.txt //可以看到文件的屬主和屬組都為1000
anonuid=1000,anongid=1000
//因為這裏uid和gid都定義了1000,所以不管你用哪個用戶操作的,最終都會以1000uid和1000gid的用戶反應給服務端和客戶端。
五十四、NFS介紹、NFS服務端安裝配置、NFS配置選項