nfs01服务器
服务端

安装
yum -y install nfs-utils rpcbind
rpm -qa nfs-utils rpcbind
启动RPC
systemctl start rpcbind
systemctl enable rpcbind

查看注册端口
rpcinfo -p 127.0.0.1

启动nfs
systemctl start nfs
systemctl enable nfs
实践:
[root@nfs01 ~]$tail /etc/exports
/data 172.16.1.0/24(rw,sync)

[root@nfs01 ~]$mkdir -p /data
重新加载nfs
[root@nfs01 ~]$systemctl reload nfs
或者
[root@nfs01 ~]$exportfs -rv
授权
[root@nfs01 ~]$chown -R nfsnobody.nfsnobody /data/
[root@nfs01 ~]$ll -d /data/
检查
[root@nfs01 ~]$showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
服务本地挂载(测试)
mount -t nfs 172.16.1.31:/data /mnt

web01、web02挂载

安装
yum -y install nfs-utils rpcbind
rpm -qa nfs-utils rpcbind

启动RPC即可

systemctl start rpcbind
systemctl enable rpcbind
[root@web01 ~]$showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24

挂载

mount -t nfs 172.16.1.31:/data /mnt

NFS客户端开机自启动

1.放在/etc/rc.local
chmod +x /etc/rc.local
echo "/bin/mount -t nfs 172.16.1.31:/data /mnt" >> /etc/rc.local
tail -2 /etc/rc.local
2.放在/etc/fstab(坑)
加载/etc/fstab >> 加载防火墙 >> 加载网卡
添加以下内容
172.16.1.31:/data                        /mnt                     nfs     defaults        0 0
延迟启动
systemctl list-unit-files |grep remote-fs.target
systemctl enable remote-fs.target
reboot
测试好,记得删除,后面部署web网站的时候,重新指定路径挂载
3.在/etc/fstab 挂载的时候添加一个_netdev参数
172.16.1.31:/data                        /mnt                     nfs     defaults,_netdev        0 0
#说明
_netdev       The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount
              these filesystems until the network has been enabled on the system).

项目实践2:
NFS共享匿名用户用www,使得客户端上传的图片都是www用户,而不是匿名nfsnobody
web01、web02客户端实现挂载nfs
NFS下面共享/data,允许web01、web02客户端对/backup可读写
web01上传图片,web02上可以删除web01上传的图片
NFS下面贡献/data1,允许web01、web02客户端192.168.238.0/24网段只读(data1)
实现开机挂载

操作:
1,所有主机(web01、web02、nfs01)上创建www用户,指定uid为1111

useradd -u 1111 -s /sbin/nologin -M www
id www

2.NFS01特殊配置

[root@nfs01 data]$cat /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=1111,anongid=1111)
/data1 192.168.238.0/24(ro)

3.创建目录并检查

[root@nfs01 /]$mkdir -p /data /data1
[root@nfs01 /]$chown -R www.www /data
[root@nfs01 /]$ll -d /data
drwxr-xr-x 2 www www 166 Nov  8 23:38 /data

4,服务端重新加载(平滑重启)

[root@nfs01 /]$systemctl reload nfs

5.web01、web02配置挂载
客户端分别安装:

yum -y install rpc-bind nfs-utils
systemctl start rpcbind
systemctl enable rpcbind

检查

showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data1 192.168.238.0/24
/data  172.16.1.0/24

web01、web02客户端分别创建/data 目录

mkdir /data
挂载
mount -t nfs 172.16.1.31:/data /data

客户端只读/data1配置

mkdir /data1
mount -t nfs 192.168.238.31:/data1 /data1

NFS客户端开机自启动

放在/etc/fstab(坑)
添加以下内容
172.16.1.31:/data                        /mnt                     nfs     defaults        0 0
延迟启动
systemctl list-unit-files |grep remote-fs.target
systemctl enable remote-fs.target
reboot

查看nfs挂载情况

cat /proc/mounts

NFS共享权限有两部分控制
1.配置文件的rw,ro控制
2.本地目录和文件权限

NFS强耦合,服务端有问题,NFS客户端容易夯住