首页 > 技术文章 > linux NFS网络存储最详细讲解

garyhtml 2021-12-30 21:07 原文

NFS网络存储

# 定义: 
NFS 就是 Network FileSystem 的缩写,最早之前是由 Sun 这家公司所发展出来的 (注1)。 它最大的功能就是可以透过网络,让不同的机器、不同的操作系统、可以彼此分享个别的档案 (share files)。所以,你也可以简单的将他看做是一个文件服务器 (file server) 呢!这个 NFS 服务器可以让你的 PC 来将网络远程的 NFS 服务器分享的目录,挂载到本地端的机器当中, 在本地端的机器看起来,那个远程主机的目录就好像是自己的一个磁盘分区槽一样 (partition)!使用上面相当的便利!

就如同上面的图示一般,当我们的 NFS 服务器设定好了分享出来的 /home/sharefile 这个目录后,其他的 NFS 客户端就可以将这个目录挂载到自己系统上面的某个挂载点 (挂载点可以自定义),例如前面图示中的 NFS client 1 与 NFS client 2 挂载的目录就不相同。我只要在 NFS client 1 系统中进入 /home/data/sharefile 内,就可以看到 NFS 服务器系统内的 /home/sharefile 目录下的所有数据了 (当然,权限要足够啊!^_^)!这个 /home/data/sharefile 就好像 NFS client 1 自己机器里面的一个 partition 喔!只要权限对了,那么你可以使用 cp, cd, mv, rm... 等等磁盘或档案相关的指令!

NFS应用

1.用户访问NFS客户端,将请求转化为函数
2.NFS通过TCP/IP连接服务器
3.NFS服务端接受请求,会先调用portmap进程进行端口映射
4.Rpc.nfsd进程用于判断NFS客户端能否连接服务端
5.Rpc.mount进程用于判断客户端对服务端的操作权限
6.如果通过权限验证,可以对服务端进行操作,修改或读取。

NFS实践

服务端

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

2、创建挂载点
[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,anonuid=666,anongid=666)

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

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

客户端

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/

4、测试NFS文件同步功能
如图:在web01 创建文件 ,同时web02也同样创建了文件。

这样就成功啦

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、控制读写
rw、ro

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

3、控制写模式
sync
async

4、控制用户
anonuid
anongid

应用项目:

#  搭建考试系统。
搭建web服务。

1、安装web软件
[root@web01 opt]# yum install httpd php php-devel -y

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

# 上传代码
这里已经提前写过了 直接copy过来 使用lrzsz或者使用xftp (文件丢到最后)

3、授权
[root@web01 html]# chown -R www.www /var/www/html

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

5、修改web软件的用户
[root@web01 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www

6、启动web软件
[root@web01 html]# systemctl start httpd

7、测试

	1、上传文件
	
	2、访问 验证结果
    http://172.16.1.7/upload/1_linux.jpg

实现文件共享

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

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软件
[root@web01 html]# yum install nfs-utils -y
[root@web02 html]# yum install nfs-utils -y
[root@web03 html]# yum install nfs-utils -y

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

6、测试
用web2上传,web3查看

这里就实现到了共享。

推荐阅读