hoyong

导航

nfs客户端挂载出错 mount.nfs access denied by server while mounting(转)

nfs客户端挂载出错 mount.nfs access denied by server while mounting
 
原始出处:http://zhaochj.blog.51cto.com/368705/1719640

    在生产环境中一系统因架构变化后,把一模块原有的单节点扩展成了两个节点,前端采用用nginx做负载的架构,而这两个节点需要一个公共的存储来存放用户上传的图片,用户的并发不高,再因原有业务模块是从nginx主机上剥离出来的,在原nginx主机上有“img”这个存储图片的目录,所以考虑在nginx主机上安装nfs服务,再在两个节点上同点挂载“img”目录实现集群节点对图片目录的访问。

    NFS服务的安装比较简单,但在上生产环境时还是应该在自己的测试环境先验证,这里把安装过程记录如下:

服务端配置

NFS服务端系统环境:

1
2
3
4
5
[root@nginx-01 ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel \r on an \m
[root@nginx-01 ~]# uname -r
2.6.32-431.el6.x86_64

如果系统没有安装nfs服务,运行以下命令安装:

1
2
3
4
[root@nginx-01 ~]# yum -y install nfs-utils rpcbind
[root@nginx-01 ~]# service rpcbind start
[root@nginx-01 ~]# service nfs start
[root@nginx-01 ~]# chkconfig nfs on

配置共享目录:

1
2
[root@nginx-01 ~]# vim /etc/exports
/home/tomcat/img/ 192.168.207.128(insecure,rw,sync,anonuid=500,anongid=500)

#此文件的配置格式为:<输出目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]

#注释

insecure    当mount监听端口大于1024时需要使用此参数

1
2
3
4
5
6
7
[root@nginx-01 ~]# ss -tnlp | grep mountd
LISTEN     0      128                       *:50288                    *:*      users:(("rpc.mountd",5354,8))
LISTEN     0      128                       *:33842                    *:*      users:(("rpc.mountd",5354,16))
LISTEN     0      128                      :::54325                   :::*      users:(("rpc.mountd",5354,14))
LISTEN     0      128                       *:43992                    *:*      users:(("rpc.mountd",5354,12))
LISTEN     0      128                      :::50657                   :::*      users:(("rpc.mountd",5354,10))
LISTEN     0      128                      :::33795                   :::*      users:(("rpc.mountd",5354,18))

rw   read-write,可读写;

sync:文件同时写入硬盘和内存;

async:文件暂存于内存,而不是直接写入内存;

no_root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,也拥有root权限。显然开启这项是不安全的。

root_squash:NFS客户端连接服务端时如果使用的是root的话,那么对服务端分享的目录来说,拥有匿名用户权限,通常他将使用nobody或nfsnobody身份;

all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端分享的目录来说都是拥有匿名用户权限;

anonuid:匿名用户的UID值,通常是nobody或nfsnobody,可以在此处自行设定;

anongid:匿名用户的GID值。

重新加载/etc/exports的配置:

1
[root@nginx-01 ~]# exportfs -r

查看本机共享的目录:

1
2
3
[root@nginx-01 ~]# showmount -e localhost
Export list for localhost:
/home/tomcat/img 192.168.207.128

客户端配置

客户端系统环境:

1
2
3
4
root@rabbit-0:~# cat /etc/issue
Debian GNU/Linux 8 \n \l
root@rabbit-0:~# uname -r
3.16.0-4-amd64

在客户端只需要安装nfs-client端即可,如果没有安装请使用以下命令进行安装:

1
root@rabbit-0:~# aptitude -y install nfs-client

列出nfs服务端共享的目录:

1
2
3
root@rabbit-0:~# showmount -e 192.168.207.129
Export list for 192.168.207.129:
/home/tomcat/img 192.168.207.128

因生产环境是需要把nfs服务器的tomcat用户家目录下的img目录共享出来后让现有的两个节点的tomcat用户来挂载到家目录的img目录下,所以需要先切换到tomcat用户下创建img目录,如下:

1
2
3
root@rabbit-0:~# su - tomcat -c "mkdir ~/img"
root@rabbit-0:~# ls /home/tomcat/img/ -ld
drwxr-xr-x 2 tomcat tomcat 4096 12月  4 15:23 /home/tomcat/img/

挂载nfs的共享目录:

root@rabbit-0:~# mount -t nfs 192.168.207.129:/home/tomcat/img /home/tomcat/img -o proto=tcp -o nolock

mount.nfs: access denied by server while mounting 192.168.207.129:/home/tomcat/img

原因:在centos 6 x64的系统下使用的是NFSv4,在/var/log/messages中能看到nfs启动时的日志输出:

1
2
3
4
Dec  4 15:21:04 nginx-01 rpc.mountd[2178]: Version 1.2.3 starting
Dec  4 15:21:04 nginx-01 kernel: NFSD: Using /var/lib/nfs/v4recovery as the NFSv4 state recovery directory
Dec  4 15:21:04 nginx-01 kernel: NFSD: starting 90-second grace period
Dec  4 15:21:58 nginx-01 kernel: svc: 192.168.207.128, port=748: unknown version (4 for prog 100003, nfsd)

使用NFSv4时,在nfs服务端做如下操作:

1
2
3
4
5
6
7
[root@nginx-01 ~]# vim /etc/sysconfig/nfs
# Optional arguments passed to rpc.nfsd. See rpc.nfsd(8)
# Turn off v2 and v3 protocol support
RPCNFSDARGS="-N 2 -N 3"
        ----->启用
# Turn off v4 protocol support
RPCNFSDARGS="-N 4"     ---->启用

重启服务:

1
[root@nginx-01 ~]# service nfs restart

再在客户端再次尝试挂载:

1
2
root@rabbit-0:~# mount -t nfs 192.168.207.129:/home/tomcat/img /home/tomcat/img -o proto=tcp -o nolock
root@rabbit-0:~#

成功挂载。

1
2
3
root@rabbit-0:~# ls /home/tomcat/img/
    #之前有两个文件存在
fstab  test

测试:直接用root用户创建一个文件,但这个文件的权限还是500的,在服务端uid为500的用户是tomcat,这正在我需要的效果。

1
2
3
root@rabbit-0:~# touch /home/tomcat/img/nfs.txt
root@rabbit-0:~# ls -l /home/tomcat/img/nfs.txt
-rw-r--r-- 1 500 500 0 12月  4 15:24 /home/tomcat/img/nfs.txt

最后让此目录自动在主机重启后自动挂载,我这里直接写入到/etc/rc.local里:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
root@rabbit-0:~# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
mount -t nfs 192.168.207.129:/home/tomcat/img /home/tomcat/img -o proto=tcp -o nolock
exit 0

重启测试一下能否成功挂载。

posted on 2016-12-21 20:07  hoyong  阅读(907)  评论(0编辑  收藏  举报