NFS(Network File System)服务配置和使用

时间:2022-12-03 08:16:41

Sun公司开发NFS (Network File System)之初就是为了在不同linux/Unix系统之间共享文件或者文件夹。可以在本地通过网络挂载远程主机的共享文件,和远程主机交互。NFS共享存储对初学者来说不太好理解,我看到过一个很好的例子,假如有三台机器A、B、C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到A、B、C。但是使用NFS只需要放到A上,然后A共享给B和C即可。访问的时候,B和C是通过网络的方式去访问A上的那个目录的。

一、NFS的优势

  • 允许本地获取远程文件
  • NFS使用标准的CS架构在Linux/Unix机器共享文件
  • NFS不要求所有机器都是相同的操作系统。
  • 用NFS可以配置集中存储的解决方案。
  • 用户获取数据是和物理位置无关的。
  • 添加新文件不需要手动刷新。
  • 新版本的NFS也支持acl,pseudo root挂载。
  • 可通过防火墙和认证来加强安全防护。

二、NFS配置相关的文件

  • /etc/exports:NFS服务核心配置文件,NFS服务器端所有共享的文件目录都在该文件中定义。
  • /etc/fstab:要在系统中挂载一个NFS目录,重启后生效,就在/etc/fstab中增加相应配置。
  • /etc/sysconfig/nfs:NFS配置文件,用来控制rpc或者其他服务正在监听哪个端口。

三、配置和使用NFS

1、环境

Linux环境配置NFS服务至少需要2台linux机子,并且保证能ping通。

NFS Server IP :10.1.101.188

NFS Client IP : 10.1.101.189

2、安装NFS Server和NFS Client

在NFS Server和NFS Client两台机子都需要装NFS包。(Red Hat Linux 用“yum”)Debian 和Ubuntu环境用"apt-get"。【以server端为例】

# apt-get install nfs-common nfs-kernel-server

NFS Server和NFS Client两台机子都启动nfs服务。

要启动portmap和nfs两个服务,并且portmap服务一定要先于nfs启动。【以server端为例】

root@nfsserver:~# /etc/init.d/portmap start
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service portmap start Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start portmap
root@nfsserver:~# /etc/init.d/nfs-kernel-server start
* Exporting directories for NFS kernel daemon... [ OK ]
* Starting NFS kernel daemon

3、NFS服务器端配置

要共享一个目录,首先要在/etc/exports中加入。我们在根目录下新建一个目录/nfsshare,共享给客户端。

