一、NFS服务介绍

NFS是 Network File system的缩写

分为2.3.4三个版本,2和3由sun公司起草开发,4.0开始netapp公司参与并主导开发

NFS数据传输基于RPC协议:

应用场景:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。

nfs服务默认端口号_运维


NFS原理图:(NFS服务不监听任何端口,但是RPC服务中的模块,rpcbind默认监听111端口,)

nfs服务默认端口号_网络_02


二、NFS服务端和客户端配置

[root@litongyao ~]# yum install -y nfs-utils         (客户端和服务端都要按章这个包)

接下来在服务端编辑:
[root@fuwuduan ~]# vim /etc/exports
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
# 第一段来定义共享目录的绝对路径  第二段指定ip和一些选项
# rw 读写
# ro 只读
# sync 同步模式,内存数据实时写入磁盘
# async 非同步模式
# no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
# root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
# all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
# anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

保存配合文件以后,因为共享目录不存在,所以做以下操作。
[root@fuwuduan ~]# mkdir /home/nfstestdir                       (创建共享目录)
[root@fuwuduan ~]# chmod 777 /home/nfstestdir/                    (权限设置为777)

我们可以看一下监听的端口
[root@fuwuduan ~]# netstat -lntp
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      4451/rpcbind
rpcbind监听的111端口。

启动服务并设置开机启动:
[root@fuwuduan ~]# systemctl start nfs
[root@fuwuduan ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

查看进程
[root@fuwuduan ~]# ps ax |grep nfs
 6209 ?        S<     0:00 [nfsd4_callbacks]
 6215 ?        S      0:00 [nfsd]
 6216 ?        S      0:00 [nfsd]
 6217 ?        S      0:00 [nfsd]
 6218 ?        S      0:00 [nfsd]
 6219 ?        S      0:00 [nfsd]
 6220 ?        S      0:00 [nfsd]
 6221 ?        S      0:00 [nfsd]
 6222 ?        S      0:00 [nfsd]
 6264 pts/1    R+     0:00 grep --color=auto nfs

客户端操作:
[root@kehu ~]# showmount -e 192.168.52.101     (查看远程共享信息)
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
我们发现报错,这是由于网络不通造成,是由防火墙导致我,所以我们把客户端和服务端的防火墙和selinux关闭
[root@fuwuduan ~]# systemctl stop firewalld
[root@fuwuduan ~]# setenforce 0

下面就可以正常了。
[root@kehu ~]# showmount -e 192.168.52.101    (我们可以看到ip为101的机器上为我们共享的目录)
Export list for 192.168.52.101:
/home/nfstestdir 192.168.52.100/24

挂载目录并与远程共享目录同步:
[root@kehu ~]# mount -t nfs 192.168.52.101:/home/nfstestdir /mnt/  (指定格式是nfs)
查看挂载目录:
[root@kehu ~]# df -h
文件系统                         容量  已用  可用 已用% 挂载点
/dev/sda3                         18G  4.7G   14G   27% /
devtmpfs                         483M     0  483M    0% /dev
tmpfs                            493M     0  493M    0% /dev/shm
tmpfs                            493M   13M  480M    3% /run
tmpfs                            493M     0  493M    0% /sys/fs/cgroup
/dev/sda1                        197M  109M   88M   56% /boot
tmpfs                             99M     0   99M    0% /run/user/0
192.168.52.101:/home/nfstestdir   18G  3.6G   15G   20% /mnt

实验:
在客户端建立一个文件,看看服务端有没有

https://blog.51cto.com/13407306/2061707