建一个网站都需要什么,网络公司运营是做什么的,创建网站的一般步骤,网站建设用cmsiptables转发tcp/udp端口请求 文章目录 前言一、路由转发涉及点二、转发如何配置本机端口转发到本机其它端口本机端口转发到其它机器 三、固化iptables总结 前言
路由转发是计算机网络中的一种重要概念#xff0c;特别是在网络设备和系统之间。它涉及到如何处理和传递数据包特别是在网络设备和系统之间。它涉及到如何处理和传递数据包以及决定数据包应该发送到哪个网络设备或路由表中的哪条路径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
htmlheadtitleDocker Rocks/titlelink relstylesheet hrefhttps://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css//headbodydiv classcontainerdiv classjumbotronh1Hello Docker learners!!!/h1pBe careful. The last time I updated the packages in this app was April 2020./pp /p/div/div/body/html将本机访问本机的 9000 端口转发到 8080端口本机可以通过 9000端口访问服务。
sudo iptables -t nat -A OUTPUT -p tcp --dport 9000 -j REDIRECT --to-port 8080curl 192.168.1.66:9000
# Out put
htmlheadtitleDocker Rocks/titlelink relstylesheet hrefhttps://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css//headbodydiv classcontainerdiv classjumbotronh1Hello Docker learners!!!/h1pBe careful. The last time I updated the packages in this app was April 2020./pp /p/div/div/body/html修改后的路由表如下
$ 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 8080Chain 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 8080Chain 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:8080Chain 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: BROADCAST,MULTICAST,UP,LOWER_UP mtu 1500 qdisc mq state UP group default qlen 1000link/ether ec:d6:8a:fa:c2:91 brd ff:ff:ff:ff:ff:ffinet 192.168.1.85/24 brd 192.168.1.255 scope global dynamic noprefixroute enp1s0valid_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:8080Chain 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实现端口转发 ↩︎