Centos7 Firewalld 端口转发

最近客户老爷们吵着要国内中转的shadowsocks服务,我用的防火墙是centos7自带的firewalld,搜了搜没有配置端口转发的教程,那我就来写一下吧。
起初我以为阿里云阉割了SNAT转发,不允许客户在机器上配置端口转发,但是实际测试是可以的。为此我发工单肛了一遍阿里云的售后。
本文使用的操作系统是centos7,防火墙为默认的firewalld。
决定你端口转发是否能成功的最重要一点是你得打开防火墙伪装IP的功能,这就是SNAT。
[sourcecode language=”plain”]
firewall-cmd –query-masquerade # 检查是否允许伪装IP
firewall-cmd –add-masquerade # 允许防火墙伪装IP
或
firewall-cmd –zone=区域 –add-masquerade
firewall-cmd –remove-masquerade# 禁止防火墙伪装IP
[/sourcecode]
不打开SNAT会怎么样呢?我们的跳板机就相当于下图的网关服务器。

如上图,网关之上没有经过SNAT处理,那么请求过后的回复数据包就不能在互联网中找到这个私网IP地址,所以用户也就不能顺利取得想要的资源了。
下图打开SNAT之后,使用了SNAT源地址转换技术,服务器应答后先由网关服务器接收,再分发给内网的用户主机,从而使得用户顺利的拿到了所需资源。

下面是端口转发的命令
[sourcecode language=”plain”]

firewall-cmd –add-forward-port=port=80:proto=tcp:toport=8080 # 将80端口的流量转发至8080
firewall-cmd –add-forward-port=port=80:proto=tcp:toaddr=192.168.1.0.1 # 将80端口的流量转发至192.168.0.1
firewall-cmd –add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080 # 将80端口的流量转发至192.168.0.1的8080端口
firewall-cmd –list-all #查询所有配置
[/sourcecode]
参考了以下内容:

第8章 Iptables与Firewalld防火墙。


https://fedoraproject.org/wiki/FirewallD/zh-cn#.E7.AB.AF.E5.8F.A3.E8.BD.AC.E5.8F.91
http://www.qingpingshan.com/m/view.php?aid=165196
https://havee.me/linux/2015-01/using-firewalls-on-centos-7.html
https://access.redhat.com/documentation/zh-CN/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


( ! ) Fatal error: Uncaught wfWAFStorageFileException: Unable to verify temporary file contents for atomic writing. in /var/www/html/nby/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php on line 51
( ! ) wfWAFStorageFileException: Unable to verify temporary file contents for atomic writing. in /var/www/html/nby/wp-content/plugins/wordfence/vendor/wordfence/wf-waf/src/lib/storage/file.php on line 51
Call Stack
#TimeMemoryFunctionLocation
11.469019624448wfWAFStorageFile->saveConfig( $category = 'livewaf' ).../file.php:0
21.469119625552wfWAFStorageFile::atomicFilePutContents( $file = '/var/www/html/nby/wp-content/wflogs/config-livewaf.php', $content = '<?php exit(\'Access denied\'); __halt_compiler(); ?>\n******************************************************************\nThis file is used by the Wordfence Web Application Firewall. Read \nmore at https://docs.wordfence.com/en/Web_Application_Firewall_FAQ\n******************************************************************\na:2:{s:4:"cron";a:3:{i:0;O:42:"wfWAFCronFetchCookieRedactionPatternsEvent":1:{s:11:"\000*\000fireTime";i:1792698201;}i:1;O:24:"wfWAFCronFetchRulesEvent":1:{s:11:"\000*\000fireTime";i:1792698201;}i:3;O:36:"'..., $prefix = ??? ).../file.php:658