1、开启portmap和nfs服务service portmap start     service nfs start     chkconfig --level nfs 35 on     chkconfig --level protmap 35 on

2、将要共享的目录写到exports文件中 假设共享的目录为 /sharedisk/     vim /etc/exports     在exports文件中添加     /sharedisk    192.168.0.0/24(rw,no_root_squash,async)     #表示将/sharedisk这个目录共享给192.168.0.*这些客户机,括号中的参数设置意义为:

   #表示将/sharedisk这个目录共享给192.168.0.*这些客户机,括号中的参数设置意义为:

01	ro                     该主机对该共享目录有只读权限

02	rw                    该主机对该共享目录有读写权限

03	root_squash       客户机用root用户访问该共享文件夹时,将root用户映射成匿名用户

04	no_root_squash  客户机用root访问该共享文件夹时,不映射root用户

05	all_squash          客户机上的任何用户访问该共享目录时都映射成匿名用户

06	anonuid             将客户机上的用户映射成指定的本地用户ID的用户

07	anongid             将客户机上的用户映射成属于指定的本地用户组ID

08	sync                  资料同步写入到内存与硬盘中

09	async                资料会先暂存于内存中,而非直接写入硬盘

10	insecure            允许从这台机器过来的非授权访问

3、重启nfs 或者使用exportfs命令使设置生效

    重启nfs:

    service nfs restart

    用exportfs

    exportfs -rv

    #exportfs用法

    -a :全部mount或者unmount /etc/exports中的内容  

    -r :重新mount /etc/exports中分享出来的目录  

    -u :umount 目录  

    -v :将详细的信息输出到屏幕上

   这样nfs的服务器端就设置好了。

4、在客户端挂载该目录:

    在本地创建挂载的目录  

    mkdir /sharedisk

    mount -t nfs 192.168.0.10:/sharedisk  /sharedisk

    #将服务器192.168.0.10上的/sharedisk/ 路径挂载到本地

    此时,如果服务器端的防火墙有开着的话,将会提示错误,如:

    mount: mount to NFS server '192.168.0.10' failed: System Error: No route to host.

    这个主要是因为防火墙的问题导致的 防火墙上开放对应端口即可

    由于nfs服务需要开启 mountd,nfs,nlockmgr,portmapper,rquotad这5个服务,需要将这5个服务的端口加到iptables里面

    而nfs 和 portmapper两个服务是固定端口的,nfs为2049,portmapper为111。其他的3个服务是用的随机端口,那就需要先把这3个服务的端口设置成固定的。

5、查看当前这5个服务的端口并记录下来 用rpcinfo -p

   把显示 nfs  2049, portmapper  111, 以及剩下的三个服务的端口随便选择一个记录下来

   mountd  976

   rquotad  966

   nlockmgr  33993

6、将这3个服务的端口设置为固定端口,修改/etc/service,添加以下内容(端口号必须在1024以下,且未被占用)

    vim  /etc/services  

    在文件的最后一行添加:

    mountd  976/tcp

    mountd  976/udp

    rquotad  966/tcp

    rquotad  966/udp

    nlockmgr 33993/tcp

    nlockmgr 33993/udp

    保存并退出。  

附带nfs服务端口的设置

[root@localhost]# vi /etc/sysconfig/nfs

# Port rquotad should listen on.

RQUOTAD_PORT=966

# TCP port rpc.lockd should listen on.

LOCKD_TCPPORT=33993

# UDP port rpc.lockd should listen on.

LOCKD_UDPPORT=33993

# Port rpc.mountd should listen on.

MOUNTD_PORT=976

 

其中 portmapper nfs 服务端口是固定的分别是 111 2049

另外 rquotad nlockmgr mountd 服务端口是随机的。由于端口是随机的,这导致防火墙无法设置。

这时需要配置/etc/sysconfig/nfs 使 rquotad nlockmgr mountd 的端口固定。

找到以下几项,将前面的#号去掉。

7、重启下nfs服务。  service nfs restart

8、在防火墙中开放这5个端口

    编辑iptables配置文件    

    vim /etc/sysconfig/iptables

    添加如下行:

01	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 111 -j ACCEPT

02	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 976 -j ACCEPT

03	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 2049 -j ACCEPT

04	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 966 -j ACCEPT

05	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p tcp --dport 33993 -j ACCEPT

06  
07	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 111 -j ACCEPT

08	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 976 -j ACCEPT

09	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 2049 -j ACCEPT

10	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 966 -j ACCEPT

