一、配置防火墙开发端口
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
标签:iptables,cmd,--,sudo,端口,firewalld,iptable,firewall,nat
From: https://www.cnblogs.com/THuang/p/17922402.html