Linux之网络文件协议-NFS

当今最主要的两大网络文件系统是Sun提出的NFS(Network File System)以及由微软、EMC和NetApp提出的CIFS(Common Internet File System),前者主要用于各种Unix平台,后者则主要用于Windows平台,我们熟悉的“网上邻居”的文件共享方式就是基于CIFS系统的。

访问网络共享的三个基本步骤
– 1. 识别要访问的远程共享
– 2. 确定挂载点, 并创建挂载点的空目录
– 3. 通过相应的配置更改挂载网络文件系统

一、NFS(网络文件系统)

        通过使用NFS,用户和程序可以像访问本地文件一样访问远端系统上的 文件,基于RPC(Remote Procedure Call Protocol远程过程调用)实现。

NFS优势:节省本地存储空间,将常用的数据,如home目录,存放在NFS服务 器上且可以通过网络访问,本地终端将可减少自身存储空间的使用。

NFS 服务端的配置

场景
NFS服务器:172.18.254.254
NFS客户端:172.18.252.118
关闭火墙:

# iptables -F
# setenforce 0

1.服务器端

1)安装NFS服务组件
#yum install nfs-utils

2) 启动NFS服务:
# systemctl start nfs

#systemctl enable nfs

2) 创建共享目录并设置权限:
# showmount -e localhost 查看当前共享的 NFS 信息
#mkdir / app/nfsdir1

  #mkdir / app/nfsdir2 创建共享目录
#chomd 777 /app/nfsdir1

3) 编辑/etc/exports。

[root@centos7 ~]# vim /etc/exports
/app/nfsdir1 *(sync)

子配置文件
[root@centos7 ~]# vim /etc/exports.d/dir2.exports
/app/nfsdir2 192.168.153.6(sync)

这里的意思是说,需要共享/app/nfsdir1这个目录,对所有客户端(*)都是可读写的(rw),并且是异步方式来访问。 因为不设置其他参数, 客户端创建文件或目录时默认的属主和组就是nfsnobody,即使客户端使用的账号是root。

4)导出共享

[root@centos7 ~]# exportfs -r

访问 NFS 导出的命令
• 1. 识别
– # showmount -e 172.18.254.254
• 2. 确定挂载点
– # mkdir /mnt/nfs1
• 3. 挂载
– # mount  172.18.254.254:/app/nfsdir1 /mnt/nfs1

怎么访问NFS共享的东西:
1.安装nfs-utils

#yum install nfs-utils -y

2.识别

#showmount -e 172.18.254.254

showmount命令用于查询NFS服务器的相关信息
# showmount --help
Usage: showmount [-adehv]
        [--all] [--directories] [--exports]
        [--no-headers] [--help] [--version] [host]
-a或--all
    以 host:dir 这样的格式来显示客户主机名和挂载点目录。
-d或--directories
    仅显示被客户挂载的目录名。
-e或--exports
    显示NFS服务器的输出清单。
-h或--help
    显示帮助信息。
-v或--version
    显示版本信。
--no-headers
    禁止输出描述头部信息。
显示NFS客户端信息
# showmount
显示指定NFS服务器连接NFS客户端的信息
# showmount 192.168.1.1  #此ip为nfs服务器的
显示输出目录列表
# showmount -e
显示指定NFS服务器输出目录列表(也称为共享目录列表)
# showmount -e 192.168.1.1
显示被挂载的共享目录
# showmount -d 
显示客户端信息和共享目录
# showmount -a
显示指定NFS服务器的客户端信息和共享目录
# showmount -a 192.168.1.1

3.挂载

#mount 172.18.254.254:/app/nfsdir1 /mnt/nfs1

   开机挂载/etc/fstab文件
   172.18.254.254:/app/nfsdir1 /mnt/nfs1 nfs defaults 0 0
    mount -a(自动挂载 /etc/fstab 里面的东西)

4.卸载:umount /mnt,删除/etc/fstab中增加的东西

自动挂载网络存储

如果一个NFS服务器上有许多的客户端一直连接的话,NFS服务器会承受很大的带宽压力,这对NFS服务器的正常使用会造成很大的影响,因此,为了解决这一弊端,我可以这样做:让NFS客户端在获取数据时与NFS服务器进行连接,而在没有获取数据的时间内,自动的断开与NFS服务器之间的连接,这就是NFS的自动挂载技术。需要注意的是,配置方法是在NFS客户端方面进行配置的。

•使用 mount 命令 , 需要具有 root 用户权限以连接到网络共享。或者 , 我们可以向 /etc/fstab 添加条目 , 但是随后与网络服务器的连接将始终处于活动状态。当某一程序试图访问网络共享中的文件时 , 可以将 automounter ( 或 autofs )服务配置为 “ 按需 ” 挂载网络共享。当共享不再使用并处于不活动状态一定时间以后 ,automounter 会对共享解除挂载

NFS

[root@centos7 ~]# yum -y install nfs-utils
[root@centos7 ~]# vim /etc/exports.d/mage.exports
/app/homedir *(rw,sync)
[root@centos7 ~]# systemctl restart nfs

1.安装autofs
  yum -y install autofs
2.配置主配置文件
[root@centos6~]# vim /etc/auto.master
/app/homedir /etc/auto.mage --timeout=10

[root@centos6 ~]# vim /etc/auto.mage
*       -fstype=nfs             192.168.153.7:/app/homedir/&

3.重启autofs服务
[root@centos6 ~]# service autofs restart


实验:在centos7中autofs 可以使用绝对路径以避免基名的上一级目录的原始内容被覆盖

[root@centos7 ~]# vim /etc/auto.master
/-      /etc/auto.home

[root@centos7 ~]# vim /etc/auto.home
/home/cdrom             -fstype=iso9660 :/dev/cdrom

[root@centos7 ~]# systemctl restart autofs


配置伪根

创建伪装目录
[root@centos7 ~]# mkdir -p /nfsroot/dir{1..2}


进行bind的挂载
[root@centos7 ~]# mount -B /app/nfsdir1 /nfsroot/dir1/
[root@centos7 ~]# mount -B /app/nfsdir2 /nfsroot/dir2/


[root@centos7 ~]# vim /etc/exports
/nfsroot *(rw,fsid=0,crossmnt)
/nfsroot/dir1 *(rw)
/nfsroot/dir2 *(ro)


[root@centos7 ~]# systemctl restart autofs.service

[root@centos6 ~]# mount 192.168.153.7:/nfsroot/dir1 /mnt/nfs1/

nfs就简单介绍到这里,下篇介绍cifs的samba服务。

猜你喜欢

转载自www.cnblogs.com/f-h-j-11-7/p/9758659.html