一、搭建环境
服务端:Linux nfsser 2.6.32-431.el6.x86_64 IP:10.10.10.11
客户端:Linux nfsclient 2.6.32-431.el6.x86_64 IP:10.10.10.12
二、服务端和客户端安装nfs-utils rpcbind
1、对服务端配置
检查系统是否安装rpcbind和nfs-utils
[root@nfsser ~]# rpm -qa |grep rpcbind [root@nfsser ~]# rpm -qa |grep nfs-utils
通过yum进行安装
[root@nfsser ~]# yum install -y rpcbind nfs-utils portmap Loaded plugins: fastestmirror, security base | 3.7 kB 00:00 base/primary_db | 4.7 MB 00:03 extras | 3.4 kB 00:00 extras/primary_db | 29 kB 00:00 updates | 3.4 kB 00:00 中间省略。。。。。。 Dependency Updated: keyutils-libs.x86_64 0:1.4-5.el6 Complete!
查看安装结果:
[root@nfsser ~]# rpm -qa nfs-utils portmap rpcbind rpcbind-0.2.0-13.el6_9.1.x86_64 nfs-utils-1.2.3-75.el6.x86_64
2、对服务端进行配置
[root@nfsclient ~]# /etc/init.d/rpcbind start #启动rpcbind服务 Starting rpcbind: [ OK ] [root@nfsclient ~]# ps -ef |grep rpcbind #查看rpcbind服务进程 rpc 26947 1 0 21:34 ? 00:00:00 rpcbind root 26951 1500 0 21:35 pts/0 00:00:00 grep rpcbind [root@nfsclient ~]# /etc/init.d/rpcbind status #查看服务运行状态 rpcbind (pid 26947) is running... [root@nfsclient ~]# rpcinfo -p localhost 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
#如查rpcbind服务停止后,查看rpcbind本地对应端口 [root@nfsclient ~]# /etc/init.d/rpcbind stop #停止rpcbind服务 Stopping rpcbind: [ OK ] [root@nfsclient ~]# rpcinfo -p localhost rpcinfo: can't contact portmapper: RPC: Remote system error - Connection refused #报错原因是因为rpcbind服务未启动 [root@nfsclient ~]# /etc/init.d/rpcbind reload #平滑加载nfs服务 Stopping rpcbind: [FAILED] Starting rpcbind: [ OK ] :通过reload启动rpcbind服务(通过reload启动服务,类似于你在5点银行下班的时候你已经在银行大 厅,不会影响你办理业务,但5点的时候外面的人在进来是进不来的) [root@nfsser ~]# /etc/init.d/nfs start #启动nfs服务 Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@nfsser ~]# rpcinfo -p localhost 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 50275 mountd 100005 1 tcp 46245 mountd 100005 2 udp 45499 mountd 100005 2 tcp 51182 mountd 100005 3 udp 36337 mountd 100005 3 tcp 52782 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 52478 nlockmgr 100021 3 udp 52478 nlockmgr 100021 4 udp 52478 nlockmgr 100021 1 tcp 41895 nlockmgr 100021 3 tcp 41895 nlockmgr 100021 4 tcp 41895 nlockmgr [root@nfsser ~]# chkconfig rpcbind on #加入开自启 [root@nfsser ~]# chkconfig nfs on [root@nfsser ~]# chkconfig --list rpcbind #检查是否加入开机自启 rpcbind 0:off1:off2:on3:on4:on5:on6:off [root@nfsser ~]# chkconfig --list nfs nfs 0:off1:off2:on3:on4:on5:on6:off
2、对客户端配置(服务端只用开启rpcbind服务即可)
[root@nfsser ~]# yum install -y rpcbind nfs-utils portmap Loaded plugins: fastestmirror, security base | 3.7 kB 00:00 base/primary_db | 4.7 MB 00:03 extras | 3.4 kB 00:00 extras/primary_db | 29 kB 00:00 updates | 3.4 kB 00:00 中间省略。。。。。。 Dependency Updated: keyutils-libs.x86_64 0:1.4-5.el6 Complete!
查看安装结果:
[root@nfsser ~]# rpm -qa nfs-utils portmap rpcbind rpcbind-0.2.0-13.el6_9.1.x86_64 nfs-utils-1.2.3-75.el6.x86_64 [root@nfsclient ~]# /etc/init.d/rpcbind start #启动rpcbind服务 Starting rpcbind: [ OK ] [root@nfsclient ~]# chkconfig rpcbind on #加入开机自启 [root@nfsclient ~]# chkconfig --list rpcbind #检查是否添加开机自启 rpcbind 0:off1:off2:on3:on4:on5:on6:off [root@nfsclient ~]# /etc/init.d/rpcbind status rpcbind (pid 27102) is running... [root@nfsclient ~]#
二、对共享文件配置
1、配置服务器端文件
[root@nfsser ~]# mkdir /sharedata #建立共享sharedata [root@nfsser ~]# vi /etc/exports #编辑nfs配置文件 [root@nfsser ~]# cat /etc/exports #查看nfs配置文件结果 ##share data at 20171018 /sharedata 10.10.10.0/24(rw,sync)
注:
[root@nfsser ~]# cat /var/lib/nfs/etab #查看nfs默认配置 /sharedata10.10.10.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,ro,root_squash,no_all_squash) /sharedata*(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash)
[root@nfsser ~]# showmount -e localhost #查看本机共享是否成功 Export list for localhost: /sharedata (everyone)
2、配置客户端文件
[root@nfsclient ~]# showmount -e 10.10.10.11 #成功检没到共享 Export list for 10.10.10.11: /sharedata (everyone)
[root@nfsclient ~]# mount -t nfs 10.10.10.11:/sharedata /mnt #挂载到/mnt下 [root@nfsclient ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 59G 1.6G 55G 3% / tmpfs 931M 0 931M 0% /dev/shm /dev/sda1 194M 34M 151M 19% /boot 10.10.10.11:/sharedata 59G 1.6G 55G 3% /alidata
[root@nfsclient mnt]# ll #查文件sharedata文件里的内容 total 72 -rw-r--r--. 1 root root 46850 Mar 23 2016 log.txt -rw-r--r--. 1 root root 14476 Mar 23 2016 mytlv-20160119.log -rw-r--r--. 1 root root 4537 Sep 5 22:12 qzj.txt [root@nfsclient mnt]# touch 123 #测试能否有写入权限 touch: cannot touch `123': Permission denied 拒绝访问
在nfsser端检查
[root@nfsser /]# ll -ld /sharedata/
drwxr-xr-x. 2 root root 4096 Oct 18 22:51 /sharedata/
[root@nfsser sharedata]# cat /var/lib/nfs/etab /sharedata10.10.10.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,ro,root_squash,no_all_squash) /sharedata*(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,anonuid=65534,anongid=65534,sec=sys,rw,root_squash,no_all_squash) [root@nfsser sharedata]# grep 65534 /etc/passwd #根据ID查找用户 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin [root@nfsser sharedata]# chown -R nfsnobody.nfsnobody /sharedata/ #给sharedata赋予权限 [root@nfsser /]# ll -ld /sharedata/ drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Oct 18 22:51 /sharedata/
[root@nfsclient alidata]# touch asd # 测试创建asd文件 [root@nfsclient alidata]# ll total 72 -rw-r--r--. 1 root root 0 Oct 18 23:53 ad -rw-r--r--. 1 nfsnobody nfsnobody 0 Oct 19 00:11 asd 创建成功 -rw-r--r--. 1 nfsnobody nfsnobody 46850 Mar 23 2016 log.txt -rw-r--r--. 1 nfsnobody nfsnobody 14476 Mar 23 2016 mytlv-20160119.log -rw-r--r--. 1 nfsnobody nfsnobody 4537 Sep 5 22:12 qzj.txt
客户端在重启后,挂载的alidata会丢失,需要加载到开机自启。
echo "mount -t nfs 10.10.10.11:/sharedata /alidata" >>/etc/rc.local