1. 程式人生 > >linux中搭建NFS伺服器

linux中搭建NFS伺服器

NFS是Network File System(網路檔案系統)。主要功能是通過網路讓不同的伺服器之間可以共享檔案或者目錄。NFS客戶端一般是應用伺服器(比如web,負載均衡等),可以通過掛載的方式將NFS伺服器端共享的目錄掛載到NFS客戶端本地的目錄下

NFS 的基本原則是“允許不同的客戶端及服務端通過一組RPC分享相同的檔案系統”,它是獨立於作業系統,容許不同硬體及作業系統的系統共同進行檔案的分享。

NFS 協議
需要遠端呼叫(RPC)服務,因為NFS本身是沒有提供資訊傳輸的協議和功能

安裝配置NFS服務
關閉防火牆和SElinux
systemctl stop firewalld
setenforce 0
getenforce

安裝NFS伺服器
yum install rpcbind nfs-utils -y
伺服器端配置
建立共享目錄
mkdir /nfs_dir
chmod 666 /nfs_dir
編輯export 檔案

vi	/etc/exports
/nfs_dir 192.168.0.155/255.255.255.0(rw,sync,no_root_squash,no_all_squash)

常見的引數

    rw:read-write,可讀寫;
    ro:read-only,只讀;
    sync:檔案同時寫入硬碟和記憶體;
    async:檔案暫存於記憶體,而不是直接寫入記憶體;
    no_root_squash:NFS客戶端連線服務端時如果使用的是root的話,那麼對服務端分享的目錄來說,也擁有root許可權。顯然開啟這項是不安全的。
    root_squash:NFS客戶端連線服務端時如果使用的是root的話,那麼對服務端分享的目錄來說,擁有匿名使用者許可權,通常他將使用nobody或nfsnobody身份;
    all_squash:不論NFS客戶端連線服務端時使用什麼使用者,對服務端分享的目錄來說都是擁有匿名使用者許可權;
    anonuid:匿名使用者的UID值,通常是nobody或nfsnobody,可以在此處自行設定;
    anongid:匿名使用者的GID值。

重啟服務
systemctl restart rpcbind
systemc restart nfs-utils

客戶端配置
安裝nfs-utils客戶端
yum install nfs_utils -y
建立掛載目錄
mkdir /nfs_dir
檢視伺服器的共享資訊

[[email protected] nfs_dir]# showmount -e 192.168.0.155
Export list for 192.168.0.155:
/nfs_dir 192.168.0.155/255.255.255.0

掛載
mount -t nfs 192.168.0.155:/nfs_dir /nfs_dir
檢視掛載結果

[[email protected] nfs_dir]# mount
192.168.0.155:/nfs_dir on /nfs_dir type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.131,local_lock=none,addr=192.168.0.155)

則掛載成功
測試
在服務端(在共享目錄下)

   [[email protected] nfs_dir]# echo 'hell' >>test1
    [[email protected] nfs_dir]# ls
    test1

在客戶端

  [[email protected] nfs_dir]# ls
  test1
  [[email protected] nfs_dir]# cat test1
  hell
  [[email protected] nfs_dir]# echo 'wqzds' >>file

在伺服器端

[[email protected] nfs_dir]# ls
file test1
[[email protected] nfs_dir]# cat file
wqzds