NFS服务实现文件共享

· 服务介绍

NFS(Need for speed)网络文件系统服务:将远程Linux系统上的文件共享资源挂载到本地主机的目录上,使得本地主机(Linux客户端)基于TCP/IP协议,读写远程Linux系统上的共享文件

RHEL 8系统中默认已经安装NFS服务
在这里插入图片描述

· 服务部署

为了检验NFS服务配置的效果,我们需要使用两台Linux主机(一台充当NFS服务器,一台充当NFS客户端)

主机名称操作系统IP地址
NFS服务器CentOS 7192.168.1.105
NFS客户端CentOS 7192.168.1.2

NFS服务器

1.修改防火墙策略

#清空防火墙策略
[root@xiudaochengxian ~]# iptables -F
[root@xiudaochengxian ~]# iptables-save 
#允许通过NFS服务
[root@xiudaochengxian ~]# firewall-cmd --permanent --zone=public --add-service=nfs
success
[root@xiudaochengxian ~]# firewall-cmd --permanent --zone=public --add-service=rpc-bind 
success
[root@xiudaochengxian ~]# firewall-cmd --permanent --zone=public --add-service=mountd 
success
[root@xiudaochengxian ~]# firewall-cmd --reload 
success

2.新建文件共享目录,设置权限

[root@xiudaochengxian ~]# mkdir /xiuxian
[root@xiudaochengxian ~]# chmod -R 777 /xiuxian/
[root@xiudaochengxian ~]# echo "welcome to nfs" > /xiuxian/test.txt

3.编辑NFS主配置文件(默认无任何内容)

格式:共享目录的路径 允许访问的NFS客户端(共享权限参数)
定义要共享的目录与相应的权限。

[root@xiudaochengxian ~]# vim /etc/exports
  1 /xiuxian 192.168.1.*(rw,sync,root_squash)

在这里插入图片描述
NFS客户端地址与权限之间没有空格

匹配IP地址有三种方法
第一种:直接写*号,代表任何主机都可以访问
第二种:实验采用的192.168.1.*通配格式,代表来自192.168.1.0/24网段的主机
第三种:直接写对方的IP地址,如192.168.1.2,代表仅允许某个主机进行访问

4.重启NFS服务并加入开机启动项

RPC(Remote Procedure Call,远程过程调用)服务将NFS服务器的IP地址和端口号等信息发送给客户端

启动NFS服务之前,还需要启用rpcbind服务程序,将这两个服务一并加入开机启动项中

[root@xiudaochengxian ~]# systemctl restart rpcbind
[root@xiudaochengxian ~]# systemctl enable rpcbind
[root@xiudaochengxian ~]# systemctl start nfs-server
[root@xiudaochengxian ~]# 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客户端

1.ping NFS服务器

[root@test ~]# ping -c4 192.168.1.105
PING 192.168.1.105 (192.168.1.105) 56(84) bytes of data.
64 bytes from 192.168.1.105: icmp_seq=1 ttl=64 time=0.846 ms
64 bytes from 192.168.1.105: icmp_seq=2 ttl=64 time=0.734 ms
64 bytes from 192.168.1.105: icmp_seq=3 ttl=64 time=0.634 ms
64 bytes from 192.168.1.105: icmp_seq=4 ttl=64 time=0.673 ms

--- 192.168.1.105 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 0.634/0.721/0.846/0.086 ms

2.查询NFS服务器的远程共享信息

格式:共享的目录名称 允许使用客户端地址

[root@test ~]# showmount -e 192.168.1.105
Export list for 192.168.1.105:
/xiuxian 192.168.1.

在这里插入图片描述
3.创建挂载目录使其永久生效

[root@test ~]# mkdir /rick
[root@test ~]# mkdir /xiuxian
[root@test ~]# vim /etc/fstab 
 12 192.168.1.105:/xiuxian /xiuxian nfs defaults 0 0

在这里插入图片描述
4.挂载设备镜像

[root@test ~]# mount -a
[root@test ~]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda3                17G  4.9G   13G  29% /
devtmpfs                1.9G     0  1.9G   0% /dev
tmpfs                   1.9G     0  1.9G   0% /dev/shm
tmpfs                   1.9G   21M  1.9G   2% /run
tmpfs                   1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/sda1              1014M  165M  850M  17% /boot
tmpfs                   378M   40K  378M   1% /run/user/0
192.168.1.105:/xiuxian   17G  3.5G   14G  21% /xiuxian

5.查看共享文件

[root@test ~]# cd /xiuxian/
[root@test xiuxian]# ls
test.txt
[root@test xiuxian]# cat test.txt 
welcome to nfs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值