什么是NFS如何部署以及实现多主机文件同步共享? 这里都有!大量案例帮你理解NFS,文末有狗狗帅照!
部署NFS实现多主机文件共享,Web01、Web02、Web03做示例客户端,实现功能如下:
NFS是Network File System的缩写及网络文件系统。NFS主要功能是通过局域网络让不同的主机系统之间可以共享文件或目录。
NFS系统和Windows网络共享、网络驱动器类似, 只不过windows用于局域网, NFS用于企业集群架构中, 如果是大型网站, 会用到更复杂的分布式文件系统FastDFS,glusterfs,HDFS,ceph。
这里的服务端是存文件的,服务端里没有安全认证但是有权限认证,客户端查找文件先从本地找,如果没有去服务端,从服务端找到返回到客户端~(portmap不需要安装,NFS自带了)
[root@nfs ~]# yum install nfs-utils rpcbind -y
# 根下创建
[root@nfs ~]# mkdir -p /web/nfs{1..9}
[root@nfs ~]# vim /etc/exports
/etc/exports文件配置格式:
[挂载点] [可以访问的IP]([权限])
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
[root@nfs ~]# setenforce 0
[root@nfs ~]# systemctl disable --now firewalld
[root@nfs ~]# systemctl start nfs-server
[root@nfs ~]# systemctl start rpcbind
# 格式:showmount -e [服务端的地址,默认是本机地址]
[root@nfs ~]# showmount -e
Export list for nfs:
/web/nfs1 172.16.1.0/20
# 可以跟着ip地址
[root@nfs ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web/nfs1 172.16.1.0/20
[root@nfs ~]# cat /var/lib/nfs/etab
[root@nfs ~]# chown -R nfsnobody.nfsnobody /web
[root@web01 opt]# yum install -y nfs-utils
[root@web01 opt]# mkdir /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 在web01中、在/opt/nfs/目录下创建文件,到NFS服务端/web/nfs1/目录下查看是否同步
[root@web01 opt]# touch /opt/nfs/test{1..9}.txt
web02和web03客户端同样的操作,使用同样的挂载点,最终实现网络同步存储
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,必须存在系统 (常用) |
所有的参数都是在NFS服务端中的文件/etc/exports中修改,修改完成服务端(NFS)需要重启nfs-server
和 rpcbind
服务,客户端需要卸载挂载
和重新挂载
# NFS部署就是rw的案例
[root@nfs ~]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
# NFS服务端的操作
# 修改配置文件
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(ro,sync,all_squash)
# 重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind
# web01客户端的操作
# 查看挂载
[root@web01 nfs]# df -h
172.16.1.31:/web/nfs1 20G 3.1G 17G 16% /opt/nfs
# 卸载挂载,如果在nfs目录下卸载会报错:umount.nfs4: /opt/nfs: device is busy
[root@web01 /]# umount /opt/nfs/
# 再次挂载
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
[root@web01 opt]# touch /opt/nfs/test.txt
touch: cannot touch ‘/opt/nfs/test.txt’: Read-only file system
# 这样就创建不了,系统提示只读
控制文件权限案例
# 修改服务端配置文件参数
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,root_squash)
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind
# 客户端卸载和挂载
[root@web01 /]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 创建文件查看是否为匿名用户
[root@web01 opt]# touch nfs/test.txt
[root@web01 nfs]# ll /opt/nfs/
-rw-r--r-- 1 nfsnobody nfsnobody 0 Dec 30 17:01 test.txt
# 验证成功nfsnobody为匿名用户
# 修改服务端配置文件参数
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,no_root_squash)
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind
# 客户端卸载和挂载
[root@web01 /]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 创建文件查看是否为root用户
[root@web01 opt]# touch nfs/10.txt
[root@web01 nfs]# ll /opt/nfs/
-rw-r--r-- 1 root root 0 Dec 30 17:07 10.txt
# 验证成功用户为root
# 修改服务端配置文件参数
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,all_squash)
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind
# 客户端使用普通用户验证
[root@web01 /]# useradd hammer
[root@web01 nfs]# cat /etc/passwd
hammer:x:1000:1000::/home/hammer:/bin/bash
# 客户端卸载和挂载
[root@web01 /]# umount /opt/nfs/
[root@web01 opt]# mount -t nfs 172.16.1.31:/web/nfs1 /opt/nfs/
# 普通用户创建文件查看是否为匿名用户
[hammer@web01 nfs]$ touch 11.txt
[hammer@web01 nfs]$ ll
-rw-rw-r-- 1 nfsnobody nfsnobody 0 Dec 30 17:18 11.txt
# 验证成功,普通用户创建文件也是匿名用户
解决NFS如果取消文件权限,客户端用户不能操作的问题,使用统一用户(其实是固定统一用户id)解决
# NFS和web01-03中创建www用户和用户组
[root@nfs nfs1]# groupadd www -g 666
[root@nfs nfs1]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@nfs nfs1]# vim /etc/exports
/web/nfs1 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs nfs1]# chown -R www.www /web/
# 查看
[root@nfs web]# ll
total 0
drwxr-xr-x 2 www www 320 Dec 30 17:18 nfs1
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs2
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs3
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs4
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs5
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs6
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs7
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs8
drwxr-xr-x 2 www www 6 Dec 30 13:42 nfs9
# 服务端重启服务
[root@nfs nfs1]# systemctl restart nfs-server
[root@nfs nfs1]# systemctl restart rpcbind
# web01中,分别用root用户和hammer用户验证创建文件所属用户和用户组是谁
# 普通用户验证
[hammer@web01 nfs]$ touch 13.txt
[hammer@web01 nfs]$ ll
-rw-rw-r-- 1 www www 0 Dec 30 17:37 13.txt
# root用户验证
[root@web01 nfs]# touch 12.txt
[root@web01 nfs]# ll
-rw-r--r-- 1 www www 0 Dec 30 17:36 12.txt
# 验证成功,结果都为www用户
统一用户的实际案例
所有客户端都操作如下步骤
# 客户端下载
[root@web01 opt]# yum install httpd php php-devel -y
[root@web01 opt]# cd /var/www/html/
# 将文件解压
[root@web01 html]# ll
total 28
-rw-r--r-- 1 root root 26995 Dec 30 17:47 kaoshi.zip
[root@web01 html]# unzip kaoshi.zip
Archive: kaoshi.zip
inflating: info.php
inflating: bg.jpg
inflating: index.html
inflating: upload_file.php
[root@web01 html]# ll
total 80
-rw-r--r-- 1 root root 38772 Apr 27 2018 bg.jpg
-rw-r--r-- 1 root root 2633 May 4 2018 index.html
-rw-r--r-- 1 root root 52 May 10 2018 info.php
-rw-r--r-- 1 root root 26995 Dec 30 17:47 kaoshi.zip
-rw-r--r-- 1 root root 1192 Jan 10 2020 upload_file.php
[root@web01 html]# chown -R www.www /var/www/html
[root@web01 html]# ll
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 17:47 kaoshi.zip
-rw-r--r-- 1 www www 1192 Jan 10 2020 upload_file.php
[root@web01 html]# setenforce 0
setenforce: SELinux is disabled
[root@web01 html]# systemctl disable --now firewalld
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
将User apache 和 Group apache 改为 User www 和 Group www
# 注.不然不会同步文件,出错!!
[root@web01 html]# systemctl start httpd
访问成功!
[root@web01 html]# mkdir upload
[root@web01 html]# chown www.www upload
# 重启服务
[root@web01 html]# systemctl restart httpd
# 从考试系统上传图片,验证是否上传到upload目录下,并且用 http://客户端ip/upload/文件名 访问到文件
[root@web01 upload]# ll
total 204
-rw-r--r-- 1 www www 205464 Dec 30 18:22 2_dog.jpg
验证成功!!!
所有客户端搭建完NFS,可以在自己的所有客户端上传验证文件,我分别在web01,web02和web03上传了二哈,吉娃娃和杜宾图片,用来验证
在服务端搭建NFS,实现多主机文件共享,通过一台客户端就能看到所有的狗狗帅照!!!
[root@nfs nfs1]# vim /etc/exports
/web/upload 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
[root@nfs nfs1]# mkdir /web/upload
[root@nfs nfs1]# chown www.www /web/upload
[root@nfs nfs1]# systemctl restart nfs-server rpcbind
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y
[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
三台客户端主机上传文件成功,客户端之间实现同步共享!
网页验证结果如下
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.cnblogs.com/48xz/p/15750226.html
内容来源于网络,如有侵权,请联系作者删除!