---
title: "Linux下NFS(网络文件系统)的建立与配置方法"
author: "dxm1986"
date: "2011-03-08T13:13:29Z"
tags: []
categories: ["操作系统"]
slug: "linux-nfs"
draft: false
lastmod: "2013-04-22T14:35:20Z"
id: 946194
url: "https://wolfdream.iteye.com/blog/946194"
---

# Linux下NFS(网络文件系统)的建立与配置方法

 

    ZDNetChina服务器站 操作系统技巧  网络文件系统（[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")，Network File System）是一种将远程主机上的分区（目录）经网络挂载到本地系统的一种机制，通过对网络文件系统的支持，用户可以在本地系统上像操作本地分区一样来对远程主机的共享分区（目录）进行操作。

    在[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 的开发过程中，开发者需要在[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器上进行所有的软件开发，交叉编译后，通用FTP 方式将可执行文件下载到[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")系统运行，但这种方式不但效率低下，且无法实现在线的调试。因此，可以通过建立[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")，把[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器上的特定分区共享到待调试的[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统上，就可以直接在[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统上操作[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器，同时可以在线对程序进行调试和修改，大大的方便了软件的开发。因此，[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 的是[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 开发的一个重要的组成部分，本部分内容将详细说明如何配置[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 的[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 开发环境。

    [嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 的[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 开发环境的实现包括两个方面：一是[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器端的[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务器支持；二是[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统的[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 客户端的支持。因此，[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 开发环境的建立需要配置[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器端和[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统端。

**一、[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器端[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务器的配置**

    以root 身份登陆[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器，编辑/etc 目录下的共享目录配置文件exports，指定共享目录及权限等。

    执行如下命令编辑文件/etc/exports：

   # vi /etc/exports

   在该文件里添加如下内容：

   /home/work 192.168.0.\*（rw,sync,no\_root\_squash）

   然后保存退出。

   添加的内容表示：允许ip 地址范围在192.168.0.\*的计算机以读写的权限来访问/home/work 目录。

   /home/work 也称为服务器输出共享目录。

   括号内的参数意义描述如下：

   rw：读/写权限，只读权限的参数为ro；

   sync：数据同步写入内存和硬盘，也可以使用async，此时数据会先暂存于内存中，而不立即写入硬盘。   
   
   no\_root\_squash：[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务器共享目录用户的属性，如果用户是 root，那么对于这个共享目录来说就具有 root 的权限。

   接着执行如下命令，启动端口映射：

   # /etc/rc.d/init.d/portmap start   || /etc/rc.d/init.d/rpcbind --help  新版本linux系统中

```
注意：如果没有安装过nfs 则执行如下命令：

yum install portmap
yum install nfs-utils

```

 

 

   最后执行如下命令启动[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务，此时[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 会激活守护进程，然后就开始监听 Client 端的请求：

   # /etc/rc.d/init.d/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") start

   用户也可以重新启动[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器，自动启动[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务。

   在[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务器启动后，还需要检查[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器的防火墙等设置（一般需要关闭防火墙服务），确保没有屏蔽掉[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 使用的端口和允许通信的主机，主要是检查[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器iptables，ipchains 等选项的设置，以及/etc/hosts.deny，/etc/hosts.allow 文件。

   我们首先在[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器上进行[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 服务器的回环测试，验证共享目录是否能够被访问。在[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器上运行如下命令：   
   
   # mount –t [NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 192.168.0.20:/home/work /mnt

   # ls /mnt

   命令将[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器的[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 输出共享目录挂载到/mnt 目录下，因此，如果[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 正常工作，应该能够在/mnt 目录看到/home/work 共享目录中的内容。

**二、[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 客户端的配置**

   在[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器设置好后，还需要对客户端进行相关配置。在配置内核时选择Load an Alternate Configuration File输入配置文件的路径和文件名添加内核对[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")的支持：

   选中networking options－》IP:kernel level auloconfiguralion项

   选中file systems－》network file systems－》下的root file system on [NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 和[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") file system support重新编译内核下载bootloader和kernel到开发板上

    在[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统的[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") Shell 下，执行如下命令来进行[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 共享目录挂载：

    # mkdir /mnt/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") //建立[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器输出共享目录的挂载点；

    # mount –t [NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 192.168.0.20:/home/work /mnt/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") –o nolock

    # cd /mnt/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")   
    
    # ls

    此时，[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统端所显示的内容即为[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器的输出目录的内容，即[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器的输出目。

    录/home/work 通过[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 映射到了[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标系统的/mnt/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 目录。用户可以用增/删/修改文件的方式来验证实际效果。mount 命令中的192.168.0.20 为[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器的IP 地址，/home/work 为[Linux](http://server.zdnet.com.cn/files/list-0-0-56718-1-1.htm "Linux") 服务器端所配置的共享输出目录，/mnt/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 为[嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")设备上的本地目录。

    在开发过程中，来回输入命令非常烦人，我写了两个简单的脚本来完成[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")的启动，挂载。

    host启动[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")：  
    s[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")  
    #!/bin/bash  
    ifconfig eth0 192.168.0.20  
    /etc/rc.d/init.d/portmap start  
    /etc/rc.d/init.d/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") start  
    [嵌入式](http://server.zdnet.com.cn/files/list-0-0-50808-1-1.htm "嵌入式")目标机挂载[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")：  
    m[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS")：  
   
    #!/bin/sh  
    mount -t [NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") 192.168.0.20:/home/work/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") /mnt/[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") -o nolock  
    echo “[NFS](http://server.zdnet.com.cn/files/list-0-0-4993-1-1.htm "NFS") ok！”

 

摘自：[http://server.zdnet.com.cn/server/2007/0831/482007.shtml](http://server.zdnet.com.cn/server/2007/0831/482007.shtml)

 

 

 

 

 

 

**Redhat Linux NFS配置**

## NFS简介：

NFS是网络文件系统的简写（network file system），主要用在linux或unix环境中，是有sun公司开发，并于1984年推出的一个RPC服务系统。

#### NFS功能介绍：

他可以将多个目录或者单个目录进行发布，例如我们在网刻系统的时候可以用NFS来共享光盘镜像，NFS是以发布目录的方式将文件进行发布，而客户端是以挂载的方式进行访问。客户端可以节省本地空间，将数据存放在nfs服务器上。NFS也可以针对但个用户以及多用户设置不同的访问权限。

#### 详细配置步骤：

安装NFS必须要开启的服务有：nfs、portmap、

NFS的配置相对于其他服务是比较简单的，我们依然还是要注意防火墙以及SElinux，

**首先我们检查软件是否已安装，这里我已经都安装了。**

![](http://img1.51cto.com/attachment/200906/200906201245481273359.bmp)

在安装redhat linux 时这些包默认就已被安装，如果你检查发现没有安装，可以使用一下命令进行安装；

#rpm –ivh nfs*

#rpm –ivh portmap   

**配置NFS，配置文件在/etc/exports**

首先我们使用vim打开/etc/exports 我们会发现这是一个空文件，里面没有任何内容。如果没有此文件，我们可以新创建一个，

这里我们举个例子来完成对NFS的配置，

例如：公司有多台计算机，而其中只有一台服务器有光驱，而我们其他的计算机也想安装光盘上的软件，现在我们就可以用NFS来实现。例如我们的服务器光驱挂载在/media上，接下来我们来编辑配置文件来实现以上的功能。

依然使用vi打开配置文件编辑以下内容：

![](http://img1.51cto.com/attachment/200906/200906201245483162078.bmp)

然后保存退出，这个文件里我们只需编辑俩个字段：前面的为共享目录，后面的为哪些人可以访问以及访问权限，\*代表所有人（ro）为只读权限。

**启动服务**

![](http://img1.51cto.com/attachment/200906/200906201245483260546.bmp)

然后我们可以使用exportfs命令查询输出的目录，

![](http://img1.51cto.com/attachment/200906/200906201245483574468.bmp)

**客户端挂载**

使用mount命令挂载，例如我/下有nfs目录，我将挂载到nfs目录上

![](http://img1.51cto.com/attachment/200906/200906201245483838515.bmp)

然后我们就可以访问光盘目录了，

反挂载使用umount /nfs

我们还可以使用shoumount --export 192.168.0.7   查看NFS所发布的目录

 

如果您想共享其他的目录也是安装同样的方法，**注意权限问题，目录权限和共享权限。**

一些相关共享权限：

ro 客户端为只读权限

rw 客户端为读写权限

root\_sqush 客户端使用root访问时映射为nobady (默认选项)

no\_root\_squash 客户端映射为root访问