NFS是Network File System的缩写;这个文件系统是基于网路层面,经过网络层面实现数据同步php
NFS最先由Sun公司开发,分2,3,4三个版本,2和3由Sun起草开发,4.0开始Netapp公司参与并主导开发,目前最新为4.1版本——>4.1版本是2010年出来还没更新过python
NFS数据传输基于RPC协议,RPC为Remote Procedure Call的简写,意思为 远程过程调用mysql
例子:linux
服务端和客户端通讯,A机器和B机器之间不能直接通讯,须要借助RPC协议来实现nginx
NFS应用场景是:A,B,C三台机器上须要保证被访问到的文件是同样的,A共享数据出来,B和C分别去挂载A共享的数据目录,从而B和C访问到的数据和A上的一致sql
NFS架构vim
/home/nfstestdir 192.168.133.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)centos
1.首先准备两台机器,我这里准备两台虚拟机,A机器IP分别为192.168.5.130,B机器IP为192.168.5.133缓存
A机器,查看IP:192.168.5.130服务器
[root@xuexi-001 ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.5.130 netmask 255.255.255.0 broadcast 192.168.5.255 inet6 fe80::9625:3e1d:12c7:4fe6 prefixlen 64 scopeid 0x2
B机器,查看IP: 192.168.5.133
[root@localhost ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.5.133 netmask 255.255.255.0 broadcast 192.168.5.255 inet6 fe80::cf14:5db2:1b49:f0c2 prefixlen 64 scopeid 0x2
2.将A机器做为服务端,并安装两个包,分别为 nfs-utils 和 rpcbind
[root@xuexi-001 ~]# yum install -y nfs-utils rpcbind
3.将B机器做为客户端,安装 nfs-utils 包
[root@localhost ~]# yum install -y nfs-utils
4.在安装完成后,须要去A机器中 /etc/exports 编辑配置文件
vim /etc/exports //加入以下内容
[root@xuexi-001 ~]# vi /etc/exports /home/nfstestdir 要分享出去的目录是哪个目录,这个目录是不存在的,后期还须要建立的 192.168.5.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) 指定你要给哪一个机器去分享这个目录,首先定义IP或IP段 保存退出
5.下面就能够启动服务了,启动服务前须要先建立目录,并设置权限
6.在A机器上建立分享的目录
[root@xuexi-001 ~]# mkdir /home/nfstestdir
7.并设置成777的权限,这里设置777权限是为了方便接下里的实验
[root@xuexi-001 ~]# chmod 777 /home/nfstestdir
8.启动 rpcbind ,再启动前查看A机器上监听的端口,就会看到启动了1/systemd,这是centos7系统的一个特性——>111端口是rpcbind服务监听的
查看A机器:
用ps能够查看服务已经启动,由于有这个服务,因此监听了111端口
[root@xuexi-001 ~]# 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:111 0.0.0.0:* LISTEN 2605/rpcbind tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1038/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 911/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1262/master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 1038/nginx: master tcp6 0 0 :::111 :::* LISTEN 2605/rpcbind tcp6 0 0 :::22 :::* LISTEN 911/sshd tcp6 0 0 ::1:25 :::* LISTEN 1262/master tcp6 0 0 :::3306 :::* LISTEN 1223/mysqld
9.再到B机器上查看下端口,会看到也启动了rpcbind
[root@localhost ~]# 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:111 0.0.0.0:* LISTEN 2577/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 852/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1029/master tcp6 0 0 :::111 :::* LISTEN 2577/rpcbind tcp6 0 0 :::22 :::* LISTEN 852/sshd tcp6 0 0 ::1:25 :::* LISTEN 1029/master
注:若是rpcbind没有启动的话可使用 systemctl start rpcbind启动服务
10.在A机器启动了rpcbind,下面就能够启动nfs服务,在使用ps查看
[root@xuexi-001 ~]# systemctl start nfs [root@xuexi-001 ~]# ps aux | grep nfs root 2713 0.0 0.0 0 0 ? S< 00:10 0:00 [nfsd4_callbacks] root 2719 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2720 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2721 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2722 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2723 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2724 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2725 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2726 0.0 0.0 0 0 ? S 00:10 0:00 [nfsd] root 2730 0.0 0.0 112720 968 pts/0 R+ 00:11 0:00 grep --color=auto nfs
11.同时也能够在A机器上ps aux |grep rpc查询,在启动nfs服务的时候,它会自动帮你启动rpc相关的一些服务
[root@xuexi-001 ~]# ps aux | grep rpc rpc 2605 0.0 0.0 65000 1416 ? Ss 00:01 0:00 /sbin/rpcbind -w rpcuser 2683 0.0 0.0 42420 1756 ? Ss 00:10 0:00 /usr/sbin/rpc.statd root 2685 0.0 0.0 0 0 ? S< 00:10 0:00 [rpciod] root 2695 0.0 0.0 43860 540 ? Ss 00:10 0:00 /usr/sbin/rpc.idmapd root 2703 0.0 0.0 42608 944 ? Ss 00:10 0:00 /usr/sbin/rpc.mountd root 2732 0.0 0.0 112720 968 pts/0 R+ 00:12 0:00 grep --color=auto rpc
12.而在客户端B机器上是没有的这些服务的
[root@localhost ~]# ps aux |grep rpc rpc 2577 0.0 0.1 64956 1048 ? Ss 00:01 0:00 /sbin/rpcbind -w root 2585 0.0 0.0 112676 980 pts/0 R+ 00:13 0:00 grep --color=auto
13.若是想让nfs开机启动,还须要执行systemctl enable nfs 命令,在服务端A机器上调用
[root@xuexi-001 ~]# systemctl enable nfs Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
1.首先在客户端B机器上安装了rpcbind,它是由nfs-utils 这个包自动安装的
yum install -y nfs-utils
2.在B机器上安装完成后不须要启动任何服务
3.在B机器上执行 showmount -e 命令
[root@localhost ~]# showmount -e 192.168.5.130 clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)
4.这时候会看到报错了,RPC: Port mapper failure说明网络不通,没法与192.168.5.130 端口通讯
5.由于以前在服务端已经开启了rpcbind服务,那这时只能说明是防火墙致使的,因此要想让NFS实现正常的通讯,还须要把防火墙关闭,由于NFS服务比较特殊,虽然rpc能够通讯,就算iptables把111端口放行但NFS也不必定能正常通讯,由于它们使用了一个不固定的端口
6.这时先把A机器服务端和B机器客户端,防火墙关闭,命令systemctl stop firewalld
A机器关闭防火墙 [root@xuexi-001 ~]# systemctl stop firewalld [root@xuexi-001 ~]# getenforce Disabled B机器关闭防火墙 [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 [root@localhost ~]# getenforce Permissive
7.这时再去B机器客户端来showmount -e 192.168.5.130 ,这时会看到能够能show到远程服务端192.168.5.130 这台机器,它共享的目录是/home/nfstestdir
[root@localhost ~]# showmount -e 192.168.5.130 Export list for 192.168.5.130: /home/nfstestdir 192.168.5.0/24
8.这时候在B机器客户端上来挂载了,而后查看——>挂载的时间可能有点慢
[root@localhost ~]# mount -t nfs 192.168.5.130:/home/nfstestdir /mnt/ [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 992M 17G 6% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 97M 100M 50% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.5.130:/home/nfstestdir 18G 8.4G 9.5G 47% /mnt
9.这个就是远程NFS服务端共享的目录
10.这时到B机器客户端共享的目录下建立文件
[root@localhost ~]# cd /mnt/ [root@localhost mnt]# ls [root@localhost mnt]# touch 111.txt [root@localhost mnt]# ls -l 总用量 0 -rw-r--r--. 1 1000 1000 0 8月 4 16:08 111.txt
11.到A机器的服务端查看,能够看到文件的属主和属组都为1000
[root@xuexi-001 ~]# ls -l /home/nfstestdir/ 总用量 0 -rw-r--r-- 1 user1 user1 0 8月 4 16:08 111.txt [root@xuexi-001 ~]# id user1 uid=1000(user1) gid=1000(user1) 组=1000(user1)
在/etc/exports文件中的/home/nfstestdir
192.168.5.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) ,其中ip与(rw之间不能有空格, 不然客户端挂载的目录会变成只读..
经常使用选项
/tmp/ 192.168.133.0/24(rw,sync,no_root_squash)
1.在B机器客户端,卸载目录
[root@localhost ~]# umount /mnt/ [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 992M 17G 6% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 97M 100M 50% /boot tmpfs 98M 0 98M 0% /run/user/0
2.而后在A机器服务端,使用exportfs -arv命令
exportfs -arv命令,从新使配置文件生效
[root@xuexi-001 ~]# exportfs -arv exporting 192.168.5.0/24:/home/nfstestdir
3.验证,在A机器上的/etc/exports配置文件中,在增长一行,把 /tmp 目录单独共享给192.168.5.133 这个IP
[root@xuexi-001 ~]# vi /etc/exports /home/nfstestdir 192.168.5.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000) /tmp 192.168.5.133(rw,sync,no_root_squash) 保存退出
4.而后在A机器服务端执行exportfs -arv命令
[root@xuexi-001 ~]# exportfs -arv exporting 192.168.5.133:/tmp exporting 192.168.5.0/24:/home/nfstestdir
5.在B机器客户端showmount -e看是否生效——>并无重启nfs服务,就已经生效
[root@localhost ~]# showmount -e 192.168.5.130 Export list for 192.168.5.130: /home/nfstestdir 192.168.5.0/24 /tmp 192.168.5.133
6.在B机器客户端挂载,将 tmp目录 挂载到 mnt 目录下
[root@localhost ~]# mount -t nfs 192.168.5.130:/tmp/ /mnt/ [root@localhost ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 992M 17G 6% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 97M 100M 50% /boot tmpfs 98M 0 98M 0% /run/user/0 192.168.5.130:/tmp 18G 8.4G 9.5G 47% /mnt
7.如今在B机器客户端,查看到的/mnt/目录就是192.168.5.130IP下的tmp目录
[root@localhost ~]# ls /mnt/ aming.sock systemd-private-cca061734a284ab68452996f54b9a3e7-chronyd.service-UKFBkE mysql.bak systemd-private-cca061734a284ab68452996f54b9a3e7-vgauthd.service-fXSgVu mysql.sock systemd-private-cca061734a284ab68452996f54b9a3e7-vmtoolsd.service-zsXw2n php-fcgi.sock user.bak
8.再到A机器上查看tmp目录,会看到两个目录下的文件内容会同样的
[root@xuexi-001 ~]# ls /tmp/ aming.sock systemd-private-cca061734a284ab68452996f54b9a3e7-chronyd.service-UKFBkE mysql.bak systemd-private-cca061734a284ab68452996f54b9a3e7-vgauthd.service-fXSgVu mysql.sock systemd-private-cca061734a284ab68452996f54b9a3e7-vmtoolsd.service-zsXw2n php-fcgi.sock user.bak
9.这时在B机器客户端的mnt目录下建立文件,并查看目录下的文件,能看到新建的1212.txt文件的属主和属组都是root
[root@localhost ~]# cd /mnt/ [root@localhost mnt]# touch 1212.txt [root@localhost mnt]# ls -l /mnt/ 总用量 652 -rw-r--r--. 1 root root 0 8月 4 16:38 1212.txt srw-rw-rw-. 1 root root 0 8月 4 15:44 aming.sock -rw-r--r--. 1 root root 657618 7月 25 22:07 mysql.bak srwxrwxrwx. 1 1003 1003 0 8月 4 15:44 mysql.sock srw-rw-rw-. 1 root root 0 8月 4 15:44 php-fcgi.sock drwx------. 3 root root 17 8月 4 15:43 systemd-private-cca061734a284ab68452996f54b9a3e7-chronyd.service-UKFBkE drwx------. 3 root root 17 8月 4 15:43 systemd-private-cca061734a284ab68452996f54b9a3e7-vgauthd.service-fXSgVu drwx------. 3 root root 17 8月 4 15:43 systemd-private-cca061734a284ab68452996f54b9a3e7-vmtoolsd.service-zsXw2n -rw-r--r--. 1 root root 7229 7月 25 22:19 user.bak
10.这时再到A机器服务端查看tmp目录,会看到1212.txt文件的属主和属组也是root
[root@xuexi-001 ~]# ls -l /tmp/ 总用量 652 -rw-r--r-- 1 root root 0 8月 4 16:38 1212.txt srw-rw-rw- 1 root root 0 8月 4 15:44 aming.sock -rw-r--r-- 1 root root 657618 7月 25 22:07 mysql.bak srwxrwxrwx 1 mysql mysql 0 8月 4 15:44 mysql.sock srw-rw-rw- 1 root root 0 8月 4 15:44 php-fcgi.sock drwx------ 3 root root 17 8月 4 15:43 systemd-private-cca061734a284ab68452996f54b9a3e7-chronyd.service-UKFBkE drwx------ 3 root root 17 8月 4 15:43 systemd-private-cca061734a284ab68452996f54b9a3e7-vgauthd.service-fXSgVu drwx------ 3 root root 17 8月 4 15:43 systemd-private-cca061734a284ab68452996f54b9a3e7-vmtoolsd.service-zsXw2n -rw-r--r-- 1 root root 7229 7月 25 22:19 user.bak
11.这就是由于在A机器服务端的配置文件中,使用了no_root_squash ,因此root用户不受约束,在B机器上到挂载点下,到共享目录下,就能够像在本地磁盘使用root用户同样,是不受限制的(一般状况下,不限制root用户的比较多)