1.NFS简介
NFS是Network File System的缩写及网络文件系统。主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。
NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中.
大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS,ceph
2.应用
1.用户访问NFS客户端,将请求转化为函数
2.NFS通过TCP/IP连接服务端
3.NFS服务端接收请求,会先调用portmap进程进行端口映射
4.Rpc.nfsd进程用于判断NFS客户端能否连接服务端;
5.Rpc.mount进程用于判断客户端对服务端的操作权限
6.如果通过权限验证,可以对服务端进行操作,修改或读取
3.DFS搭建
开启m01, nfs, web01, web02, web03,
五台主机
一定要启动 v*p*n 不然 xshell 无法使用 172.16.1.xxx的局域网
3.1服务端
[root@nfs ~]
[root@nfs ~]
[root@nfs ~]
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
:wq
格式:
[挂载点] [可以访问的IP]([权限])
[root@nfs ~]
[root@nfs ~]
[root@nfs ~]
[root@nfs ~]
[root@nfs ~]
Export list for 172.16.1.31:
/web/nfs1 172.16.1.0/20
[root@nfs ~]
Export list for nfs:
/web/nfs1 172.16.1.0/20
[root@nfs ~]
[root@nfs ~]
/web/upload 172.16.1.0/20(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=666,anongid=666,sec=sys,rw,secure,root_squash,all_squash)
[root@nfs ~]
/usr/bin/systemctl start nfs-server
/usr/bin/systemctl start rpcbind
[root@nfs ~]
3.2客户端
[root@web01 ~]
[root@web01 opt]
[root@web01 opt]
[root@web01 opt]
[root@web01 opt]
[root@web01 nfs]
# 重复上面的步奏,挂载到另外两台计算机去
4.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,必须存在系统 (常用) |
配置文件
/etc/exports
设置完后都重新挂载一次不会创建文件操作会卡
4.1读写
控制读写
rw ro
[root@nfs nfs1]
/web/nfs1 172.16.1.0/20(ro,sync,all_squash)
:wq
[root@nfs nfs1]
[root@web01 opt]
[root@web01 opt]
[root@web01 opt]
[root@web01 nfs]
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 4 19:00 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 4 19:11 2.txt
[root@web01 nfs]
touch: cannot touch ‘3.txt’: Read-only file system
[root@nfs nfs1]
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
:wq
[root@nfs nfs1]
[root@web01 nfs]
[root@web01 opt]
[root@web01 opt]
[root@web01 opt]
[root@web01 nfs]
[root@web01 nfs]
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 4 19:00 1.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 4 19:11 2.txt
-rw-r--r-- 1 nfsnobody nfsnobody 0 Jan 4 2022 3.txt
4.2制文件权限
root_squash
no_root_squash
all_squash 常用
no_all_squash 不常用,不好测试
[root@nfs nfs1]
/web/nfs1 172.16.1.0/20(rw,sync,root_squash)
:wq
[root@nfs nfs1]
[root@web01 opt]
[root@web01 nfs]
...
[root@nfs nfs1]
/web/nfs1 172.16.1.0/20(rw,sync,no_root_squash)
:wq
[root@nfs nfs1]
[root@web01 nfs]
4.3控制写模式
sync 常用
async 不常用
4.4控制用户
anonuid
anongid
groupadd www -g 666
useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@nfs nfs1]
[root@nfs nfs1]
total 0
-rw-r--r-- 1 www www 0 Jan 4 19:00 1.txt
-rw-r--r-- 1 www www 0 Jan 4 19:11 2.txt
-rw-r--r-- 1 www www 0 Jan 4 19:31 3.txt
-rw-r--r-- 1 www www 0 Jan 4 19:42 4.txt
-rw-r--r-- 1 www www 0 Jan 4 20:30 5.txt
[root@nfs nfs1]
/web/nfs1 172.16.1.0/20(rw,sync,all_squash,anonuid=666, anongid=666)
:wq
[root@nfs nfs1]
[root@web01 opt]
[root@web01 opt]
[root@web01 opt]
[root@web01 nfs]
[root@web01 nfs]
...
-rw-r--r-- 1 www www 0 Jan 4 20:49 6.txt
5.搭建考试系统
5.1搭建WEB服务
[root@web01 opt]
[root@web01 opt]
* 需要借助 lrzsz 工具才能拖动
[root@web01 html]
Archive: kaoshi.zip
inflating: info.php
inflating: bg.jpg
inflating: index.html
inflating: upload_file.php
root@web01 html]
[root@web01 html]
[root@web01 html]
total 80
-rw-r--r-- 1 www www 38772 Apr 27 2018 bg.jpg
-rw-r--r-- 1 www www 2633 May 4 2018 index.html
-rw-r--r-- 1 www www 52 May 10 2018 info.php
-rw-r--r-- 1 www www 26995 Dec 30 11:31 kaoshi.zip
drwxr-xr-x 2 www www 6 Jan 4 21:24 upload
-rw-r--r-- 1 www www 1192 Jan 10 2020 upload_file.php
[root@nfs ~]
setenforce: SELinux is disabled
[root@nfs ~]
[root@web01 html]
User www
Group www
[root@web01 html]
172.16.1.7
(禁用浏览器缓存不然,流量器会优先从缓存中取已经存在的信息,就不去访问服务器.)
[root@web01 ~]
/usr/bin/setenforce 0
/usr/bin/systemctl disable --now firewalld
/usr/bin/systemctl start httpd
[root@web01 ~]
1.上传 文件格式 xx_xx.xxx 最少有两段,中间下划线分开的格式
[root@web01 html]
[root@web01 upload]
total 160
-rw-r--r-- 1 www www 163333 Jan 4 21:57 1_web01.jpg
2.浏览器访问
http://172.16.1.7/upload/1_web01.jpg
[root@web01 upload]
# web02 web03 重复上面的步骤每各自上传一张图片
172.16.1.8 web02 上传 2_web02.jpg
172.16.1.9 web03 上传 3_web02.jpg
检测
http://172.16.1.8/upload/2_web02.jpg
http://172.16.1.9/upload/3_web03.jpg
目前数据还无法互通
http://172.16.1.7/upload/3_web03.jpg
http://172.16.1.8/upload/3_web03.jpg
5.2配合NFS实现文件共享
[root@nfs nfs1]
/web/upload 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
:wq
[root@nfs nfs1]
[root@nfs nfs1]
[root@nfs nfs1]
[root@web01 html]
[root@web02 html]
[root@web03 html]
[root@web01 html]
[root@web02 html]
[root@web03 html]
# 6.测试需要上传新的文件,之前的没有同步
(文件不要大于400kb,我预估的不知考试系统代码谁写的我拿2.3M的传不上去)
172.16.1.9 上传文件 7/8访问
http://172.16.1.7/upload/0_web.jpg
http://172.16.1.8/upload/0_web.jpg
5.3自动挂载
[root@web01 ~]
/usr/bin/setenforce 0
/usr/bin/systemctl disable --now firewalld
/usr/bin/mount -t nfs 172.16.1.31:/web/upload /var/www/html/upload
[root@web01 ~]
[root@web02 ~]
172.16.1.31:/web/upload /var/www/html/upload nfs defaults 0 0
[root@web02 ~]
[root@web02 ~]
* nfs 一定要先开启,不然加载不到
在启动是会做加载的动作,有一个时间限制,在这个时间内没有启动成功就跳过.