---
title: "nfs"
author: "hochiang"
date: "2015-10-19T10:53:02Z"
tags: []
categories: ["编程语言"]
slug: "nfs"
draft: false
lastmod: "2015-12-30T23:20:35Z"
id: 2249982
url: "https://hochiang.iteye.com/blog/2249982"
---

NFS的安装配置：
centos 5 :
yum install nfs-utils portmap
centos 6 :
yum install nfs-utils rpcbind

服务端：
1.安装
yum install nfs-utils rpcbind 
chkconfig —level 35 rpcbind on
chkconfig —level 35 nfs on 
service rpcbind start 
service nfs start
2.修改配置
vim /etc/exports
>>/home/kaifa/test xxx.xx.xx.xxx(rw,sync,all_squash,anonuid=500,anongid=500)
重启rpcbind nfs服务
查看配置是否正确：
sudo exportfs
3.若启用了防火墙，配置防火墙，如下

客户端：
1.安装
yum install nfs-utils rpcbind
2.测试连接nfs服务器
service iptables stop
showmount -e 192.168.1.75
3.挂载
sudo mount -o nolock -t nfs xx.xx.xx.xx:/drbd /var/www/resin/resource/
4.配置开机自动挂载
vi /etc/fstab
添加 #192.168.1.75:/opt/centos6/ /opt/centos6/ nfs nodev,rw 0 0
或者：
vi /etc/rc.d/rc.local #这个是启动系统时最后一个执行的程序
加入： mount -o nolock -t nfs xx.xx.xx.xx:/drbd /var/www/resin/resource/
-t :指定挂载的文件类型，这里是nfs
如要马上生效：命令行/etc/rc.d/rc.local 回车即可
5.查看挂载
mount -l
或 df
6.卸载
umount -v /dev/sda1          通过设备名卸载 
umount -v /mnt/mymount/      通过挂载点卸载 

**服务端防火墙问题>>>**
如果服务端启用了防火墙，客户端使用showmount -e 服务器IP，会出现如下错误：
clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
使用rpcinfo -p 服务器IP，也会出错：
rpcinfo: can't contact portmapper: RPC: Remote system error - No route to host

这时需要服务端打开相应端口:
# vi /etc/sysconfig/nfs

# Port rquotad should listen on.
RQUOTAD_PORT=875
# TCP port rpc.lockd should listen on.
LOCKD_TCPPORT=32803
# UDP port rpc.lockd should listen on.
LOCKD_UDPPORT=32769
# Port rpc.mountd should listen on.
MOUNTD_PORT=892
 
其中 portmapper nfs 服务端口是固定的分别是 111 2049
另外 rquotad nlockmgr mountd 服务端口是随机的。由于端口是随机的，这导致防火墙无法设置。
这时需要配置/etc/sysconfig/nfs 使 rquotad nlockmgr mountd 的端口固定。
找到以下几项，将前面的#号去掉。

使用rpcinfo -p localhost命令即可看到nfs服务需要用到的端口及协议，然后编辑iptables增加相应端口即可。
-A INPUT -s 11.40.8.144 -p tcp --dport 875 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 875 -j ACCEPT
-A INPUT -s 11.40.8.144 -p tcp --dport 32803 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 32803 -j ACCEPT
-A INPUT -s 11.40.8.144 -p tcp --dport 32679 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 32679 -j ACCEPT
-A INPUT -s 11.40.8.144 -p tcp --dport 892 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 892 -j ACCEPT
-A INPUT -s 11.40.8.144 -p tcp --dport 662 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 662 -j ACCEPT
-A INPUT -s 11.40.8.144 -p tcp --dport 111 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 111 -j ACCEPT
-A INPUT -s 11.40.8.144 -p tcp --dport 2049 -j ACCEPT
-A INPUT -s 11.40.8.144 -p udp --dport 2049 -j ACCEPT

这时到客户端再 使用命令:rpcinfo -p 服务器IP,即可看到相应信息

**问题：**
>>>
出现如下错误提示：
mount.nfs: access denied by server while mounting 172.16.12.140:/home/liangwode/test
那我们可以用以下的方法进行解决：
修改/etc/sysconfig/nfs文件，将 
 
# Turn off v2 and v3 protocol support 
#  RPCNFSDARGS="-N 2 -N 3" 
# Turn off v4 protocol support 
#RPCNFSDARGS="-N 4"    /*把这句话的#号去掉*/
 NFS分为三个版本，即NFS-2 NFS-3 NFS-4，该配置文件默认关闭了这三个的NFS版本，我们只需要打开NFS-4即可。
 
问题解决！！！


**nfs4中使用idmap映射用户的uid/gid**
server和client上的用户uid和gid不一样的情况下，nfs共享权限会出错。
解决：(让uid和gid号相同即可)

或使用idmap这个服务
Server：

安装NFS相关的软件包
$ sudo apt-get install nfs-kernel-server
2.  进行配置
/etc/default/nfs-common

这是一个common配置文件，也就是说服务器端和客户端都要有相同的配置。
NEED_IDMAPD=yes # 此项将打开ID映射
NEED_GSSD=no # no is default
注释：ID映射用于解决服务器端UID与客户机UID不同，而无法访问资源的情况。通过UID映射，只需要相同的用户名即可。
/etc/idmapd.conf

既然启用了ID映射，就需要配置：

Nobody-User = geek
Nobody-Group = geek

这里填写你需要映射都客户端的用户名和组名。
/etc/exports

/test/geek 192.168.1.0/24(rw,nohide,insecure,sync,no_root_squash)
选项解释：
rw                      – 可读写
nohide                  - 如果被挂载的目录下的其他目录也是挂载的，此选项将使它们可见
insecure                – 允许客户端不使用保留端口
sync                    - 每次写操作要同步到物理存储器上（而不是仅写入内存）
no_root_squash          – 当客户端以root的UID/GID（=0）挂载时，不会分配一个匿名UID/GID（也就是说它创建的文件将属于root）
3.  重启服务
/etc/init.d/nfs-kernel-server restart
service rpcidmapd restart

Client:
1. sudo apt-get install nfs-common
2. /etc/default/nfs-common
NEED_IDMAPD=yes  #打开ID映射
3./etc/idmapd.conf

既然启用了ID映射，就需要配置：

Nobody-User = geek
Nobody-Group = geek

4./etc/init.d/idmapd restart
5./etc/fstab
IP:/     /mnt     nfs4   _netdev,auto 0 0                      #NFS4默认用了/，不需要写除完整的路径
6.mount -a -v