Home  >  Article  >  Operation and Maintenance  >  How to set up port forwarding on Linux

How to set up port forwarding on Linux

PHPz
PHPzOriginal
2023-07-05 15:48:107031browse

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

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

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

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

sysctl -p
  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

where 749375b4434469ed7a5194cf26b2a289 is the external port, 6fccd0679a7f07f1f43815050eb515f2 is the IP address of the internal server, 63f256d5d64211b8417741a9944fd8a5 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

where881d938581b5738c7a57b1cbe4098023 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>

where 749375b4434469ed7a5194cf26b2a289 is the external port, 6fccd0679a7f07f1f43815050eb515f2 is the IP address of the internal server, 63f256d5d64211b8417741a9944fd8a5 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

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> &

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!

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