linux点滴:NFS
介绍
NFS,Network File System,网络文件系统。主要功能是通过网络让不同的主机系统间共享资源,类似于windows下的文件共享。适用于互联网中小型企业。
工作原理
- 客户端发送请求
- 客户端的RPC服务通过111端口向服务端的RPC服务发送请求
- 服务端的RPC服务找到对应的端口映射,返回给客户端的RPC服务。
- 客户端获取端口,与服务端建立连接。
部署
准备环境
系统 | 角色 | 主机名 | IP地址 |
---|---|---|---|
CentOS 6.7 X86_64 | 服务端 | nfs01 | 172.16.1.31 |
CentOS 6.7 X86_64 | 客户端 | web01 | 172.16.1.8 |
#服务端nfs01
[root@nfs01 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@nfs01 ~]# uname -r
2.6.32-573.el6.x86_64
#客户端web01
[root@web01 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@web01 ~]# uname -r
2.6.32-573.el6.x86_64
#客户端web02
[root@web02 ~]# cat /etc/redhat-release
CentOS release 6.7 (Final)
[root@web02 ~]# uname -r
2.6.32-573.el6.x86_64
注:防火墙和SELinux已关闭
服务端
安装软件
#查看软件信息
[root@nfs01 ~]# rpm -qa nfs-utils
nfs-utils-1.2.3-64.el6.x86_64
#如果没有,使用yum安装。
yum install nfs-utils -y
rpm -qa nfs-utils
- nfs-utils:NFS服务的主程序,包括
rpc.nfsd
、rpc.mountd
等 - rpcbind:管理端口映射
注:nfs-utils
软件包中含有rpcbind
,只需安装nfs-utils
即可。
启动服务
#先启动RPC服务
[root@nfs01 ~]# /etc/init.d/rpcbind start
Starting rpcbind: [ OK ]
[root@nfs01 ~]# /etc/init.d/rpcbind status
rpcbind (pid 1562) is running...
#查看RPC端口映射关系
[root@nfs01 ~]# 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
#后启动NFS服务
[root@nfs01 ~]# /etc/init.d/nfs start
Starting NFS services: [ OK ]
Starting NFS quotas: [ OK ]
Starting NFS mountd: [ OK ]
Starting NFS daemon: [ OK ]
Starting RPC idmapd: [ OK ]
[root@nfs01 ~]# /etc/init.d/nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 1623) is running...
nfsd (pid 1639 1638 1637 1636 1635 1634 1633 1632) is running...
rpc.rquotad (pid 1618) is running...
#查看端口映射关系,多出NFS服务端口。
[root@nfs01 ~]# 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 32455 mountd
100005 1 tcp 26030 mountd
100005 2 udp 14968 mountd
100005 2 tcp 30778 mountd
100005 3 udp 59967 mountd
100005 3 tcp 26265 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 31414 nlockmgr
100021 3 udp 31414 nlockmgr
100021 4 udp 31414 nlockmgr
100021 1 tcp 11059 nlockmgr
100021 3 tcp 11059 nlockmgr
100021 4 tcp 11059 nlockmgr
------------------------------------------------------------
Q:如果先启动nfs服务或者未启动rpc服务,会出现如下错误
[root@nfs01 ~]# showmount -e localhost
clnt_create: RPC: Program not registered
Q1:如果先启动nfs服务,再启动rpc服务,会出现如下错误
[root@nfs01 ~]# /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服务?NFS服务的端口是变化的,通过RPC服务来管理端口映射关系,提供给客户端NFS服务的端口信息来访问共享资源。
进程文件说明
[root@nfs01 ~]# ps -ef|egrep "nfs|rpc"
rpc 1562 1 0 03:22 ? 00:00:00 rpcbind
root 1609 2 0 03:23 ? 00:00:00 [rpciod/0]
root 1618 1 0 03:23 ? 00:00:00 rpc.rquotad
root 1623 1 0 03:23 ? 00:00:00 rpc.mountd
root 1630 2 0 03:23 ? 00:00:00 [nfsd4]
root 1631 2 0 03:23 ? 00:00:00 [nfsd4_callbacks]
root 1632 2 0 03:23 ? 00:00:00 [nfsd]
root 1633 2 0 03:23 ? 00:00:00 [nfsd]
root 1634 2 0 03:23 ? 00:00:00 [nfsd]
root 1635 2 0 03:23 ? 00:00:00 [nfsd]
root 1636 2 0 03:23 ? 00:00:00 [nfsd]
root 1637 2 0 03:23 ? 00:00:00 [nfsd]
root 1638 2 0 03:23 ? 00:00:00 [nfsd]
root 1639 2 0 03:23 ? 00:00:00 [nfsd]
root 1670 1 0 03:23 ? 00:00:00 rpc.idmapd
- nfsd:
rpc.nfsd
的进程,主进程,管理客户端是否能够登入服务端。 - mountd:
rpc.mountd
的进程,管理挂载,包括权限、用户认证等 - rpc.idmapd:名字映射进程
创建共享目录
[root@nfs01 ~]# mkdir -p /data
[root@nfs01 ~]# ls -ld /data
drwxr-xr-x. 2 root root 4096 Apr 28 04:32 /data
#将目录拥有者改为nfsnobody
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data
[root@nfs01 ~]# ls -ld /data
drwxr-xr-x. 2 nfsnobody nfsnobody 4096 Apr 28 04:32 /data
-----------------------------------------------------------
Q2:如果没有创建目录,客户端会出现如下错误
[root@web01 mnt]# touch {1..5}
touch: cannot touch `1': No such file or directory
touch: cannot touch `2': No such file or directory
touch: cannot touch `3': No such file or directory
touch: cannot touch `4': No such file or directory
touch: cannot touch `5': No such file or directory
Q3:服务端共享目录丢失,客户端卸载会出现如下错误:
[root@web01 ~]# umount /mnt
umount.nfs: /mnt: device is busy
umount.nfs: /mnt: device is busy
Q4:如果没有给nfsnobody权限,会出现如下错误
[root@web01 ~]# ll /mnt
total 0
-rw-r--r-- 1 root root 0 Apr 28 04:50 1
-rw-r--r-- 1 root root 0 Apr 28 04:50 2
[root@web01 ~]# rm -f /mnt/1
rm: cannot remove `/mnt/1': Permission denied
配置共享
[root@nfs01 ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync)
"/etc/exports" 1L, 29C written
[root@nfs01 ~]#
[root@nfs01 ~]# cat /etc/exports
/data 172.16.1.0/24(rw,sync)
#重启NFS服务
[root@nfs01 ~]# /etc/init.d/nfs reload
#查看exports输出列表
[root@nfs01 ~]# showmount -e localhost
Export list for localhost:
/data 172.16.1.0/24
Q5:如果/etc/exports配置有问题,会出现如下错误
[root@nfs01 ~]# /etc/init.d/nfs reload
exportfs: No options for /data 172.16.1.0/24: suggest 172.16.1.0/24(sync) to avoid warning
exportfs: No host name given with /data (rw,sync), suggest *(rw,sync) to avoid warning
注:/etc/exports
文件默认是存在的,但是空的,需要自行设置。
文件格式可以使用man exports
查看说明。
共享目录 | 客户端地址 | 权限 |
---|---|---|
data | 172.16.1.0(24) | rw,sync |
权限说明
- rw:读写
- ro:只读
- sync:同步到磁盘
- async:异步到磁盘
- all_aquash:压缩用户
- anonuid:配置匿名uid
- anongid:配置匿名gid
注:可以通过/var/lib/nfs/etab
查看NFS配置参数
开机自启动
[root@nfs01 ~]# chkconfig rpcbind on
[root@nfs01 ~]# chkconfig nfs on
[root@nfs01 ~]# chkconfig --list|egrep -w "rpcbind|nfs"
nfs 0:off 1:off 2:on 3:on 4:on 5:on 6:off
nfs-rdma 0:off 1:off 2:off 3:off 4:off 5:off 6:off
rpcbind 0:off 1:off 2:on 3:on 4:on 5:on 6:off
注:chkconfig
自动将rpcbind
服务开机顺序设为13,将nfs服务开机顺序设为30.也可以将命令放入到/etc/rc.local
中,但是要启动netfs
服务。linux开机默认是先启动挂载,后启动网卡,但是NFS服务是网络服务,必须先启动网卡才能挂载上去。也可以调整开机顺序,不建议。
小结
先启动RPC服务,再启动NFS服务。
默认配置文件/etc/exports
注意开机启动顺序,使用chkconfig。
客户端
安装软件
[root@web01 ~]# rpm -qa nfs-utils
nfs-utils-1.2.3-64.el6.x86_64
#如果没有,使用yum安装。
yum install nfs-utils -y
rpm -qa nfs-utils
启动服务
#客户端只需要启动rpcbind服务
[root@web01 ~]# /etc/init.d/rpcbind start
[root@web01 ~]# /etc/init.d/rpcbind status
rpcbind (pid 1327) is running...
查看共享资源
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
挂载共享目录
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
#查看挂载信息
[root@web01 ~]# mount
/dev/sda3 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)
/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)
172.16.1.31:/data on /mnt type nfs (rw,vers=4,addr=172.16.1.31,clientaddr=172.16.1.8)
[root@web01 ~]# tail -1 /proc/mounts
172.16.1.31:/data/ /mnt nfs4 rw,relatime,vers=4,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.8,minorversion=0,local_lock=none,addr=172.16.1.31 0 0
注:查看挂载信息最好不要使用df
,有可能会卡住。可以使用mount
或/proc/mounts
测试读写
[root@web01 mnt]# touch {1..5}
[root@web01 mnt]# ll
total 0
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 28 04:32 1
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 28 04:32 2
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 28 04:32 3
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 28 04:32 4
-rw-r--r-- 1 nfsnobody nfsnobody 0 Apr 28 04:32 5
[root@web01 mnt]# echo 4 >4
[root@web01 mnt]# cat 4
4
开机自启动
[root@web01 mnt]# echo "mount -t nfs 172.16.1.31:/data /mnt" >>/etc/rc.local
[root@web01 mnt]# tail -1 /etc/rc.local
mount -t nfs 172.16.1.31:/data /mnt
小结
只要启动RPC服务
开机自启动
NFS服务一般结合inotify或者sersync等实时备份,解决单点问题。
作者:kaliylinux
出处:http://www.cnblogs.com/kaliy
linux点滴:NFS的更多相关文章
- Linux系统NFS网络文件系统
Linux系统NFS网络文件系统 NFS(network file system)网络文件系统,就是通过网络让不同的主机系统之间可以共享文件或目录,此种方法NFS客户端使用挂载的方式让共享文件或目录到 ...
- 实现windows和linux的NFS交互
说明:本文是Omni-NFS-X Windows与Linux间通讯的另一种方式和在windows中配置使用NFS客户端的杂交篇 概述 windows/winnt4.0/win2000与Linux/Fr ...
- Linux服务-NFS
目录 1. nfs简介 1.1 nfs特点 1.2 使用nfs的好处 1.3 nfs的体系组成 1.4 nfs的应用场景 2. nfs工作机制 2.1 RPC 2.2 NIS 2.3 nfs工作机制 ...
- win7下设置挂载Linux服务器nfs共享的数据 -- 转
最近学习NFS文件系统的使用,Ubuntu上配置好了,想和Win7共享数据,所以网上搜到了这篇文章.借花献佛,跟大家共享一下: http://www.2cto.com/os/201207/139132 ...
- linux初学者-NFS网络文件系统篇
linux初学者-NFS网络文件系统篇 在上一篇的SAMBA篇中介绍了linux系统和windows系统之间共用的网络文件系统CIFS,主要用于客户端是windows的情况.在linux系统之间,所用 ...
- linux 提升NFS性能
如何修改同时发起的NFS请求数量 阿里云ecs服务器请查看官方文档 linux上NFS性能只有几MB速度.NFS客户端对于同时发起的NFS请求数量进行了控制,默认编译的内核中此参数值为2,严重影响性能 ...
- Linux IO/NFS tunning 性能优化及检测
Linux IO/NFS tunning:IO Test=======dd 测试读性能的命令# time dd if=/nfsfolder/testfile of=/dev/null bs=1kdd ...
- Linux搭建nfs服务器
使用linux进行嵌入式开发的时候,为了方便开发,通常是将开发板挂载到宿主机的文件系统上,然后将代码放到共享给开发板的目录中,再通过开发板运行. 首先查看是否安装了 nfs 软件包,yum list ...
- Linux 挂载 NFS
NFS(网络文件系统),这是在 Linux 系统上常用的文件共享方式.也可以做为作为一个远程存储使用,比如:我有个网站,用户可以上传文件,但文件慢慢会越来越多,这个时候我们只能把存放上传文件的目录挂在 ...
随机推荐
- 编写程序,查找并删除forward_list<int>中的奇数元素
#include<iostream> #include<forward_list> using namespace std; int main() { forward_list ...
- 如何在Linux桌面环境下自动启动程序?
大多数Linux桌面环境有各自的图形用户界面(GUI),让用户可以配置针对特定用户的自动启动程序或服务.本文将介绍如何在各种Linux桌面环境下,自动启动某个程序的方法. AD:WOT2014:用户标 ...
- java_spring_List,Map,Properties,Set注入与遍历
package com.dao.bean.www; import java.util.List; import java.util.Map; import java.util.Properties; ...
- 【转】在XCode工程中创建bundle包
http://www.giser.net/?p=859 Bundle Programming Guide: https://developer.apple.com/library/ios/docume ...
- Ubuntu10.04下载并编译Android4.3源代码
注:转载或引用请标明出处 http://blog.csdn.net/luzhenrong45/article/details/9719433 去年用Ubuntu10.10成功下载并编译Andro ...
- 网页js生成当前年月日 星期
只需将下面代码插入需要显示日期的地方即可 <div style="color: brown; font-size: 10px;">今天是: <script lan ...
- android客户端从服务器端获取json数据并解析的实现代码
今天总结一下android客户端从服务器端获取json数据的实现代码,需要的朋友可以参考下 首先客户端从服务器端获取json数据 1.利用HttpUrlConnection /** * 从指定的U ...
- IIS7下w3wp.exe进程CPU100%问题解决办法
IIS下经常会出现w3wp.exe进程的CPU使用率达到100%的情况,在IIS7出现之前,要想确定问题所在,可以通过WinDbg来调试分析,但整个过程对技术水平要求非常高,可以参考http:// ...
- Tinymce4 中Ajax多次加载时,会出现菜单在第二次进入时,显示的下拉菜单在左上角
在项目中,ajax多次导入tinymce的js包 参考了: Having issues with Multiple AJAX refresh and TinyMCE load page with ti ...
- SqlServer2005安装错误解决方法
前段时间在安装数据库SQL Server2005时,在系统检测这一步计算机报了一个错误,提示“性能监视器计数要求(错误)”,使得安装无法继续进行,于是在网上找了些方法来解决,其中有一个方法最有效,在这 ...