一、NFS服务简介
NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类Unix系统间实现磁盘文件共享的一种方法。
NFS 的基本原则是“容许不同的客户端及服务端通过一组RPC分享相同的文件系统”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。
NFS在文件传送或信息传送过程中依赖于RPC协议。RPC,远程过程调用 (Remote Procedure Call) 是能使客户端执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,这是因为NFS使用了一些其它的传输协议。而这些传输协议用到这个RPC功能的。可以说NFS本身就是使用RPC的一个程序。或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS SERVER或者NFS CLIENT。这样SERVER和CLIENT才能通过RPC来实现PROGRAM PORT的对应。可以这么理解RPC和NFS的关系:NFS是一个文件系统,而RPC是负责负责信息的传输。
RPC(Remote Procedure Call):
RPC is a powerful technique for constructing distributed, client-server based applications.
用于开发分布式、C/S架构应用程序的技术。
It is based on extending the notion of conventional, or local procedure calling, so that the called procedure need not exist in the same address space as the calling procedure.
RPC根植于本地过程调用的思想,所不同的是,RPC所涉及到的进程可以位于不同的主机。
Linux: 提供rpc服务的程序, Portmap: 111/tcp, 111/udp
RPC: 编程技术,简化分布式应用程序的开发,RPC:C --> RPC C --> RPC S --> S
NFS Client --> NFS Server
二、Linux系统版本
cat /etc/redhat-release
CentOS release 6.5 (Final)
uname -r
2.6.32-431.el6.x86_64
uname -m
x86_64
首先关闭网络防火墙
chkconfig iptables off
查看防火墙
chkconfig --list iptables
关闭seLinux
setenforce 0
三、服务器网络规划
192.168.2.132nfsserver
192.168.2.128nfsclient
四、安装nfs软件
1、首先查看系统中是否安装nfs、portmap、rpcbind
rpm -qa|grep nfs
rpm -qa|grep portmap
rpm -qa|grep rpcbind
2、安装nfs软件有两种方式
2.1 通过yum grouplist查看没有安装的包组
NFS file server
2.1.1 通过包组来安装
yum groupinstall -y "NFS file server"
2.2通过yum install 来安装
yum install -y nfs* rpcbind*
五、启动nfs软件
1、启动rpcbind
/etc/init.d/rpcbind start
查看rpc服务运行状态
/etc/init.d/rpcbind status
2、启动nfs
/etc/init.d/nfs start
3、查看nfs文件系统
showmount -e localhost
如果查询为空的话,说明还不存在远程的文件系统需要nfs配置文件中配置
4、配置nfs文件系统
4.1 mkdir /nfs/data -pv
4.2 vim /etc/exports
4.3 cat /etc/exports
#nfs share /nfs/data
/nfs/data 192.168.2.0/24(rw)
4.4 重启nfs
/etc/init.d/nfs reload
showmount -e localhost
Export list for localhost:
/nfs/data 192.168.2.0/24
六、配置nfsclient端
1、开启client端rpc服务
/etc/init.d/rpcbind start
2、开机自动
chkconfig rpcbind on
3、查看修改结果
chkconfig --list rpcbind
4、查看远程nfsserver文件系统
showmount -e 192.168.2.132
Export list for 192.168.2.132:
/nfs/data 192.168.2.0/24
5、在本机创建一个文件目录挂载远程nfs文件系统
5.1 创建一个本地目录
mkdir /webimage
5.2 挂载nfs文件系统
mount -t nfs 192.168.2.132:/nfs/data/ /webimage/
6、验证是否挂载成功
在nfsserver上创建一个文件
echo "hello" >> /nfs/data/a.txt
在nfsclient上查看文件是否创建
ll /webimage/
total 4
-rw-r--r--. 1 root root 6 Jul 15 10:45 a.txt
在nfsclient创建一个文件
echo "1" >> /webimage/b.txt
-bash: b.txt: Permission denied
此时客户端没有权限写入文件如果解决这个问题
在nfsserver端给./nfs/data授权
chmod 777 -R /nfs/data/
再到nfsclient端创建一个文件
echo "1" >> /webimage/b.txt
查看创建文件的权限
ll /webimage/b.txt
<span style="white-space:pre"> </span>-rw-r--r--. 1 nfsnobody nfsnobody 2 Jul 15 11:05 /webimage/b.txt
------------------------------------------nfs server权限问题-----------------------------------------------------------------------
如果通过nfsclient向nfsserver服务器写入文件默认的所属组所属人都是nfsnobody,查看这个默认的配置信息在
cat /var/lib/nfs/etab
/nfs/data 192.168.2.0/24(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)
在配置文件中可以看到anonuid、anongid可以查看用户
grep 65534 /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
所以可以直接修改nfsserver文件目录的用户所属组所属人
chown -R nfsnobody.nfsnobody /nfs/data/
到nfsclient端创建一个文件
echo "1" >> /webimage/b.txt
查看创建文件的权限
ll /webimage/b.txt
-rw-r--r--. 1 nfsnobody nfsnobody 2 Jul 15 11:05 /webimage/b.txt
----------------------------------------------------------------------------------------------------------------------------------------------------
7、开机时候自动挂载此文件系统(如果nfsserver没有启动,nfsclient会跳过mount挂载,不会导致系统无法启动)
cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/bin/mount 192.168.2.132:/nfs/data/ /webimage/
8、将文件挂载到系统/etc/fstab中(一般不要在fstab文件中配置网络文件系统的挂载,因为Linux启动流程中/etc/fstab 比网络优先加载导致网络文件系统无法加载)
<server>:</remote/export> </local/directory> nfs < options> 0 0
192.168.2.132:/nfs/data/ /webimage/ nfs defaults 0 0 <span style="white-space:pre"> </span>工作中需要将后两列设置为0 0否则导致系统无法启动
NFS总结
1、NFS进程说明
ps -ef | egrep "rpc|nfs"
rpc 1581 1 0 10:03 ? 00:00:00 rpcbind RPC服务主进程
root 1617 2 0 10:05 ? 00:00:00 [rpciod/0]
root 3759 1 0 12:33 ? 00:00:00 rpc.rquotad 磁盘配额进程
root 3763 1 0 12:33 ? 00:00:00 rpc.mountd 权限管理进程
root 3769 2 0 12:33 ? 00:00:00 [nfsd4]
root 3770 2 0 12:33 ? 00:00:00 [nfsd4_callbacks]
root 3771 2 0 12:33 ? 00:00:00 [nfsd] nfs服务主进程
root 3772 2 0 12:33 ? 00:00:00 [nfsd]
root 3773 2 0 12:33 ? 00:00:00 [nfsd]
root 3774 2 0 12:33 ? 00:00:00 [nfsd]
root 3775 2 0 12:33 ? 00:00:00 [nfsd]
root 3776 2 0 12:33 ? 00:00:00 [nfsd]
root 3777 2 0 12:33 ? 00:00:00 [nfsd]
root 3778 2 0 12:33 ? 00:00:00 [nfsd]
root 3801 1 0 12:33 ? 00:00:00 rpc.idmapd
NFS常用路径
/etc/exports NFS服务主配置文件,配置NFS具体共享服务的地点,默认内容为空。
/usr/sbin/exportfsNFS服务的管理命令。可以加载NFS配置生效,还可以直接配置NFS共享目录,即无需配置/etc/exports实现共享。exportfs -rv 平滑重启
/usr/sbin/showmount常用来查看客户端,查看NFS配置及挂载结果的命令。
/var/lib/nfs/etabNFS配置文件的完整参数设定文件(有很多没有配置但是默认就有的NFS参数)
/var/lib/nfs/xtab适合ContOS5.x记录曾经挂载过的NFS客户端的信息,包括IP地址,CentOS6.x文件中没有内容
NFS挂载注意事项
1、把NFS rpc服务的启动命令和挂载命令放到/etc/rc.local中然后通过监控软件监控开机后系统挂载情况
2、如果决定考虑把挂载命令放入/etc/fstab里,那么第5,6列数字要为0。即不备份,不做磁盘检查。