11	-A RH-Firewall-1-INPUT -s 192.168.0.0/24 -m state --state NEW -p udp --dport 33993 -j ACCEPT

保存退出并重启iptables

service iptables restart

重新执行步骤4挂载即可

8、nfs安全设置方面

hosts.allow和hosts.deny设置

hosts.allow设置:

portmap: ip

hosts.deny设置:

portmap:ALL

 

配置完成

 

补充资料

01	ro                      只读访问

02	rw                      读写访问

03	sync                    所有数据在请求时写入共享

04	async                   NFS在写入数据前可以相应请求

05	secure                  NFS通过1024以下的安全TCP/IP端口发送

06	insecure                NFS通过1024以上的端口发送

07	wdelay                  如果多个用户要写入NFS目录,则归组写入(默认)

08	no_wdelay               如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。

09	hide                    在NFS共享目录中不共享其子目录

10	no_hide                 共享NFS目录的子目录

11	subtree_check           如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认)

12	no_subtree_check        和上面相对,不检查父目录权限

13	all_squash              共享文件的UID和GID映射匿名用户anonymous,适合公用目录。

14	no_all_squash           保留共享文件的UID和GID(默认)

15	root_squash             root用户的所有请求映射成如anonymous用户一样的权限(默认)

16	no_root_squas           root用户具有根目录的完全管理访问权限

17	anonuid=xxx             指定NFS服务器/etc/passwd文件中匿名用户的UID

18	anongid=xxx             指定NFS服务器/etc/passwd文件中匿名用户的GID

 

1	/home/share     192.168.102.15(rw,sync,wdelay,hide,nocrossmnt,

2	secure,root_squash,no_all_squash,subtree_check,secure_locks,acl,

3	mapping=identity,anonuid=65534,anongid=65534)

4	/home/share     *(ro,sync,wdelay,hide,nocrossmnt,secure,root_squash,

5	no_all_squash,subtree_check,secure_locks,acl,mapping=identity,

6	anonuid=65534,anongid=65534)

NFS配置命令总结

#########################NFS服务端########################

1)打印系统版本

cat /etc/redhat-release

uname -r

uname -m

2)检查并安装NFS服务

rpm -aq nfs=utils portmap rpcbind

yum grouplist

yum install nfs-utils rpcbind -y

rpm -aq nfs-utils portmap rpcbind

3)启动RPC及NFS服务并检查

/etc/init.d/rpcbind start

ps -ef|grep rpc

/etc/init.d/rpcbind status

rpcinfo -p localhost

/etc/init.d/nfs start

/etc/init.d/nfs status

rpcinfo -p localhost

4)设置开机自启动并检查

chkconfig nfs on

chkconfig rpcbind on

chkconfig --list nfs

chkconfig --list rpcbind

5)配置共享/data目录

cat >>/etc/exports<<EOF

#shared data for bbs by laowoniu at 20151104

/data 100.1.1.0/24(rw,sync)

EOF

cat/etc/exports

6)创建共享目录

mkdir /data

ll /data/

chown -R nfsnobody.nfsnobody /data

7)平滑加载NFS服务并检查服务

/etc/init.d/nfs reload

showmount -e localhost

#########################NFS客户端########################

1)检查系统版本

cat /etc/redhat-release

uname -r

uname -m

2)安装rpc服务并检查

rpm -aq nfs-utils portmap rpcbind

yum groupinstall "NFS file server" -y

rpm -aq nfs-utils portmap rpcbind

3)启动rpc服务并检查

/etc/init.d/rpcbind start

/etc/init.d/rpcbind status

4)设置开机自启动并检查

chkconfig rpcbind on

chkconfig --list rpcbind

5)检查服务端的NFS是否ok

showmount -e 100.1.1.111

排查故障

ping 100.1.1.111

telnet 100.1.1.111 111

6)挂载并测试

mount -t nfs 100.1.1.111:/data /mnt

cd /mnt

ls -l

touch test.log

ls -l

7)设置开机自动挂载

echo "mount -t nfs 100.1.1.111:/data /mnt" >>/etc/rc.local

报错解决方案:

1)

[root@nfsserver data]# rpcinfo -p localhost

rpcinfo: can't contact portmapper: RPC: Remote system error - Connection

refused

解决:rpc服务未启动  /etc/init.d/rpcbind start

2)

[root@nfsclient mnt]# showmount -e 100.1.1.111

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No

route to host)

[root@nfsclient mnt]#

解决:服务端防火墙开启拦截111端口,关闭服务端防火墙service iptables stop

转载于:https://blog.51cto.com/jiarh/1709582