一、NFS服务介绍

NFS是 Network File system的缩写

NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。

分为2.3.4三个版本,2和3由sun公司起草开发,4.0开始netapp公司参与并主导开发

NFS数据传输基于RPC协议:

应用场景:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别取挂载A共享的数据目录,从而B和C访问到的数据和A上的一致。

NFS原理图:(NFS服务不监听任何端口,但是RPC服务中的模块,rpcbind默认监听111端口,)

NFS服务端安装配置

1.首先需要两台机器,一个为服务端(ip10.21.95.122)一个为客户端(ip10.21.95.58)。

2.服务端安装两个包

[root@bogon ~]# yum install -y nfs-utils rpcbind

3.客户端安装包

[root@sunyujun02 ~]# yum install -y nfs-utils

4.接下来在服务端编辑:

[root@fuwuduan ~]# vim /etc/exports
[root@fuwuduan ~]#
/home/nfstestdir 10.21.95.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
# 第一段来定义共享目录的绝对路径  第二段指定ip和一些选项
# rw 读写
# ro 只读
# sync 同步模式,内存数据实时写入磁盘
# async 非同步模式
# no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大
# root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
# all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
# anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

5.保存配置文件以后,因为共享目录不存在,所以做以下操作。  

[root@fuwuduan ~]# mkdir /home/nfstestdir
[root@fuwuduan ~]# chmod 777 /home/nfstestdir/

6.查看rpcbind所用的111端口已经启动

