Configuring port mapping on Linux

To configure port mapping on Linux, you can use the iptables tool.

Here are the steps to configure port mapping:

  1. Check if iptables is already installed. Run the following command in the terminal:
  2. What is the version of iptables installed on the system?
  3. If it is not installed, you can use the following command to install iptables:
  4. Update the system using the following commands:
    sudo apt-get update
    sudo apt-get install iptables
  5. You need to determine the network interface for port mapping. You can use the following command to view the network interface list:
  6. Show the network configuration.
  7. Find the name of the network interface for which port mapping needs to be done, such as eth0 or ens33.
  8. Open port mapping function by running the following command:
  9. Please enable IPv4 forwarding by running the command “sudo sysctl net.ipv4.ip_forward=1”.
  10. Create port mapping rules using the iptables command. Here is an example command:
  11. Use the following command to redirect traffic coming in from a specific external port on a network interface to a designated IP address and port: sudo iptables -t nat -A PREROUTING -p tcp –dport -i -j DNAT –to-destination :
  12. In this process, refers to the specific external port number being mapped, represents the name of the network interface being mapped (obtained in step 2), is the IP address that is mapped to, and is the designated port number being mapped to.
  13. For example, to map the external port 8080 to port 80 on the local 192.168.1.100, you can execute the following command:
  14. Elevate privileges to modify the network table with the following command: redirect incoming TCP traffic on port 8080 from eth0 to destination IP address 192.168.1.100 on port 80.
  15. Save the iptables rules so that they will still take effect after a restart. Execute the following command:
  16. Execute the command “iptables-save” as superuser and save the output to the file “/etc/iptables.rules”.
  17. After rebooting the system, automatically load the iptables rules. Open the /etc/network/interfaces file and add the following line:
  18. Load the iptables rules from /etc/iptables.rules before bringing up the network connection.
  19. Restart the network service to apply the configuration. Execute the following command:
  20. Please restart the networking service using systemctl.

The port mapping configuration should now be in effect. External networks can access the target IP address and port of the internal network by accessing the designated external port.

bannerAds