Linux、Windows 上还不会端口映射的网工,请低调看过来!
转自:网络工程师阿龙
一、Windows下实现端口映射
netsh interface portproxy show v4tov4
netsh interface portproxy show v4tov4 | find "[IP]"
例:
netsh interface portproxy show v4tov4 | find "192.168.1.1"
netsh interface portproxy add v4tov4 listenaddress=[外网IP] listenport=[外网端口] connectaddress=[内网IP] connectport=[内网端口]
例:
netsh interface portproxy add v4tov4 listenaddress=2.2.2.2 listenport=8080 connectaddress=192.168.1.50 connectport=80
netsh interface portproxy delete v4tov4 listenaddress=[外网IP] listenport=[外网端口]
例:
netsh interface portproxy delete v4tov4 listenaddress=2.2.2.2 listenport=8080
二、Linux下端口映射
echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i [内网网卡名称] -j ACCEPT
iptables -t nat -A POSTROUTING -s [内网网段] -o [外网网卡名称] -j MASQUERADE
例:
echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i ens33 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -o ens37 -j MASQUERADE
iptables -t nat -A PREROUTING -p tcp -m tcp --dport [外网端口] -j DNAT --to-destination [内网地址]:[内网端口]
例:
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 6080 -j DNAT --to-destination 10.0.0.100:6090
实验环境
VMWare Workstation Pro 5台最小化安装的centos 7虚拟机
实验拓扑
Server4
来说的。Server1
和Server2
为内网环境的两台服务器;Server3
为外网环境下的一台服务器;Server4
为一台双网卡主机,分别连接192.168.50.0/24
和172.16.2.0/24
两个网络。配置实验环境
1. Server1,2,3上搭建HTTP服务
Server1
上搭建一个简单的HTTP服务cd ~echo "server1" > index.htmlpython -m SimpleHTTPServer 8080
Server2、Server3
同理对照实验
client
上访问Server1
的资源curl http://192.168.50.11:8080/index.html
client
上访问Server2
的资源curl http://192.168.50.12:8080/index.htm
client
上访问Server3
的资源curl http://172.16.2.11:8080/index.html
client
是无法访问内网Server1
,Server2
的资源的。在Server4
上配置端口映射
#允许数据包转发
echo 1 >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -i ens33 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.50.0/24 -o ens37 -j MASQUERADE
#设置端口映射
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8081 -j DNAT --to-destination 192.168.50.11:8080
iptables -t nat -A PREROUTING -p tcp -m tcp --dport 8082 -j DNAT --to-destination 192.168.50.12:8080
/etc/rc.local
文件。检查效果
client
上访问Server1的资源curl http://172.16.2.100:8081/index.html
client
上访问Server2
的资源curl http://172.16.2.100:8082/index.html
client
上访问Server3
的资源curl http://172.16.2.11:8080/index.html
如果Server4
为Windows,替换一下相应的命令即可
netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8081 connectaddress=192.168.50.11 connectport=8080netsh interface portproxy add v4tov4 listenaddress=172.16.2.105 listenport=8082 connectaddress=192.168.50.12 connectport=8080netsh interface portproxy show v4tov4
检查效果
在client
节点上
curl http://172.16.2.105:8081/index.htmlcurl http://172.16.2.105:8082/index.htmlcurl http://172.16.2.11:8080/index.html
END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604215
Linux技术交流群:2636170
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章