How to use firewall iptables strategy to forward ports on Linux servers

How to use firewall iptables strategy to forward ports on Linux servers

Forwarding between two different servers

Enable port forwarding

First, enable the IP forwarding function, which is disabled by default.

Temporary modification:

[root@localhost ~]# echo 1 > /proc/sys/net/ipv4/ip_forward

The modification will take effect immediately, but will return to the default value of 0 after the system is restarted.

Permanent modification:

vi /etc/sysctl.conf

# Find the following value and change 0 to 1

net.ipv4.ip_forward = 1

# sysctl -p (make it take effect immediately)

The default value 0 disables IP forwarding, and changing it to 1 enables the IP forwarding function.

Configure port forwarding

Suppose when a user accesses 115.29.112.119:8804 , I want it to be forwarded to 42.99.16.84:8890

First, open port 8804 on the server

Modify the configuration file: vim /etc/sysconfig/iptables

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8804 -j ACCEPT

Or execute and save from command line

[root@localhost sysconfig]# iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8804 -j ACCEPT
[root@localhost sysconfig]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[OK] 

Port forwarding

Method 1: Command line execution

Need to save, otherwise it will only take effect immediately, and the firewall rules will be cleared after restart

iptables -t nat -A PREROUTING -d 115.29.112.119 -p tcp --dport 8804 -j DNAT --to-destination 42.99.16.84:8890

iptables -t nat -A POSTROUTING -d 42.99.16.84 -p tcp --dport 8890 -j SNAT --to-source 115.29.112.119

The command to save without changing the configuration file: service iptables save

Method 2: Save directly to the configuration file

Modify the /etc/sysconfig/iptables configuration file and add rules

-A PREROUTING -d 115.29.112.119 -p tcp --dport 8804 -j DNAT --to-destination 42.99.16.84:8890

-A POSTROUTING -d 42.99.16.84 -p tcp --dport 8890 -j SNAT --to-source 115.29.112.119

After configuration, restart the firewall:

1.systemctl restart iptables (centos7); 2.service iptables restart (centos7 and previous versions)

View the configured policies

iptables -t nat --list --line-numbers 

Local port forwarding

If you only need to forward ports between different machines, it is relatively easy. For example, if I visit http://ip:8888 and want to return the content of http://ip:6666, the configuration is as follows:

[root@localhost ~]# iptables -t nat -A PREROUTING -p tcp --dport 8888 -j REDIRECT --to-ports 6666
[root@localhost ~]# service iptables save
[root@localhost ~]# service iptables restart

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Linux firewall status check method example
  • How to configure Linux firewall and open ports 80 and 3306
  • A brief analysis of Linux to check the firewall status and the status of the ports open to the outside world
  • How to modify firewall configuration in Linux system
  • How to check if the firewall is turned off in Linux

<<:  Detailed explanation of WeChat Mini Program official face verification

>>:  Database query optimization: subquery optimization

Recommend

A detailed discussion on detail analysis in web design

In design work, I often hear designers participati...

MySQL 8.0.16 installation and configuration graphic tutorial under macOS

This article shares the installation and configur...

Solution to the problem of saving format in HTML TextArea

The format of textarea can be saved to the databas...

Detailed method of using goaccess to analyze nginx logs

Recently I want to use goaccess to analyze nginx ...

HTML n ways to achieve alternate color code sample code

This article mainly introduces the sample code of...

How to use .htaccess to prohibit a certain IP from accessing the website

Preface For cost considerations, most webmasters ...

HTML markup language - reference

Click here to return to the 123WORDPRESS.COM HTML ...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

MySQL explain obtains query instruction information principle and example

explain is used to obtain query execution plan in...

CSS animation property usage and example code (transition/transform/animation)

During development, a good user interface will al...

Example code for converting Mysql query result set into JSON data

Mysql converts query result set into JSON data Pr...

How to connect to MySQL remotely through Navicat

Using Navicat directly to connect via IP will rep...

How to generate PDF and download it in Vue front-end

Table of contents 1. Installation and introductio...