一、NFS簡介
NFS是Network File System的縮寫,即網絡檔案系統。一種使用于分散式檔案協定,有SUN公司開發。功能是通過網絡讓不同的機器、不同的作業系統能夠分享個人資料,讓應用程式通過網絡可以通路位于伺服器磁盤中的資料。
NFS在檔案傳送或資訊傳送的過過程中,依賴于RPC協定。RPC,遠端過程調用(Remote Procedure Call),是使用戶端能夠執行其他系統中程式的一種機制。NFS本身是沒有提供資訊傳輸的協定和功能的,但NFS卻能讓我們通過網絡進行資料的分享,就是因為NFS使用了RPC提供的傳輸協定,可以說NFS就是使用PRC的一個程式。
NFS服務端、RPC協定、用戶端三者可以了解為房源、中介、租客之間的關系:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuETM3kzMhlzMmlDMyEmZkJzY1cjMmdTMjNGO0gTZ5ATOfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.png)
二、系統環境
CentOSrelease 6.7 (Final) 2.6.32-573.el6.i686
NFS IP:172.16.1.31
web IP : 172.16.1.8
/etc/init.d/iptables status
iptables:未運作防火牆
SElinux :getenforce Permissive
三、開始搭建
1)軟體安裝,NFS隻需要安裝兩個軟體,在通常情況下是作為系統預設軟體安裝的
【rpcbind】centos 下面RPC主程式
【nfs-utils】NFS服務主程式,包括NFS的基本指令和監控程式
[root@nfs01 ~]# yum install rpcbind nfs-utils
2)開啟RCP服務
[root@nfs01 ~]# /etc/init.d/rpcbind start
檢視rpcbind服務端口
[root@nfs01 ~]# netstat -antlp|grep rpcbind
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1368/rpcbind
檢視此時rpc服務上面是否有端口注冊
[root@nfs01 ~]# rpcinfo -p localhost
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
3)開啟NFS服務
[root@nfs01 ~]# /etc/init.d/nfs start
現在rpc上應該能看到好多新被注冊的nfs端口了
[root@nfs01 ~]# rpcinfo -p localhost
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049 nfs_acl
100227 3 tcp 2049 nfs_acl
設定兩個服務開機自啟動
[root@nfs01 ~]# chkconfig rpcbind on
[root@nfs01 ~]# chkconfig nfs on
4)服務端配置共享目錄(/data)
配置前确認rpcbind、nfs服務程序正常
[root@nfs01 ~]# /etc/init.d/nfs status
[root@nfs01 ~]# /etc/init.d/rpcbind status
[root@nfs01 ~]# ps -ef|egrep "rpc|nfs"
rpc 1368 1 0 00:14 ? 00:00:00 rpcbind
rpcuser 1391 1 0 00:14 ? 00:00:00 rpc.statd
root 1440 2 0 00:14 ? 00:00:00 [rpciod/0]
root 1449 1 0 00:14 ? 00:00:00 rpc.rquotad
root 1454 1 0 00:14 ? 00:00:00 rpc.mountd
root 1461 2 0 00:14 ? 00:00:00 [nfsd4]
root 1462 2 0 00:14 ? 00:00:00 [nfsd4_callbacks]
root 1463 2 0 00:14 ? 00:00:00 [nfsd]
root 1464 2 0 00:14 ? 00:00:00 [nfsd]
root 1465 2 0 00:14 ? 00:00:00 [nfsd]
root 1466 2 0 00:14 ? 00:00:00 [nfsd]
建立共享目錄并授權("nfsnobody")
#nfsnobody 使用者是開啟rpc、nfs程序後系統自動建立的
[root@nfs01 ~]# mkdir /data
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data
修改服務端配置檔案(/etc/exports)
[root@nfs01 ~]# vim /etc/exports
#share /data by oldboy for bingbing at 20160524
/data 172.16.1.0/24(rw,sync)
####
/data1 172.16.1.0/24(rw,sync,all_squash,anonuid=65534,anongid=65534)
注意!
#此時可以修改“anonuid”值來修改NFS預設虛拟使用者,前提是使用者在系統中存在,可以指定“-s /sbin/nologin”
檢視系統加載的配置
[root@nfs01 ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)
重新平滑加載服務
[root@nfs01 ~]# /etc/init.d/nfs reload
确認服務、目錄等配置正确,共享成功
[root@nfs01 ~]# showmount -e
Export list for nfs01:
/data 172.16.1.0/24
5)用戶端配置
用戶端隻需要安裝rpcbind程式,并确認服務正常
[root@web01 ~]# /etc/init.d/rpcbind status
rpcbind (pid 1361) 正在運作...
挂載nfs共享目錄
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 6.9G 1.3G 5.2G 20% /
tmpfs 503M 0 503M 0% /dev/shm
/dev/sda1 190M 33M 147M 19% /boot
172.16.1.31:/data 6.9G 1.3G 5.2G 20% /mnt
開機自動挂載
echo "mount -t nfs172.16.1.31:/data /mnt">>/etc/rc.local
另外,實作NFS共享自動挂載也可以在/ets/fstab裡實作,不過此時要系統開啟netfs服務
因為有可能在系統在重新開機過程中,先實作挂載,後啟動網絡,此時出現挂載不成功問題
開啟netfs服務
[root@web01 ~]# chkconfig netfs on