14.1 NFS介绍

1. NFS是Network File System的缩写

2. NFS最早由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,最新为4.1版本

3. NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写。

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

14.2 NFS服务端安装配置

准备两个centos 7版本服务器:hao1服务端  hao2客户端

1. 128服务端安装

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

2. 编辑添加内容 

[root@aminglinux-128 ~]# vim /etc/exports

添加内容 

[ 共享目录 ip段.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) ]

/home/nfstestdir 192.168.193.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

3. 创建共享目录 

mkdir /home/nfstestdir

4. 共享目录设置777权限 

[root@aminglinux-128 ~]# chmod 777 /home/nfstestdir

5. 启动rpcbind服务 :

[root@aminglinux-128 ~]# systemctl start rpcbind

6. 搜索rpcbind是否启动 

[root@aminglinux-128 ~]# ps aux |grep rpcbind
rpc         566  0.0  0.0  69220   732 ?        Ss   04:32   0:00 /sbin/rpcbind -w
root       9248  0.0  0.0 112724   984 pts/0    R+   16:16   0:00 grep --color=auto rpcbind

7. 查看rpcbind监听端口(显示systemd也是正常的) :

[root@aminglinux-128 ~]# 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      566/rpcbind         
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1688/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      994/sshd            
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      993/cupsd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1352/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1278/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      566/rpcbind         
tcp6       0      0 :::22                   :::*                    LISTEN      994/sshd            
tcp6       0      0 ::1:631                 :::*                    LISTEN      993/cupsd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1352/master

8. 启动nfs 

[root@aminglinux-128 ~]#  systemctl start nfs

9. 搜索nfs是否启动 

[root@aminglinux-128 ~]# ps aux |grep nfs
root       9360  0.0  0.0      0     0 ?        S<   16:25   0:00 [nfsd4_callbacks]
root       9366  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9367  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9368  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9369  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9370  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9371  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9372  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9373  0.0  0.0      0     0 ?        S    16:25   0:00 [nfsd]
root       9404  0.0  0.0 112720   984 pts/0    R+   16:28   0:00 grep --color=auto nfs

10. 设置nfs开机启动 

[root@aminglinux-128 ~]#  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配置选项

NFS配置选项:

rw 读写

ro 只读

sync 同步模式,内存数据实时写入磁盘

async 非同步模式

no_root_squash 客户端挂载NFS共享目录后,root用户不受约束,权限很大

root_squash 与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户

all_squash 客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户

anonuid/anongid 和上面几个选项搭配使用,定义被限定用户的uid和gid

1. host客户端上安装:

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

2. 关闭128服务端防火墙

[root@aminglinux-128 ~]# systemctl stop firewalld
[root@aminglinux-128 ~]# getenforce
Disabled
[root@aminglinux-128 ~]#  setenforce 0
setenforce: SELinux is disabled

3. 关闭local客户端防火墙

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# getenforce
Disabled
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled

4. 访问128服务端ip,查看服务端共享目录ip段

[root@localhost ~]# showmount -e 192.168.193.128
Export list for 192.168.193.128:
/home/nfstestdir 192.168.193.0/24

5. local客户端 nfs挂载

mount -t nfs 服务端ip:共享目录 挂载点

[root@localhost ~]# mount -t nfs 192.168.193.128:/home/nfstestdir /mnt

6. 查看挂载

[root@localhost ~]# df -h
文件系统                          容量  已用  可用 已用% 挂载点
/dev/sda3                          76G  7.7G   69G   11% /
devtmpfs                          472M     0  472M    0% /dev
tmpfs                             488M     0  488M    0% /dev/shm
tmpfs                             488M  8.1M  480M    2% /run
tmpfs                             488M     0  488M    0% /sys/fs/cgroup
/dev/sda1                         197M  111M   86M   57% /boot
tmpfs                              98M     0   98M    0% /run/user/0
192.168.193.128:/home/nfstestdir   76G  7.8G   68G   11% /mnt

7. 挂载点mnt下,创建一个文件:

[root@localhost ~]# touch /mnt/hao.txt

8.localhost客户端查看挂载点hao.txt文件 属主 属组

[root@localhost ~]# ls -l /mnt/hao.txt
-rw-r--r-- 1 mysql mysql 0 8月  24 16:49 /mnt/hao.txt

9. localhost客户端查看mysql的id:

[root@localhost ~]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

10. 128服务端查看共享目录下的hao.txt文件 属主 属组

[root@aminglinux-128 ~]# ls -l /home/nfstestdir/hao.txt
-rw-r--r-- 1 mysql mysql 0 8月  24 16:49 /home/nfstestdir/hao.txt

11. 128服务端查看mysql的id:

[root@aminglinux-128 ~]#  id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

12. 因为/etc/exports里设置了anonuid=1000,anongid=1000

  所以属主属组就显示的机器对应的1000id用户