How to set up port forwarding on Linux

PHPz
Release: 2023-07-05 15:48:10
Original
7035 people have browsed it

How to set up port forwarding on Linux

Port forwarding is a very important function in various network applications. It allows you to forward external traffic to specific ports on your internal network. On Linux systems, the port forwarding function is implemented by using iptables and sysctl. This article explains how to set up port forwarding on Linux and provides corresponding code examples.

  1. Check the kernel parameters

Before starting to set up port forwarding, we first need to check whether the kernel parameters allow the forwarding function. By running the following command, you can view the forwarding settings of the current system:

sysctl net.ipv4.ip_forward
Copy after login

If the output result is net.ipv4.ip_forward = 1, it means that the forwarding function has been enabled. If the output result is net.ipv4.ip_forward = 0, it means that the forwarding function is not enabled. When the forwarding function is not enabled, you can temporarily enable the forwarding function by running the following command:

sysctl -w net.ipv4.ip_forward=1
Copy after login

If you need to permanently enable the forwarding function, you can edit the /etc/sysctl.conf file, And add or modify the following parameters:

net.ipv4.ip_forward=1
Copy after login

After modification, save the file and run the following command to make it effective:

sysctl -p
Copy after login
  1. Set port forwarding

There are many ways to implement port forwarding. Below we will introduce two common ways: using iptables and using the socat tool.

A. Using iptables

iptables is a commonly used firewall tool on Linux systems. We can use it to set up port forwarding. The following is a sample code to set up port forwarding through iptables:

iptables -t nat -A PREROUTING -p tcp --dport <external_port> -j DNAT --to-destination <internal_ip>:<internal_port>
iptables -t nat -A POSTROUTING -j MASQUERADE
Copy after login

where <external_port> is the external port, <internal_ip> is the IP address of the internal server, <internal_port> is the port number of the internal server. These two commands will forward external traffic to the internal server. If you want the source IP address to be correctly identified after external traffic is forwarded, you can add the following command:

iptables -t nat -A POSTROUTING -o <external_interface> -j MASQUERADE
Copy after login

where<external_interface> is the name of the external interface, such as eth0.

B. Use the socat tool

Socat is a powerful network tool that can perform various network connections and forwarding. The following is a sample code to set up port forwarding through socat:

socat TCP-LISTEN:<external_port>,fork TCP:<internal_ip>:<internal_port>
Copy after login

where <external_port> is the external port, <internal_ip> is the IP address of the internal server, <internal_port> is the port number of the internal server. This command will forward external traffic to the internal server.

  1. Apply forwarding rules

Whether you use iptables or socat tool, the forwarding rules set are only valid in the current session. If you want the rules to remain valid after a system restart, you need to apply these rules to the system.

A. Using iptables

You can apply iptables rules to the system by running the following command:

iptables-save > /etc/sysconfig/iptables
Copy after login

B. Using the socat tool

socat tool default Running in the background, if you want the socat rules to remain valid after the system restarts, you can add the socat configuration to the system startup script. For example, on an Ubuntu system you can edit the /etc/rc.local file and add the following content:

/path/to/socat TCP-LISTEN:<external_port>,fork TCP:<internal_ip>:<internal_port> &
Copy after login

Note that /path/to/socat needs to be replaced with The actual socat tool path.

Summary

This article describes how to set up port forwarding on a Linux system and provides code examples using the iptables and socat tools. By setting up port forwarding, you can flexibly forward external traffic to internal servers to achieve load balancing, port mapping and other functions for network applications.

The above is the detailed content of How to set up port forwarding on Linux. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template