Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Port Mapping in Linux system

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to configure port mapping in the Linux system. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Aforementioned

Iptables is used for port mapping. If the system you are using is Centos/Redhat/Fedora, then you need to shut down the firewalld service to succeed.

For the relationship between firewall and iptables firewalls, please make your own Baidu.

Turn on IP forwarding

If IP forwarding is enabled (the router device is enabled by default), IP forwarding must be allowed, otherwise even if the nat mapping rule is set, mapping cannot occur, because packets cannot be sent without forwarding enabled.

Echo 1 > / proc/sys/net/ipv4/ip_forward maps from one machine to another

Mapping relationship 192.168.122.1 virtual 80-> 192.168.122.221 virtual 80

192.168.122.221 turns on the nginx server at port 80

Link-local messages do not need to go through PREROUTING and POSTROUTING, so OUTPUT must be configured or tests on the local machine will find that the forwarding rules are invalid!

The following rules are configured in 192.168.122.1, and then the machine on 192.168.122.1 (native) can access "192.168.122.1 virtual 80"

Iptables-t nat-An OUTPUT-p tcp-d 192.168.122.1-- dport 80-j DNAT-- to 192.168.122.221

The following rules are configured in 192.168.122.1, and then on 192.168.122.29 (other devices) you can access "192.168.122.1virtual 80"

# configure destination address translation iptables-t nat-A PREROUTING-p tcp-d 192.168.122.1-- dport 80-j DNAT-- to 192.168.122.221-- configure source address translation iptables-t nat-A POSTROUTING-p tcp-d 192.168.122.221-- dport 80-j SNAT-- to 192.168.122.1 from one port of this machine to another port

Mapping relationship 192.168.122.221 virtual 8080-> 192.168.122.221purl 80

Messages on the local link do not need to go through PREROUTING and POSTROUTING, so OUTPUT must be configured or tests on the local machine will find that the forwarding rules are invalid!

192.168.122.221 turns on the nginx server at port 80

In 192.168.122.221, configure the following rule, mapping 8080 to 80, so that the machine accesses "127.0.0.1 virtual 8080"

Iptables-t nat-An OUTPUT-p tcp-d 127.0.0.1-- dport 8080-j DNAT-- to-port 127.0.0.1 tcp 80

In 192.168.122.221, configure the following rule, which maps 8080 to 80, so that you can access "192.168.122.221 pur8080"

Iptables-t nat-A PREROUTING-p tcp-- dport 8080-j REDIRECT-- to-ports 80 other # View nat chain rule iptables-t nat-L-n-- line-number# delete rule POSTROUTING rule 1iptables-t nat-D POSTROUTING 1 on how to configure port mapping in Linux system, this article ends here. I hope the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report