IP forwarding with the internal network connected to the Internet on Linux

IP forwarding concept:

Linux machine so as to transmit data from one network to another as a router. Therefore, it can act as a router or proxy server, to achieve a connection of Internet or network connection sharing to multiple client machines.

1. Enable IPv4 Forwarding

First, we need to enable IPv4 forwarding on our Linux operating system. To do this, we need to use the root user to perform the following command at a shell or terminal.

#echo 1 > /proc/sys/net/ipv4/ip_forward

Temporary IP Forwarding

NOTE: The above command ip forwarding can be enabled immediately, but only temporary, until the next reboot. To permanently enabled, we need to use our favorite text editor to open /etc/sysctl.conf file.

#vim /etc/sysctl.conf

Then, increase net.ipv4.ip_forward = 1 to file or delete a comment that line, save the file and exit.

net.ipv4.ip_forward= 1

Run the following command to enable the change.

#sysctl -p /etc/sysctl.conf

 

2. Configure Iptables firewall

We need to allow a certain (or all) of the data packets through routers our. Prior to this, we need to know the name of the interface connected to our network of Linux devices. We can get the name of the interface by running the following command in the terminal or shell.

#ifconfig -a

Here, in our machines, eth0 is connected to the Internet or a network interface card, wlan2 we want to use iptables to forward packets from eth0 interface. To achieve forward, we need to run the following command.

#iptables -A FORWARD -i wlan2 -o eth0 -j ACCEPT

Note: Please replace eth0 with the available equipment and wlan2 name of your Linux machine.

Now, due to the netfilter / iptables is a stateless firewall, we need to let iptables allows the connection has been established through. To do this, we have to run the following command.

 

3. Configure NAT

Then, finally, we need to modify the source address of a packet transmitted over the Internet is eth0 by executing the following command.

#iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Guess you like

Origin www.cnblogs.com/qxfy/p/12022545.html