NAS(Network Attached Storage)网络存储基于标准网络协议实现数据传输,为网络中的Windows / Linux / Mac OS 等各种不同操作系统的计算机提供文件共享和数据备份。

        Linux可以通过开启nfs服务器,将系统变成一台NAS。一旦将NAS的共享目录映射到本地,就可以像操作本地文件一样操作NAS服务器上的文件。

1、 设置NFS服务器

1.1、安装NFS,rpcbind

确保系统已安装nas跟rpcbind,如果没有的话可以使用yum命令安装。

yum install rpcbind
yum install nfs-utils

1.2、建立一个共享目录,也可以不建,任何目录都可以通过NFS共享出去。建议挂载一个独立的硬盘作为共享目录。如何在linux挂载一个硬盘,可以参考阿里云的帮助文档, https://aliyun.com/knowledge_detail/5974154.htm

1.3、编写NFS配置文件。配置文件位于 /etc/exports,编辑内容如下:

/nfsfileshare 192.168.12.7(rw,sync,no_root_squash)

/nfsfileshare:为需要共享的本地目录

192.168.12.7:可以访问该目录的客户端机器的地址,地址也可以写成192.168.12.7/24这种形式

rw,sync,no_root_squash:表示该地址对此共享文件夹的权限。具体权限描述如下表:

rw ro rw:读写,ro:只读。最终能否读写,跟文件系统的rwx及身份也有关系
sync async sync:数据同步写入内存跟硬盘中, async:数据先暂存内存,而非直接写入硬盘
no_root_squash root_squash root_squash:客户端的root身份被当成NAS服务器的nsfnobody, no_root_squash:客户端的root身份等同于NAS服务器的root用户
all_squash 无论客户端为何种身份登陆,都被NAS服务器视为nsfnobody

配置文件可以同时写入多行多列,例如:

/tmp          *(rw,no_root_squash)
/home/public  192.168.100.0/24(rw)    *(ro)
/home/test    192.168.100.10(rw)
/home/linux   *.centos.vbird(rw,all_squash,anonuid=45,anongid=45)

如果nfs服务已经启动,可以通过一下命令动态加载配置文件,而不用重启nfs服务。

exportfs -arv

1.4、启动NFS服务

service rpcbind start
service nfs start

1.5、打开防火墙端口,当然你也可以关闭整个防火墙

firewall-cmd --permanent --add-service mountd
firewall-cmd --permanent --add-service rpc-bind
firewall-cmd --permanent --add-service nfs
firewall-cmd --reload

这里用的是添加服务而不是添加端口的方式,因为nfs每次启动都会随机使用1024以内的任意可用端口。

1.6、查看NFS注册情况

[root@www ~]# rpcinfo -t localhost nfs
program 100003 version 2 ready and waiting
program 100003 version 3 ready and waiting
program 100003 version 4 ready and waiting

可以发现,这里提供了3种NFS使用版本,分别是2,3,4

2、设置NFS客户端

2.1、查看可用的网络存储目录

[root@client ~]# showmount -e 192.168.12.5

Export list for 192.168.12.5:
/nfsfileshare 192.168.12.7

可以看到 192.168.12.5 服务器上共享了一个 /nfsfileshare 的目录

2.2、挂载共享目录到本地

mkdir -p /mnt/nfsfileshare
mount 192.168.12.5:/nfsfileshare /mnt/nfsfileshare

2.3、查看映射情况

[root@client ~]# df -hT

192.168.12.5:/nfsfileshare nfs4       50G  858M   50G   2% /mnt/nfsfileshare

2.4、如果想中断连接的话,可以使用umount卸载磁盘命令

umount /mnt/nfsfileshare

2.5、编辑 /etc/fstab 文件,执行开机挂载磁盘。增加以下内容

#<server>:</remote/export></local/directory><nfs-type><options> 0 0
192.168.12.5:/nfsfileshare/ /mnt/nfsfileshare nfs rw,sync,hard,intr 0 0

官方说明:

Replace <server> with the hostname, IP address, or fully qualified domain name of the server exporting the file system.

Replace </remote/export> with the path to the exported directory.

Replace </local/directory> with the local file system on which the exported directory is mounted. This mount point must exist before /etc/fstab is read or the mount fails.

Replace <nfs-type> with either nfs for NFSv2 or NFSv3 servers, or nfs4 for NFSv4 servers.

The following are options commonly used for NFS mounts:

  • fsid=num — Forces the file handle and file attributes settings on the wire to be num, instead of a number derived from the major and minor number of the block device on the mounted file system. The value 0 has special meaning when used with NFSv4. NFSv4 has a concept of a root of the overall exported file system. The export point exported with fsid=0 is used as this root.

  • hard or soft — Specifies whether the program using a file via an NFS connection should stop and wait (hard) for the server to come back online, if the host serving the exported file system is unavailable, or if it should report an error (soft).

    If hard is specified, the user cannot terminate the process waiting for the NFS communication to resume unless the intr option is also specified.

    If soft is specified, the user can set an additional timeo=<value> option, where <value> specifies the number of seconds to pass before the error is reported.

2.6、至此,可以像使用本地目录一样使用NAS的共享目录了。

By charlie

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注