iptables转发tcp/udp端口请求

文章目录

前言一、路由转发涉及点二、转发如何配置本机端口转发到本机其它端口本机端口转发到其它机器

三、固化iptables总结

前言

路由转发是计算机网络中的一种重要概念,特别是在网络设备和系统之间。它涉及到如何处理和传递数据包,以及决定数据包应该发送到哪个网络设备或路由表中的哪条路径,iptables是本文主要应用的软件技术。12

本文主要介绍以下几个情况的配置:

本机端口转发到本机其它端口本机端口转发到其它机器

本文所描述技术可以应用的几种场景:

本地服务器搭建samba共享文件夹,挂载到云服务器本地服务器搭建gitlab服务器,挂载到云服务器本地搭建的其它服务器挂载到云服务指定端口

一、路由转发涉及点

路由转发涉及到以下几个方面:

路由表:这是网络中用于确定数据包应该发送到哪个物理网络或下一个路由器的表。路由表通常包含有关到达不同网络的目标地址的信息,例如下一跳路由器、路径权重等。数据包转发:当一个路由器收到一个数据包时,它会根据其路由表确定如何将该数据包发送到下一个路由器或目标网络。这可以通过直接将数据包发送到下一个路由器或通过另一个路由器(即链路)转发来实现。数据包过滤:路由器通常还具有过滤机制,用于确定哪些数据包应该被转发到下一个路由器,哪些应该被丢弃或被拒绝。这可能涉及到诸如防火墙规则、病毒防护或访问控制列表等安全功能。 路由转发的过程通常由路由器硬件和软件协同完成,并且受到网络拓扑、路由协议(如静态路由、动态路由等)以及网络流量等因素的影响。

路由转发是计算机网络中网络设备之间传递数据包的关键过程,它涉及到如何确定数据包的下一跳位置以及如何处理和传递数据包以到达最终目的地。

二、转发如何配置

测试网站:3

docker run --rm --name web1 --publish 8080:8080 nigelpoulton/web

修改/etc/sysctl.conf文件,让包转发功能在系统启动时自动生效:

# 启动当前设备的包转发功能,增加下边这句

net.ipv4.ip_forward = 1

使用sudo sysctl -p应用修改

本机端口转发到本机其它端口

#mermaid-svg-2l60Ixx6sBVe2u92 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 .error-icon{fill:#552222;}#mermaid-svg-2l60Ixx6sBVe2u92 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-2l60Ixx6sBVe2u92 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-2l60Ixx6sBVe2u92 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-2l60Ixx6sBVe2u92 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-2l60Ixx6sBVe2u92 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-2l60Ixx6sBVe2u92 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-2l60Ixx6sBVe2u92 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-2l60Ixx6sBVe2u92 .marker.cross{stroke:#333333;}#mermaid-svg-2l60Ixx6sBVe2u92 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-2l60Ixx6sBVe2u92 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 .cluster-label text{fill:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 .cluster-label span{color:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 .label text,#mermaid-svg-2l60Ixx6sBVe2u92 span{fill:#333;color:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 .node rect,#mermaid-svg-2l60Ixx6sBVe2u92 .node circle,#mermaid-svg-2l60Ixx6sBVe2u92 .node ellipse,#mermaid-svg-2l60Ixx6sBVe2u92 .node polygon,#mermaid-svg-2l60Ixx6sBVe2u92 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-2l60Ixx6sBVe2u92 .node .label{text-align:center;}#mermaid-svg-2l60Ixx6sBVe2u92 .node.clickable{cursor:pointer;}#mermaid-svg-2l60Ixx6sBVe2u92 .arrowheadPath{fill:#333333;}#mermaid-svg-2l60Ixx6sBVe2u92 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-2l60Ixx6sBVe2u92 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-2l60Ixx6sBVe2u92 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-2l60Ixx6sBVe2u92 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-2l60Ixx6sBVe2u92 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-2l60Ixx6sBVe2u92 .cluster text{fill:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 .cluster span{color:#333;}#mermaid-svg-2l60Ixx6sBVe2u92 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-2l60Ixx6sBVe2u92 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

9000 8080

9000 8080

192.168.1.2

192.168.1.66

将网内其它设备访问本IP的 9000 端口转发到 8080端口,同网段内其它设备可以通过9000端口访问服务。

$ sudo iptables -t nat -A PREROUTING -p tcp --dport 9000 -j REDIRECT --to-port 8080

$ curl 192.168.1.66:9000

# Out put

Docker Rocks

Hello Docker learners!!!

Be careful. The last time I updated the packages in this app was April 2020.

将本机访问本机的 9000 端口转发到 8080端口,本机可以通过 9000端口访问服务。

sudo iptables -t nat -A OUTPUT -p tcp --dport 9000 -j REDIRECT --to-port 8080

curl 192.168.1.66:9000

# Out put

Docker Rocks

Hello Docker learners!!!

Be careful. The last time I updated the packages in this app was April 2020.

修改后的路由表如下:

$ sudo iptables -t nat -nL --line

Chain PREROUTING (policy ACCEPT)

num target prot opt source destination

1 DOCKER all -- 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL

2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9000 redir ports 8080

Chain INPUT (policy ACCEPT)

num target prot opt source destination

Chain OUTPUT (policy ACCEPT)

num target prot opt source destination

1 DOCKER all -- 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL

2 REDIRECT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:9000 redir ports 8080

Chain POSTROUTING (policy ACCEPT)

num target prot opt source destination

1 MASQUERADE all -- 172.17.0.0/16 0.0.0.0/0

