NFS 共享存储服务
NFS :network file system,在计算机网络中共享文件系统的的协议。
计算机之间可以通过网络共享目录和文件。分为两个部分:
- rpcbind :远程共享调用,端口号:111
- nfs :共享服务,端口号:2049(隐藏端口,查询不到)
启动服务需要先启动rpcbind服务,启用远程共享调用,再启动nfs服务。
nfs的配置文件:/etc/exports
服务端发起共享的配置方法
主机:test1
@20.0.0.10/24
,作为服务端发起共享
mkdir /opt/share
#准备共享的文件夹,可自定义
chmod 777 /opt/share
#为共享文件夹赋权
vim /etc/exports
#编辑nfs的配置文件
/opt/share 20.0.0.0/24(rw,sync,no_root_squash)
对编辑内容的解释
-
/opt/share :声明本机的共享目录
-
20.0.0.0/24 :声明网段,该网段的主机都可以访问本机的共享目录。(作业:声明指定的主机可以访问共享目录)
-
(rw,sync,no_root_squash) :共享目录的使用者的权限
-
rw :读写
-
ro :read-only,只读
-
sync :同步写入到硬盘中
-
root_squash :默认的,客户机以root用户访问共享目录,只获得匿名用户权限
-
no_root_squash :如果客户机以root用户访问共享目录,获得和本机的root用户一样的权限
-
all_squash :所有访问用户都映射为匿名用户或用户组
-
async : 将数据先保存在内存缓冲区中,必要时才写入磁盘。
-
subtree_check(默认) :若输出目录是一个子目录,则nfs服务器将检查其父目录的权限。
-
no_subtree_check :即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率。
-
更改配置文件要重启服务:
systemctl restart rpcbind
#先重启rpcbind
systemctl restart nfs
#再重启nfs
showmount -e
#查看本机共享出去的目录,
#在客户机查看主机暴露出的共享目录需要接主机IP
客户端获取共享的方法
客户端把目标主机共享出的目录挂载到本地目录,即可获取共享文件
客户端test2
@20.0.0.20/24
临时挂载到本地:
mount 20.0.0.10:/opt/share /opt/test2
解释:挂载源:20.0.0.10:/opt/share
,20.0.0.10
主机的共享目录/opt/share
showmount -e 20.0.0.10
可以查看目标主机暴露出的共享目录。
如果要永久挂载,编辑/etc/fstab
文件
vim /etc/fstab
#编辑内容
20.0.0.10:/opt/share /opt/test1 nfs defaults._netdev 0 0
#挂载源:共享文件夹 挂载点 文件系统 默认.挂载需要网络才能成功
指定客户端获取共享目录
问:要使nfs共享只对指定IP开放,怎么操作?
目标:
test1
@20.0.0.10/24
test2
@20.0.0.20/24
test3
@20.0.0.30/24
以test2
为服务端的主机,共享目录为/opt/share2
,共享对test3
开放,对test1
不开放
实现:
-
首先,对
test1
,test2
,test3
都关闭防火墙,关闭安全机制,都安装
rpcbind
,nfs-utils
软件systemctl stop firewalld setenforce 0 yum -y install rpcbind nfs-utils
-
对
test2
操作-
输入命令
mkdir /opt/share2 chmod 777 /opt/share2 vim /etc/exports #编辑文本内容 /opt/share 20.0.0.30/32(rw,sync,no_root_squash)
指定主机
20.0.0.30/32
(不加/32也能实现要求) -
重启服务
systemctl restart rpcbind systemctl restart nfs
-
查看本机共享出去的目录
[root@test2 share2]# showmount -e Export list for test2: /opt/share2 20.0.0.30/32
-
-
对
test3
操作-
挂载共享文件夹到本地目录
mkdir /opt/test3 mount 20.0.0.20:/opt/share2 /opt/test3
挂载成功。
-
df -hT
查看挂载情况(图略)
-
-
对
test1
操作挂载共享文件夹到本地目录
mkdir /opt/test1 mount 20.0.0.20:/opt/share2 /opt/test1
挂载失败,报错:
[root@test1 opt]# mount 20.0.0.20:/opt/share2 /opt/test1 mount.nfs: access denied by server while mounting 20.0.0.20:/opt/share2
-
实现对指定IP主机共享功能。