To enable the IP forwarding function of the Linux operating system, you can follow the steps below:
-
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
-
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.conf
file: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
-
-
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 + O
and this will display a prompt asking you if you want to save the edited content. PressEnter
the key to confirm saving. -
After confirming saving, you can modify the file name (if necessary) and press
Enter
the key. -
Finally, press
Ctrl + X
to exitnano
the 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.
-