安装 rpcbind 和 nfs-utils
1 yum install rpcbind
2 yum install nfs-utils
设置共享目录
[root@bogon ~]# vim /etc/exports #修改/etc/exports文件,输出共享目录
添加一行, 例如
/mnt/share *(rw,sync,no_root_squash)
或
/mnt/share 192.168.1.0/24(ro,async) 192.168.0.0/24(rw,sync)
[root@localhost ~]# more /etc/exports
/home/tomcat/archive/ 192.168.1.0/24(ro,async)
/etc/exports文件内容格式:
<输出目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]
a. 输出目录:
输出目录是指NFS系统中需要共享给客户机使用的目录;
b. 客户端:
客户端是指网络中可以访问这个NFS输出目录的计算机
客户端常用的指定方式
指定ip地址的主机:192.168.0.200
指定子网中的所有主机:192.168.0.0/24 192.168.0.0/255.255.255.0
指定域名的主机:david.bsmart.cn
指定域中的所有主机:*.bsmart.cn
所有主机:*
c. 选项:
选项用来设置输出目录的访问权限、用户映射等。NFS主要有3类选项:
访问权限
设置输出目录只读:ro
设置输出目录读写:rw
用户映射
all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);
no_all_squash:与all_squash取反(默认设置);
root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);
no_root_squash:与rootsquash取反;
anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);
其它选项
secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);
insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);
no_wdelay:若有写操作则立即执行,应与sync配合使用;
subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;
启动NFS服务器
为了使NFS服务器能正常工作,需要启动rpcbind和nfs两个服务,并且rpcbind一定要先于nfs启动。
# service rpcbind start
# service nfs start
查询NFS服务器状态
# service rpcbind status
# service nfs status
停止NFS服务器
要停止NFS运行时,需要先停止nfs服务再停止rpcbind服务,对于系统中有其他服务(如NIS)需要使用时,不需要停止portmap服务
# service nfs stop
# service rpcbind stop
客户端
客户端也需要安装rpcbind 和 nfs-utils, 也需要启动这两个服务
查看可用的nfs服务:
[root@bogon ~]# showmount -e 10.10.14.52
Export list for 10.10.14.52:
/mnt/share *
挂载对应的nfs目录到本地, 挂载前要检查此目录是否存在
mount -t nfs 10.10.14.52:/mnt/share /mnt
取消挂载(对应的本地路径)
umount /mnt
固定nfs服务端口以便设置防火墙
vi /etc/sysconfig/nfs
# uncomment the following options
RQUOTAD_PORT=875
LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
STATD_PORT=662
# then stop nfs, stop prcbind, start rpcbind, start nfs
# use "lsof -nPi" to check the listening ports
iptables中需要开放的端口
-A INPUT -m state --state NEW -m tcp -p tcp --dport 111 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 111 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 662 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 662 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 875 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 875 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 892 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 892 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2049 -j ACCEPT
-A INPUT -m state --state NEW -m udp -p udp --dport 2049 -j ACCEPT
问题解决
1. Centos5.x 客户端
安装的组件和Centos6.x不一样
~]#yum install rpcbind
~]#yum install portmap
~]#yum install nfs-utils
~]#service portmap start
~]#service nfs start
~]#showmount -e 192.168.1.20
~]#cd /mnt/
~]#mkdir nfshare
~]#mount -t nfs 192.168.1.20:/home/tomcat/archive /mnt/nfshare
~]#ll /mnt/nfshare/
2. 出现 access denied by server 错误
在Centos6.5上安装的nfs服务, 在Centos5.8上挂载正常, 但是在另一台Centos6.5上挂载出现 access denied by server.
nfs客户端:
[root@localhost ~]# showmount -e 192.168.1.20
Export list for 192.168.1.20:
/home/tomcat/archive 192.168.1.0/24
[root@localhost ~]# mount -t nfs 192.168.1.20:/home/tomcat/archive /mnt/nfshare
mount.nfs: access denied by server while mounting 192.168.1.20:/home/tomcat/archive
nfs服务端:
[root@localhost ~]# more /etc/exports
/home/tomcat/archive/ 192.168.1.0/24(ro,async)
经过测试, 如果在后面加入 no_root_squash 参数, (ro,async,no_root_squash) 后, 就不会再出现 access denied by server 错误, 但是这个选项是不推荐使用的.
后来发现在nfs客户端使用这个命令能正常挂载
mount -o v3 192.168.1.20:/home/tomcat/archive /mnt/nfshare
这个 -o v3 表明使用的是 nfs version 3, 使用以下命令可以查看nfs version
[root@localhost ~]# mount -v
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
nfsd on /proc/fs/nfsd type nfsd (rw)
192.168.1.20:/home/tomcat/archive on /mnt/nfshare type nfs (rw,v3,addr=192.168.1.20)
[root@localhost ~]# nfsstat -m
/mnt/nfshare from 192.168.1.20:/home/tomcat/archive
Flags: rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.20,mountvers=3,mountport=892,mountproto=udp,local_lock=none,addr=192.168.1.20
设置启动自动加载, 编辑 /etc/fstab, 例子
[root@iZ2578hac3vZ ~]# more /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 14 21:16:42 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=94e4e384-0ace-437f-bc96-057dd64f42ee / ext4 defaults,barrier=0 1 1
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
192.168.1.2:/archive/nfshare /mnt/nfshare nfs ro 0 0
同时要确保 rpcbind, nfs, netfs 这3个服务是开机自启动的
chkconfig nfs on
chkconfig netfs on
chkconfig --list