Linux部署NFS服务共享文件

NFS(网络文件系统)用于linux共享文件

第1步:配置所需要的环境

使用两台Linux主机

主机名称 操作系统 IP地址
NFS Centos7 192.168.218.139
NFSa Centos7 192.168.218.140

配置好yum源,安装nfs服务,清空nfs服务器上iptables防火墙默认策略。

[root@NFS ~]# yum install nfs-utils -y

第2步:在NFS服务器上建立用于NFS文件共享的目录,并设置足够的权限确保其他人也有写入权限。

[root@NFS ~]# mkdir /nfs
[root@NFS ~]# chmod -Rf 777 /nfs

[root@NFS ~]# echo "Welcome to purple" > /nfs/hello

第3步:编辑NFS服务程序的配置文件/etc/exports。

配置NFS服务程序配置文件的参数

参数 作用
ro 只读
rw 读写
root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户
no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员
all_squash 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户
sync 同时将数据写入内存与硬盘中,保证不丢失数据
async 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据

[root@NFS ~]# vim /etc/exports

/nfs 192.168.218.0/24(rw,sync,root_squash)
第4步:启动和启动NFS服务程序。由于在使用NFS服务进行文件共享之前,需要使用RPC(Remote Procedure Call,远程过程调用)服务将NFS服务器的IP地址和端口信号等信息发送给客户端。

[root@NFS ~]# systemctl restart rpcbind
[root@NFS ~]# systemctl enable rpcbind
[root@NFS ~]# systemctl start nfs-server
[root@NFS ~]# systemctl enable nfs-server
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

NFS客户端使用showmount命令查询NFS服务器的远程共享信息,其输出格式为“共享的目录名称  允许使用客户端地址”。

showmount命令中可用的参数以及作用

参数 作用
-e 显示NFS服务器的共享列表
-a 显示本机挂载的文件资源情况
-v 显示版本号

[root@NFSa ~]# showmount -e 192.168.218.139
Export list for 192.168.218.139:
/nfs 192.168.218.*

在NFS客户端创建一个挂载目录。

[root@NFSa ~]# mkdir /nfs

[root@NFSa ~]# mount -t nfs 192.168.218.139:/nfs /nfs

[root@NFSa ~]# cat /nfs/hello
Welcome to purple

可以将此写入开机自动挂载文件中

[root@NFSa ~]# vim /etc/fstab

192.168.10:/nfs         /nfs                    nfs     defaults        0 0

猜你喜欢

转载自www.cnblogs.com/DevonL/p/11204478.html