[Linux] Enable the IP forwarding function of the Linux operating system

To enable the IP forwarding function of the Linux operating system, you can follow the steps below:

  1. Temporarily enable: If you only want to temporarily enable the IP forwarding function, you can use the following command:

    sudo sysctl -w net.ipv4.ip_forward=1
  2. Permanently enabled: If you want to enable the IP forwarding function permanently, you need to edit the configuration file so that it will still take effect when the system is restarted.

    • Edit /etc/sysctl.conffile:

      sudo nano /etc/sysctl.conf
      
    • Add the following line at the end of the file to enable the IP forwarding function:

      net.ipv4.ip_forward=1
      
    • Save and close the file. ps : Ctrl+o, the file name has been modified, Ctrl+x save and exit the current file

    • Make the changes take effect:

      sudo sysctl -p
      
  3. Use firewalld or iptables to set forwarding rules: In addition to enabling the IP forwarding function, if you need to forward network addresses, you also need to set forwarding rules. You can use firewalld or iptables to set it up. The specific steps vary depending on the operation scenario and needs.

    • Use firewalld:

      sudo firewall-cmd --add-masquerade --permanent sudo firewall-cmd --reload
    • Use iptables:

      sudo iptables -t nat -A POSTROUTING -o 出口网卡 -j MASQUERADE
      
    • After the modification is completed, press Ctrl + Oand this will display a prompt asking you if you want to save the edited content. Press Enterthe key to confirm saving.

    • After confirming saving, you can modify the file name (if necessary) and press Enterthe key.

    • Finally, press Ctrl + Xto exit nanothe editor.

    Please choose the appropriate method based on your network settings and needs, and ensure that network security and configuration correctness are taken into account when setting up forwarding functions and rules.

Guess you like

Origin blog.csdn.net/Your_Boy_Tt/article/details/132396163