NFS服务简介  


NFSNetwork File System的缩写,即网络文件系统。NFS是由Sun开发并发展起来的一项用于在不同机器,不同操作系统之间通过网络互相分享各自的文件。NFS server也可以看作是一个FILE SERVER,用于在UNIX类系统之间共享文件,可以轻松的挂载(mount)到一个目录上,操作起来就像本地文件一样的方便。

一、nfs-server(192.168.2.1)


1、检查是否安装


[root@host ~]# rpm -qa |grep nfs     //查看是否安装了nfs


nfs-utils-lib-1.0.8-7.6.el5


nfs-utils-1.0.9-42.el5              //nfs主程序,已安装


2NFS配置文件设置


若要共享目录或文件,需要在共享清单中指明该文件/etc/exports  (语法参照 man  exports)


#vim   /etc/exports


/home/nfs 192.168.2.0/24(rw,sync)    (192.168.2.0网段的用户对/home/nfs NFS卷具有读写)
/media/cdrom 192.168.2.*(ro)        (192.168.2.0网段的用户对/media/cdromNFS卷具有读)


/etc/exports,配置文件有三部分: 输出目录,客户端,参数选项


a输出目录是指NFS系统中需要共享给客户机使用的目录;


客户端是指网络中可以访问这个NFS输出目录的计算机


b客户端常用的指定方式


指定ip地址的主机 192.168.0.200


指定子网中的所有主机 192.168.0.0/24


指定域名的主机 a.liusuping.com


指定域中的所有主机 *.liusuping.com


所有主机 *


c选项:


选项用来设置输出目录的访问权限、用户映射等。NFS主要有3类选项:


访问权限选项


设置输出目录只读 ro


设置输出目录读写 rw


用户映射选项


all_squash 将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);


no_all_squash all_squash取反(默认设置);


root_squash root用户及所属组都映射为匿名用户或用户组(默认设置);


no_root_squash rootsquash取反;


anonuid=xxx 将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);


anongid=xxx 将远程访问的所有用户组都映射为匿名用 户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);


其它选项


secure 限制客户端只能从小于1024tcp/ip端口连接nfs服务器(默认设置);


insecure 允许客户端从大于1024tcp/ip端口连接服务器;


sync 将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;


async 将数据先保存在内存缓冲区中,必要时才写入磁盘;


wdelay 检查是否有相关的写操作,如果有则将这些写操作 一起执行,这样可以提高效率(默认设置);


no_wdelay 若有写操作则立即执行,应与sync配合使用;


subtree 若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置)


no_subtree 即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率


3、启动nfs服务


# service nfs start


启动 NFS 服务:                                            [确定]


关掉 NFS 配额:                                            [确定]


启动 NFS 守护进程:                                        [确定]


启动 NFS mountd:                                          [确定]


4、查看自己的rpc进程,与NFS相关的是否正常开启,如下表明已经正常开启:


   #rpcinfo -p  192.168.2.1 (本机ip,)


    100011    1   udp    821  rquotad


    .........


100003    2   udp   2049  nfs


........


    100021    1   tcp  34647  nlockmgr


     .......


    100005    1   udp    852  mountd


      ........


5导出共享清单


[root@host ~]# exportfs -rv


exporting 192.168.2.0/24:/home/nfs


exporting 192.168.2.*:/media/cdrom


二、nfs客户端(192.168.2.20/24)


1查看自己的rpc进程是否开启


[root@client ~]# rpcinfo -p

   程序 版本 协议   端口

100000    2   tcp    111  portmapper

........

100024    1   udp    794  status

........

100021    1   udp  58859  nlockmgr

........


2查看nsf-serverexports文件是否可以访问:


[root@client ~]# showmount -e 192.168.2.1


Export list for 192.168.2.1:


/media/cdrom 192.168.2.*


/home/nfs    192.168.2.0/24


3、创建挂载点,手动挂载


#mkdir  /tmp/abc


#mkdir  /tmp/cdrom


# mount 192.168.2.1:/home/nfs  /tmp/abc


# mount 192.168.2.1:/media/cdrom  /tmp/cdrom


4、设置开机自动挂载


#vim /etc/fstab


192.168.2.1:/home/nfs   /tmp/abc                nfs     defaults,soft,intr      0 0


