环境说明:

两台Linux Centos6.4版本


nfsServer:172.16.10.61

nfsClient:172.16.10.63



NFS共享文件服务的应用场景说明

wKioL1bqD2XjdIbOAAFu96VFVlI652.png



一.NFS软件安装

先检查一下环境

[root@nfsServer ~]# uname -r     //查看Linux内核版本号
2.6.32-358.el6.x86_64
[root@nfsServer ~]# cat /etc/redhat-release //查看centos版本号
CentOS release 6.4 (Final)
[root@nfsServer ~]# uname -m        //查看是32位 还是64位 系统
x86_64

rpm检查当前服务器是否有安装 nfs-utils 和 rpcbind 这两个服务

[root@nfsServer ~]# rpm -qa nfs-utils rpcbind      //如果没有任何的结果 就说明没安装过


这里我们采用 yum install 方式进行安装这两个软件


开始安装:

[root@nfsServer ~]# yum install nfs-utils rpcbind -y    开始安装,如下图片所示
Loaded plugins: fastestmirror, security

wKiom1bqGc6QJ-I6AAA9_9ykBEk985.png


nfs-utils和rpcbind 安装完毕 我们先检查一下情况

[root@nfsServer ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-11.el6_7.x86_64
nfs-utils-1.2.3-64.el6.x86_64

先开 rpcbind 服务  这个一定要先开的 不然后面可能会报错什么的....

[root@nfsServer ~]# /etc/init.d/rpcbind start                //开启rpc服务
Starting rpcbind:                                          [  OK  ]
[root@nfsServer ~]# ps -ef |grep -i rpc                    //检查是否有进程
rpc       1550     1  0 18:59 ?        00:00:00 rpcbind
root      1554  1410  0 18:59 pts/0    00:00:00 grep -i --color=auto -i rpc
[root@nfsServer ~]# /etc/init.d/rpcbind status         //检查运行状态
rpcbind (pid  1550) is running...

开启NFS服务

[root@nfsServer ~]# /etc/init.d/nfs start //开启NFS服务 向rpc注册nfs连接端口信息
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
Starting RPC idmapd:                                       [  OK  ]
[root@nfsServer ~]# /etc/init.d/nfs status //查看nfs运行状态
rpc.svcgssd is stopped
rpc.mountd (pid 1621) is running...
nfsd (pid 1636 1635 1634 1633 1632 1631 1630 1629) is running...
rpc.rquotad (pid 1617) is running...
[root@nfsServer ~]# rpcinfo -p 172.16.10.61 //查看rpc信息
   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  54898  mountd
    100005    1   tcp  56474  mountd
    100005    2   udp  41339  mountd
    100005    2   tcp  51710  mountd
    100005    3   udp  43880  mountd
    100005    3   tcp  55842  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  51604  nlockmgr
    100021    3   udp  51604  nlockmgr
    100021    4   udp  51604  nlockmgr
    100021    1   tcp  33013  nlockmgr
    100021    3   tcp  33013  nlockmgr
    100021    4   tcp  33013  nlockmgr

将nfs rpcbind这两个服务加入到开机自启动项里面

[root@nfsServer ~]# chkconfig rpcbind on
[root@nfsServer ~]# chkconfig nfs on    
[root@nfsServer ~]# chkconfig --list rpcbind
rpcbind         0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@nfsServer ~]# chkconfig --list nfs
nfs             0:off   1:off   2:on    3:on    4:on    5:on    6:off


============================

服务端配置 NFS的文件是 /etc/exports 

============================


现在我们要将服务端的 /data 目录配置共享 如果根目录下面没有 那就创建一个 mkdir /data


******************************************************************************************


vi /etc/exports 配置文件

[root@nfsServer ~]# 
[root@nfsServer ~]# vi /etc/exports    

##### this is my nfs server
/data 172.16.10.0/16(rw,sync)

保存wq 并检查语法 优雅重起nfs服务

[root@nfsServer ~]# 
[root@nfsServer ~]# /etc/init.d/nfs reload    //重启nfs服务
[root@nfsServer ~]#


重启完毕之后  我们就自己检查一下是否能成功挂载


在nfs服务器自身检查一下

[root@nfsServer ~]# 
[root@nfsServer ~]# showmount -e 172.16.10.61
Export list for 172.16.10.61:
/data 172.16.10.0/16
[root@nfsServer ~]#

在nfs客户端上面检查一下

[root@nfsClient ~]# showmount -e 172.16.10.61
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

如果在检查的时候 出现以上的问题 则说明 nfs服务端上面的防火墙是打开状态 不允许连接

那么我们就要关闭防火墙!


关闭防火墙命令:

[root@nfsServer ~]# /etc/init.d/iptables stop
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Unloading modules:



在关闭防火墙服务之后 我们再去检查连接 

[root@nfsClient ~]# showmount -e 172.16.10.61    //有结果 则说明连接成功...
Export list for 172.16.10.61:
/data 172.16.10.0/16
[root@nfsClient ~]#



客户端挂载共享目录 mount -t nfs 172.16.10.61:/data /mnt/  

[root@nfsClient ~]# mount -t nfs 172.16.10.61:/data /mnt/
[root@nfsClient ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              18G  1.8G   15G  11% /
tmpfs                 497M     0  497M   0% /dev/shm
/dev/sda1             194M   28M  156M  16% /boot
172.16.10.61:/data     18G  1.8G   15G  11% /mnt
[root@nfsClient mnt]# pwd
/mnt
[root@nfsClient mnt]# ll
total 0
-rw-r--r--. 1 root root 0 Mar 17 19:41 index.php





*************************************************************

常见问题


  1. 在我们成功挂载网络文件系统之后 我们是不可能把共享目录里面写入数据的

  因为我们没有对共享目录有权限,默认的用户是nfsnobosy

    

[root@nfsClient mnt]# 
[root@nfsClient mnt]# touch news.php
touch: cannot touch `news.php': Permission denied    //提示没有权限
[root@nfsServer nfs]# cat /var/lib/nfs/etab      //查看nfs访问用户及其它的参数

wKiom1bqKfmg3GzZAABhWCiV6MU399.png


[root@nfsServer nfs]# cat /etc/passwd|grep 65534
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin


可以看的到是 网络访问是通过 nfsnobody 这个用户进行访问的

所以这里我们要在服务端上面 把 /data 的属主和属组都修改在 nfsnoboy 

这样子就可以实现读写权限的功能

[root@nfsServer /]# chown nfsnobody.nfsnobody data
[root@nfsServer /]# ll -ld data
drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Mar 17 19:41 data
[root@nfsServer /]#




到这里 nfs基本上就配置好了  你可以从客户端上面新建文件了