2 MASQUERADE tcp -- 172.17.0.2 172.17.0.2 tcp dpt:8080

Chain DOCKER (2 references)

num target prot opt source destination

1 RETURN all -- 0.0.0.0/0 0.0.0.0/0

2 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:172.17.0.2:8080

本机端口转发到其它机器

#mermaid-svg-fKuPUUEYzyZtfA7l {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l .error-icon{fill:#552222;}#mermaid-svg-fKuPUUEYzyZtfA7l .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-fKuPUUEYzyZtfA7l .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-fKuPUUEYzyZtfA7l .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-fKuPUUEYzyZtfA7l .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-fKuPUUEYzyZtfA7l .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-fKuPUUEYzyZtfA7l .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-fKuPUUEYzyZtfA7l .marker{fill:#333333;stroke:#333333;}#mermaid-svg-fKuPUUEYzyZtfA7l .marker.cross{stroke:#333333;}#mermaid-svg-fKuPUUEYzyZtfA7l svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-fKuPUUEYzyZtfA7l .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l .cluster-label text{fill:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l .cluster-label span{color:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l .label text,#mermaid-svg-fKuPUUEYzyZtfA7l span{fill:#333;color:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l .node rect,#mermaid-svg-fKuPUUEYzyZtfA7l .node circle,#mermaid-svg-fKuPUUEYzyZtfA7l .node ellipse,#mermaid-svg-fKuPUUEYzyZtfA7l .node polygon,#mermaid-svg-fKuPUUEYzyZtfA7l .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-fKuPUUEYzyZtfA7l .node .label{text-align:center;}#mermaid-svg-fKuPUUEYzyZtfA7l .node.clickable{cursor:pointer;}#mermaid-svg-fKuPUUEYzyZtfA7l .arrowheadPath{fill:#333333;}#mermaid-svg-fKuPUUEYzyZtfA7l .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-fKuPUUEYzyZtfA7l .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-fKuPUUEYzyZtfA7l .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-fKuPUUEYzyZtfA7l .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-fKuPUUEYzyZtfA7l .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-fKuPUUEYzyZtfA7l .cluster text{fill:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l .cluster span{color:#333;}#mermaid-svg-fKuPUUEYzyZtfA7l div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-fKuPUUEYzyZtfA7l :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

9000

8080

192.168.1.2

192.168.1.85

192.168.1.66

将网内其它设备访问本IP的 9000 端口转发到另一台机器的 8080端口,同网段内其它设备可以通过9000端口访问服务。

$ sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.66:8080

$ sudo iptables -t nat -A POSTROUTING -p tcp -d 192.168.1.66 --dport 8080 -j SNAT --to-source 192.168.1.85

# 如果配置上述两个后还是上不了网,请检查filter FORWARD表

$ sudo iptables -nvL

# Out put,这里可以看到默认配置是丢弃

Chain FORWARD (policy DROP 813 packets, 42988 bytes)

# 修改对应网卡为接受

$ ip a

# Out put, 这里看到对应网卡为enp1s0

2: enp1s0: mtu 1500 qdisc mq state UP group default qlen 1000

link/ether ec:d6:8a:fa:c2:91 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.85/24 brd 192.168.1.255 scope global dynamic noprefixroute enp1s0

valid_lft 65632sec preferred_lft 65632sec

$ sudo iptables -A FORWARD -o enp1s0 -j ACCEPT

修改后的路由表如下:

$ sudo iptables -t nat -nL --line

Chain PREROUTING (policy ACCEPT)

num target prot opt source destination

1 DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:192.168.1.66:8080

Chain INPUT (policy ACCEPT)

num target prot opt source destination

Chain OUTPUT (policy ACCEPT)

num target prot opt source destination

Chain POSTROUTING (policy ACCEPT)

num target prot opt source destination

1 SNAT tcp -- 0.0.0.0/0 192.168.1.66 tcp dpt:8080 to:192.168.1.85

$ sudo iptables -t filter -nvL --line

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)

num pkts bytes target prot opt in out source destination

Chain FORWARD (policy DROP 813 packets, 42988 bytes)

num pkts bytes target prot opt in out source destination

1 52 5506 ACCEPT all -- * enp1s0 0.0.0.0/0 0.0.0.0/0

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)

num pkts bytes target prot opt in out source destination

三、固化iptables

这里只记录一种比较简单的方式,还有其它方式可以自行搜索。

$ sudo apt-get install iptables-persistent

/etc/iptables$ tree -f

# 打印输出,可以看到这里多了一个/etc/iptables的文件夹

.

├── ./rules.v4

└── ./rules.v6

如果需要更新调用下边的命令:

# 保存ipv4路由信息

$ sudo iptables-save -f /etc/iptables/rules.v4

# 保存ipv6路由信息

$ sudo ip6tables-save -f /etc/iptables/rules.v6

如果需要重启恢复初始路由或卸载,使用下边命令:

$ sudo apt-get remove iptables-persistent

$ sudo rm -rf /etc/iptables/

总结

如果是云服务器,对应上述情况建立ip转发即可,例如samba转发tcp 445 139端口,udp 137 138端口,通过iptables -p参数切换udp/tcp。4567

debian/ubuntu/windows配置wiregurad内网服务器(包含掉线自启动) ↩︎ iptables配置NAT实现端口转发与ss命令的讲解 ↩︎ 如何建立并使用docker ↩︎ Linux iptables 防火墙软件命令详解 ↩︎ 在Linux上打开端口 ↩︎ linux查看服务器开放的端口和启用的端口五种方式 ↩︎ 利用iptables实现端口转发 ↩︎

推荐链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: