NFS工作原理
NFS:Network File System 网络文件系统,基于内核的文件系统。Sun 公司开发,通过使用 NFS,用 户和程序可以像访问本地文件一样访问远端系统上的文件,基于RPC(Remote Procedure Call Protocol 远程过程调用)实现 RPC采用C/S模式,客户机请求程序调用进程发送一个有进程参数的调用信息到服务进程,然后等待应 答信息。在服务器端,进程保持睡眠状态直到调用信息到达为止。当一个调用信息到达,服务器获得进 程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息, 获得进程结果,然后调用执行继续进行
NFS优势:节省本地存储空间,将常用的数据,如:/home目录,存放在NFS服务器上且可以通过网络访 问,本地终端将可减少自身存储空间的使用
安装nfs软件包
yum -y install nfs-utils #安装
systemctl enable nfs-server --now #设置开机自启以及开启服务
root@sre ~]# rpcinfo -p #查看端口
program vers proto port service
100000 4 tcp 111 portmapper
100000 3 tcp 111 portmapper
100000 2 tcp 111 portmapper
100000 4 udp 111 portmapper
100000 3 udp 111 portmapper
100000 2 udp 111 portmapper
100024 1 udp 55432 status
100024 1 tcp 50839 status
100005 1 udp 20048 mountd
100005 1 tcp 20048 mountd
100005 2 udp 20048 mountd
100005 2 tcp 20048 mountd
100005 3 udp 20048 mountd
100005 3 tcp 20048 mountd
100003 3 tcp 2049 nfs
100003 4 tcp 2049 nfs
100227 3 tcp 2049 nfs_acl
100021 1 udp 42402 nlockmgr
100021 3 udp 42402 nlockmgr
100021 4 udp 42402 nlockmgr
100021 1 tcp 46487 nlockmgr
100021 3 tcp 46487 nlockmgr
100021 4 tcp 46487 nlockmgr
[root@sre ~]# ss -ntulp #查看端口
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
udp UNCONN 0 0 0.0.0.0:20048 0.0.0.0:* users:(("rpc.mountd",pid=2991,fd=7))
udp UNCONN 0 0 192.168.201.128:53 0.0.0.0:* users:(("named",pid=1041,fd=514))
udp UNCONN 0 0 172.31.7.201:53 0.0.0.0:* users:(("named",pid=1041,fd=513))
udp UNCONN 0 0 127.0.0.1:53 0.0.0.0:* users:(("named",pid=1041,fd=512))
udp UNCONN 0 0 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=2893,fd=5),("systemd",pid=1,fd=71))
udp UNCONN 0 0 0.0.0.0:123 0.0.0.0:* users:(("chronyd",pid=926,fd=8))
udp UNCONN 0 0 0.0.0.0:55432 0.0.0.0:* users:(("rpc.statd",pid=2980,fd=7))
udp UNCONN 0 0 127.0.0.1:323 0.0.0.0:* users:(("chronyd",pid=926,fd=6))
udp UNCONN 0 0 0.0.0.0:514 0.0.0.0:* users:(("rsyslogd",pid=1001,fd=4))
udp UNCONN 0 0 127.0.0.1:659 0.0.0.0:* users:(("rpc.statd",pid=2980,fd=50))
udp UNCONN 0 0 0.0.0.0:42402 0.0.0.0:*
udp UNCONN 0 0 [::]:20048 [::]:* users:(("rpc.mountd",pid=2991,fd=9))
udp UNCONN 0 0 [::]:44784 [::]:*
udp UNCONN 0 0 [::]:44832 [::]:* users:(("rpc.statd",pid=2980,fd=9))
udp UNCONN 0 0 [::1]:53 [::]:* users:(("named",pid=1041,fd=515))
udp UNCONN 0 0 [::]:111 [::]:* users:(("rpcbind",pid=2893,fd=7),("systemd",pid=1,fd=73))
udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=926,fd=7))
udp UNCONN 0 0 [::]:514 [::]:* users:(("rsyslogd",pid=1001,fd=5))
tcp LISTEN 0 64 0.0.0.0:2049 0.0.0.0:*
tcp LISTEN 0 25 0.0.0.0:514 0.0.0.0:* users:(("rsyslogd",pid=1001,fd=6))
tcp LISTEN 0 128 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=2893,fd=4),("systemd",pid=1,fd=70))
tcp LISTEN 0 128 0.0.0.0:20048 0.0.0.0:* users:(("rpc.mountd",pid=2991,fd=8))
tcp LISTEN 0 10 192.168.201.128:53 0.0.0.0:* users:(("named",pid=1041,fd=23))
tcp LISTEN 0 10 172.31.7.201:53 0.0.0.0:* users:(("named",pid=1041,fd=22))
tcp LISTEN 0 10 127.0.0.1:53 0.0.0.0:* users:(("named",pid=1041,fd=21))
tcp LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=958,fd=3))
tcp LISTEN 0 64 0.0.0.0:46487 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:50839 0.0.0.0:* users:(("rpc.statd",pid=2980,fd=8))
tcp LISTEN 0 128 127.0.0.1:953 0.0.0.0:* users:(("named",pid=1041,fd=25))
tcp LISTEN 0 64 [::]:2049 [::]:*
tcp LISTEN 0 25 [::]:514 [::]:* users:(("rsyslogd",pid=1001,fd=7))
tcp LISTEN 0 64 [::]:45257 [::]:*
tcp LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=2893,fd=6),("systemd",pid=1,fd=72))
tcp LISTEN 0 128 [::]:20048 [::]:* users:(("rpc.mountd",pid=2991,fd=10))
tcp LISTEN 0 128 [::]:57173 [::]:* users:(("rpc.statd",pid=2980,fd=10))
tcp LISTEN 0 10 [::1]:53 [::]:* users:(("named",pid=1041,fd=24))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=958,fd=4))
tcp LISTEN 0 128 [::1]:953 [::]:* users:(("named",pid=1041,fd=26))
设置共享文件夹/目录
mkdir -p /data/www/ #创建共享目录
[root@sre ~]# cat /etc/exports
/data/www *(rw,all_squash,anonuid=80,anongid=80)
#目录 共享的机器(以ip确定 *代表所有) (rw)给这个共享接受者 读写权限 默认只读 可以不写或者(ro)
[root@sre ~]# exportfs -r #重载配置文件
[root@sre ~]# exportfs -v
/data/www <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
cd /data/www/ #创建文件 好共享查看
[root@sre www]# ls
[root@sre www]# cp /etc/os-release os.txt
[root@sre www]# cat os.txt
[root@sre www]# groupadd -g 80 www #创建 组id 为80 的www组
[root@sre www]# useradd -u 80 -g www www #创建 www 用户 指定用户id为80 所属组为www
chmod 777 /data/www 改为所有权限
默认选项:(ro,sync,root_squash,no_all_squash) ro,rw 只读和读写 async 异步,数据变化后不立即写磁盘,先写入到缓冲区中,过一段时间再写入磁盘,性能高,安全性 低 sync(1.0.0后为默认)同步,数据在请求时立即写入共享存储磁盘,性能低,安全性高 root_squash (默认)远程root映射为nfsnobody,UID为65534,CentOS8 为nobody,CentOS 7以前的版本为nfsnobody no_root_squash 远程root映射成NFS服务器的root用户 all_squash 所有远程用户(包括root)都变成nfsnobody,CentOS8 为nobody no_all_squash (默认)保留共享文件的UID和GID anonuid和anongid 指明匿名用户映射为特定用户UID和组GID,而非nobody,可配合all_squash使 用
客户端机器装软件
yum -y install nfs-utils httpd
[root@web ~]# showmount -e 172.31.7.201 #核实是否共享成功
Export list for 172.31.7.201:
/data/www *
mount 172.31.7.210:/data/www /var/www/html #吧172.31.7.210:/data/www 挂载到本机的/var/www/html 下面
[root@web ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 450M 0 450M 0% /dev
tmpfs 469M 0 469M 0% /dev/shm
tmpfs 469M 6.6M 463M 2% /run
tmpfs 469M 0 469M 0% /sys/fs/cgroup
/dev/mapper/rl-root 17G 2.2G 15G 13% /
/dev/nvme0n1p1 1014M 232M 783M 23% /boot
tmpfs 94M 0 94M 0% /run/user/0
172.31.7.201:/data/www 17G 2.2G 15G 13% /var/www/html
#核实挂载成功
[root@web ~]# curl 172.31.7.203 #测试是否成功
test_nfs
echo 172.31.7.201:/data/www /var/www/html nfs defaults,_netdev 0 0 >> /etc/fstab
#设置开机自动挂载
mount -a 挂载所有