CentOS7.9环境上NFS搭建及使用

1. 服务器规划

IP主机名用途
172.16.32.3file_serverNFS服务器(文件服务器)
172.16.32.4app_server应用服务器(挂载文件服务器目录)

实例为将172.16.32.3服务器上的/data挂载到172.16.32.4的/data1目录上

2. NFS服务器配置

2.1 主机名设置

hostnamectl set-hostname file_server && bash
// 设置主机名,并在当前会话中生效

在这里插入图片描述

2.2 nfs安装

2.2.1 repo文件替换

wget https://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
// CentOS自带yum源不可用,故直接从网络下载阿里云repo配置文件覆盖默认配置文件

# 清理缓存
yum clean all && yum makecache

在这里插入图片描述
附:CentOS-Base.repo配置文件全部内容

[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#released updates 
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
 
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

2.2.2 NFS服务安装

yum install -y nfs-utils

2.3 nfs配置

  1. 创建共享目录
mkdir /data

在这里插入图片描述
2. 配置共享/data目录

cat >> /etc/exports<<'EOF'
/data 172.16.32.4(sync,rw,no_root_squash)
EOF
// 配置本机/data目录允许172.16.32.4服务器进行连接

以下是一些常用的NFS共享参数:
sync:数据同步写入到内存与磁盘中,保证数据不会在发生断电时丢失,提高了数据的安全性。
rw:表示NFS文件系统可以进行读写操作。
ro:表示NFS文件系统只能进行读操作。
no_root_squash:客户机以root用户访问该共享目录时,它所拥有的权限为该服务器本地的root用户权限,可以对该目录进行读写操作,这会带来一定的安全隐患。
root_squash:客户机以root用户访问该共享目录时,它的权限会变成nfsnobody用户(一般没有权限),只能对该目录进行读操作。
all_squash:不管使用NFS的客户机的用户身份为何,他的身份都会变成nfsnobody。
anonuid/anongid:可以将访问NFS的用户身份映射成指定的用户。

在这里插入图片描述

2.4 服务查看

systemctl status rpcbind.service
systemctl status nfs.service
# 查看状态,如果没有启动,则需要使用下述命令启动对应服务
# 演示环境nfs服务未启动,故需要手动启动nfs服务
systemctl start rpcbind.service
systemctl start nfs.service
ps -ef|grep nfs

在这里插入图片描述

2.5 资源发布

exportfs -rv

在这里插入图片描述

2.6 配置nfs服务开机自启

systemctl enable rpcbind.service
systemctl enable nfs-server.service

2.7 端口开放

方案1:直接关闭防火墙

systemctl stop firewalld

方案2:放行NFS指定端口

# 查看防火墙放行端口
firewall-cmd --zone=public --list-ports
# 若不放行任何端口,在showmount时,报错:clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)

# 配置端口放行
firewall-cmd --permanent --zone=public --add-port=2049/tcp
firewall-cmd --permanent --zone=public --add-port=2049/udp
firewall-cmd --permanent --zone=public --add-port=111/tcp
firewall-cmd --permanent --zone=public --add-port=111/udp
# 重新加载防火墙策略
firewall-cmd --reload
# 只放行2049和111端口后,在其他服务器上showmount报错:rpc mount export: RPC: Unable to receive; errno = No route to host

# 除此之外,还需要放行rpc.mountd对应的端口(查看命令:netstat -lntp)
firewall-cmd --permanent --zone=public --add-port=20048/tcp
firewall-cmd --permanent --zone=public --add-port=20048/udp
firewall-cmd --reload

在这里插入图片描述

3.应用服务器配置

3.1 主机名设置

hostnamectl set-hostname app_server && bash
// 设置主机名,并在当前会话中生效

在这里插入图片描述

3.2 nfs安装

3.2.1 repo文件替换

wget https://mirrors.aliyun.com/repo/Centos-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
// CentOS自带yum源不可用,故直接从网络下载阿里云repo配置文件覆盖默认配置文件

# 清理缓存
yum clean all && yum makecache

在这里插入图片描述

3.2.2 NFS服务安装

yum install -y nfs-utils

3.3 挂载配置及测试

3.3.1 创建挂载目录

mkdir /data1

3.3.2 测试挂载

showmount -e 172.16.32.3
mount -t nfs -o sync,noac 172.16.32.3:/data /data1
查看:df -Th

在这里插入图片描述

3.3.3 读写测试

cd /data/
touch 111.txt

在这里插入图片描述
应用服务器删除,文件服务器查看

rm -rf /data1/111.txt

在这里插入图片描述

3.3.4 配置开机自动挂载

echo '172.16.32.3:/data /data1 nfs sync,noac,_netdev 0 0' >> /etc/fstab

测试fstab配置文件挂载是否异常:mount -a

在这里插入图片描述

3.3.5 配置开机自启动rpcbind服务

systemctl enable rpcbind.service

3.3.6 重启应用服务器测试

reboot

在这里插入图片描述

4.其他

showmount漏洞–在其他服务器上,使用showmount也可以打印出NFS(文件)服务器配置的共享目录信息
修复方法:

[root@file_server data]# systemctl list-unit-files | grep rpcbind
rpcbind.service                               enabled 
rpcbind.socket                                enabled 
rpcbind.target                                static  

[root@file_server data]# echo 'rpcbind:ALL:deny' >> /etc/hosts.deny
[root@file_server data]# cat /etc/hosts.deny | grep -v "#" | grep -v "^$"
rpcbind:ALL:deny

[root@file_server data]# cat /etc/hosts.allow | grep -v "#" | grep -v "^$"
[root@file_server data]# echo 'rpcbind:172.16.32.4:allow' >> /etc/hosts.allow
[root@file_server data]# echo 'rpcbind:localhost:allow' >> /etc/hosts.allow

[root@file_server data]# cat /etc/hosts.allow | grep -v "#" | grep -v "^$"
rpcbind:172.16.32.4:allow
rpcbind:localhost:allow

[root@file_server data]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值