1.什么是NFS?

NFS,全名叫Network File System,中文叫网络文件系统,是Linux、UNIX系统的分布式文件系统的一个组成部分,可实现在不同网络上共享远程文件系统。NFS由Sun公 司开发,目前已经成为文件服务的一种标准之一。其最大的功能就是可以通过网络,让不同操作系统的计算机可以共享数据, 所以可以把NFS看做是一个文件服务器。NFS缺点是其读写性能比本地硬盘要差一些。

2.使用yum安装nfs-utils、rpcbind

实验环境
centos 6.6

nfs-server    10.0.0.130
lamp-client  10.0.0.137
lnmp-client  10.0.0.138
3.nfs服务端软件安装
[root@nfs-server ~]# yum -y install nfs-utils rpcbind   
 创建共享文件夹
 [root@nfs-server ~]# mkdir -p {/data/r_shared,/data/w_shared}查看文件是否创建成功
[root@nfs-server data]# ll


总用量 8

drwxr-xr-x 2 nfsnobody root 4096 4月  23 15:13 r_shared
 drwxr-xr-x 2 nfsnobody root 4096 4月  23 15:24 w_shared
 [root@nfs-server data]# 
 给文件授权[root@nfs-server data]# chown -R nfsnobody  r_shared w_shared
[root@nfs-server data]# ll 
 总用量 8
 drwxr-xr-x 2 nfsnobody root 4096 4月  23 15:13 r_shared
 drwxr-xr-x 2 nfsnobody root 4096 4月  23 15:24 w_shared
 [root@nfs-server data]#

4.启动rpcbind、nfs

[root@nfs-server ~]# /etc/init.d/rpcbind start 
 [root@nfs-server ~]# /etc/init.d/nfs start        
 [root@nfs-server ~]# /etc/init.d/nfs status 
 rpc.svcgssd 已停
 rpc.mountd (pid 4759) 正在运行...
 nfsd (pid 4775 4774 4773 4772 4771 4770 4769 4768) 正在运行...
 rpc.rquotad (pid 4754) 正在运行...
 [root@nfs-server ~]# /etc/init.d/rpcbind status 
 rpcbind (pid  1656) 正在运行...
 [root@nfs-server ~]# [root@nfs ~]# lsof -i :111
 COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
 rpcbind 2664  rpc    6u  IPv4  15806      0t0  UDP *:sunrpc 
 rpcbind 2664  rpc    8u  IPv4  15809      0t0  TCP *:sunrpc (LISTEN)
 rpcbind 2664  rpc    9u  IPv6  15811      0t0  UDP *:sunrpc 
 rpcbind 2664  rpc   11u  IPv6  15814      0t0  TCP *:sunrpc (LISTEN)
 [root@nfs ~]# netstat -tunpl|grep "111"
 tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2664/rpcbind        
 tcp        0      0 :::111                      :::*                        LISTEN      2664/rpcbind        
 udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2664/rpcbind        
 udp        0      0 :::111                      :::*                                    2664/rpcbind        
 [root@nfs ~]# 
 [root@nfs ~]#  chkconfig --list rpcbind  查看开机是否自启
 rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off
 [root@nfs ~]#
 [root@nfs ~]# /etc/init.d/nfs status
 rpc.svcgssd 已停
 rpc.mountd is stopped
 nfsd is stopped
 rpc.rquotad is stopped
 [root@nfs ~]# /etc/init.d/nfs start
 Starting NFS services:                                     [  OK  ]
 Starting NFS quotas:                                       [  OK  ]
 Starting NFS mountd:                                       [  OK  ]
 Starting NFS daemon:                                       [  OK  ]
 正在启动 RPC idmapd:                                      [确定]
 [root@nfs ~]# netstat -tunpl |grep 2049
 tcp        0      0 0.0.0.0:2049                0.0.0.0:*                   LISTEN      -                   
 tcp        0      0 :::2049                     :::*                        LISTEN      -                   
 udp        0      0 0.0.0.0:2049                0.0.0.0:*                               -                   
 udp        0      0 :::2049                     :::*                                    -                   
 [root@nfs ~]# lsof -i:2049
 [root@nfs ~]# rpcinfo -p localhost
    program vers proto   port  service
     100000    4   tcp    111  portmapper
     100000    3   tcp    111  portmapper
     100000    2   tcp    111  portmapper
     100000    4   udp    111  portmapper
     100000    3   udp    111  portmapper
     100000    2   udp    111  portmapper
     100011    1   udp    875  rquotad
     100011    2   udp    875  rquotad
     100011    1   tcp    875  rquotad
     100011    2   tcp    875  rquotad
     100005    1   udp  48501  mountd
     100005    1   tcp  36022  mountd
     100005    2   udp  43391  mountd
     100005    2   tcp  39405  mountd
     100005    3   udp  49218  mountd
     100005    3   tcp  41793  mountd
     100003    2   tcp   2049  nfs
     100003    3   tcp   2049  nfs
     100003    4   tcp   2049  nfs
     100227    2   tcp   2049  nfs_acl
     100227    3   tcp   2049  nfs_acl
     100003    2   udp   2049  nfs
     100003    3   udp   2049  nfs
     100003    4   udp   2049  nfs
     100227    2   udp   2049  nfs_acl
     100227    3   udp   2049  nfs_acl
     100021    1   udp  59606  nlockmgr
     100021    3   udp  59606  nlockmgr
     100021    4   udp  59606  nlockmgr
     100021    1   tcp  56597  nlockmgr
     100021    3   tcp  56597  nlockmgr
     100021    4   tcp  56597  nlockmgr
 [root@nfs ~]#

