共享服务之 NFS

网友投稿 254 2022-11-08

共享服务之 NFS

NFS

Network File System 网络文件系统文件共享服务 网络文件系统。挂载的功能.将本地的资源挂着网络==NFS优势==:节省本地存储空间,将常用的数据,如:/home目录,存放在NFS服务器上且可以通过网络访问,本地终端将可减少自身存储空间的使用

nfs.ko

NFS 是一个文件系统,是操作系统来提供的,在内核里面就提供的有xfs.ko 那么具有一个nfs.ko

[root@localhost ~]# yum install mlocate -y [root@localhost ~]# locate xfs.ko locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory [root@localhost ~]# updatedb [root@localhost ~]# locate xfs.ko /usr/lib/modules/3.10.0-327.el7.x86_64/kernel/fs/xfs/xfs.ko [root@localhost ~]# locate nfs.ko /usr/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/xen/xenfs/xenfs.ko /usr/lib/modules/3.10.0-327.el7.x86_64/kernel/fs/nfs/nfs.ko

nfs-utils 服务安装包

yum install nfs-utils

开启服务

centos7: systemctl enable --now nfs systemctl enable --now nfs.service systemctl enable --now nfs-server [root@c7-107 ~]# ll /lib/systemd/system/nfs* 发现软连接的关系,所以centos8 上发生变化 =============================================== centos8 systemctl enable --now nfs-server #只要开启nfs.service,那么 rpcbind 就自动打开了 #监听的端口:rpcbind 是111 nfs.service 是很多的端口的集合 [root@c7-127 ~]# lsof -i :111 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rpcbind 547 rpc 6u IPv4 17466 0t0 UDP *:sunrpc rpcbind 547 rpc 8u IPv4 17472 0t0 TCP *:sunrpc (LISTEN) rpcbind 547 rpc 9u IPv6 17473 0t0 UDP *:sunrpc rpcbind 547 rpc 11u IPv6 17475 0t0 TCP *:sunrpc (LISTEN)

rpcbind 注册中心

nfs 依赖于 rpcbind 服务 用户访问rpcbind 的111 端口才能访问 nfs 换句话说 :就是在centos7的以上的版本,开启了nfs 服务就自动开启了 rpcbind 服务

rpcbind 基于 socket 和 service 两种

换句话说就是要关闭 rpcbind 和 rpcbind.socket 两个才行。 但是 nfs 服务是依赖于rpcbind的,所以只要重启 nfs 服务,那么 rpcbind 就自动复活

[root@c7-107 ~]# systemctl stop rpcbind Warning: Stopping rpcbind.service, but it can still be activated by: rpcbind.socket [root@c7-107 ~]# systemctl status rpcbind.socket #关闭服务了,但是socket还是开的 ● rpcbind.socket - RPCbind Server Activation Socket Loaded: loaded (/usr/lib/systemd/system/rpcbind.socket; enabled; vendor preset: enabled) Active: active (listening) since Tue 2021-06-29 20:11:05 CST; 26min ago Listen: /var/run/rpcbind.sock (Stream) Jun 29 20:11:05 c7-107 systemd[1]: Listening on RPCbind Server Activation Socket. ====================================================== [root@c7-107 ~]# systemctl stop rpcbind.socket [root@c7-107 ~]# systemctl stop rpcbind [root@c7-107 ~]# systemctl restart nfs #重启就可以复活 rpcbind和rpcbind.socket [root@c7-107 ~]# systemctl status rpcbind ● rpcbind.service - RPC bind service Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-06-29 20:43:45 CST; 7s ago [root@c7-107 ~]# systemctl status rpcbind.socket ● rpcbind.socket - RPCbind Server Activation Socket Loaded: loaded (/usr/lib/systemd/system/rpcbind.socket; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-06-29 20:43:45 CST; 41s ago

rpcinfo

