---
title: "Linux下NFS(网络文件系统)的建立与配置方法"
author: "vinter"
date: "2013-10-17T17:37:13Z"
tags: []
categories: ["操作系统"]
slug: "linux-nfs"
draft: false
lastmod: "2013-10-17T17:37:13Z"
id: 1960069
url: "https://vinter.iteye.com/blog/1960069"
---

网络文件系统（[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

最后执行如下命令启动[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！”