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原理图
2 nfs服务端安装配置
下载nfs相应的包:
[root@linux01 ~]# yum install -y nfs-utils rpcbind
编辑exports配置文件:
[root@linux01 ~]# vim /etc/exports //加入如下内容
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)
保存配置文件后,执行如下准备操作
创建目录并设置权限:
[root@linux01 ~]# mkdir /home/nfstestdir
[root@linux01 ~]# chmod 777 /home/nfstestdir
启动相关服务:
[root@linux01 ~]# systemctl start rpcbind
[root@linux01 ~]# systemctl start nfs
将相关服务添加到开机启动:
[root@linux01 ~]# systemctl enable rpcbind
Created symlink from /etc/systemd/system/multi-user.target.wants/rpcbind.service to /usr/lib/systemd/system/rpcbind.service.
[root@linux01 ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
客户端配置:
安装nfs相应的包:
[root@linux02 ~]# yum install -y nfs-utils
查看挂载目录
[root@linux02 ~]# showmount -e 192.168.139.111 //该ip为NFS服务端ip,查看挂载目录
Export list for 192.168.139.111:
/home/nfstestdir 192.168.139.0/24
挂载共享目录:
[root@linux02 ~]# mount -t nfs 192.168.139.111:/home/nfstestdir /mnt
查看挂载目录:
[root@linux02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.2G 18G 7% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 492M 0 492M 0% /dev/shm
tmpfs 492M 7.5M 485M 2% /run
tmpfs 492M 0 492M 0% /sys/fs/cgroup
/dev/sda1 197M 112M 85M 57% /boot
tmpfs 99M 0 99M 0% /run/user/0
192.168.139.111:/home/nfstestdir 19G 2.7G 17G 15% /mnt
创建测试文件:
[root@linux02 ~]# ls -l /mnt/
total 0
-rw-r--r-- 1 user1 user1 0 Nov 25 17:48 hello.txt
-rw-r--r-- 1 user1 user1 0 Dec 2 21:38 test.txt
服务端查看:
[root@linux01 ~]# ls -l /home/nfstestdir/
total 0
-rw-r--r-- 1 mysql mysql 0 Nov 25 17:48 hello.txt
-rw-r--r-- 1 mysql mysql 0 Dec 2 21:38 test.txt
[root@linux02 ~]# ls -l /mnt/test.txt //可以看到文件的属主和属组都为1000,因为配置文件里限定了uid和gid为1000
3 nfs配置选项
rw
读写
ro
只读
sync
同步模式,内存数据实时写入磁盘
async
非同步模式
no_root_squash
客户端挂载NFS共享目录后,root用户不受约束,权限很大
root_squash
与上面选项相对,客户端上的root用户收到约束,被限定成某个普通用户
all_squash
客户端上所有用户在使用NFS共享目录时都被限定为一个普通用户
anonuid/anongid
和上面几个选项搭配使用,定义被限定用户的uid
和gid