1. 程式人生 > 實用技巧 >Linux搭建NFS服務(基礎)

Linux搭建NFS服務(基礎)

【RHEL8】—NFSserver ;【Centos7】—NFSclient

!!!測試環境我們首關閉防火牆和selinux(NFSserver和NFSclient都需要)

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0

前言

1、NFS服務介紹

1️⃣:NFS—Network File System—網路檔案系統

2️⃣:主要用於linux系統上實現檔案共享的一種協議,其客戶端主要是Linux

3️⃣:企業應用:為叢集中的web server提供後端儲存

4️⃣:沒有使用者認證機制,且資料在網路上傳送的時候是明文傳送,一般只能在區域網中使用 支援多節點同時掛載及併發寫入

5️⃣:一般只在區域網中使用

2、NFS服務的元件

1️⃣:RPC(Remote Procedure Call Protocol):遠端過程呼叫協議,它是一種通過網路從遠端計算機程式上請求服務,不需要了解底層網路技術的協議。

2️⃣:客戶端工具:(1)Linux系統:showmount 、mount.nfs;(2)Windows系統:無

3、NFS服務概述

1️⃣:服務全稱:Network File System,網路檔案系統

2️⃣:服務功能:在類UNIX主機之間共享目錄和檔案資源

3️⃣:服務模式:客戶機/伺服器工作模式

4️⃣:軟體包名:rpcbind 和 nfs-utils

        rpcbind:負責NFS的資料傳輸,遠端過程呼叫

nfs-utils:控制共享哪些檔案,許可權管理

5️⃣:程序名稱:rpcbind 、rpc.nfsd 、rpc.mountd、 exportfs

6️⃣:程序埠:伺服器端RPC(111) 、NFS(隨機)

一、NFS服務端安裝NFS服務

1、首先檢視一下服務端IP

