当前位置:网站首页>How to configure iptables to realize local port forwarding
How to configure iptables to realize local port forwarding
2022-04-23 01:57:00 【nginx】
scene
If you're using resin Debug one Web Program , Need to restart frequently resin. This Web The program needs to be on 80 On port , and Linux Limit 1024 The following ports must have root Permission to open . But you don't want to always turn on one when adjusting the program root terminal . under these circumstances , You can take resin On default 8080 On port , And then use iptables To realize and really open the service in 80 The same effect on the port .
Method
Will work with 80 Port of TCP Connect to local 8080 On port . Use DNAT (Destination Network Address Translation) Technology can meet this requirement . because iptables The method of dealing with local connection and remote connection is different , So it needs to be dealt with separately . Let's assume that IP yes 192.168.4.177.
Remote connection
Remote connection refers to the connection of another machine to this machine . The packets of this connection are in iptables Will go through first PREROUTING chain , So just in PREROUTING Chain work DNAT.
The code is as follows :
Local connection
# iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.4.177 --dport 80 -j DNAT --to 192.168.4.177:8080
Local connection refers to the local connection , use 127.0.0.1 Or this machine IP To access the port of this machine . Locally connected packets will not pass through the network card , Instead, it is processed by the kernel and sent directly to the local process . This kind of packet is in iptables Only through OUTPUT chain , Instead of going through PREROUTING chain . So you need to be in OUTPUT In the chain DNAT. Except yes 127.0.0.1 outside , For this machine IP ( namely 192.168.4.177) Access to is also a local connection .
The code is as follows :
matters needing attention
# iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j DNAT --to 127.0.0.1:8080# iptables -t nat -A OUTPUT -p tcp -d 192.168.4.177 --dport 80 -j DNAT --to 127.0.0.1:8080
You may need to open... With the following command IP forward :
The code is as follows :
During the test , If you want to reset iptables, You need to empty... First nat surface :
# echo 1 > /proc/sys/net/ipv4/ip_forward
The code is as follows :
Example operation
# iptables -F -t nat
Here will be the local interface IP 61.144.a.b Of 3389 port Forwarding to 116.6.c.d Of 3389 ( Mainly visit 61.144.a.b Of 3389 port , It will jump to 116.6.c.d Of 3389)
1、 The first thing to do is /etc/sysctl.conf Profile's net.ipv4.ip_forward = 1 The default is 0 This allows iptalbes FORWARD.
2、 service iptables stop Turn off firewall
3、 Reconfigure rules
The code is as follows :
Save the current rule to /etc/sysconfig/iptables
iptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 3389 -j DNAT --to-destination 116.6.c.d:3389iptables -t nat -A POSTROUTING --dst 116.6.c.d -p tcp --dport 3389 -j SNAT --to-source 61.144.a.bservice iptables save
If you are familiar with this file, directly modifying the content here is also equivalent to command-line input rules .
5、 start-up iptables service , service iptables start
Can be written into a script , The equipment starts automatic operation ;
The code is as follows :
another :iptables The location of the configuration file :/etc/sysconfig/iptables If the Internet address changes, just modify it in the configuration file .
# vi /etc/rc.local#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.</p> <p>touch /var/lock/subsys/local</p> <p>sh /root/myshipin.log---------------------------------------------------------------------vi myshipin.log#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.</p> <p>iptables -F -t natiptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 3389 -j DNAT --to-destination 116.6.c.d:3389iptables -t nat -A POSTROUTING --dst 116.6.a.b -p tcp --dport 3389 -j SNAT --to-source 61.144.c.d~----------------------------------------------------------------TCP</p> <p>iptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 9304 -j DNAT --to-destination 10.94.a.b:9304iptables -t nat -A POSTROUTING --dst 10.94.a.b -p tcp --dport 9304 -j SNAT --to-source 61.144.a.b</p> <p>UDPiptables -t nat -A PREROUTING --dst 61.144.a.b -p udp --dport 9305 -j DNAT --to-destination 10.94.a.b:9305iptables -t nat -A POSTROUTING --dst 10.94.a.b -p udp --dport 9305 -j SNAT --to-source 61.144.a.b
版权声明
本文为[nginx]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204011159186925.html
边栏推荐
- Sqlserver data transfer to MySQL
- What should I pay attention to when using proxy IP?
- 2022.4.10-----leetcode. eight hundred and four
- Keil MDK Chinese garbled code, two solutions, the font is no longer ugly
- [tutorial] how to use GCC "zero assembly" for white whoring MDK
- W801/W800/W806唯一ID/CPUID/FLASHID
- How can e-procurement become a value-added function in the supply chain?
- Is CICC fortune a state-owned enterprise and is it safe to open an account
- After disk D is distributed to Disk C, what should I do if the database recovery hangs? Please answer
- 2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
猜你喜欢

浅析一下隧道代理IP的优缺点。

Micro build low code zero foundation introductory course

有哪些业务会用到物理服务器?

Is it better to use a physical machine or a virtual machine to build a website?

Analyze the three functions of static proxy IP.

Summary of LSF usage

Cc2541 emulator CC debugger tutorial

JSP basic knowledge summary

How to set computer IP?

J-Link RTT使用
随机推荐
What is BGP server and what are its advantages?
力扣(LeetCode)112. 路径总和(2022.04.22)
揭秘被Arm编译器所隐藏的浮点运算
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
What is a proxy IP pool and how to build it?
Implementation of Base64 encoding / decoding in C language
How to write the resume of Software Test Engineer so that HR can see it?
W801 / w800 WiFi socket development (I) - UDP
Shardingsphere broadcast table and binding table
什么是api接口?
使用代理IP是需要注意什么?
2022 low voltage electrician examination questions and answers
Shardingsphere introduction and sub table usage
Solve the problem when installing MySQL
Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
The leader / teacher asks to fill in the EXCEL form document. How to edit the word / Excel file on the mobile phone and fill in the Excel / word electronic document?
postman里面使用 xdebug 断点调试
Performance introduction of the first new version of cdr2022
How to choose a good dial-up server?
Quel est le fichier makefile?