一、NFS的原理介紹
NFS為Network File System的縮寫,可以用于不用的機器,不同作業系統之間的檔案共享。與samba相比,NFS更快捷友善。簡單的講,可以把NFS伺服器當做是一個檔案伺服器,NFS伺服器可以讓PC機将遠端的NFS主機共享出來的目錄挂載到本地機器中,挂載之後,這個目錄就像是本地的一個分區一樣,在有足夠權限的情況下可以随意使用。
1、NFS需要啟動的程序
rpc.nfsd 這個程序主要功能是管理client端是否能夠登入主機,其中包含複雜的登入權限檢測。
rpc.moutd 負責NFS的檔案系統,當client通過rpc.nfsd登陸server後,對client存取server 的檔案進行一系列的管理。NFS server 在linux平台下需要兩個套件 nfs-utils 和PORTMAP。
nfs-utils 提供rpc.nfsd 和rcp.mountd兩個NFS daemon的套件
portmap 遠端過程調用RPC的守護程序
2、NFS工作的大概原理:
NFS伺服器共享目錄、目錄權限的檢查
NFS服務使用RPC協定進行資料的傳輸
NFS伺服器會開啟一系列的RPC服務程序和端口
portmap啟動一個111端口等待用戶端連接配接
portmap會把通路111端口的資料發送給RPC服務對應的端口
二、NFS服務端安裝、配置
1、安裝nfs相關軟體
[root@localhost ~]# yum install nfs-utils -y ##nfs-utils 為nfs的安裝包
……………………
Installed:
nfs-utils.x86_64 1:1.2.3-36.el6
Dependency Installed:
keyutils.x86_64 0:1.4-4.el6
libevent.x86_64 0:1.4.13-4.el6
libgssglue.x86_64 0:0.1-11.el6
libtirpc.x86_64 0:0.2.1-6.el6_4
nfs-utils-lib.x86_64 0:1.1.5-6.el6
rpcbind.x86_64 0:0.2.0-11.el6
Complete!
2、NFS配置檔案的介紹
/etc/exports NFS的主配置檔案,一般是空檔案或者不存在,需要自己建立(相關的參數在本文最下面介紹)
/usr/bin/exportfs 維護nfs共享資源的指令,可以使用這個指令來重新共享/etc/exports更改的目錄資源、将NFS伺服器共享的目錄解除安裝或者重新共享,是nfs伺服器中非常重要的一個指令。
/usr/sbin/showmout 該指令主要用在服務端,而show mount 主要用在用戶端,可以使用它來檢視NFS的伺服器共享出來的目錄資源。
/var/lib/nfs/*tab nfs伺服器的日志檔案都放在該目錄下,包含兩個日志檔案,一個是etab,主要記錄NFS共享出來的目錄的完整地權限設定值;另一個是xtab,記錄曾經連接配接到此NFS主機的相關用戶端資料。
/etc/init.d/nfs nfs的服務程序,啟動nfs服務
/etc/init.d/rpcbind rpcbind的守護程序,基本上第一次啟動之後以後每次都會開機自動運作,不會關閉。
3、修改配置檔案:
[root@localhost ~]# id nobody ###檢視nobody的id 和uid
uid=99(nobody) gid=99(nobody) groups=99(nobody)
[root@localhost ~]# cat /etc/exports ###檢視nfs的主配置檔案
/nfs 192.168.254.46(rw,all_squash,anonuid=99,anongid=99) *(ro,all_squash)###允許192.168.254.46這個主機讀寫我本機的/nfs目錄,其他使用者隻讀
[root@localhost ~]# mkdir /nfs ##我将要共享出去的檔案夾
[root@localhost ~]# chown nobody.nobody /nfs -R ####共享出去的檔案夾屬主要為普通使用者,我選nobody
4、啟動rpcbind服務
[root@localhost ~]# /etc/init.d/rpcbind restart
Stopping rpcbind: [ OK ]
Starting rpcbind: [ OK ]
5、重新開機nfs服務
[root@localhost ~]# service nfs restart
Shutting down NFS daemon: [ OK ]
Shutting down NFS mountd: [ OK ]
Shutting down NFS quotas: [ OK ]
Shutting down NFS services: [ OK ]
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
6、檢視一下服務程序
[root@localhost ~]# netstat -tunlp | grep 111 ####rpcbind開啟的端口為111
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2814/rpcbind
tcp 0 0 :::111 :::* LISTEN 2814/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 2814/rpcbind
udp 0 0 :::111 :::* 2814/rpcbind
7、檢視一下NFS共享的目錄
[root@localhost ~]# exportfs
/nfs 192.168.254.46
[root@localhost ~]# exportfs -au ###下載下傳掉所有共享目錄
[root@localhost ~]# exportfs ###解除安裝後就無法看到共享目錄了
[root@localhost ~]# exportfs -av ####加載所有的共享目錄
exporting 192.168.254.46:/nfs
三、用戶端(192.168.254.46)
[root@localhost ~]# yum install nfs-utils -y
2、 檢視nfs server的共享目錄
[root@localhost ~]# showmount -e 192.168.254.153 ####showmount檢視nfs server上的共享檔案夾
Export list for 192.168.254.153:
/test (everyone)
3、用戶端挂載
[root@localhost ~]# mkdir /tmp/test
[root@localhost ~]# mount.nfs 192.168.254.153:/test /tmp/test/ ###nfs共享目錄的挂載
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
ext4 48G 3.7G 41G 9% /
tmpfs tmpfs 499M 0 499M 0% /dev/shm
/dev/vda1 ext4 485M 29M 431M 7% /boot
/dev/mapper/VolGroup-lv_home ext4 49G 180M 47G 1% /home
192.168.254.153:/nfs nfs 70G 4.7G 62G 8% /tmp/test
4、測試讀寫
[root@localhost ~]# cd /tmp/test/
[root@localhost test]# ls
[root@localhost test]# touch a
[root@localhost test]# mkdir test
[root@localhost test]# ls ###檢視建立的檔案和檔案夾
a test
四、用戶端 (192.168.254.38)
[root@localhost ~]# ifconfig eth0 | grep Bcast | awk '{print $2}' ###檢視一下用戶端IP
addr:192.168.254.38
[root@localhost ~]# showmount -e 192.168.254.153 ###檢視153的共享檔案夾
/nfs 192.168.254.46
[root@localhost ~]# mkdir /test
[root@localhost ~]# mount -t nfs 192.168.254.153:/nfs /test
mount: 192.168.254.153:/nfs failed, reason given by server: Permission denied
[root@localhost ~]# cd /test/
touch: 無法觸碰 “a”: 權限不夠 ####38用戶端允許挂載,權限是隻讀
五、/etc/exports的參數
[root@localhost ~]# cat /etc/exports
/nfs 192.168.254.46(rw,all_squash,anonuid=99,anongid=99) *(ro,all_squash)
nfs server共享的目錄; 允許挂載的IP或主機 ; 代表權限;其他IP或主機;其他主機的權限
主要介紹一下權限參數
rw:讀寫權限,不解釋;
ro:隻讀權限,不解釋;
no_root_squash: 如果是root使用者挂載nfs共享目錄,那麼就是root身份,對于這個共享目錄來說使用者具有root權限,這個參數非常不安全,給使用者的權限太大,慎用或者不建議使用!
root_squash:如果root使用者使用nfs共享目錄,那麼使用者權限就會被壓縮成匿名使用者,預設為nobody;
all_squash:任何使用者的權限都被壓縮成匿名使用者,預設為nobody;
anonuid:anon是anonymous的縮寫,通常是nobody的uid‘
anongid:nobody的gid;
sync:資料同步寫入到記憶體與磁盤中
async:資料暫存在記憶體中而不是直接寫入到磁盤。