1、是UNIX和linux所特有的,可以在windows、linux、UNIX之间共享文件,共享以后可将远程的文件目录共享到本地的磁盘,可以复制、写、删除等操作,跟本地的文件是没什么区别的。

例如,A、B、C三台linux主机,需要访问同样的资源,就可以把资源放到A上,B、C通过NFS访问A的资源。

建议在centos5上搭建NFS,centos6上不稳定。

2、安装NFS

[root@client ~]# yum -y install nfs-utils   //安装nfs-utils包
Installing : rpcbind-0.2.0-11.el6.i686                                    1/3
  Installing : nfs-utils-lib-1.1.5-6.el6.i686                               2/3
  Installing : 1:nfs-utils-1.2.3-39.el6.i686                                3/3
  Verifying  : 1:nfs-utils-1.2.3-39.el6.i686                                1/3
  Verifying  : nfs-utils-lib-1.1.5-6.el6.i686                               2/3
  Verifying  : rpcbind-0.2.0-11.el6.i686                                    3/3   
//rpcbind这个服务是用来将RPC程序号转换成端口号
Installed:
  nfs-utils.i686 1:1.2.3-39.el6
Dependency Installed:
  nfs-utils-lib.i686 0:1.1.5-6.el6          rpcbind.i686 0:0.2.0-11.el6

3、编写NFS配置文件/etc/exports

[root@client ~]# vim /etc/exports
/tmp/123 192.168.0.0/24(rw,sync,all_squash,anonuid=503,anongid=505)
/tmp/345 192.168.0.0/24(rw,sync,no_root_squash)
/*共分为三部分,第一部分就是本地要共享出去的目录,/tmp/123
第二部分为允许访问的主机(可以是一个IP也可以是一个IP段)192.168.0.0/24
第三部分就是小括号里面的,为一些权限选项。
rw :读写;
ro :只读;
sync :同步模式,内存中数据时时写入磁盘;
async :不同步,把内存中数据定期写入磁盘中;
no_root_squash :加上这个选项后,root用户就会对共享的目录拥有至高的权限控制,就像是对本机的目录操作一样。不安全,不建议使用;
root_squash:和上面的选项对应,root用户对共享目录的权限不高,只有普通用户的权限,即限制了root;
all_squash:不管使用NFS的用户是谁,他的身份都会被限定成为一个指定的普通用户身份;
anonuid/anongid :要和root_squash 以及all_squash一同使用,用于指定使用NFS的用户限定后的uid和gid,前提是本机的/etc/passwd中存在这个uid和gid。
*/

4、启动NFS

[root@client 123]# /etc/init.d/nfs start  
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [FAILED]
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
rpc.nfsd: unable to set any sockets for nfsd   //需要RPC支持
                                                           [FAILED]
[root@client 123]# /etc/init.d/rpcbind start
Starting rpcbind:                                          [  OK  ]
[root@client 123]# /etc/init.d/nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]

5、查看共享目录

[root@localhost 345]# showmount -e 192.168.0.105
Export list for 192.168.0.105:
/tmp/345 192.168.0.0/24
/tmp/123 192.168.0.0/24

6、客户端挂载共享目录

[root@client ~]# yum install -y nfs-utils //安装NFS
[root@client ~]# mount -t nfs 192.168.0.105:/tmp/123 /123
[root@client ~]# mount -t nfs 192.168.0.105:/tmp/123 /345
//挂载,-t指定文件系统类型,共享服务器IP地址和目录
[root@localhost 345]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      6.5G  2.0G  4.1G  33% /
tmpfs                 250M     0  250M   0% /dev/shm
/dev/sda1             485M   30M  430M   7% /boot
/dev/sdb1            1020M   34M  935M   4% /guazai
192.168.0.105:/tmp/123
                       15G  2.0G   12G  15% /123
192.168.0.105:/tmp/345
                       15G  2.0G   12G  15% /345

测试all-squash

[root@localhost 123]# mkdir yuan cheng   //客户端没有权限
mkdir: cannot create directory `yuan': Permission denied
mkdir: cannot create directory `cheng': Permission denied
root@client 123]# chmod 777 /tmp/123   //服务端给目录设置权限
[root@localhost 123]# mkdir yuan cheng   //客户端
[root@localhost 123]# ll
total 20
-rw-r--r-- 1 root   root      0 Apr 21 01:08 ccccc
drwxr-xr-x 2 nobody nobody 4096 Apr 21 01:13 cheng   //所属主是nobody
drwxr-xr-x 2 root   root   4096 Apr 21 01:08 tt
drwxr-xr-x 2 root   root   4096 Apr 21 01:08 uuu
-rw-r--r-- 1 root   root      0 Apr 21 01:08 xxxxc
drwxr-xr-x 2 nobody nobody 4096 Apr 21 01:13 yuan
drwxr-xr-x 2 root   root   4096 Apr 21 01:08 yyy
-rw-r--r-- 1 root   root      0 Apr 21 01:08 zz

测试no-root-squash

[root@localhost 345]# mkdir yuan cheng
[root@localhost 345]# ll
total 20
drwxr-xr-x 2 root root 4096 Apr 21 01:24 cheng   //所属主和所属组都是root
drwxr-xr-x 2 root root 4096 Apr 21 01:04 ee
-rw-r--r-- 1 root root    0 Apr 21 01:04 fff
-rw-r--r-- 1 root root    0 Apr 21 01:04 gggg
drwxr-xr-x 2 root root 4096 Apr 21 01:04 qq
drwxr-xr-x 2 root root 4096 Apr 21 01:04 w
drwxr-xr-x 2 root root 4096 Apr 21 01:24 yuan

7、exportfs用法

[root@client 123]# exportfs -v   //查看本机共享目录
/tmp/123        192.168.0.0/24(rw,wdelay,root_squash,all_squash,no_subtree_check,anonuid=503,anongid=505)
/tmp/345        192.168.0.0/24(rw,wdelay,no_root_squash,no_subtree_check)

-a :全部挂载或者卸载;
-r :重新挂载;
-u :卸载某一个目录;
-v :显示共享的目录