NFS数据共享(全面讲解使用教程)

一:NFS数据共享

1.NFS简介:
NFS是Network File System的缩写及网络文件系统。NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。

NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS,ceph

image

2.什么是NFS?
NFS就是Network File System的缩写,它最大的功能就是可以通过网络,让不同的机器、不同的操作系统可以共享彼此的文件。

​ NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中来看,那个远程主机的目录就好像是自己的一个磁盘分区一样,在使用上相当便利;

NFS一般用来存储共享视频,图片等静态数据。

image

  • 如上图:
当我们在NFS服务器设置好一个共享目录/home/public后,其他的有权访问NFS服务器的NFS客户端就可以将这个目录挂载到自己文件系统的某个挂载点,这个挂载点可以自己定义,如上图客户端A与客户端B挂载的目录就不相同。并且挂载好后我们在本地能够看到服务端/home/public的所有数据。如果服务器端配置的客户端只读,那么客户端就只能够只读。如果配置读写,客户端就能够进行读写。挂载后,NFS客户端查看磁盘信息命令:df –h。
3.NFS的应用
1.用户访问NFS客户端,将请求转化为函数
2.NFS通过TCP/IP连接服务端
3.NFS服务端接收请求,会先调用portmap进程进行端口映射
4.Rpc.nfsd进程用于判断NFS客户端能否连接服务端;
5.Rpc.mount进程用于判断客户端对服务端的操作权限
6.如果通过权限验证,可以对服务端进行操作,修改或读取
  • NFS客户端和NFS服务器的过程:
    image

二:NFS数据共享实践

  • 服务端
1、安装NFS和rpcbind
[root@nfs ~]# yum install nfs-utils rpcbind -y

2、创建挂载点(挂载的目录)
[root@nfs ~]# mkdir /web
[root@nfs ~]# mkdir /web/nfs{1..9}

3、配置挂载点
[root@nfs ~]# vim /etc/exports
格式:
[挂载点] [可以访问的IP]([权限])
/web/nfs1  172.16.1.0/20(rw,sync,all_squash)

4、关闭selinux和防火墙
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld

重启
[root@nfs ~]# systemctl restart nfs-server rpcbind

5、启动Nfs和rpcbind服务
[root@nfs ~]# systemctl start nfs-server 
[root@nfs ~]# systemctl start rpcbind

6、检查服务端是否正常
[root@nfs ~]# showmount -e [服务端的地址,默认是本机地址]

[root@nfs ~]# showmount -e
结果为正常:
Export list for nfs:
/web/nfsv1 172.16.1.0/20
[root@nfs ~]# showmount -e 172.16.1.31
结果为正常:
Export list for 172.16.1.31:
/web/nfsv1 172.16.1.0/20

[root@nfs ~]# cat /var/lib/nfs/etab

7、给挂载点授权
[root@nfs ~]# chown -R nfsnobody.nfsnobody /web

chown		: 将指定文件的拥有者改为指定的用户或组
-R		    : 指定目录以及其子目录下的所有文件
/web		: 指定目录文件
  • 客户端
1、安装NFS
[root@web01 opt]# yum install -y nfs-utils

2、创建目录
[root@web01 opt]# mkdir /opt/nfs/

3、挂载NFS
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1  /opt/nfs/

解析:
-t		: 指定文件设备类型
nfs		: 类型
172.16.1.31:/web/nfs1	: 挂载点
/opt/nfs/	: 挂载到该,目录下
将NFS/web/nfs1挂载到 web1主机opt/nfs/目录下
实现目录下的数据共享
如删除将都不存在

4、测试NFS文件同步功能
[root@web01 opt]# touch nfs/{1..9}.txt
[root@web01 opt]# ll nfs/

结果为正常:
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt
服务端
[root@nfs ~]# ll /web/nfs1

结果为正常:
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt

二:NFS配置详解

nfs共享参数 参数作用
rw 读写权限 (常用)
ro 只读权限 (不常用)
root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户 (不常用)
no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员 (不常用)
all_squash 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户 (常用)
no_all_squash 无论NFS客户端使用什么账户访问,都不进行压缩 (不常用)
sync 同时将数据写入到内存与硬盘中,保证不丢失数据 (常用)重要数据使用
async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 (不常用)不重要数据使用
anonuid 配置all_squash使用,指定NFS的用户UID,必须存在系统 (常用)
anongid 配置all_squash使用,指定NFS的用户GID,必须存在系统 (常用)
  • 配置解析
1.控制文件权限

root_squash(当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户)

1.进入 vim /etc/exports 修改配置文件(NFS)
/web/nfs1  172.16.1.0/20(rw,sync,root_squash)
2.重启
systemctl restart nfs-server rpcbind

1.nfs内创建(1-9)文件(客户端)
[root@web01 nfs]# touch {1..9}txt
2.wed(客户端)之前挂载过,先取消挂载
[root@web01 opt]# umount /opt/nfs/
3.实现共享文件
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/

* 测试共享文件
1.客户端
ll nfs



* 测试共享文件
1.服务端
ll /web/nfs1

[root@nfs nfs1]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt

no_root_squash(当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员)

.进入 vim /etc/exports 修改配置文件(NFS)
/web/nfs1  172.16.1.0/20(rw,sync,no_root_squash)
2.重启
systemctl restart nfs-server rpcbind

