NFS(网络文件系统)
全称 Network File System,网络文件系统
专用于Linux与Linux之间的文件共享服务
NFS服务可以将远程Linux机器上的文件目录数据,通过挂载的形式映射在本地机器
1.特点
类似于Windows网络共享,与samba也有相似。
但samba一般用于局域网。
中小型网站后端一般都是使用NFS.
2.共享存储
软件共享存储——NFS服务搭建
硬件共享存储
- 中小型企业一般不会购买硬件存储设备;大公司需要。
- 但随着业务进一步增长,大多数企业仍会选择使用开源软件搭建集群替代应该,比如NFS集群服务
3.工作原理
通过将远端文件资料目录挂载在本地设备的某一个目录下,就可以通过访问本地目录实现访问远端设备的资料文件。
NFS通过port传输数据。数据传输时,端口随机选择(重启NFS服务, 观察端口变化情况)。
用户如何知道使用哪个端口进行数据传送?
NFS通过RPC服务,进行端口注册。就可以将工作端口告知用户。
RPC:远程过程调用。
服务流程:
1.启动NFS服务与RPC服务,两个服务缺一不可;
2.NFS服务将自己的服务端口注册至RPC;
3.客户端将业务请求发往RPC;
4.RPC返回服务端口;
5.客户端通过提供的端口访问NFS服务器获取服务。
RPC服务默认使用111端口与NFS客户端进行交流。
RPC在centos7平台,实现的软件为RPCBIND服务。先启动RPCBIND,再启动NFS.
如果RPCBIND服务重启,注册信息丢失,必须重启NFS重新注册。
如果修改了NFS配置文件,不需要重启NFS服务,只需要执行如下命令:
exportfs -rv
systemctl reload nfs
4.安装配置
需要安装:
- nfs-utils:nfs服务的主程序,包括rpc.nfsd和rpc.mounted两个守护进程,相关的文件配置信息
- rpcbind:RPC服务进程
安装:
[root@localhost ~]# yum install nfs-utils rpcbind -y
环境配置:
C/S模式,所以要准备一台linux服务端设备,一台linux客户端设备
在NFS服务端创建一个共享文件夹,且设置响应的读写权限
[root@localhost ~]# mkdir /nfsshare [root@localhost ~]# ll -d /nfsshare/ drwxr-xr-x 2 root root 6 Sep 1 19:59 /nfsshare/ [root@localhost ~]# chmod -R 777 /nfsshare/
修改配置文件:
/etc/exports
这个文件默认是空的
配置规则如下:
nfs共享目录 nfs客户端1地址(参数1,参数2······) nfs客户端2地址(参数1,参数2······) nfs共享目录 client1(rw) client2(rw) #允许client1、client2挂载后使用,具有RW权限 nfs共享目录 *(rw) #允许任何主机挂载后使用,具有RW权限 nfs共享目录 IP地址(ro,root_squash) #只允许主机(IP地址)挂载,挂载后只读
- nfs共享目录:是NFS服务端需要共享出去的实际目录,必须写绝对路径;需注意目录本地读写权限,如果想要允许客户端具备读写权限,那么other必须有读写权限
- nfs客户端地址:是NFS服务器授权可以访问共享目录的地址,可以写主机名、通配符、ip地址等
- 权限参数:对NFS客户端进行权限控制的参数
客户端地址的形式:
- 单一客户端 如10.0.0.119 基本很少使用
- 整个局域网网段 10.0.0.0/24 用的比较多
- 授权域名客户端 xxxx.com 用得很少
- 授权子域名客户端 *.xxxx.com 用得很少
权限参数:
ro 只读访问
rw 读写访问
sync 所有数据在请求时写入共享
async NFS在写入数据前可以相应请求
secure NFS通过1024以下的安全TCP/IP端口发送
insecure NFS通过1024以上的端口发送
wdelay 如果多个用户要写入NFS目录,则归组写入(默认)
no_wdelay 如果多个用户要写入NFS目录,则立即写入,当使用async时,无需此设置。
Hide 在NFS共享目录中不共享其子目录
no_hide 共享NFS目录的子目录
subtree_check 如果共享/usr/bin之类的子目录时,强制NFS检查父目录的权限(默认)
no_subtree_check 和上面相对,不检查父目录权限
all_squash 共享文件的UID和GID映射匿名用户anonymous,适合公用目录。
no_all_squash 保留共享文件的UID和GID(默认)
root_squash root用户的所有请求映射成如anonymous用户一样的权限(默认)
5.启动NFS服务端的文件目录共享
5.1保证rpcbind正常运行
[root@localhost ~]# systemctl start rpcbind
[root@localhost ~]# systemctl start nfs
[root@localhost ~]# netstat -tunlp |grep 111
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 8286/rpcbind
tcp6 0 0 :::111 :::* LISTEN 8286/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 8286/rpcbind
udp6 0 0 :::111 :::* 8286/rpcbind
rpcbind即便停了,也有rpcbind.socket在支持运行:
[root@localhost ~]# systemctl stop rpcbind Warning: Stopping rpcbind.service, but it can still be activated by: rpcbind.socket
建议使用如下方法停止:
[root@localhost ~]# systemctl stop rpcbind.socket
重新启动:
[root@localhost ~]# systemctl restart rpcbind
5.2 配置nfs并启动
配置文件:
[root@localhost ~]# mkdir /nfsshare/ [root@localhost ~]# chmod 777 /nfsshare/ #环境准备中已经做过了 [root@localhost ~]# touch /nfsshare/老夏的秘密文档.txt [root@localhost ~]# touch /nfsshare/我没有秘密.txt 考虑到映射的关系,修改一下共享目录的属组属主(放置采用root_squash之后,没有权限访问): [root@localhost ~]# chown -R nfsnobody.nfsnobody /nfsshare/ [root@localhost ~]# ll -d /nfsshare/ drwxrwxrwx 2 nfsnobody nfsnobody 66 Sep 1 22:02 /nfsshare/ 修改服务端配置文件,进行客户端授权: [root@localhost ~]# vim /etc/exports 修改完毕后,内容: [root@localhost ~]# cat /etc/exports /nfsshare *(insecure,rw,sync) #insecure 不限制端口,允许客户端从大于1024的端口上发请求
配置完毕,重新加载nfs,读取文件
[root@localhost ~]# systemctl restart nfs
检查服务端的本地目录共享情况:
[root@localhost ~]# showmount -e 127.0.0.1 Export list for 127.0.0.1: /nfsshare *
检查服务端的共享参数:
[root@localhost ~]# cat /var/lib/nfs/etab /nfsshare *(rw,sync,wdelay,hide,nocrossmnt,insecure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,root_squash,no_all_squash) 对照一下有没有写错
尝试在本地挂载nfs:
找一个目录,比如/mnt,检查其挂载及数据写入情况: [root@localhost ~]# ls /mnt [root@localhost ~]# mount -l |grep mnt [root@localhost ~]# 一切就绪(如果目录有东西需要删掉) 挂载: [root@localhost ~]# mount -t nfs 127.0.0.1:/nfsshare/ /mnt [root@localhost ~]# mount -l |grep mnt 127.0.0.1:/nfsshare on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=127.0.0.1,local_lock=none,addr=127.0.0.1) [root@localhost ~]# ls /mnt 我没有秘密.txt 老夏的秘密文档.txt 这两个文件本来就是在/nfsshare中创建的
取消挂载:
[root@localhost ~]# umount /mnt [root@localhost ~]# ls /mnt [root@localhost ~]#
重新挂载:
[root@localhost ~]# mount -t nfs 127.0.0.1:/nfsshare/ /mnt [root@localhost ~]# ls /mnt 我没有秘密.txt 老夏的秘密文档.txt [root@localhost ~]# touch /mnt/123321.txt [root@localhost ~]# ls /mnt 123321.txt 我没有秘密.txt 老夏的秘密文档.txt [root@localhost ~]# umount /mnt [root@localhost ~]# ls /mnt [root@localhost ~]# ls /nfsshare/ 123321.txt 我没有秘密.txt 老夏的秘密文档.txt
6.远程客户端挂载
客户端安装软件:
[root@admin ~]# yum install nfs-utils rpcbind -y
确保rpcbind正常:
[root@localhost ~]# systemctl start rpcbind [root@localhost ~]# systemctl status rpcbind ● rpcbind.service - RPC bind service Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2022-09-01 21:58:24 CST; 51min ago Process: 8428 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS) Main PID: 8429 (rpcbind) CGroup: /system.slice/rpcbind.service └─8429 /sbin/rpcbind -w Sep 01 21:58:24 localhost.localdomain systemd[1]: Starting RPC bind service... Sep 01 21:58:24 localhost.localdomain systemd[1]: Started RPC bind service.
在客户端确认NFS服务器的状态:
[root@admin ~]# showmount -e 10.0.0.130 Export list for 10.0.0.130: /nfsshare *
进行以nfs协议的挂载使用:
[root@admin ~]# mkdir /nfs_dir [root@admin ~]# mount -t nfs 10.0.0.130:/nfsshare/ /nfs_dir/ [root@admin ~]# mount -l |tail -1 10.0.0.130:/nfsshare on /nfs_dir type nfs4 (rw,relatime,vers=4.1,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.101,local_lock=none,addr=10.0.0.130) [root@admin ~]# ls /nfs_dir/ 123321.txt 我没有秘密.txt 老夏的秘密文档.txt [root@admin nfs_dir]# touch 电影都没有你这叫什么文件共享.txt [root@admin nfs_dir]# ls 123321.txt 我没有秘密.txt 电影都没有你这叫什么文件共享.txt 老夏的秘密文档.txt [root@admin nfs_dir]# [root@admin nfs_dir]# [root@admin nfs_dir]# umount /nfs_dir/ umount.nfs4: /nfs_dir: device is busy 退出目录再取消挂载: [root@admin nfs_dir]# cd [root@admin ~]# umount /nfs_dir/
7.自动挂载服务
配置开机挂载服务,开机就能使用nfs
[root@admin ~]# vim /etc/fstab 最后面写上: 10.0.0.130:/nfsshare /nfs_dir nfs defaults 0 0
autofs自动挂载服务
- 如果在/etc/fstab中配置太多的自动挂载信息,会给本机产生压力
- 如果开机就挂载很多内容,但是又不经常使用,也会给服务器带来很大压力
- 因此一些具有动态特性的文件系统,可以进行动态挂载
- 光盘、U盘、移动硬盘、nfs、samba等即插即用的文件系统
autofs与mount的不同点:
- autofs是一个守护进程,会在后台检测用户是否要访问一个未挂载的文件系统。autofs会自动检测该文件系统是否存在,如果存在则进行挂载。挂载之后,如果长时间不适用,又会自动卸载。
- autofs只有在用户请求时才会挂载,如果是高并发场景,突然产生大量挂载请求,会给服务器造成较大压力;因此高并发场景一般不使用。
安装:
[root@admin ~]# yum install autofs -y
修改配置文件:
[root@admin ~]# vim /etc/auto.master 添加/- /etc/auto.home 定义好配置文件,文件名可以自定义 编写该文件: [root@admin ~]# vim /etc/auto.home 内容: [root@admin ~]# cat /etc/auto.home /nfs_dir -rw,soft,intr 10.0.0.130:/nfssgare soft:指任一方脱机,RPC会周期性呼叫;默认值为hard,指脱机后会持续呼叫 intr:hard模式下,呼叫可以被中断
启动autofs
[root@admin ~]# systemctl start autofs
检查先有的挂载情况:
[root@admin ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 146M 869M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
此时NFS未挂载。
如果我要查看挂载点的内容:
[root@admin ~]# ls /nfs_dir/
123321.txt 我没有秘密.txt 电影都没有你这叫什么文件共享.txt 老夏的秘密文档.txt
[root@admin ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 898M 0 898M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 9.6M 901M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 1014M 146M 869M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
10.0.0.130:/nfsshare 17G 1.4G 16G 8% /nfs_dir
一旦查看,就自动挂载了。