192.168.2.1:/media/cdrom   /tmp/cdrom           nfs     defaults,soft,intr      0 0


 注:(softintr等参数可查看man nfs


 -ro,soft,intr:  -ro:挂载时的权限,soft:如果有I/O错误时,会告知系统,中继挂载;intr:挂载时有大量超时时,中继挂载,并告知系统


http://thenubbyadmin.com/2013/04/10/solving-nfs-mounts-at-boot-time/

_netdev
The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).


This is awesome, and exactly what we want. So you modify your entry in fstab to look like this:

10.10.10.1:/vol1/fs1 /data nfs defaults,_netdev 0 0

You’ve been bitten by NFS mounting in the past so you throw this in your test environment and reboot immediately. After the system comes up you notice a problem. Your NFS volumes are still unmounted. You see, there’s a bit of a hitch. Automounter followed the same procedure that it did before, except this time it didn’t even attempt to mount /data. The _netdev option doesn’t tell the system to mount the filesystem when network comes up, it says don’t attempt to mount it at all if the network isn’t up. There is still a missing piece to the puzzle. If you look at your init scripts there is a service called netfs. If you read the script you can see in the chkconfig header this description:

# description: Mounts and unmounts all Network File System (NFS),
# CIFS (Lan Manager/Windows), and NCP (NetWare) mount points.

This is exactly what you need. It is a service whose sole purpose is to read your /etc/fstab and mount network filesystems. All you have to do is enable it

chkconfig netfs on

and watch the magic happen. Now your mount boot process should look something like this:

Automounter reads /etc/fstab

Ignores /data since it has _netdev option set

Mounts all other filesystems

Finishes mount jobs and allows system to continue booting

Network comes up

Service netfs started

netfs reads /etc/fstab and finds an nfs filesystem

netfs mounts /data

What’s funny is that while I was researching this problem I never stumbled across netfs as a service. I had even gone so far as to start planning out my own custom init script that would do exactly this, except specifically for my mount points instead of generalizing. It’s nice to see that I was on the right track, but even better that the tools already existed.


#umount /tmp/cdrom    /tmp/abc


#mount -a


5、这种方式的挂载需要消耗大量的资源来维持连接,可以使用自动挂载当切换到该目录时进行挂载,退出时,取消挂载


a安装autofs软件包


[root@client tmp]# rpm -qa|grep autofs


autofs-5.0.1-0.rc2.131.el5


b编辑autofs的配置文件/etc/auto.master


#vim /etc/auto.master


/tmp /etc/nfs.misc   --timeout=08


/tmp /etc/nfs.misc   --timeout=08


:wq


c、编辑要求的产生/etc/nfs.misc


#vim /etc/nfs.misc


abc     -fstype=nfs   192.168.2.1:/home/nfs


cdrom   -fstype=nfs   192.168.2.1:/media/cdrom


:wq


d、启动autofs服务


#service autofs  start


#rm -rf /tmp/abc


#rm -rf /tmp/cdrom


e、测试



abccdrom 本地目录,我们不需要创建它,用于挂载远程共享文件192.168.2.1:/home/nfs,在我们想要使用远程共享文件/home/nfs时,只要在本地目录/tmp下键入cd abc 系统就会自动挂载到远程共享目录:192.168.2.10:/home/nfs,我们就可以正常使用里面的共享文件了 ,在到达超时时间时就会自动卸载。


f、以rw自动挂载/home/nfs/ NFS卷到/tmp/abc目录下,却发现无法创建目录,因为nfs-server的本地目录还没有写权限,


[root@client abc]# touch nfs.txt


touch: 无法触碰 “nfs.txt”权限不够


gnfs-server服务端修改/home/nfs的本地权限


[root@host home]# chmod o+wt nfs/


就可以正常创建了,


[root@client abc]# touch nfs.txt


[root@client abc]# dir


hello-word.txtnfs.txt  nft-test


实验结束。


注:NFS客户端与NFS服务器在连接上之后,为了保证其正常的连接,NFS客户端与NFS服务器之间要不断的发送数据包,来宣告自己还在与NFS服务器进行着连接,但是,如果一个NFS服务器上有许多的客户端一直连接的话,NFS服务器会承受很大的带宽压力,这对NFS服务器的正常使用会造成很大的影响,因此,为了避免这种情况的发生,人们就想到了一种方法,让NFS客户端在获取数据时与NFS服务器进行连接,而在没有获取数据的时间内,自动的断开与NFS服务器之间的连接,但只要客户端发送获取数据的请求进,客户端就与服务器自动连接上,自动挂载的方法对客户端主机与服务器主机都有很大的好处。配置方法是在NFS客户端方面进行配置的