Home  >  Article  >  Operation and Maintenance  >  What is port mapping in linux

What is port mapping in linux

青灯夜游
青灯夜游Original
2022-05-09 13:49:424450browse

Port mapping, also known as port forwarding, refers to mapping the port of the IP address of the external host to a computer in the intranet. When the user accesses this port of the external network IP, the server automatically maps the request to the corresponding port. On a machine inside the LAN; this can be achieved by routing an ADSL broadband router using a dynamic or fixed public network IP.

What is port mapping in linux

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

During the development process, due to network environment reasons, it is necessary to access some ports of other servers through a certain server. This involves the issue of Linux port mapping. On Linux servers, Linux port mappings below 1024 are prohibited from being used by non-root users. Therefore, if you want to use port 80 to access tomcat, modify the port of conf/server. way, such as mapping port 80 to 8080.

What is linux port mapping?

Linux port mapping, also known as port forwarding, maps the port of the IP address of an external host to a computer in the intranet to provide corresponding services.

When a user accesses this port of the external network IP, the server automatically maps the request to the machine inside the corresponding LAN.

Port mapping can be achieved by routing the ADSL Broadband Router using a dynamic or fixed public network IP. ADSL connects directly to a hub or switch to allow computers to share the Internet.

If a user accesses the port of a Linux host that provides a mapped port, the server will send a request to the host in the LAN that provides this specific service. Multiple ports on an external IP address machine can also be mapped to different ports on different computers in the internal network. Port mapping can also perform some specific proxy functions.

Port mapping is divided into dynamic and static. Dynamic port mapping: When a computer on the intranet wants to access a website, it will send a data packet to the NAT gateway. The header includes the IP and port of the other website and the local IP and port. The NAT gateway will replace the local IP and port with its own. public network IP and an unused port, and this mapping relationship will be recorded for future use in forwarding data packets.

How to do port mapping

You can use iptables for port mapping:

Step 1: Open Port mapping function:

  • Method one: (Allow packet forwarding)

sudo echo '1' > /proc/sys/net/ipv4/ip_forward
  • Method two:

vim /etc/sysctl.conf
将 ;net.ipv4.ip_forward = 0 这一行的注视去掉 , 并将 0 改为 1
修改后的结果为 : 
net.ipv4.ip_forward = 1

Step 2: Mapping:

  • DNAT

iptables -t nat -A PREROUTING -d 本机IP -p tcp --dport 本机端口 -j DNAT --to-destination 目标机IP:目标机端口
  • SNAT

iptables -t nat -A PREROUTING -d 本机IP -p tcp --dport 本机端口 -j SNAT --to-destination 目标机IP:目标机端口

Related recommendations: "Linux Video Tutorial"

The above is the detailed content of What is port mapping in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is rhce in linuxNext article:What is rhce in linux