Linux下部署NFS服务 (* ̄︶ ̄)
NFS在文件传送或信息传送的过过程中,依赖于RPC协议。RPC,远程过程调用(Remote Procedure Call),是使客户端能够执行其他系统中程序的一种机制。NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,就是因为NFS使用了RPC提供的传输协议,可以说NFS就是使用PRC的一个程序。
NFS 存储 服务器主要用于用户上传的数据 ,图片 音频 、等信息
NFS服务端、RPC协议、客户端三者可以理解为房源、中介、租客之间的关系:
② ,大型网站不会用 NFS 的, 用的都是 分布式存储 ====================================
三、 NFS 服务端干的三件事 ;
NFS 客户端干的三件事 ;
==========================NFS 部署 环境准备=========================
四 、 服务端配置
4.1 环境准备2台 CentOS 6.9 ,IP可以自定义 ;
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.9 (Final)
五 、 检查系统中是否安装 NFS 和 RPC ,并进行安装NFS 和RPC;
[[email protected] ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-13.el6.x86_64nfs-utils-1.2.3-75.el6.x86_64
[[email protected] ~]# yum -y install nfs-utils rpcbind #使用 yum 安装nfs 和 rpc ;
已加载插件:fastestmirror设置安装进程
Determining fastest mirrors
* base: mirrors.tuna.tsinghua.edu.cn
* extras: mirrors.tuna.tsinghua.edu.cn
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
extras/primary_db
六 、在服务端 创建共享目录/data/nfs/,并且属主和属组都为:nfsnobody,其中nfsnobody是安装nfs服务时默认的用户;
[[email protected] ~]# mkdir -p /data/nfs/
[[email protected] ~]# chown -R nfsnobody.nfsnobody /data/nfs/
[[email protected] ~]# chmod 666 /data/nfs/
[[email protected] ~]# ll /data/
总用量 4
drw-rw-rw-. 2 nfsnobody nfsnobody 4096 6月 27 06:17 nfs
七、编辑配置 NFS 配置文件 ;
[[email protected] ~]# cat >>/etc/exports<<EOF
> /data/nfs 172.16.1.0/24(rw,sync)
> EOF
[[email protected] ~]# cat /etc/exports
/data/nfs 172.16.1.0/24(rw,sync)
[[email protected] ~]#
其中:/data/nfs 是服务器端共享的目录
172.16.1.0/24共享目录的客户端ip地址
(rw,sync) ,其中rw代表拥有读写的权限,sync代表数据同步写入NFS服务器端的硬盘中。
也可以用async,async是大数据时使用,是先写到缓存区,再写到磁盘里。
八、启动RPC 和 NFS 服务 ;
[[email protected] ~]# /etc/init.d/rpcbind start # 先启动rpc
[[email protected] ~]# /etc/init.d/nfs start #启动NFS
[[email protected] ~]# /etc/init.d/rpcbind status #查看一下 rpc 的运行状态
rpcbind (pid 27193) 正在运行...
[[email protected] ~]# /etc/init.d/nfs status #查看一下 nfs 的运行状态
rpc.mountd (pid 27337) 正在运行...
nfsd (pid 27353 27352 27351 27350 27349 27348 27347 27346) 正在运行...
[[email protected] ~]#
九、查看NFS服务是否向rpc注册端口信息,主端口号是:111
[[email protected] ~]# 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
100005 1 udp 46776 mountd
100005 1 tcp 58319 mountd
100005 2 udp 45857 mountd
100005 2 tcp 40719 mountd
100005 3 udp 48297 mountd
100005 3 tcp 56860 mountd
-p :针对某 IP (未写则预设为本机) 显示出所有的 port 与 porgram 的信息;
-t :针对某主机的某支程序检查其 TCP 封包所在的软件版本;
-u :针对某主机的某支程序检查其 UDP 封包所在的软件版本;
[[email protected] ~]# showmount -e localhost
Export list for localhost:
/data/nfs 172.16.1.0/24
[[email protected] ~]#
选项与参数:
-a :显示目前主机与客户端的 NFS 联机分享的状态;
-e :显示某部主机的 /etc/exports 所分享的目录数据。
参数说明:
#rpcinfo -p 检查nfs服务是否有注册端口信息
#showmount -e 检查共享目录信息
十一 、设置服务为开机自启 ;
[[email protected] ~]# chkconfig nfs on
[[email protected] ~]# chkconfig --list nfs
nfs 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[[email protected] ~]# chkconfig --list rpcbind
rpcbind 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[[email protected] ~]#
[[email protected] ~]# tail -2 /etc/rc.local #加入到开机自启中
/etc/init.d/rpcbind start
/etc/init.d/nfs start
[[email protected] ~]#
==========================客户端配置=============================
一 、查看系统中是否有 nfs 和rpc
[[email protected] ~]# rpm -qa nfs-utils rpcbind
rpcbind-0.2.0-13.el6.x86_64nfs-utils-1.2.3-75.el6.x86_64、
二 、进行安装服务,并启动服务 ;
[[email protected] ~]# yum -y install nfs-utils rpcbind
已加载插件:fastestmirror
设置安装进程
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
包 1:nfs-utils-1.2.3-75.el6_9.x86_64 已安装并且是最新版本
解决依赖关系
[[email protected] ~]# /etc/init.d/rpcbind start
[[email protected] ~]# /etc/init.d/nfs start
启动 NFS 服务: [确定]
启动 NFS mountd: [确定]
启动 NFS 守护进程: [确定]
正在启动 RPC idmapd: [确定]
三 、创建挂载目录 ;
[[email protected]]# mkdir -p /data/nfs
四 、查看客户端是否可以收到服务端的共享信息 ;
[[email protected] nfs]# showmount -e 172.16.1.9
Export list for 172.16.1.9:
/data/nfs 172.16.1.0/24
[[email protected] nfs]#
五 、进行nfs 共享目录的挂载 ;
[[email protected] nfs]# mount -t nfs 172.16.1.9:/data/nfs /mnt
[[email protected] nfs]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 2.7G 16G 15% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 39M 141M 22% /boot
172.16.1.9:/data/nfs 20G 2.7G 16G 15% /mnt
服务端 ;
[[email protected] nfs]# echo "nfs" > test.txt
客户端创建一个文件 ;
[[email protected] data]# cat /data/nfs/test.txtnfs
########################### nfs常见问题排错思 ###############################
nfs共享目录权限相关因素
①. 配置文件中的权限指定
②. 共享目录本身的权限,有w权限
③. 共享目录本身的所属用户与所属组的权限指定
### ###################### NFS客户端挂载排错思路 ###########################
客户端排查三部曲
①. 检查服务端房源信息是否还在
rpcinfo -p 172.16.1.9
②. 检查服务端共享目录信息是否存在
showmount -e 172.16.1.9
③. 直接进行目录挂载测试
mount -t nfs 172.16.1.9:/data /mnt
######################### 服务端排查三部曲 #################################
①. 检查服务端房源信息是否还在
rpcinfo -p localhost
如果没有注册的房源信息了,会是什么情况?
①. nfs服务没有启动
②. nfs服务于rpc服务启动顺序不正确
②. 检查服务端共享目录信息是否存在
showmount -e localhost
①. nfs配置文件编写格式错误
③. 直接进行目录挂载测试
mount -t nfs 172.16.1.9:/data /mnt
# 实现nfs客户端开机自动挂载方式
①. 将挂在命令追加到/etc/rc.local开机自启动文件中
②. 编写fstab文件,并且需要配合netfs服务使用,实现开机自动挂载
### nfs常见问题排错
示例1:客户端挂载报错“No such file or directory”
[[email protected] ~]# showmount -e 172.16.1.9
Export list for 172.16.1.9:
/data 172.16.1.0/24
[[email protected] ~]# mount -t nfs 172.16.1.9:/data /mnt
mount.nfs: mounting 172.16.1.9:/data failed, reason given by server: No such file or directory
解答:原因是NFS服务器端没有共享目录/data,创建即可。命令如下:
[[email protected] ~]# mkdir /data
示例2:NFS服务器端启动失败,如下:
[[email protected] ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno = Connection refused
rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp).
[FAILED]
Starting NFS mountd: [FAILED]
Starting NFS daemon: rpc.nfsd: writing fd to kernel failed: errno 111 (Connection refused)
rpc.nfsd: unable to set any sockets for nfsd
[FAILED]
解答:这是因为RPC服务没有在NFS前面启动,需要先启动RPC服务再启动NFS,解决方法为,按顺序启动rpcbind及NFS,命令如下:
[[email protected] ~]# /etc/init.d/rpcbind restart
[[email protected] ~]# /etc/init.d/nfs restart
示例3:注册RPC服务失败,出现failed:RPC Error:Program not registered错误。
[[email protected] ~]# mount -t nfs 172.16.1.9:/data /mnt
mount.nfs: requested NFS version or transport protocol is not supported
[[email protected] ~]# showmount -e 172.16.1.9
clnt_create: RPC: Program not registered
解答:服务器端的NFS没有启动,客户端没有收到服务器端发来的随机端口信息。
解决方法如下:
[[email protected] ~]# /etc/init.d/rpcbind restart
[[email protected] ~]# /etc/init.d/nfs restart
示例4:卸载挂载设备时显示device is busy。
[[email protected] mnt]# umount /mnt
umount.nfs: /mnt: device is busy
umount.nfs: /mnt: device is busy
解答:有可能是当前目录就是挂载的NFS目录(/mnt),也有可能是NFS Server挂了。对于第一种情况,解决办法为退出挂载目录/mnt,再执行umount /mnt卸载。对于第二种情况,NFS Server挂了,NFS Client就会出现问题(df -h窗口会死掉),这时只能强制卸载,方法为:
umount -lf /mnt 其中的参数-f为强制卸载,参数-l为懒惰的卸载。
示例5:CentOS 6.6客户端NFS挂载时遇到问题。
[[email protected] ~]# mount -t nfs 172.16.1.9:/data /mnt
mount:wrong fs type,bad option,bad option,bad superblock on 10.0.0.7:/data,
missing codepage or helper program,or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program )
In some cases useful info is found in syslog - try
meg | tail or so
排查思路:同样的机器,NFS服务器本地可以挂载,但是客户端挂载时wrong fs type,因此尝试所有客户端安装nfs-utils。CentOS6.5及以前的NFS没有遇到这个问题。
解决方法:执行yum install nfs-utils -y,客户端安装NFS软件,但不启动服务。
示例六:共享目录挂载很卡
mount -t nfs 172.16.1.9:/data /mnt
cd /mnt
time touch test.txt
原因分析:
NFS服务端重启之后。立刻进行挂载会出现此问题,因为NFS自身重启的时候,拥有无敌的时间,默认是90秒;在无敌时间内,是不能对共享目录进行更改的操作;
在系统配置中/etc/sysconfig/nfs中指定了无敌时间的配置参数
NFSD_V4_GRACE=90
NFSD_V4_LEASE=90
NLM_GRACE_PERI0D=90
find /proc -name | grep -i ” NLM_GRACE_PERIOD”
find /proc -iname ” NLM_GRACE_PERIOD”
重启NFS服务没有按照顺序进行重启,一旦NFS重启了,需要确认rpcbind服务有没有接收,即rpcinfo -p localhost;先启动rpcbind服务再启动nfs服务
示例七:ls: cannot open directory .: Stale file handle
Stale file handle
客户端报错
mount -t nfs 172.16.1.9:/data /mnt
mount.nfs: Stale file handle
服务端挂载报错
[[email protected] data]# mount -t nfs 172.16.1.9:/data /mnt/
mount.nfs: access denied by server while mounting 172.16.1.9:/data
查看配置文件发现
[[email protected] data]# cat /etc/exports
#share /data by lidao at 20160913
/data 173.16.1.0/24(rw,sync)
原因分析:
/proc/mounts客户端挂载文件中已经存在了相应的挂载记录,没有正确卸载掉,就再次进行挂载,就会出现以上错误。