[root02:53 PMcentos8 ~]#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 58678 status 100024 1 tcp 51107 status 100005 1 udp 20048 mountd #查看远程客服端的协议 [root@c7-107 ~]# rpcinfo -s 10.0.0.127 program version(s) netid(s) service owner 100000 2,3,4 local,udp,tcp,udp6,tcp6 portmapper superuser 100024 1 tcp6,udp6,tcp,udp status 29 100005 3,2,1 tcp6,udp6,tcp,udp mountd superuser 100003 4,3 udp6,tcp6,udp,tcp nfs superuser 100227 3 udp6,tcp6,udp,tcp nfs_acl superuser 100021 4,3,1 tcp6,udp6,tcp,udp nlockmgr superuser #++*nfs服务启动的端口太多,只能企业内部用,因为不好配防火墙*++

NFS配置文件

·```badh[root@localhost ~]# rpm -qf /etc/exportssetup-2.8.71-6.el7.noarchyum install setup -y[root@localhost ~]# rpm -ql setup/etc/exports/etc/exports.d/*.exports

```bash [root@c7-127 ~]# cat /etc/exports /data/wordpress *(rw) [root@c7-127 ~]# exportfs -r 重新加载文件 [root@c7-127 ~]# [root@c7-127 ~]# exportfs -v /data/wordpress (sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash) [root@localhost ~]# vim /etc/exports /data/wordpress_8 * #默认情况下是只读的 * 表示所以的主机 #表示将 /data/wordpress_8 的目录共享了 #加载服务才生效 exportfs -r (前提是 nfs 服务是开启状态)

exportfs 管理 NFS 导出的文件系统

exportfs -r 重读配置文件,加载服务 exportfs -v 查看本机的所有 NFS exportfs -a 输出本机的所有共享 exportfs -au 停止本机的所有共享,暂停但是nfs 依然是active状态

showmount 查看远程服务端的共享文件夹.

文件夹的完整路径都看的清清楚楚

[root@localhost ~]# showmount -e 10.0.0.87 Export list for 10.0.0.87: /data/wordpress_8 *

临时挂载NFS共享

语法: mount -o rw,nosuid,fg,hard,intr 172.16.0.1:/testdir /mnt/nfs/ fg (默认)前台挂载 bg 后台挂载 hard (默认)持续请求 soft   非持续请求 intr   和hard配合,请求可中断 rsize 和wsize 一次读和写数据最大字节数,rsize=32768 _netdev 无网络不挂载 vers   指定版本,客户端centos8默认4.2 ,centos7默认4.1 centos6默认4.0 范例: [root@centos7 ~]#mkdir /mnt/nfs [root@centos7 ~]#mount 10.0.0.8:/data/wordpress /mnt/nfs #将远程的共享文件夹共享到本机的 /mnt/nfs [root@centos7 ~]#ls /mnt/nfs index.html [root@centos7 ~]#df -T /mnt/nfs Filesystem               Type 1K-blocks   Used Available Use% Mounted on 10.0.0.8:/data/wordpress nfs4  52403200 398336  52004864   1% /mnt/nfs

开机挂载

[root@c7-107 ~]# vim /etc/fstab UUID=0d45af8c-aa8d-4c1f-96fd-f5a10e37f95c / xfs defaults 0 0 UUID=6460a63b-39ff-498b-b296-4d33ee247f5e /boot xfs defaults 0 0 UUID=5feb1aeb-849e-45ae-aece-83966626d519 /data ext4 defaults 1 2 UUID=db09b02d-78d1-4e30-851d-b24c5ef0cc62 swap swap defaults 0 0 10.0.0.127:/data/wordpress /mnt/data nfs _netdev 0 0

[root@c7-107 data]# touch 107 touch: cannot touch ‘107’: Permission denied #文件本身的权限 文件系统的权限 [root@c7-127 ~]# ls /data/wordpress/ -dl #服务端给的权限太少 drwxr-xr-x 2 root root 4096 Jun 26 15:06 /data/wordpress/ [root@c7-127 ~]# ls /data/wordpress/ -dl drwxr-xr-x 2 root root 4096 Jun 26 15:06 /data/wordpress/ #服务端给文件夹写权限 [root@c7-127 ~]# chmod o+w /data/wordpress/ #客服端在共享文件夹写入数据 [root@c7-107 data]# touch 107 [root@c7-107 data]# ll total 0 -rw-r--r-- 1 nfsnobody nfsnobody 0 Jun 26 15:55 107 #虽然是root用户但是被映射为 nfsnobody