然后重启服务使配置生效(或者使用命令#exportfs -rv)。

root@nfsserver:~# mkdir /nfsshare
root@nfsserver:~# cat /etc/exports
# /etc/exports: the access control list for filesystems which may be exported
#               to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
/nfsshare 10.1.101.189(rw,sync,no_root_squash)
root@nfsserver:~# /etc/init.d/nfs-kernel-server restart
/nfsshare 10.1.101.189(rw,sync,no_root_squash)这句话意思是根分区下的/nfsshare目录被共享给IP“10.1.101.189”,并且有readwrite(rw)权限,这里也可以用主机名(hostname)来代替IP。

exports文件中客户端主机地址

"客户端主机地址"字段可以使用多种形式表示主机地址

10.1.101.189:指定IP地址的主机

nfsclient.test.com:指定域名的主机

10.1.101.0/24:指定网段中的所有主机

*.test.com:指定域下的所有主机

*:所有主机

exports文件中配置选项

exports文件中的“配置选项”放在括号中,选项之间逗号分隔。

  • ro:该选项表示read only,客户端只允许读共享文件。
  • rw:该选项表示输出的共享目录可读(read)写(wirte),客户端有读文件的权限也有写文件的权限。
  • sync:将数据同步写入缓冲区与磁盘中,效率低,但可以保证数据的一致性,推荐所有NFS共享目录都使用该选项。
  • async:将数据先保存在内存缓冲区中,必要时才写入磁盘。
  • wdelay(默认):检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率。
  • no_wdelay:若有写操作则立即执行,应与sync配合使用。
  • all_squash:所有访问用户都映射为匿名用户或用户组。
  • no_all_squash(默认):将访问用户先与本机用户匹配,匹配失败后再映射为匿名用户或用户组。
  • subtree_check(默认):若输出目录是一个子目录,则nfs服务器将检查其父目录的权限。
  • no_subtree_check:表示禁用子树检查。如果禁用子树检查会提高效率,但会降低其安全性。
  • root_squash(默认):来访的root用户映射为匿名用户或用户组。
  • no_root_squash:来访的root用户保持root账户权限,就像对本地的目录操作一样,不安全,不建议使用。
  • anonuid=<UID>:指定匿名访问用户的本地用户UID,默认为nfsnobody(65534)。
  • anongid=<GID>:指定匿名访问用户的本地用户组GID,默认为nfsnobody(65534)。
  • secure(默认):限制客户端只能从小于1024的tcp/ip端口连接服务器。
  • insecure:允许客户端大于1024的tcp/ip端口连接服务器。

4、NFS客户端配置

服务器端配置好后,在客户端挂载共享目录或分区。

第一步:查看NFS Server的共享文件

root@nfsclient:~# showmount -e 10.1.101.188
Export list for 10.1.101.188:
/nfsshare 10.1.101.189

第二步:新建挂载点

root@nfsclient:~# mkdir -p /mnt/nfsshare

第三步:挂载,注意权限。

root@nfsclient:~# mount -t nfs 10.1.101.188:/nfsshare /mnt/nfsshare
mount.nfs: access denied by server while mounting 10.1.101.188:/nfsshare
root@nfsserver:/# chmod 777 -R /nfsshare/
root@nfsserver:/# /etc/init.d/nfs-kernel-server restart
* Stopping NFS kernel daemon [ OK ]
* Unexporting directories for NFS kernel daemon... [ OK ]
* Exporting directories for NFS kernel daemon... exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "10.1.101.189:/nfsshare".
Assuming default behaviour ('no_subtree_check').
NOTE: this default has changed since nfs-utils version 1.0.x [ OK ]
* Starting NFS kernel daemon
root@nfsclient:~# mount -t nfs 10.1.101.188:/nfsshare /mnt/nfsshare  

第四步,检查挂载是否成功:

root@nfsclient:~# mount |grep nfs
10.1.101.188:/nfsshare on /mnt/nfsshare type nfs (rw,vers=4,addr=10.1.101.188,clientaddr=10.1.101.189)

以上挂载只是暂时的,机子重启后就没有了。要永久挂载,可在"/etc/fsab"中配置。

即在/etc/fstab中加入一行:

10.1.101.188:/nfsshare /mnt/nfsshare nfs defaults 0 0

root@nfsclient:~# cat /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
proc /proc proc nodev,noexec,nosuid 0 0
# / was on /dev/xvda1 during installation
UUID=0c681b37-97ed-4d10-bd79-8d5931c443f8 / ext4 errors=remount-ro 0 1
# swap was on /dev/xvda5 during installation
UUID=9e2efc1b-ef13-4b7c-b616-34d2a62f04ea none swap sw 0 0
10.1.101.188:/nfsshare /mnt/nfsshare nfs defaults 0 0

然后执行命令:mount -a

root@nfsclient:~# mount -a
root@nfsclient:~# mount |grep nfs
10.1.101.188:/nfsshare on /mnt/nfsshare type nfs (rw,vers=4,addr=10.1.101.188,clientaddr=10.1.101.189)

5、测试NFS配置

在NFS Server端新建一个测试文件,检查在NFS Client端是否能获取到,反之亦然。

第一步,在NFS Server的共享目录中新建文件"nfsTestServer.txt"。

root@nfsserver:/nfsshare# cat nfsTestServer.txt
This is a test file to test the working of NFS server setup.
This file is created at nfs server end.

第二步,在NFS Client端无需刷新就可以看到“nfsTestServer.txt”文件。

root@nfsclient:/mnt/nfsshare# ls
nfstest.txt
root@nfsclient:/mnt/nfsshare# cat nfsTestServer.txt
This is a test file to test the working of NFS server setup.
This file is created at nfs server end.

第三步,在NFS Client端,新建一个测试文件"nfsTestClient.txt"。

root@nfsclient:/mnt/nfsshare# cat nfsTestClient.txt
This is a test file to test the working of NFS server setup.
This file is created at nfs client end.

第四步,在NFSServer端无需刷新就可以看到“nfsTestClient.txt”文件。

root@nfsserver:/nfsshare# cat nfsTestClient.txt
This is a test file to test the working of NFS server setup.
This file is created at nfs client end.

6、卸载

文件共享完后执行umount命令卸载。

root@nfsclient:~# df -h -F nfs
Filesystem              Size  Used Avail Use% Mounted on
10.1.101.188:/nfsshare   19G  1.7G   17G  10% /mnt/nfsshare
root@nfsclient:~# umount /mnt/nfsshare
root@nfsclient:~# df -h -F nfs
df: no file systems processed

7、卸载排错

root@nfsclient:/mnt/nfsshare# umount /mnt/nfsshare
umount.nfs: /mnt/nfsshare: device is busy

首先:注意不要在当前目录去执行umount,否则会报错。

root@nfsclient:~# umount /mnt/nfsshare
umount.nfs: /mnt/nfsshare: device is busy

如果退出该目录还是不行,则判断是有一个进程在用该目录,找出。

root@nfsclient:~# fuser -m /mnt/nfsshare
/mnt/nfsshare: 923c

找到使用该目录的进程:

root@nfsclient:~# ps aux |grep 923
root 923 0.0 0.3 21452 4036 pts/0 Ss+ 10:12 0:00 -bash
root 1323 0.0 0.0 8104 924 pts/1 S+ 11:22 0:00 grep --color=auto 923

杀死进程

root@nfsclient:~# kill -9 923

然后就可以卸载了。

root@nfsclient:~# umount /mnt/nfsshare

四、NFS命令

showmount

  • showmount -e <server-ip or hostname>:显示主机NFS服务器输出列表。-e或--exports
  • showmount -d <server-ip or hostname> :显示被客户机挂载的目录。-d 或--directories
  • showmount -a <server-ip or hostname>:列出nfs服务器的所有客户端主机及所连接的目录,即挂载点

exportfs管理工具可以对“exports”文件进行管理

  • exportfs -v:输出主机共享的文件和选项到屏幕:
root@nfsserver:~# exportfs -v
/nfsshare 10.1.101.188(rw,wdelay,no_root_squash,no_subtree_check)
  • exportfs -a:全部挂载或卸载/etc/exports中的内容,输出/etc/exports文件中设置的所有共享文件
root@nfsserver:~# exportfs -a
exportfs: /etc/exports [1]: Neither 'subtree_check' or 'no_subtree_check' specified for export "10.1.101.189:/nfsshare".
Assuming default behaviour ('no_subtree_check').
NOTE: this default has changed since nfs-utils version 1.0.x
  • exportfs -u:卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
  • exportfs -r:重新读取/etc/exports文件的设置,并使设置立即生效,而不重启服务。并同步更新/var/lib/nfs/xtab
  • exportfs -rv:重新输出共享目录。

设置自动启动nfs服务

chkconfig --level 35 portmap on

chkconfig --level 35 nfs on

rpcinfo:查看rpc服务注册情况

-p:显示所有的端口和程序信息:

root@nfsserver:~# rpcinfo -p 10.1.101.188
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
100024 1 udp 44370 status
100024 1 tcp 35677 status
100003 2 tcp 2049 nfs
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 2 tcp 2049
100227 3 tcp 2049
100003 2 udp 2049 nfs
100003 3 udp 2049 nfs
100003 4 udp 2049 nfs
100227 2 udp 2049
100227 3 udp 2049
100021 1 udp 40301 nlockmgr
100021 3 udp 40301 nlockmgr
100021 4 udp 40301 nlockmgr
100021 1 tcp 39133 nlockmgr
100021 3 tcp 39133 nlockmgr
100021 4 tcp 39133 nlockmgr
100005 1 udp 46249 mountd
100005 1 tcp 40795 mountd
100005 2 udp 42966 mountd
100005 2 tcp 41292 mountd
100005 3 udp 33508 mountd
100005 3 tcp 54892 mountd

推荐资源链接

https://help.ubuntu.com/community/SettingUpNFSHowTo

NFS(Network File System)服务配置和使用的更多相关文章

  1. 【Azure 存储服务】如何把开启NFS 3&period;0协议的Azure Blob挂载在Linux VM中呢&quest;&lpar;NFS&colon; Network File System 网络文件系统&rpar;

    问题描述 如何把开启NFS协议的Azure Blob挂载到Linux虚拟机中呢? [答案]:可以使用 NFS 3.0 协议从基于 Linux 的 Azure 虚拟机 (VM) 或在本地运行的 Linu ...

  2. NFS - Network File System网络文件系统

    NFS(Network File System/网络文件系统): 设置Linux系统之间的文件共享(Linux与Windows中间文件共享采用SAMBA服务): NFS只是一种文件系统,本身没有传输功 ...

  3. CentOS7 配置NFS&lpar;Network File System&rpar;及其使用

    1.       服务端配置 1.1.    安装NFS yum -y install nfs* 1.2.    查看是否安装了NFS与RPCBIND rpm -qa | grep nfs rpm - ...

  4. Centos7——NFS(Network File System)服务

    NFS(Network File System)即网络文件系统,允许计算机之间通过网络共享资源:在NFS客户端即可NFS服务端所共享的目录挂载到本地,此时即可像读写本地目录一样读写远程计算机的目录与文 ...

  5. CentOS7&period;5搭建NFS(Network File System)

    NFS(Network File System)即网络文件系统,是由Sun公司开发的一种通过网络方式共享文件系统的通用共享解决方案.可以将远程Linux系统上的文件共享资源挂载到本地主机(Linux客 ...

  6. Design and Implementation of the Sun Network File System

    Introduction The network file system(NFS) is a client/service application that provides shared file ...

  7. Network File System

    Network File System 2014-12-31 #system 接着上一篇博客Distributed Systems 分布式系统来扯淡,之前的博客一再在写文件系统,这次继续,只不过是分布 ...

  8. nfs 是Network File System 网络文件系统

    NFS的基本原刚是容许不同的客户端及服务通过一组PRC分享相同的文件系统,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享.NFS在文件传送过程中依赖于RPC协议.远程过程调用Rem ...

  9. nfs&lpar;Network FileSystem&rpar;的简单配置

    如果想要在window和linux之间共享文件,那么用samba.如果仅仅想在Unix like系统之间共享文件,那么可以用NFS. 这篇文章分为两个部分,第一部分只是简单的用:照着写的步骤一步步来就 ...

随机推荐

  1. C&plus;&plus;代码重构——从C global到C&plus;&plus; template

    在学数据结构的时候,我常有这样目标--写出能够最大程度复用的代码(算法正确,封装优秀).我常想--如何能在短时间内达成"算法正确,封装优秀"这样的目标.经过一段时间的摸索,我的结论 ...

  2. 使用BigDecimal进行精确运算

    首先我们先来看如下代码示例: 1 public class Test_1 { 2 public static void main(String[] args) { 3 System.out.print ...

  3. &lbrack;php入门&rsqb; 3、WAMP中的集成MySQL相关基础操作

    前言:本文以小白视角了解WAMP集成开发环境中的MYSQL,涉及的面广而浅,算是导读性质. 1.启动运行熟悉WAMP中的MySQL 先有库.再有表.数据最终以记录的形式插入表中.其中对数据进行操作使用 ...

  4. MySQL学习笔记&lowbar;2&lowbar;MySQL创建数据表(上)

    MySQL创建数据表(上) 一.创建数据表的SQL语句模型[弱类型] CREATETABLE [IF NOT EXISTS] 表名称( 字段名1列的类型[属性][索引], 字段名2 列的类型[属性][ ...

  5. lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为一行

    如何联合索引查询? 所以给定查询过滤条件 age=18 的过程就是先从term index找到18在term dictionary的大概位置,然后再从term dictionary里精确地找到18这个 ...

  6. linux下神奇的script命令

    script 是一个神奇命令,script 能够将终端的会话过程录制下来,然后使用 scriptreplay 就可以将其录制的结果播放给他人观看.script 的好处就在于你在终端中的所有操作.敲过的 ...

  7. 【cogs858】磁性链

    [题目描述] 有N块编号为1~N的特殊磁石相互吸附组成一条磁性链,只有它们紧挨着时才会传递吸力,他们之间的吸力很大,如果我们要从N块相连的磁石中取出一块,那么需要消耗N-1个单位的能量,空缺处不再有吸 ...

  8. (十)unity4&period;6学习Ugui中文文档-------參考-UGUI Canvas Components

     大家好,我是孙广东.   转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unit ...

  9. Python学习之--异常处理

    Python中的Exceptions是所有异常的基类,内置的异常类都放在了exceptions模块中,通过dir()函数可以看到这些内置的类 通过raise 语句触发异常,如 >>> ...

  10. Python中wx&period;FlexGridSizer

    FlexGridSizer是GridSizer的一个更灵活的版本.它与标准的GridSizer几乎相同,除了下面3点例外: 1.每行和每列可以有各自的尺寸.2.默认情况下,当尺寸调整时,它行和列整体改 ...