Linux挂载远程共享目录

256 阅读2分钟

一、NFS远程挂载

1、概念

NFS:即网络文件系统(Network File System)分布式文件系统协议

2、操作步骤

[机器A]

  • 安装NFS
#由于NFS是依赖于RPC协议来进行的协议传输,所以,此时需同时安装,NFS 和 RPC 两个应用程序
#安装NFS和RPC(安装nfs-utils,rpcbind) 
yum -y install nfs-utils rpcbind
  • 设置共享目录

NFS的配置文件在/etc/exports,内容默认为空。配置格式为

目录位置 客户机地址(权限选项)

vim /etc/exports
/home/share 192.168.1.2(rw,sync,no_root_squash)
 
#客户机地址 可以是 :  主机名、IP地址、网段地址、或者"*、?"通配符;
#权限选项:rw表示允许读写(ro为只读)
#     sync表示同步写
#      no_root_squash表示当前客户机以root身份访问时,赋予本地root权限(默认是root_squash,将作为nfsnobody用户降权对待)  (NFS 服务器共享目录用户的属性,如果用户是 root,那么对于这个共享目录来说就具有 root 的权限。)
 
#给多个地址授权
/home/share 192.168.1.2(rw,sync,no_root_squash)  192.168.1.3(rw,sync,no_root_squash)
#给某个网段内所有IP授权
/home/share 192.168.1.*(rw,sync,no_root_squash)
  • 启动NFS服务

配置完上述的目录文件配置后,则启动NFS服务;先启动 RPC服务,再启动 NFS 服务

#启动rpc服务
systemctl start rpcbind
#启动nfs服务
systemctl start nfs
#查看rpc服务状态
systemctl status rpcbind
#查看nfs服务状态
systemctl status nfs
#查看对应进程信息
ps -ef | grep rpcbind 
ps -ef | grep nfs
  • 查看当前机器已经发布的NFS共享目录
showmount -e 192.168.1.1
显示
Export list for 192.168.1.1:
/home/share 192.168.1.2

此时共享机器A的配置已经完成,可直接在机器B进行目录的挂载操作

[机器B]

  • 安装RPC服务

目录的挂载于共享是基于RPC协议进行的,所以B服务器作为挂载方,也应同时具备RPC的应用功能,所以也应同时安装对应的 rpcbind 服务插件。(安装rpcbind时,最好也可以直接把 nfs-utils 同步安装下,后续再次作为共享方时,则也会方便很多)

yum -y install rpcbind nfs-utils
  • 挂载
使用mount命令,此处表示将IP为:192.168.1.1所共享的/home/share目录,挂载到当前服务的 /home/share 目录下
 
mount -t nfs 192.168.1.1:/home/share /home/share
  • 开机自动挂载
vi /etc/fstab

192.168.1.1:/home/share    /home/share    nfs    defaults,_netdev 0 0
  • 开机自动启动
systemctl enable rpcbind.service
systemctl enable nfs-server.service
  • 查看当前机器挂载点
df -h