linux-firewalld 和iptable的策略配置(nat 以及端口开放)
一、配置防火墙开发端口
1、
sudo yum install firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld
2、配置防火墙规则
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --permanent --add-service=psp3
firewall-cmd --permanent --add-service=smtp
3、重载防火墙规则
sudo firewall-cmd --reload
4、检查防火墙是否能正确应用
sudo firewall-cmd --reload
二、通过iptable开启端口
1、安装
sudo yum install iptables-services
2、启动
sudo systemctl start iptables
sudo systemctl enable iptables
3、配置规则
# 清除现有规则
sudo iptables -F
sudo iptables -X
# 设置默认策略
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
# 允许回环接口
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A OUTPUT -o lo -j ACCEPT
# 允许SSH (默认端口 22)
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 允许HTTP (默认端口 80)
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 允许HTTPS (默认端口 443)
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许SMTP (默认端口 25)
sudo iptables -A INPUT -p tcp --dport 25 -j ACCEPT
# 允许POP3 (默认端口 110)
sudo iptables -A INPUT -p tcp --dport 110 -j ACCEPT
# 允许FTP (默认端口 21)
sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT
# 保存规则
sudo service iptables save
三、firewalld实现NAT方式共享上网的功能
1、开启firewalld
sudo yum install firewalld
sudo systemctl start firewalld
sudo systemctl enable firewalld
2、配置规则
# 启用NAT
sudo firewall-cmd --permanent --zone=public --add-masquerade
# 允许转发
sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080 # 举例,将80端口转发到本地8080端口
# 允许出站流量
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" masquerade'
该地址为内网地址
# 重新加载防火墙规则
sudo firewall-cmd --reload
3、开启ipv4转发
路径:打开 /etc/sysctl.conf 文件并确保以下行被取消注释:
net.ipv4.ip_forward = 1
后执行一下命令
sudo sysctl -p
4、配置网络接口
打开 /etc/sysconfig/network-scripts/ifcfg-eth0 文件(假设你的主要网络接口是 eth0)
并确保以下行被添加:
NM_CONTROLLED=no
5、重启网络服务
systemctl restart network.service
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)