1.nfs内创建文件(客户端)
[root@web01 nfs]# touch 11.txt
2.wed(客户端)之前挂载过,先取消挂载
[root@web01 opt]# umount /opt/nfs/
3.实现共享文件
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
[root@web01 opt]# touch nfs/11.txt

* 测试共享文件
1.客户端
ll nfs


* 测试共享文件
1.服务端
ll /web/nfs1
 [root@nfs web]# ll nfs1
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:52 10.txt
-rw-r--r-- 1 root      root      0 Dec 30 16:02 11.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt

all_squash(无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户

1.进入 vim /etc/exports 修改配置文件(NFS)
/web/nfs1  172.16.1.0/20(rw,sync,all_squash)
2.重启
systemctl restart nfs-server rpcbind


1.wed(客户端)之前挂载过,先取消挂载
[root@web01 opt]# umount /opt/nfs/
2.实现共享文件
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
3.创建新用户
[root@web01 opt]# useradd test
4.切换用户
[root@web01 opt]# su - test
5.创建新文件
[test@web01 opt]$ touch nfs/12.tct

[test@web01 nfs]$ ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:52 10.txt
-rw-r--r-- 1 root      root      0 Dec 30 16:02 11.txt
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Dec 30 16:14 12.tct
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt

no_all_squash(无论NFS客户端使用什么账户访问,都不进行压缩 (其他默认压缩))

服务端(修改共享文件权限(普通用户无法修改使用))
[root@nfs nfs1]# chmod 000 ./*
[root@nfs nfs1]# ll
total 4
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:52 10.txt
---------- 1 root      root      0 Dec 30 16:02 11.txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 16:14 12.txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
---------- 1 nfsnobody nfsnobody 4 Dec 30 16:28 1.txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt

客户端(普通用户无法使用)
[test@web01 nfs]$ echo 123 > 1.txt
-bash: 1.txt: Permission denied
[test@web01 nfs]$ ll
total 4
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:52 10.txt
---------- 1 root      root      0 Dec 30 16:02 11.txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 16:14 12.txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 1txt
---------- 1 nfsnobody nfsnobody 4 Dec 30 16:28 1.txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 2txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 3txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 4txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 5txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 6txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 7txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 8txt
---------- 1 nfsnobody nfsnobody 0 Dec 30 15:45 9txt

image

三:配置文件分类

1、控制读写
rw、ro

2、控制文件权限
root_squash
no_root_squash
all_squash
no_all_squash

3、控制写模式
sync
async

4、控制用户
anonuid
anongid

四:NFS统一用户

  • 简介:
统一用户,为了平台统一 一个id 保证组内用户能够完全访问共享数据权限
1、创建用户(客户端与服务端 都要输入)
[root@nfs nfs1]# groupadd www -g 666
[root@nfs nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin ​

image

2.服务端写入配置
[root@nfs ~]# vim /etc/exports
/web/nfs1  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)​

image

3.重启(服务端)
systemctl restart nfs-server rpcbind​
4.修改挂载点权限(客户端)
[root@nfs nfs1]# chown -R www.www /web/
3、客户端使用
root@web01 opt]# touch nfs/13.txt
[root@web01 opt]# ll nfs

image

  • 服务器与客户端区分
nfs		: 服务端(数据共享服务器)
m01		: OpenVPN中央服务器
wed01	:客户端1
wed02	:客户端2
wed3	:客户端3

五:搭建WEB服务(实现数据同步共享)

  • 搭建考试系统(以考试系统为例)
1、安装web软件(客户端同按以下步骤)
[root@web01 opt]# yum install httpd php php-devel -y

image

2.将代码放置于网站的根目录
2、将代码放置于网站的根目录
[root@web01 opt]# cd /var/www/html/

# 上传代码到 /var/www/html/ 目录下
两种方式:
1.拉取文件到该目录下
2.xftp
协议:SFTP

image

3.压缩包方式需要解压

image

4.授权
[root@web01 html]# chown -R www.www /var/www/html
5.关闭selinux和防火墙
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld
6.修改web软件的用户
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www

image

7.启动web软件
systemctl start httpd
8.测试

内网链接,开启OPENVPN
1、上传
image
2、访问
http://172.16.1.7/upload/1_linux.jpg
···
image

六:实现文件共享前操作

1.将客户端/var/www/html 考试系统传输给另两个客户端。
[root@web01 html]# scp kaoshi.zip 172.16.1.8:/var/www/html/

[root@web01 html]# scp kaoshi.zip 172.16.1.9:/var/www/html/

image

2.web2解压压缩包
3.web3解压压缩包

image

  • web与web3执行一遍WEB服务流程

七:配合NFS实现文件数据同步共享

1、修改NFS配置文件(服务端)
[root@nfs nfs1]# vim /etc/exports
/web/upload  172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)

image

2、创建挂载点
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload
3.重启NFS
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
4.客户端安装NFS软件
wed01
[root@web01 html]# yum install nfs-utils -y
wed02
[root@web02 html]# yum install nfs-utils -y
wed03
[root@web03 html]# yum install nfs-utils -y

image

5、挂载
[root@web01 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web02 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web03 html]# mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload

image

6.数据共享测试

用web2上传,web3查看
image

posted @ 2021-12-30 21:08  AlexEvans  阅读(2887)  评论(0编辑  收藏  举报