权限问题

root_squash no_all_squash

只是映射root,其他的用户不映射

no_root_squash all_squash

服务端设置配置文件还指定UID gid [root@c7-127 wordpress]# vim /etc/exports [root@c7-127 wordpress]# exportfs -r [root@c7-127 wordpress]# cat /etc/exports /data/wordpress *(rw,all_squash,anonuid=1000,anongid=1000) [root@c7-127 wordpress]# 客服端:每次映射过去都是固定的UID gid [root@c7-107 data]# useradd hua;echo hua:123456|chpasswd [root@c7-107 data]# su hua [hua@c7-107 data]$ touch hua.`date +%T` [hua@c7-107 data]$ ll total 0 -rw-rw-r-- 1 wang wang 0 Jun 26 19:16 hua.19:16:50 -rw-r--r-- 1 nfsnobody nfsnobody 0 Jun 26 19:09 root.19:09:09 -rw-rw-r-- 1 wang wang 0 Jun 26 19:09 wang.19:09:30 -rw-rw-r-- 1 nfsnobody nfsnobody 0 Jun 26 19:13 wang.19:13:45

特别的主机特别的挂载

默认选项:(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使 用

解决无法找到/misc/cd 和 /net 目录的问题

#开启 autofs 服务以后 ,只要进入/misc/cd 就自动挂载光盘 只要进入 /net 就自动挂载网络文件 yum -y install autofs systemctl enable --now autofs systemctl status autofs ls /misc/cd cd /misc/cd

神奇的自动挂载

[root@c7-107 ~]# [root@c7-107 ~]# ls / bin data etc lib media mnt opt root sbin sys usr boot dev home lib64 misc net proc run srv tmp var net文件夹自动生成的 [root@c7-107 ~]# cd /net/10.0.0.127/data/wordpress/ [root@c7-107 wordpress]# ls hua.19:16:50 root.19:09:09 wang.19:09:30 wang.19:13:45 [root@c7-107 wordpress]#

自动挂载资源有两种格式:

相对路径法:将mount point 路径分成 dirname 和 basename 分别配置,可能会影响现有的目录结构 绝对路径法:直接匹配全部的绝对路径名称,都写入到指定的配置文件里,不会影响本地目录结构 相对路径法: \1. /etc/auto.master 格式挂载点的dirname     指定目录的配置文件路径,如:/etc/test.auto\2. 指定目录的配置文件格式#/etc/test.auto挂载点的basename     挂载选项     选项设备

相对路径自动挂载光盘的原理

前提是要开启autofs 服务 #第一步:将dirname 写入 auto.master [root@centos8 ~]#vim /etc/auto.master /misc   /etc/auto.misc #第二步:将basename 写入 auto.misc [root@centos8 ~]#vim /etc/auto.misc cd      -fstype=iso9660,ro,nosuid,nodev   :/dev/cdrom #这样编写配置文件就自动实现了进入目录自动挂载光盘

相对路径法为支持通配符

vim /etc/auto.master /misc   /etc/auto.misc vim /etc/auto.misc #表示/misc下面的子目录和nfs共享/export目录的子目录同名 * server:/export/& [root@c7-107 wordpress]# egrep -v '^#|^$' /etc/auto.master /misc /etc/auto.misc /net -hosts +dir:/etc/auto.master.d +auto.master [root@c7-107 wordpress]# egrep -v '^#|^$' /etc/auto.misc cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom

进入到一个不存在的目录就自动挂载

#操作步骤1 编写这一条: /misc /etc/auto.misc 在/etc/auto.master文件里面 [root@c7-107 ~]# egrep '^[^#]' /etc/auto.master /misc /etc/auto.misc /net -hosts +dir:/etc/auto.master.d +auto.master 2.编写这一条:nfs -fstype=nfs,rw 10.0.0.127:/data/wordpress 在/etc/auto.misc文件里面 nfs -fstype=nfs,rw 10.0.0.127:/data/wordpress [root@c7-107 ~]# egrep '^[^#]' /etc/auto.misc cd -fstype=iso9660,ro,nosuid,nodev :/dev/cdrom nfs -fstype=nfs,rw 10.0.0.127:/data/wordpress 3.进入到 /misc/nfs 目录就自动 挂载 [root@c7-107 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7836 490000 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1604016 93243732 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 [root@c7-107 ~]# cd /misc/nfs [root@c7-107 nfs]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7836 490000 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1604036 93243712 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 10.0.0.127:/data/wordpress 103080960 61440 97760256 1% /misc/nfs

直接进入net 的子目录

[root@c7-107 ~]# cd /net/ [root@c7-107 net]# ls [root@c7-107 net]# cd 10.0.0.127 [root@c7-107 10.0.0.127]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7836 490000 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1604208 93243540 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 [root@c7-107 10.0.0.127]# ls data [root@c7-107 10.0.0.127]# cd data/wordpress/ #进入到子目录就自动的挂载了 [root@c7-107 wordpress]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7836 490000 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1604324 93243424 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 10.0.0.127:/data/wordpress 103080960 61440 97760256 1% /net/10.0.0.127/data/wordpress

自动生成目录,进入目录自动挂载

#第一步: [root@c7-107 ~]# grep '^[^#]' /etc/auto.master /misc /etc/auto.misc /- /etc/allpath #编辑此行 /net -hosts +dir:/etc/auto.master.d +auto.master #第二步: 编写此文件 [root@c7-107 ~]# cat /etc/allpath /d1/d2/d3/nfs -fstype=nfs 10.0.0.127:/data/wordpress /dir1/dir2/dir3/cdrom -fstype=iso9600 :/dev/cdrom #第三步:由于修改了主配置文件所以要重启服务 systemctl restart autofs #第四步; [root@c7-107 ~]# tree /d1/d2/d3/nfs/ /d1/d2/d3/nfs/ ├── hua.19:16:50 ├── root.19:09:09 ├── wang.19:09:30 └── wang.19:13:45 [root@c7-107 nfs]# tree /dir1/ -d /dir1/ └── dir2 └── dir3 └── cdrom ├── EFI │   └── BOOT │   └── fonts ├── images │   └── pxeboot ├── isolinux ├── LiveOS ├── Packages └── repodata [root@c7-107 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7900 489936 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1604868 93242880 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 #进入目录就自动挂载 ,而且目录是自动生成 [root@c7-107 ~]# cd /d1/d2/d3/nfs/ 10.0.0.127:/data/wordpress [root@c7-107 nfs]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7900 489936 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1604848 93242900 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 10.0.0.127:/data/wordpress 103080960 61440 97760256 1% /d1/d2/d3/nfs /dev/sr0 4600876 4600876 0 100% /dir1/dir2/dir3/cdrom

绝对路径法

\1. /etc/auto.master 格式

/-         指定配置文件路径

\2. 指定配置文件格式

绝对路径       挂载选项     选项设备

NFS服务器的家目录作为共享实验

1.三台机器都开启autofs 服务 开机自动启动 2.三台机器都开启nfs 服务 开机自动启动 #10.0.0.127 的nfs服务器 1 创建user1 UID为1001 ,(也要确保所有客户机器的user的UID相同) [root@c7-127 wordpress]# useradd -d /data/home/user1 user1 2 编辑文件 [root@c7-127 home]# vim /etc/exports.d/test.exports /data/home *(rw) 3 加载服务且查看 [root@c7-127 wordpress]# exportfs -r [root@c7-127 wordpress]# exportfs -v /data/wordpress (sync,wdelay,hide,no_subtree_check,anonuid=1000,anongid=1000,sec=sys,rw,secure,root_squash,all_squash) /data/home (sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash) #10.0.0.117 客服端 1. 创建user且UID为1001 [root@c7-117 ~]# useradd -M -u 1001 user1 2 写 etc/auto.master 文件 [root@c7-117 ~]# vim /etc/auto.master /misc /etc/auto.misc /home /etc/auto.home 3 写 /etc/auto.home [root@c7-117 ~]# vim /etc/auto.home * -fstype=nfs 10.0.0.127:/data/home/& 注意: * 对应 & 表示 * 是啥 &就是啥 一致。此方法只适合相对路径 4 查看 [root@c7-117 ~]# showmount -e 10.0.0.127 Export list for 10.0.0.127: /data/home * /data/wordpress * 5 重启服务就被隐藏了 [root@c7-117 ~]# ll /home drwx------ 2 mage mage 62 Jun 26 21:05 mage drwx------ 2 wang wang 62 Jun 26 21:06 wang [root@c7-117 ~]# systemctl restart autofs #重启服务之前的ho [root@c7-117 ~]# ll /home/ total 0 6.家目录就被覆盖了 [root@c7-117 ~]# su - wang su: warning: cannot change directory to /home/wang: No such file or directory -bash-4.2$ whoami wang 7.切换user1 就自动挂载 用相对路径法导致只有user1能登录了 [root@c7-117 ~]# su - user1 Last login: Sat Jun 26 20:55:39 CST 2021 on pts/1 [user1@c7-117 ~]$ pwd /home/user1 [user1@c7-117 ~]$ df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7836 490000 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1605476 93242272 2% / /dev/sda1 1562624 134224 1428400 9% /boot /dev/sda2 103080888 61464 97760160 1% /data tmpfs 99568 0 99568 0% /run/user/0 10.0.0.127:/data/home/user1 103080960 61440 97760256 1% /home/user1 #10.0.0.107 客服端。用绝对路径法,所有能登录的用户不受影响 [root@c7-107 ~]# df Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7844 489992 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1605572 93242176 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 2.同样建立uid为1001 且 用户名为user1的用户,无家目录 [root@c7-107 ~]# id wang uid=1000(wang) gid=1000(wang) groups=1000(wang) [root@c7-107 ~]# useradd -M -u 1001 user1 [root@c7-107 ~]# id user1 uid=1001(user1) gid=1001(user1) groups=1001(user1) 3. [root@c7-107 ~]# ll /home total 0 drwx------ 2 wang wang 83 Jun 26 16:22 wang 4. [user1@c7-107 ~]# vim /etc/auto.master /misc /etc/auto.misc /- /etc/auto.home 5 使用绝对路径 [user1@c7-107 ~]# vim /etc/auto.home /home/user1 -fstype=nfs 10.0.0.127:/data/home/user1 6 重启服务且切换用户 [root@c7-107 ~]# systemctl restart autofs [root@c7-107 ~]# su - user1 #user 虽然无家目录却可以登录 [user1@c7-107 ~]$ ls [user1@c7-107 ~]$ df #且自动挂载 Filesystem 1K-blocks Used Available Use% Mounted on devtmpfs 487120 0 487120 0% /dev tmpfs 497836 0 497836 0% /dev/shm tmpfs 497836 7836 490000 2% /run tmpfs 497836 0 497836 0% /sys/fs/cgroup /dev/sda5 94847748 1605580 93242168 2% / /dev/sda2 103080888 61464 97760160 1% /data /dev/sda1 1562624 134224 1428400 9% /boot tmpfs 99568 0 99568 0% /run/user/0 10.0.0.127:/data/home/user1 103080960 61440 97760256 1% /home/user1 [user1@c7-107 ~]$ ls /home #互不影响 user1 wang

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:EZ-USB处理器的配置特性、接口设备开发方法与应用
下一篇:Type-C接口边充电边OTG转接器方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~