端口转发(Linux/Windows)

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介:

【目的】

  监听本机 7777 端口,将数据转发到 192.168.7.8 的 8888 端口,实现 TCP 数据转发。


【方法】

1、ncat(Linux/Windows 通用)(ncat端口转发

1
ncat --sh- exec  "ncat 192.168.7.8 8888"  -l 7777 --keep- open


2、netsh(Windows)(port forwarding in windows

2.1、设置

1
2
#将本机 7777 端口收到的内容转发到 192.168.7.8 的 8888 端口
netsh interface portproxy add v4tov4 listenport=7777 listenaddress=0.0.0.0 connectport=8888 connectaddress=192.168.7.8

2.2、查看

1
netsh interface portproxy show all

2.3、移除

1
netsh interface portproxy delete v4tov4 listenport=7777 listenaddress=0.0.0.0


3、iptables(Ubuntu 16.04)(How-To: Redirecting network traffic to a new IP using IPtables

3.1、清空规则

1
2
3
4
5
6
7
8
9
sudo  iptables -F
sudo  iptables -X
sudo  iptables -t nat -F
sudo  iptables -t nat -X
sudo  iptables -t mangle -F
sudo  iptables -t mangle -X
sudo  iptables -P INPUT ACCEPT
sudo  iptables -P FORWARD ACCEPT
sudo  iptables -P OUTPUT ACCEPT

3.2、开启端口转发(/etc/sysctl.conf)

1
2
3
4
# 开启端口转发
sudo  sysctl net.ipv4.ip_forward=1
# 查看
sudo  sysctl -a |  grep  ip_forward

3.3、配置端口转发

1
2
3
4
5
# 转发规则配置(可添加详细的限制规则)
sudo  iptables -t nat -A PREROUTING -p tcp --dport 7777 -j DNAT --to-destination 192.168.7.8:8888
sudo  iptables -t nat -A POSTROUTING -j MASQUERADE
# 查看
sudo  iptables -t nat -nL

3.3、移除示例

1
2
3
4
#查看
sudo  iptables -t nat -nL --line-numbers
#移除。最后的数字为加 --line-numbers 参数后 num 显示的序号
sudo  iptables -t nat -D POSTROUTING 1

3.4、端口查看

1
sudo  netstat  -anpt |  grep  7777

可以看到 iptables 端口转发的连接并不能用 netstat 查看,因为 NAPT 并不需要占用端口(为啥?),7777 端口仍然可以被其它程序使用。若需查看,可使用 netstat-nat 命令。


相关阅读:

1、网络端口的转发和重定向(Python)

2、Windows 和 Linux 平台下的端口转发工具

3、简单的TCP代理服务器

4、centos6.5 iptables实现端口转发


*** walker的流水账 ***

本文转自walker snapshot博客51CTO博客,原文链接http://blog.51cto.com/walkerqt/1947585如需转载请自行联系原作者


RQSLT

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
2月前
|
Web App开发 安全 Ubuntu
Nexpose 8.13.0 for Linux & Windows - 漏洞扫描
Nexpose 8.13.0 for Linux & Windows - 漏洞扫描
97 3
Nexpose 8.13.0 for Linux & Windows - 漏洞扫描
|
1月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025080401 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025080401 (Linux, Windows) - 专业渗透测试框架
54 0
|
20天前
|
安全 Linux iOS开发
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
132 0
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
|
20天前
|
安全 Linux 生物认证
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
31 0
Nexpose 8.18.0 for Linux & Windows - 漏洞扫描
|
22天前
|
安全 Linux iOS开发
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
95 0
SonarQube Server 2025 Release 4.2 (macOS, Linux, Windows) - 代码质量、安全与静态分析工具
|
25天前
|
NoSQL IDE MongoDB
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
107 0
Studio 3T 2025.14 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
2月前
|
Linux API iOS开发
Blender 4.5 (Linux, macOS, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
Blender 4.5 (Linux, macOS, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
112 1
Blender 4.5 (Linux, macOS, Windows) - 开源 3D 创意软件 (渲染 建模 雕刻)
|
2月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
292 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
2月前
|
Linux 虚拟化 iOS开发
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux
430 0
VMware Workstation 17.6.4 Pro Unlocker & OEM BIOS 2.7 for Windows & Linux