5.修改nfs配置文件

[root@nfs-server ~]# vi /etc/exports 
 /data/w_shared  10.0.0.0/24(rw,sync,all_squash)
 /data/r_shared  10.0.0.0/24(ro)

注:下面介绍一些配置文件中常用的参数含义,以及客户端的IP地址的格式。
rw:可读写权限。
ro:只读权限。
no_root_squash:当登录NFS服务器主机使用共享目录的使用者是root时,其权限将被转换成为匿名使用者,通常它的UID与GID都会变成nobody身份。
root_squash;如果登录NFS主机使用共享目录的使用者是root,那么对于这个共享的目录来说,它具有root的权限,这样会涉及到安全性的问题。
all_squash:忽略登录NFS使用者的身份,其身份都会被转换为匿名使用者,通常即nobody。
anonuid:通常为nobody,也可以自行设定这个UID的值,UID必须存在于/etc/passwd中。 
anongid:同anonuid,但是变为Group ID。
sync:同步写入资料到内存与硬盘中。
async:资料会先暂存于内存中,而非直接写入硬盘。

[root@nfs-server ~]#exportfs -r   让其平滑生效
6.nfs客服端配置
[root@lamp01 ~]# yum -y install nfs-utils rpcbind   
 创建挂载点目录
[root@lamp01 ~]# mkdir -p {/data/b_r,/data/b_w}查看文件是否创建成功
[root@lamp01 ~]# ll /data 
 总用量 8
 drwxr-xr-x 2 nfsnobody root 4096 4月  23 15:13 b_r
 drwxr-xr-x 2 nfsnobody root 4096 4月  23 15:24 b_w测试挂载
[root@lamp01 ~]# showmount -e 10.0.0.130
 Export list for 10.0.0.130:
 /data/r_shared 10.0.0.0/24
 /data/w_shared 10.0.0.0/24
 [root@lamp01 ~]#挂载
[root@lamp01 ~]#mount.nfs 10.0.0.130:/data/r_shared  /data/b_r
[root@lamp01 ~]#mount -t nfs 10.0.0.130:/data/w_shared /data/b_w

查看是否挂载成功

[root@lamp01 ~]# df 
 Filesystem           1K-blocks    Used Available Use% Mounted on
 /dev/sda3             18244476 1582428  15728624  10% /
 tmpfs                   515244       0    515244   0% /dev/shm
 /dev/sda1               194241   25657    158344  14% /boot
10.0.0.130:/data/r_shared
                       18244480 1582464  15728640  10% /data/b_r
 10.0.0.130:/data/w_shared
                       18244480 1582464  15728640  10% /data/b_w


 

umount.nfs: device is busy解决



    #umount  /mnt/nfs 可能会出现device is busy的问题。

    解决方法:

   


1.     /etc/init.d/rpcbind stop, /etc/init.d/rpcbind  start 
     2.      然后umount ,如不行 umount –f /mnt/nfs强行卸载。
     3      再执行上述命令umount文件系统。

    #umount  /mnt/nfs 可能会出现device is busy的问题。


    解决方法:


    1.     /etc/init.d/rpcbind stop, /etc/init.d/rpcbind  start 

    2.      然后umount ,如不行 umount –f /mnt/nfs强行卸载。

    3      再执行上述命令umount文件系统。