MySQL 端口 3306
nfs 无固定端口,一般是2049
NFS(Network File System,网络文件系统)是FreeBSD支持的文件系统中的一种,它允许网络中的计算机(不同的计算机、不同的操作系统)之间通过TCP/IP网络共享资源,主要在unix系列操作系统上使用。在NFS的应用中,本地NFS的客户端应用可以透明地读写位于远端NFS服务器上的文件,就像访问本地文件一样。
NFS服务器可以让PC将网络中的NFS服务器共享的目录挂载到本地端的文件系统中,而在本地端的系统中看来,那个远程主机的目录就好像是自己的一个磁盘分区一样。
由于NFS支持的功能比较多,而不同的功能都会使用不同的程序来启动,每启动一个功能就会启用一些端口来传输数据,因此NFS的功能所对应的端口并不固定,而是随机取用一些未被使用的小于1024的端口用于传输。但如此一来就会产生客户端连接服务器的问题,因为客户端需要知道服务器端的相关端口才能够连接。
此时就需要RPC(Remote Procedure Call,远程过程调用)的服务。由于当服务器在启动NFS时会随机选取数个端口号,并主动向RPC注册,所以RPC知道每个NFS功能所对应的端口号,RPC将端口号通知给客户端,让客户端可以连接到正确的端口上去。RPC采用固定端口号port 111来监听客户端的需求并向客户端响应正确的端口号。
注:在启动NFS之前,要先启动RPC,否则NFS会无法向RPC注册。另外,RPC若重新启动,原来注册的数据会消失不见,因此RPC重启后,它管理的所有服务都需要重新启动以重新向RPC注册。
一个文件系统可以连接多个挂载点目录。 一个挂载点目录可以连接多个文件系统。一个挂载点目录下只能连接一次nfs系统。
正常多个文件系统可以挂载到同一挂载点目录,但是,nfs4文件系统默认一个挂载点下只能挂载一个nfs4文件系统
1.在配置文件里 /etc/exports
2.在挂载时 mount -o (remount) rw
3.挂载后 chmod o+w 文件名
共享目录 访问主机(参数权限)
创建共享目录和文件
编辑配置文件 /etc/exports
exportfs -ra nfs服务重新导出配置
mount:显示所有挂载点信息
删除文件:dnf remove nginx -y
[root@localhost ~]# yum install rpcbind
[root@localhost ~]# yum install nfs-utils
#systemctl stop firewalld
#setenforce 0
#systemctl start nfs-server
#服务端(192.168.168.128)
[root@server ~]# mkdir /public
[root@server ~]# touch /public/{1..10}
[root@server public]# vim /etc/exports
/public *(ro)
#/public *(ro,no_root_squash)
#/public *(ro,all_squash)
#共享目录 可访问的主机名(权限)
#可以使用完整的IP或者是网络号,例如172.24.8.128或172.24.8.0/24或者172.24.8.128/255.255.255.0;也可以使用*表示所有主机
#权限相关参数可以写多个,多个参数之间用逗号隔开,具体相关参数说明如下:
[root@server public]# exportfs -r
Client
#yum install nfs-utils -y
#mkdir /nfs
#mount 192.168.10.135:/public /nfs
------------------------------------------
#showmount -e 192.168.10.135 查看共享的文件系统
注:client 管理员root访问nfs文件系统默认做了用户映射root --nobody(65534)
#mount -o remount,ro /nfs
#mount -o ro 192.168.10.130:/public /nfs
注:
一个挂载点目录可以同时挂载多个文件系统,通过挂载点查看是最后一次挂载文件系统中的数据文件。
参数值 | 说明 |
---|---|
rw,ro | 该目录共享的权限是可读写还是只读,但最终能否读写,还是与 文件系统的rwx有关 |
sync,async | sync代表数据会同步写入到内存与硬盘中,确保数据的完整和可靠性。 async则代表数据会 先暂存于内存当中,而非直接写入硬盘 |
no_root_squash root_squash | 若客户端在共享目录里创建的文件的所属者和所属组是root用户 和root组,那么显示文件的属主和属组时有以下两种情况: no_root_squash表示,文件的所属者和所属组是root用户和 root组(客户端访问服务端的文件时身份为管理员,缺少安全性);root_squash表示将root用户和组映射为匿名用户和组 (默认设置)。 |
all_squash no_all_squash | all_squash:客户端所有用户创建文件时,客户端会将文件的用户 和组映射为匿名用户和组 no_all_squash:客户端普通用户创建的 文件的UID和GID是多少,服务端就显示为多少(默认设置) |
anonuid= anongid= | 将文件的用户和组映射为指定的UID和GID,若不指定默认为 65534(nfsnobody)root UIB=0(唯一判断标志) |
[root@server data]# chmod o+w /data/
[root@server data]# systemctl disable firewalld --now
[root@server data]# getenforce
Enforcing
[root@server data]# setenforce 0
[root@server data]# systemctl restart nfs-server
[root@server data]# showmount -e 192.168.168.128
Export list for 192.168.168.128:
/data 192.168.168.140
#客户端(192.168.168.140)
[root@client ~]# showmount -e 192.168.168.128
Export list for 192.168.168.128:
/data 192.168.168.140
[root@client ~]# mkdir -p /nfsclient/client-data/
[root@client ~]# mount 192.168.168.128:/data /nfsclient/client-data/
[root@client ~]# df -h
Filesystem Size Used Avail Use% Mounted on
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/mapper/centos-root 37G 1.8G 36G 5% /
/dev/sda1 1014M 150M 865M 15% /boot
tmpfs 182M 0 182M 0% /run/user/0
192.168.168.128:/data 40G 6.2G 34G 16% /nfsclient/client-data
练习:
网站需求:
1.基于域名www.openlab.com可以访问网站内容为 welcome to openlab!!!
2.给该公司创建三个子界面分别显示学生信息,教学资料和缴费网站,基于www.openlab.com/student 网站访问学生信息,www.openlab.com/data网站访问教学资料
www.openlab.com/money网站访问缴费网站。
3.要求 (1)学生信息网站只有song和tian两人可以访问,其他用户不能访问。
(2)访问缴费网站实现数据加密基于https访问。
架设一台NFS服务器,并按照以下要求配置
1、开放/nfs/shared目录,供所有用户查询资料
[root@localhost ~]# mkdir /nfs
[root@localhost ~]# touch /nfs/shared
[root@localhost ~]# vim /etc/exports
/nfs/shared *(ro)
2、开放/nfs/upload目录,为192.168.xxx.0/24网段主机可以上传目录,
并将所有用户及所属的组映射为nfs-upload,其UID和GID均为210
[root@localhost ~]# touch /nfs/upload
[root@localhost ~]# vim /etc/exports
/nfs/upload 192.168.111.0/24(rw,all_squash,anonuid=210,anongid=210,)
[root@localhost ~]# chmod o+w /nfs/upload
3、将/home/tom目录仅共享给192.168.xxx.xxx这台主机,并只有用户tom可以完全访问该目录
添加tom用户,用户的权限可以在目录层级设置
服务器
[root@localhost ~]# systemctl stop firewalld -----关闭防火墙
[root@localhost ~]# setenforce 0 ------------关闭selinux
[root@localhost ~]# vim /etc/exports ------------修改配置文件
/nfs/shared *(ro,sync) --------供所有用户查询资料
/nfs/upload 192.168.111.0/24(rw,anonuid=210,anongid=210,sync) ----------为192.168.xxx.0/24网段主机可以上传目录, 并将所有用户及所属的组映射为nfs-upload,其UID和GID均为210
/home/tom 192.168.111.128(rw)-------仅共享给192.168.xxx.xxx这台主机,并只有用户tom可以完全访问该目录
[root@localhost ~]# mkdir /nfs/{shared,upload} -pv -----创建目录
[root@localhost ~]# chmod o+w /nfs/upload ----修改nfs-upload的权限
[root@localhost ~]# useradd -u 210 -r nfs-upload ------添加nfs-upload用户
[root@localhost ~]# id nfs-upload ------查看UID和GID
用户id=210(nfs-upload) 组id=210(nfs-upload) 组=210(nfs-upload)
[root@localhost ~]# useradd tom ------添加tom用户
[root@localhost ~]# id tom
用户id=1001(tom) 组id=1001(tom) 组=1001(tom)
[root@localhost ~]# ll /home/tom
总用量 0
[root@localhost ~]# ll /home/tom/ -d
drwx------. 3 tom tom 78 10月 28 21:26 /home/tom/
[root@localhost ~]# exportfs -ra --------NFS服务器重新读取其导出配置,并立即应用对此配置的任何更改
[root@localhost ~]# systemctl restart nfs-server
客户端
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /mnt: /dev/sr0 already mounted on /mnt.
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# showmount -e 192.168.111.130 ------列出可以通过 NFS访问的共享目录,以及哪些客户端可以访问这些目录。
Export list for 192.168.111.130:
/nfs/shared *
/nfs/upload 192.168.111.0/24
/home/tom 192.168.111.128
要求一配置
[root@localhost ~]# mkdir /test{1..3} ------创建挂载目录
[root@localhost ~]# mount 192.168.111.130:/nfs/shared /test1 ---------挂载
[root@localhost ~]# mount 192.168.111.130:/nfs/upload /test2
[root@localhost ~]# mount 192.168.111.130:/home/tom /test3
[root@localhost ~]# mount -o remount,ro 192.168.111.130:/nfs/shared /test1 ------修改权限为只读
[root@localhost ~]# mount | grep /test1 ---------通过此命令可以查看权限
192.168.111.130:/nfs/shared on /test1 type nfs4 (ro,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.111.128,local_lock=none,addr=192.168.111.130)
[root@localhost ~]# ll /test1 -------验证
total 0
要求二配置
[root@localhost ~]# vim /etc/fstab -----------设置自动挂载
192.168.111.130:/nfs/upload /test2 nfs4 defaults 0 0
[root@localhost ~]# mount | grep /test2
192.168.111.130:/nfs/upload on /test2 type nfs4 (ro,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.111.128,local_lock=none,addr=192.168.111.130)
[root@localhost ~]# mount -o remount,rw 192.168.111.130:/nfs/upload /test2 ---------修改权限为可读可写
[root@localhost ~]# mount | grep /test2 ------------查看权限
192.168.111.130:/nfs/upload on /test2 type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.111.128,local_lock=none,addr=192.168.111.130)
[root@localhost ~]# cd /test2 ------------验证
[root@localhost test2]# touch upload
[root@localhost test2]# ll
total 0
-rw-r--r--. 1 210 210 0 Oct 28 10:45 upload
要求三配置
[root@localhost test2]# useradd -u 1001 -r tom -----------添加tom用户
[root@localhost test2]# id tom -------------查看id
uid=1001(tom) gid=971(tom) groups=971(tom)
[root@localhost test2]# groupmod -g 1001 tom -----------------修改gid
[root@localhost test2]# id tom
uid=1001(tom) gid=1001(tom) groups=1001(tom) ------------UID和GID要与服务端一致
[root@localhost ~]# mount | grep /test3
192.168.111.130:/home/tom on /test3 type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.111.128,local_lock=none,addr=192.168.111.130)
验证
[root@localhost ~]# cd /test3
bash: cd: /test3: Permission denied ---------用root身份不可进入/test3,满足条件
[root@localhost ~]# su - tom ------------切换用户tom
su: warning: cannot change directory to /home/tom: No such file or directory
[tom@localhost root]$ cd /test3
[tom@localhost test3]$ ll
total 0
[tom@localhost test3]$ touch tom
[tom@localhost test3]$ ll
total 0
-rw-rw-r--. 1 tom tom 0 Oct 28 11:08 tom
添加用户:useradd -r -u 210 username
-u 添加系统用户
-r和不-r的区别:-r 不用再去配置GID
[root@localhost ~]# useradd -r -u 111 haha
[root@localhost ~]# id haha
用户id=111(haha) 组id=111(haha) 组=111(haha)
[root@localhost ~]# useradd -u 112 xixi
useradd warning: xixi's uid 112 outside of the UID_MIN 1000 and UID_MAX 60000 range.
[root@localhost ~]# id xixi
用户id=112(xixi) 组id=1002(xixi) 组=1002(xixi)
#vim /etc/fstab
.....
#文件系统名名称 挂载点目录 格式化类型 默认参数 开机是否备份 开机是否检查
172.25.250.132:/test /t nfs4 defaults 0 0
/dev/sr0 /mnt iso9660 defaults 0 0
#mount -a #加载/etc/fstab文件中的文件系统在当前主机状态下挂载(检测是否配置有问题,即预挂载)
在一般NFS文件系统的使用过程中,如果客户端要使用服务端所提供的文件系统,可以在 /etc/rc.d/rc.local 中设置开机时自动挂载( /etc/rc.d/rc.local 文件中写入的命令,在每次启动系统用户登录之前都会执行一次);也可以在登录系统后手动利用mount来挂载。
由于网络的问题,NFS服务器与客户端的连接不会一直存在,当我们挂载了NFS服务器之后,任何一方脱机都可能造成另外一方等待超时。为了解决这样的问题,就出现了下面的想法:
于是就产生了autofs这个服务。
autofs这个服务是在客户端的上面,它会持续的检测某个指定的目录,并预先设置当使用到该目录的某个子目录时,将会取得来自服务器端的NFS文件系统资源,并进行自动挂载的操作。
#客户端配置autofs
[root@client ~]# yum install autofs -y
[root@client ~]# grep suibian /etc/auto.master
/client /etc/auto.suibian
#本地端目录 具体挂载配置文件
[root@client ~]# cat /etc/auto.suibian
upload 192.168.168.128:/nfs/upload
#本地端子目录 挂载参数 服务器:服务器对应目录
[root@client ~]# systemctl restart autofs
#触发自动挂载
[root@client ~]# cd /client
[root@client ~]# cd upload
服务端
[root@localhost wordpress]# dnf install nfs-utils
[root@localhost ~]# systemctl start nfs-server
[root@localhost ~]# rpcinfo -p
[root@localhost ~]# vim /etc/exports
/pub *(ro)
[root@localhost ~]# mkdir /pub
[root@localhost ~]# touch /pub/{1..10}
[root@localhost ~]# ll /pub
总用量 0
-rw-r--r--. 1 root root 0 10月 27 11:55 1
-rw-r--r--. 1 root root 0 10月 27 11:55 10
-rw-r--r--. 1 root root 0 10月 27 11:55 2
-rw-r--r--. 1 root root 0 10月 27 11:55 3
-rw-r--r--. 1 root root 0 10月 27 11:55 4
-rw-r--r--. 1 root root 0 10月 27 11:55 5
-rw-r--r--. 1 root root 0 10月 27 11:55 6
-rw-r--r--. 1 root root 0 10月 27 11:55 7
-rw-r--r--. 1 root root 0 10月 27 11:55 8
-rw-r--r--. 1 root root 0 10月 27 11:55 9
[root@localhost ~]# chmod o+w /pub ----修改客户端权限
客户端
[root@localhost ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: device write-protected, mounted read-only.
[root@localhost ~]# yum install nfs-utils -y
[root@localhost ~]# mkdir /nfs
[root@localhost ~]# mount 192.168.111.130:/pub /nfs
[root@localhost ~]# showmount -e 192.168.111.130
Export list for 192.168.111.130:
/pub *
[root@localhost ~]# ll /nfs
total 0
-rw-r--r--. 1 root root 0 Oct 26 23:55 1
-rw-r--r--. 1 root root 0 Oct 26 23:55 10
-rw-r--r--. 1 root root 0 Oct 26 23:55 2
-rw-r--r--. 1 root root 0 Oct 26 23:55 3
-rw-r--r--. 1 root root 0 Oct 26 23:55 4
-rw-r--r--. 1 root root 0 Oct 26 23:55 5
-rw-r--r--. 1 root root 0 Oct 26 23:55 6
-rw-r--r--. 1 root root 0 Oct 26 23:55 7
-rw-r--r--. 1 root root 0 Oct 26 23:55 8
-rw-r--r--. 1 root root 0 Oct 26 23:55 9
[root@localhost nfs]# mount -o rw 192.168.111.130:/pub /nfs 修改挂载权限为rw 服务端也要修改
[root@localhost nfs]# touch a
[root@localhost nfs]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 26 23:55 1
-rw-r--r--. 1 root root 0 Oct 26 23:55 10
-rw-r--r--. 1 root root 0 Oct 26 23:55 2
-rw-r--r--. 1 root root 0 Oct 26 23:55 3
-rw-r--r--. 1 root root 0 Oct 26 23:55 4
-rw-r--r--. 1 root root 0 Oct 26 23:55 5
-rw-r--r--. 1 root root 0 Oct 26 23:55 6
-rw-r--r--. 1 root root 0 Oct 26 23:55 7
-rw-r--r--. 1 root root 0 Oct 26 23:55 8
-rw-r--r--. 1 root root 0 Oct 26 23:55 9
-rw-r--r--. 1 nobody nobody 0 Oct 27 02:39 a
取消挂载
umount /nfs
[root@localhost ~]# vim /etc/fstab
192.168.111.130:/test /p nfs4 defaults 0 0
/dev/sr0 /mnt iso9660 defaults 0 0
[root@localhost ~]# mount -a
mount.nfs4: mount point /p does not exist
mount: (hint) your fstab has been modified, but systemd still uses
the old version; use 'systemctl daemon-reload' to reload.
[root@localhost ~]# mkdir /p
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# mount -a
[root@localhost ~]# reboot