centos 搭建 nfs (nfs+firewall 设置)

2023-05-11  本文已影响0人  Mr_窦先生

centos 搭建 nfs (nfs+firewall 设置)

一、规划

机器IP: 192.168.21.6
共享路径:/home/data

二、 nfs 部署

1、安装 nfs

yum install -y nfs-utils
yum install -y rpcbind

2、配置开机启动

chkconfig nfs on
chkconfig rpcbind on

3、启动服务

service rpcbind start
service nfs start

4、配置共享位置

mkdir /home/data

#编辑文件
vim /etc/exports

/home/data  *(rw,async,insecure,no_root_squash,no_subtree_check)

#刷新-立即生效
exportfs -a

#验证配置内容
exportfs -rv

#重启服务
systemctl restart rpcbind  && systemctl restart nfs 

#开机启动
systemctl enable nfs && systemctl enable rpcbind

5、 修改nfs 进程数,提高读写速度
vim /etc/sysconfig/nfs

 15 # Number of nfs server processes to be started.
 16 # The default is 8. 
 17 RPCNFSDCOUNT=32

6、指定 nfs 端口

vim /etc/sysconfig/nfs

放开或添加如下代码:

RQUOTAD_PORT=30001
LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
STATD_PORT=662

7、配置 nfs 端口锁定

vim /etc/modprobe.d/lockd.conf

此文件是用来配置 lockd 内核模块的参数的。lockd 是一个 Linux 内核模块,用于支持 NFS (Network File System) 文件共享协议中的文件锁定功能

#
# Set the NFS lock manager grace period. n is measured in seconds. 
#options lockd nlm_grace_period=90
#
# Set the TCP port that the NFS lock manager should use. 
# port must be a valid TCP port value (1-65535).
options lockd nlm_tcpport=32803
#
# Set the UDP port that the NFS lock manager should use.
# port must be a valid UDP port value (1-65535).
options lockd nlm_udpport=32769
#
# Set the maximum number of outstanding connections 
#options lockd nlm_max_connections=1024
#
# Set the default time value for the NFS lock manager
# in seconds. Default is 10 secs (min 3 max 20)
#options lockd nlm_timeout=10
#
# Choose whether to record the caller_name or IP address
# this peer in the local rpc.statd's database.
#options lockd nsm_use_hostnames=0

8、重启服务

systemctl restart rpcbind.service
systemctl restart nfs.service
systemctl restart nfs-config
systemctl restart nfs-idmap
systemctl restart nfs-lock
systemctl restart nfs-server

9、验证

rpcinfo -p

三、firewall 防火墙设置

1、 添加 nfs 规则配置

vim /etc/firewalld/services/nfs.xml

该文件是用于配置 Firewalld 防火墙服务的 NFS (Network File System) 规则的文件

<service>
  <short>nfs</short>
  <description>nfs</description>
  <port protocol="tcp" port="111"/>
  <port protocol="udp" port="111"/>
  <port protocol="tcp" port="662"/>
  <port protocol="udp" port="662"/>
  <port protocol="tcp" port="892"/>
  <port protocol="udp" port="892"/>
  <port protocol="tcp" port="2049"/>
  <port protocol="udp" port="2049"/>
  <port protocol="udp" port="32769"/>
  <port protocol="tcp" port="32803"/>
</service>

说明:
Firewalld 是 CentOS 中默认使用的动态防火墙管理工具,它通过定义服务和区域来管理网络连接的访问权限。每个服务都有一个相应的 XML 配置文件,用于指定该服务的防火墙规则。

/etc/firewalld/services/nfs.xml 文件定义了 NFS 服务的规则。NFS 是一种网络文件系统协议,允许在网络上共享文件系统。为了确保安全和控制访问,Firewalld 可以使用该文件中的规则来限制 NFS 服务的访问权限。

该 XML 文件包含以下信息:

<service> 元素:定义服务的名称和描述。
<port>元素:指定 NFS 使用的端口号或端口范围。
<protocol>元素:指定使用的协议(通常是 TCP 或 UDP)。
<module> 元素:指定 Firewalld 使用的内核模块。
<source> 元素:指定允许连接到 NFS 服务的来源 IP 地址或地址范围。
通过编辑 /etc/firewalld/services/nfs.xml 文件,你可以修改 NFS 服务的防火墙规则,例如更改允许连接的来源 IP 地址、更改使用的端口号或协议等。一旦修改完成,你需要重新加载 Firewalld 配置才能使规则生效。

2、向 firewall 添加规则

firewall-cmd --permanent --add-service=rpc-bind --zone=public 
firewall-cmd --permanent --add-service=nfs
firewall-cmd --permanent --add-service=mountd --zone=public 
firewall-cmd --permanent --add-service=nfs --zone=public 

# 重新加载配置
firewall-cmd --reload 

3、手动挂载

mount -o vers=3 192.168.21.6:/home/data /data

4、 自动挂载设置

vim /etc/fstab

192.168.21.6:/home/data /data nfs defaults,vers=3 0 0
上一篇 下一篇

猜你喜欢

热点阅读