Linux 安装 NFS 服务

1. 什么是NFS?

       NFS就是Network File System的缩写,它最大的功能就是可以通过网络,让不同的机器、不同的操作系统可以共享彼此的文件。NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中来看,那个远程主机的目录就好像是自己的一个磁盘分区一样,在使用上相当便利

2. NFS原理(略)

3. 部署

基本信息说明:
服务器内核: CentOS Linux release 7.8.2003 (Core)
NFS服务端ip: 172.27.57.39
服务端共享目录: /data
NFS客户端ip: 172.27.57.33
NFS客户端挂载点: /nfs

3.1 查看Linux系统版本

[root@Server]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

笔者使用的是Centos 7.8,不同内核的Linux服务器可能导致安装方式的些许差别.

3.2 服务器硬盘挂载

笔者在部署NFS时,硬盘尚未挂载.挂载过程放在另一篇博客。

3.3 服务端安装 NFS 服务及配置

3.3.1 检查服务器是否安装过 nfs-utils, rpcbind
[root@Servcice]# rpm -qa nfs-utils rpcbind
[root@]#                                                                          # 执行结果为空,说明没有安装
3.3.2 安装 nfs-utils, rpcbind
[root@Servcice]# yum install -y nfs-utils rpcbind 			# 等待安装
[root@Servcice]# rpm -qa nfs-utils rpcbind					# 检查,有执行结果,安装完毕
rpcbind-0.2.0-49.el7.x86_64
nfs-utils-1.3.0-0.68.el7.x86_64
3.3.3 创建共享文件目录并授权
[root@Servcice]# mkdir -p /data
[root@Servcice]# chown -R nfsnobody:nfsnobody /data       # 变更文件夹拥有者
[root@Service]# chmod 766 /data										   # 授权

注意:共享文件夹 nfsnobody 用户一定要有 '执行' 权限,否则 3.3.4中,当配置 all_squash, no_root_squas 时,无法进入共享文件夹

3.3.4. 配置 NFS
[root@Servcice]# vim /etc/exports
# 以下是笔者添加的配置
配置结构语法: 共享的目录 主机名或IP(参数,参数)
/data 172.27.57.0/24(rw,sync,all_squash)
翻译: 将 /data 放到 172.27.57.1 - 172.27.57.255 的所有机器共享,并且权限为 读写
参数不写会使用默认配置。默认的共享选项是 sync,ro,root_squash,no_delay
配置参数了列表
		ro                      只读访问 
        rw                      读写访问 
        sync                    所有数据在请求时写入共享 
        async                   NFS在写入数据前可以相应请求 
        secure                  NFS通过1024以下的安全TCP/IP端口发送 
        insecure                NFS通过1024以上的端口发送 
        wdelay                  如果多个用户要写入NFS目录,则归组写入(默认) 
        no_wdelay               如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。 
        hide                    在NFS共享目录中不共享其子目录 
        no_hide                 共享NFS目录的子目录 
        subtree_check           如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认) 
        no_subtree_check        和上面相对,不检查父目录权限 
        all_squash              共享文件的UID和GID映射匿名用户anonymous,适合公用目录。 
        no_all_squash           保留共享文件的UID和GID(默认) 
        root_squash             root用户的所有请求映射成如anonymous用户一样的权限(默认) 
        no_root_squas           root用户具有根目录的完全管理访问权限 
        anonuid=xxx             指定NFS服务器/etc/passwd文件中匿名用户的UID 
        anongid=xxx             指定NFS服务器/etc/passwd文件中匿名用户的GID
3.3.5 启动 NFS 服务
# 1. 启动 rpcbind 和 nfs 服务
[root@Servcice]# systemctl start rpcbind && systemctl start nfs
# 2. 查看状态 
[root@Servcice]# systemctl statusrpcbind && systemctl status nfs
# 3. 查看相关端口
[root@Servcice]#  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
    100024    1   udp  44503  status
    100024    1   tcp  41945  status
    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
    100003    3   tcp   2049  nfs
    100003    4   tcp   2049  nfs
    100227    3   tcp   2049  nfs_acl
    100003    3   udp   2049  nfs
    100003    4   udp   2049  nfs
    100227    3   udp   2049  nfs_acl
    100021    1   udp  59685  nlockmgr
    100021    3   udp  59685  nlockmgr
    100021    4   udp  59685  nlockmgr
    100021    1   tcp  39281  nlockmgr
    100021    3   tcp  39281  nlockmgr
    100021    4   tcp  39281  nlockmgr 
# 4. 查看服务端是否正确加载了设置的 /etc/exports 配置 
[root@Servcice]# showmount -e localhost
Export list for localhost:
/data/share 172.27.57.0/24
# 5. 将 rpcbind , nfs 设置为开机自启
[root@Servcice]# systemctl enable rpcbind
[root@Servcice]# systemctl enable nfs
[root@Servcice]# reboot                # 重启

3.4 客户端安装 NFS 服务

NFS 服务端需要安装 rpcbind 和 nfs-utils,客户端只需要安装 nfs-utils 即可。

