闲者博客 - CentOS7 https://bk1314.com/tag/CentOS7/ zh-CN Wed, 23 Feb 2022 16:45:00 +0800 Wed, 23 Feb 2022 16:45:00 +0800 Linux:CentOS7防火墙 开放端口配置和端口转发/映射 https://bk1314.com/12.html https://bk1314.com/12.html Wed, 23 Feb 2022 16:45:00 +0800 飞鱼 1.查看已开放的端口

firewall-cmd --list-ports

2.开放端口(开放后需要要重启防火墙才生效)

firewall-cmd --zone=public --add-port=3338/tcp --permanent

3.关闭端口(关闭后需要要重启防火墙才生效)

firewall-cmd --zone=public --remove-port=3338/tcp --permanent

4.重启防火墙

firewall-cmd --reload

5.开机启动防火墙

systemctl enable firewalld

6.开启防火墙

systemctl start firewalld

7.禁止防火墙开机启动

systemctl disable firewalld

8.停止/关闭防火墙

systemctl stop firewalld

9.开启系统路由模式功能

# 开启系统路由模式功能 
echo net.ipv4.ip_forward=1>>/etc/sysctl.conf 
#使内核修改生效 
sysctl -p

下面是转发端口/端口映射

防火墙管理命令 firewall-cmd 端口转发

[scode type="red"]转发之前一定要注意的[/scode]

设置IP地址伪装

firewall-cmd --add-masquerade --permanent

1.转发规则

# 格式
port=<源端口号>:proto=<匹配协议>:toport=<目的端口号>[:toaddr=<目的IP地址>]
# 示例
port=888:proto=tcp:toport=22
# 将888端口的tcp流量转发至22端口
port=8080:proto=tcp:toport=80
# 将8080端口的tcp流量转发至80端口
port=8081:proto=tcp:toport=80:toaddr=192.168.10.10
# 将8081端口的tcp流量转发至192.168.10.10的80端口

2.完整示例

# 查看永久生效模式下,已配置的端口转发规则
firewall-cmd --permanent --list-forward-ports

# 将888端口的tcp流量转发至22端口,永久生效模式
firewall-cmd --permanent --add-forward-port=port=888:proto=tcp:toport=22

# 将8081端口的tcp流量转发至192.168.10.10的80端口,永久生效模式
firewall-cmd --permanent --add-forward-port=port=8080:proto=tcp:toport=80:toaddr=192.168.10.10

# 查看当前生效的端口转发规则
firewall-cmd --list-forward-ports

#查看所有规则
firewall-cmd --list-all


# 删除已配置的转发规则(添加的有哪些参数删除的时候也要有还要端口,支持888-999格式)
firewall-cmd --remove-forward-port=port=888:proto=tcp:toport=22
# 仅当前有效

firewall-cmd --permanent --remove-forward-port=port=888:proto=tcp:toport=22
# 永久有效

firewall-cmd --reload                                                            # 使配置的永久有效规则立即生效

scode type="share"

参考资料:https://blog.csdn.net/weixin_46310295/article/details/107704545

https://www.cnblogs.com/nhdlb/p/11910615.html

https://www.cnblogs.com/cnsevennight/p/13141421.html

https://blog.csdn.net/teisite/article/details/84999582

https://www.linuxprobe.com/chapter-08.html#83_Firewalld

https://www.cnblogs.com/shawhe/p/11050088.html

]]>
0 https://bk1314.com/12.html#comments https://bk1314.com/feed/tag/CentOS7/