1. 开放/nfs/shared目录,供所有用户查阅资料
服务端
(1)安装nfs服务,nfs-utils包中包含rpcbind(rpc守护进程)
[root@node1-server ~]# yum install -y nfs-utils # nfs-utils包中包含rpcbind
[root@node1-server ~]# rpm -q rpcbind # 检查rpcbind是否安装
(2)创建共享目录
[root@node1-server ~]# mkdir -p /nfs/shared # -p 递归创建目录
[root@node1-server ~]# ll /nfs
total 0
drwxr-xr-x. 2 root root 6 Mar 27 10:08 shared
(3)关闭防火墙、selinux设置宽容模式、查看状态
[root@node1-server ~]# systemctl stop firewalld
[root@node1-server ~]# setenforce 0
[root@node1-server ~]# getenforce # 查看selinux模式
Permissive # 宽容模式
[root@node1-server ~]# systemctl status firewalld.service
○ firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
(4)启动服务、设置开机自启、查看服务状态
[root@node1-server ~]# systemctl start nfs-server
[root@node1-server ~]# systemctl start rpcbind
[root@node1-server ~]# systemctl enable nfs-server
Created symlink /etc/systemd/system/multi-user.target.wants/nfs-server.service → /usr/lib/systemd/system/nfs-server.service.
[root@node1-server ~]# systemctl enable rpcbind
[root@node1-server ~]# systemctl status nfs-server
● nfs-server.service - NFS server and services
[root@node1-server ~]# systemctl status rpcbind
● rpcbind.service - RPC Bind
(5)编辑/etc/exports文件
[root@node1-server ~]# vim /etc/exports # /etc/exports NFS服务的是核心配置文件,作用是对共享的目录、允许访问的客户端以及访问权限等信息进行配置
/nfs/shared *(ro) # 将 /nfs/shared 目录共享出去,并且允许网络上的所有主机以只读权限访问该目录 *()中间没有空格
(6)重启服务
[root@node1-server ~]# exportfs -avr # exportfs 用于管理 NFS共享目录 ; -a选项表示 “all”,即对 /etc/exports 文件中定义的所有共享目录执行相应操作 ; -v 选项代表 “verbose”,也就是详细模式 ; -r 选项意味着 “reexport”,即重新导出所有共享目录
客户端
(1)安装服务
[root@node2-client ~]# yum install -y nfs-utils
(2)启动服务
[root@node2-client ~]# systemctl start nfs-server.service
[root@node2-client ~]# systemctl start rpcbind
(3)关闭防火墙、selinux设置宽容模式、查看状态
[root@node1-client ~]# systemctl stop firewalld
[root@node1-client ~]# setenforce 0
[root@node1-client ~]# getenforce
[root@node1-client ~]# systemctl status firewalld.service
(4)查看服务端提供的nfs服务
[root@node2-client ~]# showmount -e 192.168.11.135 # 查询 IP 地址为192.168.11.135的 NFS 服务器当前共享的目录列表 showmount:获取其共享目录信息 -e:向指定的 NFS 服务器请求其当前共享的目录列表
Export list for 192.168.11.135:
/nfs/shared (everyone)
(5)创建挂载点、临时挂载、查看挂载设备
[root@node2-client ~]# mkdir -p /mnt/shared
[root@node2-client ~]# mount -t nfs 192.168.11.135:/nfs/shared /mnt/shared/ # -t:用于指定要挂载的文件系统类型 ;192.168.11.135是 NFS 服务器的 IP 地址 ;:/nfs/shared 表示该服务器上共享出来的目录路径
[root@node2-client ~]# df -h # df:报告文件系统磁盘空间的使用情况 ; -h:将磁盘空间的容量以人类可读的格式显示出来
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 866M 0 866M 0% /dev/shm
tmpfs 347M 7.1M 340M 3% /run
/dev/mapper/rhel-root 15G 4.2G 11G 28% /
/dev/nvme0n1p2 960M 291M 670M 31% /boot
/dev/nvme0n1p1 1022M 7.0M 1016M 1% /boot/efi
tmpfs 174M 52K 174M 1% /run/user/42
tmpfs 174M 36K 174M 1% /run/user/0
192.168.11.135:/nfs/shared 15G 4.3G 11G 29% /mnt/shared
[root@node2-client ~]# ll -d /mnt/shared/ # -d:仅列出目录本身的信息,不递归列出目录内部的文件和子目录
drwxr-xr-x. 2 root root 6 Mar 27 10:08 /mnt/shared/
2. 开放/nfs/upload目录为x.x.x.0/24网段的数据上传目录,并将所有用户及所属的用户组都映射为redhat,其UID与GID均为3000
服务端(开启服务同上,略)
(1)创建共享目录
[root@node1-server ~]# mkdir -p /nfs/upload
[root@node1-server ~]# ll /nfs/
total 0
drwxr-xr-x. 2 root root 6 Mar 27 10:08 shared
drwxr-xr-x. 2 root root 6 Mar 27 15:18 upload
(2)创建GID为3000的组redhat,创建GID和UID均为3000的用户redhat
[root@node1-server ~]# groupadd -g 3000 redhat # 创建GID为3000的用户前必须要有一个GID为3000的组
[root@node1-server ~]# tail -1 /etc/group # 查看最后一行组信息
redhat:x:3000:
[root@node1-server ~]# useradd -g 3000 -u 3000 redhat # -g 指定GID -u 指定UID
[root@node1-server ~]# tail -1 /etc/passwd # 查看最后一行用户信息
redhat:x:3000:3000::/home/redhat:/bin/bash
[root@node1-server ~]# id redhat # 查看redhat用户的id
uid=3000(redhat) gid=3000(redhat) groups=3000(redhat)
(3)编辑 /etc/exports 文件
[root@node1-server ~]# vim /etc/exports
/nfs/upload 192.168.11.0/24(rw,all_squash,anonuid=3000,anongid=3000)
# 将 /nfs/shared 目录共享出去,允许192.168.11.0网段中的所有主机以读写读权限访问该目录 ip网段和()中间没有空格 ;所有客户端用户(包括 root 用户)的身份都会被映射为 UID 和 GID 均为3000的匿名用户。
(4)修改共享目录 /nfs/upload 的所属组和所属用户
[root@node1-server ~]# chown -R redhat:redhat /nfs/upload/ # -R 修改一个目录及其内部所有子文件和子目录的所有者(递归修改)
[root@node1-server ~]# ll /nfs
total 0
drwxr-xr-x. 2 root root 6 Mar 27 10:08 shared
drwxr-xr-x. 2 redhat redhat 6 Mar 27 15:18 upload
(5)重启服务
[root@node1-server ~]# exportfs -avr
exporting 192.168.11.0/24:/nfs/upload
客户端(开启服务同上,略)
(1)查看共享目录
[root@node2-client ~]# showmount -e 192.168.11.135 # 查询 IP 地址为192.168.11.135的 NFS 服务器当前共享的目录列表
Export list for 192.168.11.135:
/nfs/upload 192.168.11.0/24
(2)创建挂载点
[root@node2-client ~]# mkdir -p /mnt/upload
[root@node2-client ~]# ll /mnt
ls: cannot access '/mnt/shared': Stale file handle
total 0
drwxr-xr-x. 2 root root 6 Mar 24 17:34 hgfs
d?????????? ? ? ? ? ? shared # 由于服务端配置文件 /etc/exports 中已删除配置 /nfs/shared *(ro) ,故此处有未知信息
drwxr-xr-x. 2 root root 6 Mar 27 15:56 upload
(3)永久挂载
[root@node2-client ~]# vim /etc/fstab # /etc/fatab 是一个非常重要的配置文件,用于定义文件系统的挂载点、挂载参数及其他相关属性;每次系统启动时,会自动读取该文件并挂载相应的文件系统
16 192.168.11.135:/nfs/upload /mnt/upload nfs defaults 0 0
# 192.168.11.135:/nfs/upload 指定了要挂载的 NFS 共享资源的位置
# /mnt/upload 本地系统上的一个挂载点,它是当前文件系统树中的一个目录
# nfs 指定了要挂载的文件系统类型
[root@node2-client ~]# mount -a # mount -a会遍历 /etc/fstab 文件的每一行,并尝试挂载所有未被挂载的文件系统
mount: (hint) your fstab has been modified, but systemd still uses
the old version; use 'systemctl daemon-reload' to reload.
[root@node2-client ~]# df -h # 查看挂载情况
df: /mnt/shared: Stale file handle
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 866M 0 866M 0% /dev/shm
tmpfs 347M 7.1M 340M 3% /run
/dev/mapper/rhel-root 15G 4.2G 11G 28% /
/dev/nvme0n1p2 960M 291M 670M 31% /boot
/dev/nvme0n1p1 1022M 7.0M 1016M 1% /boot/efi
tmpfs 174M 52K 174M 1% /run/user/42
tmpfs 174M 36K 174M 1% /run/user/0
192.168.11.135:/nfs/upload 15G 4.3G 11G 29% /mnt/upload
[root@node2-client ~]# ll -d /mnt/upload/ # 查看挂载情况
drwxr-xr-x. 2 3000 3000 6 Mar 27 15:18 /mnt/upload/
(4)创建测试文件
[root@node2-client ~]# touch /mnt/upload/textfile.txt
[root@node2-client ~]# ll /mnt/upload/
total 0
-rw-r--r--. 1 3000 3000 0 Mar 27 16:05 textfile.txt