3.4.1 客户端安装 nfs-utils
[root@Servcice]# rpm -qa nfs-utils							# 执行结果为空
[root@Servcice]# yum install -y nfs-utils					# 安装
[root@Servcice]# rpm -qa nfs-utils
nfs-utils-1.3.0-0.68.el7.x86_64
3.4.2 客户端挂载可能遇到的错误及解决方法

客户端在挂载网络共享盘时,可能遇到的错误:
错误一: clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

[root@client] showmount -e 172.27.57.39
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

此错误是由于 NFS 服务器防火墙导致
解决方法:

  1. 服务端关闭防火墙(简单粗暴,但是不推荐)
    [root@Service]# systemctl stop firewalld
    
  2. 服务端开放对应端口(892/tcp,111/udp)
    编辑/etc/sysconfig/nfs去掉端口MOUNTD_PORT=892前面的#,开放 892/tcp,111/udp
    [root@Service]# vim /etc/sysconfig/nfs
    # Port rpc.mountd should listen on.
    MOUNTD_PORT=892                  #  放开
    [root@Service]# firewall-cmd --zone=public --add-port=111/udp --permanent
    [root@Service]# firewall-cmd --zone=public --add-port=892/tcp --permanent
    [root@Service]# firewall-cmd --reload							# 重启防火墙
    [root@Service]# firewall-cmd --permanent --list-port     		# 查看开放端口
    892/tcp 111/udp
    [root@Service]# systemctl restart rpcbind 						# 重启 rpcbind 服务
    [root@Service]# systemctl restart nfs-server 					# 重启 nfs 服务
    
    注意: 111端口时 udp,不是 tcp, 笔者因为这个小问题,浪费了半天时间!!!

错误二:mount.nfs: No route to host

```
[root@Client ]# showmount -e 172.27.57.39
Export list for 172.27.57.39:
/data/share 172.27.57.0/24
[root@Client ]# mount -t nfs 172.27.57.39:/data /nfs
mount.nfs: No route to host                                                                
```

此错误是由于 NFS 服务器防火墙导致
解决方法:
1. 服务端关闭防火墙(简单粗暴,但是不推荐)
2. 服务端开启 2049/tcp 端口

3.4.3 成功挂载及验证

再次挂载

[root@Client]# mount -t nfs 172.27.57.39:/data/ /nfs
[root@Client]# df -h
Filesystem                Size  Used Avail Use% Mounted on
devtmpfs                  7.9G     0  7.9G   0% /dev
tmpfs                     7.9G  8.0K  7.9G   1% /dev/shm
tmpfs                     7.9G  418M  7.5G   6% /run
tmpfs                     7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/vda2                  99G  2.2G   97G   3% /
/dev/vdb1                 197G   62M  187G   1% /data
/dev/vda1                1014M  131M  884M  13% /boot
172.27.57.39:/data 493G   72M  467G   1% /nfs     # 成功挂载
tmpfs                     1.6G     0  1.6G   0% /run/user/1000

验证
在 NFS 服务端(Service)创建文件,看是否能在客户端(Client)看到,
在客户端(Client)创建文件,看是否能在服务端(Service)看到

3.4.4 设置开机自动挂载

修改/etc/fstab文件,添加 (172.27.57.39:/data /nfs nfs defaults 0 0)

[root@Service]# vim /etc/fstab 
172.27.57.39:/data /nfs nfs defaults 0 0

4.命令整理

命令整理成一起,方便大家使用
服务端:

1. 检查是否安装过
rpm -qa nfs-utils rpcbind
2.  安装 + 创建贡献文件夹 + 更改文件夹用户和组 + 给文件夹赋权限
yum install -y nfs-utils rpcbind && mkdir -p /data && chown -R nfsnobody:nfsnobody /data && chmod 766 /data	
3. 修改配置(/etc/exports),添加  (/data 172.27.57.0/24(rw,sync,all_squash))
4. 编辑(/etc/sysconfig/nfs)去掉端口MOUNTD_PORT=892前面的#
5. 开启防火墙端口 +  重启防火墙 + 
firewall-cmd --zone=public --add-port=111/udp --add-port=892/tcp --add-port=2049/tcp --permanent && firewall-cmd --reload && firewall-cmd --permanent --list-port
6. 启动 rpcbind nfs 服务 + 查看服务状态 + 设置开机自启 
 systemctl start rpcbind && systemctl start nfs && systemctl statusrpcbind && systemctl status nfs && systemctl enable rpcbind && systemctl enable nfs 
7. 查看本地可挂载共享盘
showmount -e localhost

客户端:

1. 检查是否安装 nfs-utils
rpm -qa nfs-utils
2. 安装 nfs-utils
yum install -y nfs-utils
3. 查看可挂载列表
showmount -e 172.27.57.39 
4. 挂载
mount -t nfs 172.27.57.39:/data /nfs && df -h 
5. 设置开机自动挂载
vim /etc/fstab 
添加 (172.27.57.39:/data /nfs nfs defaults 0 0)

注意:成功挂载网络盘后,无论是客户端还是服务端,请重启服务器,看是否能正常工作

猜你喜欢

转载自blog.csdn.net/weixin_47236888/article/details/131060172