CentOS7转发非3306端口到3306端口

mac2026-02-12  12

需求场景

服务器的mysql需要对外访问,但为了安全起见,对外需使用33077端口。

实现方案

通过防火墙firewalld来实现端口转发。

过程代码

查询端口转发情况

firewall-cmd --query-masquerade firewall-cmd --add-masquerad --permanent firewall-cmd --reload

注意事项:一定要加--permanent参数

转发33077端口到本机的3306端口

firewall-cmd --permanent --zone=public --add-forward-port=port=33077:proto=tcp:toport=3306

注意,一定要加参数--permanent,否则不会生效。

测试

外网链接远程服务器

mysql -h147.11.16.39 -uu_user -P33077 -p

其他用法

firewall-cmd --query-masquerade # 检查是否允许伪装IP firewall-cmd --add-masquerade # 允许防火墙伪装IP firewall-cmd --remove-masquerade# 禁止防火墙伪装IP # 将80端口的流量转发至8080 firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 # 将80端口的流量转发至 firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.0.1192.168.0.1 # 将80端口的流量转发至192.168.0.1的8080端口 firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080

参考端口转发: https://www.cnblogs.com/ilvutm/p/9301919.html

最新回复(0)