NFS服务器搭建与autofs自动挂载

一、 NFS是什么:

NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。

Autofs是什么:

mount是用来挂载文件系统的,可以在系统启动的时候挂载也可以在系统启动后挂载。对于本地固定设备,如硬盘可以使用mount挂载;而光盘、软盘、NFS、SMB等文件系统具有动态性,即需要的时候才有必要挂载。光驱和软盘我们一般知道什么时候需要挂载,但NFS和SMB共享等就不一定知道了,即我们一般不能及时知道NFS共享和SMB什么时候可以挂载。而autofs服务就提供这种功能,好像windows中的光驱自动打开功能,能够及时挂载动态加载的文件系统。免去我们手动挂载的麻烦。要实现光驱,软盘等的动态自动挂载,需要进行相关的配置。

二.NFS文件详解

  1. /data/ 表示需要共享的目录
  2. IP 表示允许哪个客户端访问。
  3. IP 后括号里的设置表示对该共享文件的权限。
  4. ro 只读访问
  5. rw 读写访问
  6. sync 所有数据在请求时写入共享
  7. all_squash 共享文件的UID和GID映射匿名用户anonymous,适合公用目录。
  8. no_all_squash 保留共享文件的UID和GID(默认)
  9. root_squash root用户的所有请求映射成如anonymous用户一样的权限(默认)
  10. no_root_squash root用户具有根目录的完全管理访问权限

三. NFS服务器搭建与autofs自动挂载

(1).安装NFS服务器

1 yum install nfs-utils -y #安装nfs
2 systemctl start nfs #开启nfs服务
3 systemctl enable nfs #开机自启动

(2)配置NFS服务器

1 mkdir /westos
2 echo ‘hello,world’ > /westos/hello # 建立目录 /westos, 在其中创建测试文件hello

(3).通过修改NFS配置,共享/data/share/目录

[root@nfsserver ~]# mkdir /data/share
[root@nfsserver ~]# vim /etc/exports
shared data for bbs by oldboy at 20160825
#/data/share 192.168.100.0/24(rw,sync,hide)
/data/share 192.168.100.0/24(rw,sync,anonuid=555,anongid=555,all_squash)

(4).客户端安装NFS客户端及autofs并启动相关服务

[root@nfsclient ~]# yum install rpcbind nfs-utils -y
[root@nfsclient ~]# yum install autofs -y
[root@nfsclient ~]# /etc/init.d/rpcbind start
[root@nfsclient ~]# /etc/init.d/autofs start

(5).服务器测试

1 showmount -e 192.168.1.152
2 Export list for 192.168.1.152:
3 /westos *

(6).配置客户端自动挂载(autofs)NFS服务端共享目录

[root@nfsclient ~]# vim /etc/auto.master
Sample auto.master file
This is a ‘master’ automounter map and it has the following format:
mount-point [map-type[,format]:]map [options]
For details of the format look at auto.master(5).
#/misc /etc/auto.misc
/mnt /etc/auto.misc timeout=60#/mnt为挂载点 /etc/auto.misc为挂载动作
[root@nfsclient ~]# vim /etc/auto.misc
This is an automounter map and it has the following format
key [ -mount-options-separated-by-comma ] location
Details may be found in the autofs(5) manpage
cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom
the following entries are samples to pique your imagination
#linux -ro,soft,intr ftp.example.org:/pub/linux
#boot -fstype=ext2 :/dev/hda1
#floppy -fstype=auto :/dev/fd0
#floppy -fstype=ext2 :/dev/fd0
#e2floppy -fstype=ext2 :/dev/fd0
#jaz -fstype=ext2 :/dev/sdc1
#removable -fstype=ext2 :/dev/hdd
nfsdata -fstype=nfs 192.168.100.100:/data/share

(7).重新启动autofs服务,并检查自动挂载文件是否生效

[root@nfsclient ~]# /etc/init.d/autofs restart
[root@nfsclient ~]# cd /mnt/nfsdata
[root@nfsclient nfsdata]# ls - total 0
-rw-r–r-- 1 oldgirl oldgirl 0 Aug 27 00:14 andy
-rw-r–r-- 1 oldgirl oldgirl 0 Aug 27 00:10 oldboy
-rw-rw-r-- 1 oldgirl oldgirl 0 Aug 27 00:10 oldgirl
[root@nfsclient nfsdata]# df –h
Filesystem Size Used Avail Use% Mounted on
Filesystem /dev/mapper/vg_muban-moban_root 38G 1.9G 34G 6% /
tmpfs 491M 0 491M 0%/
dev/shm/dev/sda1 194M 29M 155M 16%/boot
192.168.100.100:/data/share 38G 1.9G 34G 6% /mnt/nfsdata

这样我们就基本完成NFS服务器搭建与autofs自动挂载了。