nfs网络文件系统可以实现远程资源的共享,这样使我们去访问资源时,得到便捷。
下面实时搭建环境实现文件共享
环境准备
Redhat 7
CentOs 7
在这里以redhat 7 linux 系统充当服务器,centos 7 linux 系统充当客户端。
在服务器进行如下配置:
安装NFS服务
[root@linuxprobe ~]# yum install nfs-utils
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
xiaoyang | 4.1 kB 00:00:00
(1/2): xiaoyang/group_gz | 134 kB 00:00:00
(2/2): xiaoyang/primary_db | 3.4 MB 00:00:00
Package 1:nfs-utils-1.3.0-0.el7.x86_64 already installed and latest version
Nothing to do
清空NFS服务器防火墙相关策略,否则后面会失败,我们可以尝试一下!!!
[root@linuxprobe ~]# iptables -F
[root@linuxprobe ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
在NFS服务器上建立用于NFS文件共享的目录,并设置足够的权限确保其他人也有写入权限。
[root@linuxprobe ~]# mkdir /xiaoyang
[root@linuxprobe ~]# chmod -Rf 755 /xiaoyang
[root@linuxprobe ~]# echo "i love you" > /xiaoyang/a.txt
NFS的配置文件为/etc/exports,编辑配置文件
[root@linuxprobe ~]# vim /etc/exports
/xiaoyang 192.168.43.*(rw,sync,root_squash)
~
【注】一般格式为:共享目录的路径 ip地址(…);
rw:读写
sync:同时将数据写入到内存与硬盘中,保证不丢失数据
root_squash:NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户
重启rpcbind
[root@linuxprobe ~]# systemctl restart rpcbind
[root@linuxprobe ~]# systemctl enable rpcbind
[root@linuxprobe ~]# systemctl restart nfs-server
[root@linuxprobe ~]# systemctl enable nfs-server
ln -s '/usr/lib/systemd/system/nfs-server.service' '/etc/systemd/system/nfs.target.wants/nfs-server.service'
在客户端进行如下操作:
[root@localhost ~]# showmount -e 192.168.43.215
Export list for 192.168.43.215:
/xiaoyang 192.168.43.*
showmount相关参数如下:
-e:显示NFS服务器的共享列表
-a:显示本机挂载的文件资源共享的情况
-v:显示版本号
创建挂载目录,并进行挂载
[root@localhost ~]# mkdir /xiaoyang
[root@localhost ~]# mount -t nfs 192.168.43.215:/xiaoyang /xiaoyang
【注】挂载格式:mount -t 文件系统类型 服务器的ip地址:服务器共享目录 本地挂载目录
[root@localhost ~]# cat /xiaoyang/a.txt
i love you
发现以及成功共享。
我们想开机并启动,需要将其相关写入fstab配置文件中
[root@localhost ~]# echo "192.168.43.215:/xiaoyang /xiaoyang nfs defaults 0 0" > /etc/fstab