14.1 NFS介绍

  • NFS是Network File System的缩写
  • NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本
  • NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。
  • NFS应用场景是:A,B,C三台机器上需要保证被访问到的文件是一样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致

NFS架构

NFS原理图

14.2 NFS服务端安装配置

  • 准备

需要两台虚拟机,一个作为服务端(192.168.1.15/24),一个作为客户端(192.168.1.16/24)。

  • 下载安装
#客户端和服务端都要安装
[root@taoyun ~]# yum install -y nfs-utils rpcbind
#下载两个包 nfs-utils && rpcbind //rpcbind可以不加,nfs-utils默认安装
  • 编辑配置文件
#编辑exports文件
[root@taoyun ~]# vim /etc/exports

#添加一行如下内容
/home/nfstestdir 192.168.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
#指定共享的机器ip地址段
#:wq 保存退出
  • 创建目录及配置权限
#创分享出去的目录
[root@taoyun ~]# mkdir /home/nfstestdir

#设置权限
[root@taoyun ~]# chmod 777 /home/nfstestdir/
  • 启动服务
#查看端口
[root@taoyun ~]# netstat -lnpt
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:111             0.0.0.0:*               LISTEN      1/systemd           
#监听111 端口
#可以确定服务启动了

#启动nfs服务
[root@taoyun ~]# systemctl start nfs
[root@taoyun ~]# ps aux | grep nfs
root       1552  0.0  0.0      0     0 ?        S<   17:05   0:00 [nfsd4_callbacks]
root       1558  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1559  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1560  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1561  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1562  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1563  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1564  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1565  0.0  0.0      0     0 ?        S    17:05   0:00 [nfsd]
root       1569  0.0  0.0 112676   980 pts/0    S+   17:05   0:00 grep --color=auto nfs

[root@taoyun ~]# ps aux | grep rpc
root        503  0.0  0.0      0     0 ?        S<   16:51   0:00 [rpciod]
rpcuser    1538  0.0  0.0  42376  1752 ?        Ss   17:04   0:00 /usr/sbin/rpc.statd
rpc        1539  0.0  0.0  64956  1352 ?        Ss   17:04   0:00 /sbin/rpcbind -w
root       1540  0.0  0.0  42564   944 ?        Ss   17:04   0:00 /usr/sbin/rpc.mountd
root       1541  0.0  0.0  43844   644 ?        Ss   17:05   0:00 /usr/sbin/rpc.idmapd
root       1571  0.0  0.0 112680   980 pts/0    S+   17:06   0:00 grep --color=auto rpc
#rpc是nfs关联服务

#设置开机启动
[root@taoyun ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

14.3 NFS配置选项

[root@taoyun ~]# cat /etc/exports
/home/nfstestdir 192.168.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

含义: rw: 读写 ro: 只读 sync: 同步模式,内存数据实时写入磁盘 async :非同步模式 no_root_squash: 客户端挂载NFS共享目录后,root用户不受约束,权限很大 root_squash: 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户 all_squash: 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户 anonuid/anongid: 和上面几个选项搭配使用,定义被限定用户的uid和gid

客户端挂载

  • 安装 nfs-utils
  • 设置IP为NFS服务端ip
[root@taoyun ~]# showmount -e 192.168.1.15
Export list for 192.168.1.15:
/home/nfstestdir 192.168.1.0/24
#查看是否有权限
#需要关闭防火墙 两边都关闭 
#systemctl stop firewalld
#getenforce
  • 挂载
[root@taoyun ~]# mount -t nfs 192.168.1.15:/home/nfstestdir /mnt/
[root@taoyun ~]# df -h 
文件系统                       容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root         17G  1.4G   16G    8% /
devtmpfs                       478M     0  478M    0% /dev
tmpfs                          489M     0  489M    0% /dev/shm
tmpfs                          489M  6.7M  482M    2% /run
tmpfs                          489M     0  489M    0% /sys/fs/cgroup
/dev/sda1                     1014M  125M  890M   13% /boot
tmpfs                           98M     0   98M    0% /run/user/0
192.168.1.15:/home/nfstestdir   17G  6.8G   11G   40% /mnt
#最后一行为远程服务端
  • 测试
#客户端操作
[root@taoyun ~]# cd /mnt/
[root@taoyun mnt]# ls
[root@taoyun mnt]# touch taoyuam.111
[root@taoyun mnt]# ls -l
总用量 0
-rw-r--r--. 1 user user 0 1月  16 17:31 taoyuam.111

#服务端操作
[root@taoyun ~]# ls -l /home/nfstestdir/
总用量 0
-rw-r--r-- 1 user user 0 1月  16 17:31 taoyuam.111
#不管用哪个用户操作,将以1000uid,1000gid 操作

[root@taoyun ~]# id user
uid=1000(user) gid=1000(user) 组=1000(user)