[root@fuwuduan ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0: 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 75879/nginx: worker
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 4161/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1475/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1477/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2778/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 75879/nginx: worker
tcp6 0 0 :::3306 :::* LISTEN 3849/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1475/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1477/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 2778/master
[root@fuwuduan ~]#

7.客户端和服务端都启动rpcbind

[root@fuwuduan ~]# systemctl start rpcbind
[root@fuwuduan ~]# ps aux|grep rpc
root 752 0.0 0.0 0 0 ? S< 2月19 0:00 [rpciod]
rpc 37786 0.6 0.1 64952 1040 ? Ss 17:20 0:00 /sbin/rpcbind -w
root 37788 0.0 0.0 112684 976 pts/2 S+ 17:20 0:00 grep --color=auto rpc
[root@fuwuduan ~]#
[root@sunyujun02 ~]# systemctl start rpcbind
[root@sunyujun02 ~]# ps aux|grep rpc
root 818 0.0 0.0 0 0 ? S< 2月20 0:00 [rpciod]
rpc 47335 0.2 0.1 64936 1052 ? Ss 17:20 0:00 /sbin/rpcbind -w
root 47337 0.0 0.0 112668 976 pts/1 R+ 17:20 0:00 grep --color=auto rpc
[root@sunyujun02 ~]#

8.服务端启动nfs

[root@fuwuduan ~]# systemctl start nfs
[root@fuwuduan ~]# ps aux|grep nfs
root 37863 0.0 0.0 0 0 ? S< 17:22 0:00 [nfsd4]
root 37864 0.0 0.0 0 0 ? S< 17:22 0:00 [nfsd4_callbacks]
root 37872 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37873 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37874 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37875 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37876 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37877 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37878 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37879 0.0 0.0 0 0 ? S 17:22 0:00 [nfsd]
root 37895 0.0 0.0 112680 972 pts/2 R+ 17:22 0:00 grep --color=auto nfs
[root@fuwuduan ~]#

9.启动nfs后发现rpc.statd也自动启动了

[root@fuwuduan ~]# ps aux|grep rpc
root 752 0.0 0.0 0 0 ? S< 2月19 0:00 [rpciod]
rpc 37786 0.0 0.1 64952 1412 ? Ss 17:20 0:00 /sbin/rpcbind -w
rpcuser 37852 0.0 0.1 44464 1828 ? Ss 17:22 0:00 /usr/sbin/rpc.statd
root 37853 0.0 0.0 42560 948 ? Ss 17:22 0:00 /usr/sbin/rpc.mountd
root 37854 0.0 0.0 21384 416 ? Ss 17:22 0:00 /usr/sbin/rpc.idmapd
root 37939 0.0 0.0 112684 972 pts/2 R+ 17:23 0:00 grep --color=auto rpc
[root@fuwuduan ~]#

10.设置开机启动

[root@fuwuduan ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[root@fuwuduan ~]#

11.客户端挂载

12.客户端测试提示错误

[root@sunyujun02 ~]# showmount -e 10.21.95.122

  

clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
我们发现报错,这是由于网络不通造成,是由防火墙导致我,所以我们把客户端和服务端的防火墙和selinux关闭

13.关掉防火墙(最好把服务端和客户端防火墙都关掉)

[root@fuwuduan ~]# systemctl stop firewalld
[root@fuwuduan ~]# setenforce 0

14.再次测试成功

[root@sunyujun02 ~]# showmount -e 10.21.95.122
Export list for 10.21.95.122:
/home/nfstestdir 10.21.95.0/24
[root@sunyujun02 ~]#

15.客户端挂载远程nfs到mnt目录下

[root@sunyujun02 ~]# mount -t nfs 10.21.95.122:/home/nfstestdir /mnt/
[root@sunyujun02 ~]#

16.df 查看下

[root@sunyujun02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 18G 7.9G 9.6G 46% /
devtmpfs 474M 0 474M 0% /dev
tmpfs 489M 144K 489M 1% /dev/shm
tmpfs 489M 14M 476M 3% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 497M 157M 341M 32% /boot
tmpfs 98M 24K 98M 1% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /run/media/root/CentOS 7 x86_64
10.21.95.122:/home/nfstestdir 18G 13G 5.0G 72% /mnt
[root@sunyujun02 ~]#

17.在客户端新建一个文件试试看是否可以同步到服务端

[root@sunyujun02 ~]# cd /mnt/
[root@sunyujun02 mnt]# ls
[root@sunyujun02 mnt]# touch linux.111
[root@sunyujun02 mnt]# ls -l
总用量 0
-rw-r--r-- 1 sunyujun sunyujun 0 2月 23 11:36 linux.111
[root@sunyujun02 mnt]#

18.服务端成功同步,太神奇了

[root@fuwuduan ~]# cd /home/nfstestdir/
[root@fuwuduan nfstestdir]# ls
linux.111
[root@fuwuduan nfstestdir]#

19.因为配置文件中配置的是uid  gid 1000所以文件的所属组、属主也是1000用户sunyujun

[root@fuwuduan nfstestdir]# id sunyujun
uid=1000(sunyujun) gid=1000(sunyujun) 组=1000(sunyujun)
[root@fuwuduan nfstestdir]#
[root@sunyujun02 mnt]# id sunyujun
uid=1000(sunyujun) gid=1000(sunyujun) 组=1000(sunyujun)
[root@sunyujun02 mnt]#

exportfs命令

1. nfs进程不能随便从新启动,如果不得已要重启,必须把客户端的挂载目录卸载,然后在从新启动。这时候就可以用exportfs命令

[root@sunyujun02 mnt]# cd
[root@sunyujun02 ~]# umount /mnt/
[root@sunyujun02 ~]#

2.编辑配置文件,添加tmp目录

[root@fuwuduan nfstestdir]# exportfs -u
/home/nfstestdir
10.21.95.0/24
[root@fuwuduan nfstestdir]# vim /etc/exports
[root@fuwuduan nfstestdir]#

  

/home/nfstestdir 10.21.95.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
/tmp/ 10.21.95.58/24(rw,sync,no_root_squash)

3.执行命令

[root@fuwuduan nfstestdir]# exportfs -arv
exporting 10.21.95.58/24:/tmp
exporting 10.21.95.0/24:/home/nfstestdir
[root@fuwuduan nfstestdir]#

4.客户端执行showmount发现也生效了

[root@sunyujun02 ~]# !showm
showmount -e 10.21.95.122
Export list for 10.21.95.122:
/tmp 10.21.95.58/24
/home/nfstestdir 10.21.95.0/24
[root@sunyujun02 ~]#

5.客户端重新挂载/tmp/

[root@sunyujun02 ~]# mount -t nfs 10.21.95.122:/tmp/ /mnt/
[root@sunyujun02 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 18G 7.9G 9.6G 46% /
devtmpfs 474M 0 474M 0% /dev
tmpfs 489M 144K 489M 1% /dev/shm
tmpfs 489M 14M 476M 3% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 497M 157M 341M 32% /boot
tmpfs 98M 24K 98M 1% /run/user/0
/dev/sr0 4.1G 4.1G 0 100% /run/media/root/CentOS 7 x86_64
10.21.95.122:/tmp 18G 13G 5.0G 72% /mnt
[root@sunyujun02 ~]# ls /mnt/
linux.sock
mysql2.sql
mysql_all.sql
mysqlbak.sql
mysql.sock
php-fcgi.sock
ssh-VkkK9OKJsW89
systemd-private-b091a55929414513a9b7db2f688afef6-colord.service-B5wa5w
systemd-private-b091a55929414513a9b7db2f688afef6-cups.service-RpVATe
systemd-private-b091a55929414513a9b7db2f688afef6-rtkit-daemon.service-gDlUSL
systemd-private-b091a55929414513a9b7db2f688afef6-vmtoolsd.service-TLnArm
test.com.log
test.com.log-20180213
test.com.log-20180222
user.sql
yum_save_tx.2018-02-14.10-18.boB57M.yumtx
yum_save_tx.2018-02-14.10-33.ffqojN.yumtx
[root@sunyujun02 ~]#

6.服务端和客户端同时有了tmp目录

[root@fuwuduan nfstestdir]# ls /tmp/
linux.sock
mysql2.sql
mysql_all.sql
mysqlbak.sql
mysql.sock
php-fcgi.sock
ssh-VkkK9OKJsW89
systemd-private-b091a55929414513a9b7db2f688afef6-colord.service-B5wa5w
systemd-private-b091a55929414513a9b7db2f688afef6-cups.service-RpVATe
systemd-private-b091a55929414513a9b7db2f688afef6-rtkit-daemon.service-gDlUSL
systemd-private-b091a55929414513a9b7db2f688afef6-vmtoolsd.service-TLnArm
test.com.log
test.com.log-20180213
test.com.log-20180222
user.sql
yum_save_tx.2018-02-14.10-18.boB57M.yumtx
yum_save_tx.2018-02-14.10-33.ffqojN.yumtx
[root@fuwuduan nfstestdir]#

7.在客户端新建一个文件1111.txt

[root@sunyujun02 ~]# vim /mnt/1111.txt
[root@sunyujun02 ~]# ls /mnt/
1111.txt
linux.sock
mysql2.sql
mysql_all.sql
mysqlbak.sql
mysql.sock
php-fcgi.sock
ssh-VkkK9OKJsW89
systemd-private-b091a55929414513a9b7db2f688afef6-colord.service-B5wa5w
systemd-private-b091a55929414513a9b7db2f688afef6-cups.service-RpVATe
systemd-private-b091a55929414513a9b7db2f688afef6-rtkit-daemon.service-gDlUSL
systemd-private-b091a55929414513a9b7db2f688afef6-vmtoolsd.service-TLnArm
test.com.log
test.com.log-20180213
test.com.log-20180222
user.sql
yum_save_tx.2018-02-14.10-18.boB57M.yumtx
yum_save_tx.2018-02-14.10-33.ffqojN.yumtx
[root@sunyujun02 ~]#

8.查看服务端同样生成了1111.txt文件并且文件的属组属主都是root就是因为设置了no_root_squash

 

[root@fuwuduan nfstestdir]# ls /tmp/
1111.txt
linux.sock
mysql2.sql
mysql_all.sql
mysqlbak.sql
mysql.sock
php-fcgi.sock
ssh-VkkK9OKJsW89
systemd-private-b091a55929414513a9b7db2f688afef6-colord.service-B5wa5w
systemd-private-b091a55929414513a9b7db2f688afef6-cups.service-RpVATe
systemd-private-b091a55929414513a9b7db2f688afef6-rtkit-daemon.service-gDlUSL
systemd-private-b091a55929414513a9b7db2f688afef6-vmtoolsd.service-TLnArm
test.com.log
test.com.log-20180213
test.com.log-20180222
user.sql
yum_save_tx.2018-02-14.10-18.boB57M.yumtx
yum_save_tx.2018-02-14.10-33.ffqojN.yumtx
[root@fuwuduan nfstestdir]#

NFS客户端问题  

在centos6中遇到的比较多,NFS4版本可能会有该问题

客户端挂载共享目录后,不管是root还是普通用户,创建新文件时所属主,属组为nobody。

解决方法:

第一种:

客户端挂在时加上-o nfsvers=3         (指定NFS版本为3)

[root@sunyujun02 ~]# mount -t nfs -o nfsvers=3 10.21.95.122:/tmp/ /mnt/
[root@sunyujun02 ~]# mount -t nfs -oremount,nfsvers=3 10.21.95.122:/tmp/ /mnt/
[root@sunyujun02 ~]#

第二种:

客户端和服务端都要做:

vim /etc/idmapd.conf //

把“#Domain = local.domain.edu” 改为 “Domain = xxx.com” (这里的xxx.com,随意定义吧),然后再重启rpcidmapd服务

NFS介绍的更多相关文章

  1. NFS介绍 NFS服务端安装配置 NFS配置选项

    NFS 介绍 • NFS是Network File System的缩写 • NFS最早由Sun公司开发,分2,,4三个版本,2和3由Sun起草开发,.0开始Netapp公司参与并主导开发,最新为4.1 ...

  2. NFS介绍、服务端安装配置、NFS配置选项

    6月21日任务 14.1 NFS介绍14.2 NFS服务端安装配置14.3 NFS配置选项 14.1 NFS介绍 14.2 NFS服务端安装配置 1.首先需要2台机器,一台是服务端,一台是客户端,分别 ...

  3. Linux centosVMware NFS介绍、NFS服务端安装配置、NFS配置选项

    一.NFS介绍 NFS是Network File System的缩写 NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版 ...

  4. NFS介绍和安装

    NFS简单介绍 NFS 是Network File System的缩写,即网络文件系统. 一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外发布.功能是通过网络让不同的机器.不同的操作 ...

  5. NFS介绍与搭建

    一.NFS的介绍 1.1.什么是NFS NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布. NFS在文件传 ...

  6. Linux NFS 介绍

    由SUN开发,用于在不同的系统之间使用,所以其设计与主机系统无关,用户想使用远程文件时,只需要mount –t nfs就可以把远程的文件拿到本地,像使用本地文件一样使用,非常方便. 它采用C/S架构, ...

  7. NFS、FTP介绍

    第二十五课 NFS.FTP介绍 目录 一. NFS介绍 二.NFS服务端安装配置 三.NFS配置选项 四.exportfs命令 五.NFS客户端问题 六.FTP介绍 七.使用vsftpd搭建ftp 八 ...

  8. NFS文件共享系统

    1.NFS介绍 NFS是Network File System的缩写,主要功能是通过网络让不同的机器系统之间可以彼此共享文件或目录.NFS服务器可以允许NFS客户端将远端NFS服务端的共享目录挂载到本 ...

  9. NFS存储服务部署

    第1章 NFS介绍 1.1 NFS服务内容的概述 □ RPC服务知识概念介绍说明,以及RPC服务存在价值(必须理解掌握) □ NFS服务工作原理讲解(必须理解掌握) □ NFS共享文件系统使用原理讲解 ...

随机推荐

  1. List the Books

    描述 Jim is fond of reading books, and he has so many books that sometimes it's hard for him to manage ...

  2. Eclipse错误导致无法启动The workspace exited with unsaved changes in the previous session

    MyOpenSUSE:/home/jin/workspace # tail -f .metadata/.log !SESSION 2014-05-04 11:35:58.869 ----------- ...

  3. HDU 3473 Minimum Sum(划分树)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. Excel 电子表格文件格式剖析

    Excel 电子表格文件格式,这种Excel和其他的Excel是不同的.他的本质上是一个Xml文件(用英文版的UtraEdit可以看到),所以他可以保存任何符号的字符,包括&(它在Xml文件中 ...

  5. 用Hexo搭建属于自己的iOS技术博客,搬家了

    搬家了,本来还打算在博客园混一段时间的,可是当我看到Hexo的时候,已经难以抵挡它的诱惑,简单不简约的界面让我花了整整一天的时间,买域名的过程中发生一点小问题导致DNS解析错误了,但还是成功了.欢迎朋 ...

  6. 轻量级web服务器lighttpd的编译及配置(for x86-linux)

    转自:http://blog.163.com/ljf_gzhu/blog/static/131553440201211522317367/ 备注: PC Linux:Ubuntu-10.10 Linu ...

  7. Unity3d通用工具类之解压缩文件

    今天,我们来写写c#是如何通过代码解压缩文件的. 在游戏的项目中呢,常常我们需要运用到解压缩的技术.比如,当游戏需要更新的时候,我们会从服务器中下载更新的压缩文件包. 这时候我们就需要解压文件,然后覆 ...

  8. 二十四种设计模式:模板方法模式(Template Method Pattern)

    模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...

  9. 谈谈Ext JS的组件——布局的用法续一

    盒子布局 盒子布局主要作用是以水平(Ext.layout.container.HBox)或垂直方式(Ext.layout.container.VBox)来划分容器区域.这也是比較常有的布局方式. 使用 ...

  10. [转]Getting started with SSIS - Part 10: Event Handling and Logging

    本文转自:http://beyondrelational.com/modules/12/tutorials/24/tutorials/9686/getting-started-with-ssis-pa ...