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
 //在文件的最后写入

猜你喜欢

转载自www.cnblogs.com/520qiangge/p/13397980.html
今日推荐