[root@NFSserver ~]# ifconfig 
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.0.140  netmask 255.0.0.0  broadcast 10.255.255.255
        inet6 fe80::fa13:32e0:3b9f:2196  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:cd:6a:1b  txqueuelen 1000  (Ethernet)
        RX packets 5419  bytes 465927 (455.0 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3930  bytes 558300 (545.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 594  bytes 50308 (49.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 594  bytes 50308 (49.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

2、安裝nfs-utils和rpcbind包

[root@NFSserver ~]# yum install -y rpcbind nfs-utils
[root@NFSserver ~]# rpm -qa rpcbind nfs-utils
rpcbind-1.2.5-4.el8.x86_64
nfs-utils-2.3.3-26.el8.x86_64

3、先啟動rpcbind和nfs,並開啟服務(!!!注意:必須先開啟 rpcbind 然後再開啟 nfs ,否則會出現問題!!!)

[root@NFSserver ~]# systemctl start rpcbind
[root@NFSserver ~]# systemctl enable rpcbind
[root@NFSserver ~]# systemctl start nfs
Failed to start nfs.service: Unit nfs.service not found.
[root@NFSserver ~]# systemctl start nfs-server
[root@NFSserver ~]# systemctl enable nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[root@NFSserver ~]# netstat -tunlp 
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      30223/rpc.mountd    
tcp        0      0 0.0.0.0:52373           0.0.0.0:*               LISTEN      28778/rpc.statd     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1101/sshd           
tcp        0      0 0.0.0.0:35197           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::20048                :::*                    LISTEN      30223/rpc.mountd    
tcp6       0      0 :::36949                :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      1101/sshd           
tcp6       0      0 :::41119                :::*                    LISTEN      28778/rpc.statd     
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
udp        0      0 0.0.0.0:42713           0.0.0.0:*                           -                   
udp        0      0 127.0.0.1:970           0.0.0.0:*                           28778/rpc.statd     
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1611/dhclient       
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1/systemd           
udp        0      0 0.0.0.0:20048           0.0.0.0:*                           30223/rpc.mountd    
udp        0      0 0.0.0.0:40540           0.0.0.0:*                           28778/rpc.statd     
udp6       0      0 :::111                  :::*                                1/systemd           
udp6       0      0 :::35034                :::*                                28778/rpc.statd     
udp6       0      0 :::33084                :::*                                -                   
udp6       0      0 :::20048                :::*                                30223/rpc.mountd 
檢查守護程序狀態
    /etc/rc.d/init.d/rpcbind status
檢查服務程序狀態
    /etc/rc.d/init.d/nfs status
服務程序

4、服務埠檢視

[root@NFSserver ~]# rpcinfo -p
   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
    100024    1   udp  40540  status
    100024    1   tcp  52373  status
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100021    1   udp  42713  nlockmgr
    100021    3   udp  42713  nlockmgr
    100021    4   udp  42713  nlockmgr
    100021    1   tcp  35197  nlockmgr
    100021    3   tcp  35197  nlockmgr
    100021    4   tcp  35197  nlockmgr
    100005    1   udp  20048  mountd
    100005    1   tcp  20048  mountd
    100005    2   udp  20048  mountd
    100005    2   tcp  20048  mountd
    100005    3   udp  20048  mountd
    100005    3   tcp  20048  mountd

5、建立共享目錄

[root@NFSserver ~]# mkdir /nfsdir
[root@NFSserver ~]# ll /nfsdir/
總用量 0

6、編輯NFS主配置檔案

[root@NFSserver ~]# vim /etc/exports
[root@NFSserver ~]# cat /etc/exports
/nfsdir 10.0.0.0/24(ro,rw)
 //第一次進入該檔案為空白檔案,需要使用者自行建立
1、/etc/exports檔案格式
<輸出目錄> [客戶端1 選項(訪問許可權,使用者對映,其他)] [客戶端2 選項(訪問許可權,使用者對映,其他)](exprots檔案中每一行提供了一個共享目錄的設定)
2、相關說明
    1️⃣:輸出目錄:輸出目錄是指NFS系統中需要共享給客戶機使用的目錄;
    2️⃣:客戶端:客戶端是指網路中可以訪問這個NFS輸出目錄的計算機;
           客戶端常用的指定方式:
                     指定ip地址的主機:192.168.10.10
                     指定子網中的所有主機:192.168.10.0/24   192.168.10.* 
                     指定域名的主機:www.test.com.cn
                     指定域中的所有主機:*.test.com.cn
                     所有主機:*
    3️⃣:選項的種類:
           1)、訪問許可權選項:
                       設定輸出目錄只讀:ro
                       設定輸出目錄讀寫:rw
           2)、使用者對映選項:
                        all_squash:將遠端訪問的所有普通使用者及所屬組都對映為匿名使用者或使用者組(nfsnobody);
                        no_all_squash:與all_squash取反(預設設定);
                        root_squash:將root使用者及所屬組都對映為匿名使用者或使用者組(預設設定);
                        no_root_squash:與rootsquash取反;
                        anonuid=xxx:將遠端訪問的所有使用者都對映為匿名使用者,並指定該使用者為本地使用者(UID=xxx);
                        anongid=xxx:將遠端訪問的所有使用者組都對映為匿名使用者組賬戶,並指定該匿名使用者組賬戶為本地使用者組賬戶(GID=xxx);
             3)、其它選項:
                        secure:限制客戶端只能從小於1024的tcp/ip埠連線nfs伺服器(預設設定);
                        insecure:允許客戶端從大於1024的tcp/ip埠連線伺服器;
                        sync:將資料同步寫入記憶體緩衝區與磁碟中,效率低,但可以保證資料的一致性;
                        async:將資料先儲存在記憶體緩衝區中,必要時才寫入磁碟;
                        wdelay:檢查是否有相關的寫操作,如果有則將這些寫操作一起執行,這樣可以提高效率(預設設定);
                        no_wdelay:若有寫操作則立即執行,應與sync配合使用;
                        subtree:若輸出目錄是一個子目錄,則nfs伺服器將檢查其父目錄的許可權(預設設定);
                        no_subtree:即使輸出目錄是一個子目錄,nfs伺服器也不檢查其父目錄的許可權,這樣可以提高效率;
                        no_root_squash:是讓root保持許可權;
                        root_squash:是把root對映成nobody,no_all_squash 不讓所有使用者保持在掛載目錄中的許可權。
相關說明

7、重啟NFS服務

[root@NFSserver ~]# systemctl restart nfs-server
[root@NFSserver ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      30394/rpc.mountd    
tcp        0      0 0.0.0.0:52373           0.0.0.0:*               LISTEN      28778/rpc.statd     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1101/sshd           
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:44931           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::20048                :::*                    LISTEN      30394/rpc.mountd    
tcp6       0      0 :::46481                :::*                    LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      1101/sshd           
tcp6       0      0 :::41119                :::*                    LISTEN      28778/rpc.statd     
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
udp        0      0 127.0.0.1:970           0.0.0.0:*                           28778/rpc.statd     
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1611/dhclient       
udp        0      0 0.0.0.0:111             0.0.0.0:*                           1/systemd           
udp        0      0 0.0.0.0:47351           0.0.0.0:*                           -                   
udp        0      0 0.0.0.0:20048           0.0.0.0:*                           30394/rpc.mountd    
udp        0      0 0.0.0.0:40540           0.0.0.0:*                           28778/rpc.statd     
udp6       0      0 :::111                  :::*                                1/systemd           
udp6       0      0 :::35034                :::*                                28778/rpc.statd     
udp6       0      0 :::45389                :::*                                -                   
udp6       0      0 :::20048                :::*                                30394/rpc.mountd 

二、客戶端訪問NFS伺服器

1、客戶端安裝nfs-utils(其實原本需要在客戶端安裝的工具是showmount和mount.nfs,在安裝這兩個包時,就是安裝的nfs-utils這個包)

[root@nfsclient ~]# yum install -y nfs-utils
[root@nfsclient ~]# rpm -qa nfs-utils
nfs-utils-1.3.0-0.66.el7.x86_64
[root@nfsclient ~]# which showmount mount.nfs
/usr/sbin/showmount
/usr/sbin/mount.nfs
 //出現命令的路徑,說明已經安裝

2、客戶端檢視服務端提供的共享資源

 //命令格式:showmount -e 伺服器端ip
[root@nfsclient ~]# showmount -e 10.0.0.140
Export list for 10.0.0.140:
/nfsdir 10.0.0.0/24

3、客戶端建立需要掛載的目錄

[root@nfsclient ~]# mkdir /sharedir
[root@nfsclient ~]# ll /sharedir/
總用量 0
[root@nfsclient ~]# df -h
檔案系統                 容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root   50G  1.1G   49G    3% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M     0  912M    0% /dev/shm
tmpfs                    912M  8.6M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/mapper/centos-home   27G   33M   27G    1% /home
/dev/sda1               1014M  143M  872M   15% /boot
tmpfs                    183M     0  183M    0% /run/user/0

4、客戶端訪問共享資源

//命令格式:mount -t nfs nfs伺服器端ip:/共享目錄 掛載點
或者
//命令格式:mount.nfs nfs伺服器端ip:/共享目錄 掛載點
[root@nfsclient ~]# mount.nfs 10.0.0.140:/nfsdir /sharedir
[root@nfsclient ~]# df -h
檔案系統                 容量  已用  可用 已用% 掛載點
/dev/mapper/centos-root   50G  1.1G   49G    3% /
devtmpfs                 901M     0  901M    0% /dev
tmpfs                    912M     0  912M    0% /dev/shm
tmpfs                    912M  8.6M  904M    1% /run
tmpfs                    912M     0  912M    0% /sys/fs/cgroup
/dev/mapper/centos-home   27G   33M   27G    1% /home
/dev/sda1               1014M  143M  872M   15% /boot
tmpfs                    183M     0  183M    0% /run/user/0
10.0.0.140:/nfsdir        50G  2.0G   49G    4% /sharedir
 // 最後一行說明已經掛載成功

三、測試

1、在服務端建立一個資料夾,並寫入檔案

[root@NFSserver ~]# cd /nfsdir/
[root@NFSserver nfsdir]# ls
[root@NFSserver nfsdir]# ll
總用量 0
[root@NFSserver nfsdir]# touch test.txt
[root@NFSserver nfsdir]# echo "I am so cool!" > test.txt 

2、在客戶端檢視服務端共享的檔案

[root@nfsclient ~]# cd /sharedir/
[root@nfsclient sharedir]# ls
test.txt
[root@nfsclient sharedir]# ll
總用量 4
-rw-r--r--. 1 root root 14 7月  29 16:23 test.txt
[root@nfsclient sharedir]# cat test.txt 
I am so cool!

四、拓展

1、客戶端開機自動掛載

[root@nfsclient ~]# vim /etc/fstab 
.........
10.0.0.140:/nfsdir      /sharedir       nfs     default,_rnetdev        0 0
 //在檔案的最後寫入