【Centos7系列】NFS 配置

Iceboundnana 发布于 2023-05-25 349 次阅读


节点设置

  • server:作为 NFS 服务的服务器,提供共享文件挂载
  • IP:192.168.100.10
  • client:作为 NFS 服务的客户端,拉取 server 服务器文件并挂载至本地使用
  • IP:192.168.100.20

server 服务器配置

配置 yum 源

[root@server ~]# cat > /etc/yum.repos.d/centos.repo <<EOF
> [centos]
> name=centos
> baseurl=file:///opt/centos
> gpgcheck=0
> enabled=1
> EOF

挂载光盘

[root@server ~]# mount /dev/sr0 /opt/centos

安装 NFS 服务

[root@server ~]# yum install nfs-utils rpcbind -y
安装 NFS 服务必须依赖 RPC,所以运行NFS就必须安装RPC

配置服务相关项

[root@server ~]# systemctl start rpcbind && systemctl enable rpcbind
[root@server ~]# systemctl start nfs && systemctl enable nfs
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i "s,SELINUX=enforcing,SELINUX=disabled,g" /etc/selinux/config

创建共享目录

[root@server ~]# mkdir /mnt/test
具体共享看实际情况,此处仅做测试

修改配置文件

[root@server ~]# vi /etc/exports
/mnt/test 192.168.100.0/24(rw)

配置文件参数说明:

  • /mnt/test:为共享目录
  • 192.168.100.0/24: 可以为一个网段,也可以是一个IP,还可以是域名(域名支持通配符,如*.qq.com)
  • rw:read-write:[读写]
  • ro:read-only:[只读]
  • sync:文件同时写人硬盘和内存
  • async:文件暂存于内存,而不是直接写人内存
  • wdelay:延迟写操作
  • norootsquash:NFS客户端连接服务端时,如果使用的是 root,那么对服务端共享的目录来说,也拥有 root 权限(开启此项考虑安全性)
  • root_squash:NFS客户端连接服务端时,如果使用的是 root,那么对服务端共享的目录来说,拥有匿名用户权限,通常它将使用 nobody 或 nfsnobody 身份
  • all_squash:不论NFS客户端连接服务端时使用什么用户,对服务端共享的目录来说,都拥有匿名用户权限
  • anonuid:匿名用户的UID(User Identification,用户身份证明)值,可以在此处自行设定
  • anongid:匿名用户的GID(Group Identification,共享资源系统使用者的群体身份)值

生效配置文件并重启服务

[root@server ~]# exportfs -r
[root@server ~]# systemctl restart rpcbind && systemctl restart nfs

查看共享目录

[root@server ~]# showmount -e 192.168.100.10
Export list for 192.168.100.10:
/mnt/test 192.168.100.0/24
此处为 server 服务器IP地址

client 客户端配置

配置 yum 源

[root@client ~]# cat > /etc/yum.repos.d/centos.repo <<EOF
> [centos]
> name=centos
> baseurl=file:///opt/centos
> gpgcheck=0
> enabled=1
> EOF

挂载光盘

[root@client ~]# mount /dev/sr0 /opt/centos

安装 NFS 服务

[root@client ~]# yum install nfs-utils rpcbind -y
安装 NFS 服务必须依赖 RPC,所以运行NFS就必须安装RPC

配置服务相关项

[root@client ~]# systemctl start rpcbind && systemctl enable rpcbind
[root@client ~]# systemctl start nfs && systemctl enable nfs
[root@localhost ~]# systemctl stop firewalld && systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i "s,SELINUX=enforcing,SELINUX=disabled,g" /etc/selinux/config

拉取服务器共享目录挂载至本地

[root@client ~]# mount -t nfs 192.168.100.10:/mnt/test /mnt

查看挂载情况

[root@client ~]# df
Filesystem               1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root   17811456 1058988  16752468   6% /
devtmpfs                   1002852       0   1002852   0% /dev
tmpfs                      1015072       0   1015072   0% /dev/shm
tmpfs                      1015072    9692   1005380   1% /run
tmpfs                      1015072       0   1015072   0% /sys/fs/cgroup
/dev/sda1                  1038336  132376    905960  13% /boot
tmpfs                       203016       0    203016   0% /run/user/0
/dev/sr0                   4364408 4364408         0 100% /opt/centos
192.168.100.10:/mnt/test  17811456 1058816  16752640   6% /mnt

验证 NFS 共享存

在 client 客户端上,在挂载目录中创建 ‘abc.txt’ 文件并计算 MD5 值

[root@client ~]# touch /mnt/abc.txt
[root@client ~]# md5sum /mnt/abc.txt
d41d8cd98f00b204e9800998ecf8427e  /mnt/abc.txt

  • MD5,一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致
  • 每个文件对应的 MD5 值是固定的,文件内容发生变化时,MD5 值也会发生变化,文件内容相同时, MD5 值相同
  • MD5 值可以作为文件的唯一标识符

在 server 服务端,验证共享目录中 MD5 值

[root@server ~]# md5sum /mnt/test/abc.txt 
d41d8cd98f00b204e9800998ecf8427e  /mnt/test/abc.txt
当挂载文件为CD光盘文件时,文件及目录不可写(只读文件系统)
此作者没有提供个人介绍。
最后更新于 2023-05-31