---
title: "ubuntu安装NFS及测试"
author: "duguyiren3476"
date: "2015-05-08T09:37:17Z"
tags: []
categories: ["操作系统"]
slug: "ubuntu-nfs"
draft: false
lastmod: "2015-05-08T09:39:08Z"
id: 2209331
url: "https://duguyiren3476.iteye.com/blog/2209331"
---

# ubuntu安装NFS及测试

## 测试环境

- ubuntu 14.04 单机

### 安装nfs-kernel-server

```xml
apt-get install nfs-kernel-server
```

### 创建共享目录

```shell
mkdir /opt/nfs #建立一个nfs服务的专有的文件夹;
```

### 修改 exports

```shell
root@localhost:~# vim /etc/exports #在文件末尾添加如下一行
/opt/nfs *(rw,sync,no_root_squash)
```

> 说明：   
> \*：允许所有的网段访问，也可以使用具体的IP   
> rw：挂接此目录的客户端对该共享目录具有读写权限   
> sync：资料同步写入内存和硬盘   
> no\_root\_squash：root用户具有对根目录的完全管理访问权限。   
> no\_subtree\_check：不检查父目录的权限。

### 重启rpcbind 服务

```shell
root@localhost:/opt/nfs# /etc/init.d/rpcbind restart
```

> nfs是一个RPC程序，使用它前，需要映射好端口，通过rpcbind 设定,此处直接restart或者start即可

### 重启nfs服务

```shell
root@localhost:/opt/nfs#/etc/init.d/nfs-kernel-server restart
```

### 创建目标目录

```shell
root@localhost:/opt/nfs# mkdir -p /tmp/nfs
```

> 创建在根目录下的tmp下

### 挂载

```shell
root@localhost:/opt/nfs# mount -t nfs localhost:/opt/nfs /tmp/nfs
```

> 将共项目录/opt/nfs挂载到 /tmp/nfs目录下，复制一个文件到/opt/nfs目录下，然后从/tmp/nfs目录下面即可看到/opt/nfs下的文件 表示挂载成功

### 查看本机挂载状况

```shell
root@localhost:/opt/nfs# showmount -e localhost
```

### 开机自动挂载

把 上述指令 sudo mount -t nfs localhost:/opt/nfs /tmp/nfs 写到 /etc/rc.local 文件中即可，因为/tmp/下的文件将会在主机重启时清空目录，所以请自行选择替他目录作为挂载点即可

> NFS常用参数如下：   
> ro 只读访问   
> rw 读写访问sync 所有数据在请求时写入共享   
> async nfs在写入数据前可以响应请求   
> secure nfs通过1024以下的安全TCP/IP端口发送   
> insecure nfs通过1024以上的端口发送   
> wdelay 如果多个用户要写入nfs目录，则归组写入(默认)   
> no\_wdelay 如果多个用户要写入nfs目录，则立即写入，当使用async时，无需此设置。   
> hide 在nfs共享目录中不共享其子目录   
> no\_hide 共享nfs目录的子目录   
> subtree\_check 如果共享/usr/bin之类的子目录时，强制nfs检查父目录的权限(默认)   
> no\_subtree\_check 和上面相对，不检查父目录权限   
> all\_squash 共享文件的UID和GID映射匿名用户anonymous，适合公用目录。   
> no\_all\_squash 保留共享文件的UID和GID(默认)   
> root\_squash root用户的所有请求映射成如anonymous用户一样的权限(默认)   
> no\_root\_squas root用户具有根目录的完全管理访问权限   
> anonuid=xxx 指定nfs服务器/etc/passwd文件中匿名用户的UID   
> anongid=xxx 指定nfs服务器/etc/passwd文件中匿名用户的GID