How to share internet via iptables? You need
hardware (e. g. workstation) with two network interface cards (NIC).
They are may be wired or wireless. It doesn't matter.
Names of your NICs you may see via "ip a" or "ifconfig" command.
For this example we named their "eth0" and "eth1":
- eth0 = the network adapter with internet (external or WAN).
- eth1 = the network adapter to which a second computer is attached (internal or LAN).
192.168.0.x = IP subnet for eth1.
But first we need enable packet forwarding in operating system's kernel. Add in bottom of /etc/sysctl.conf file this line:
net.ipv4.ip_forward=1
sudo sysctl -p
sudo ip addr add 192.168.0.1/24 dev eth1It will be gateway for your 192.168.0.1/24 network.
sudo iptables -A FORWARD -o eth0 -i eth1 -s 192.168.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
sudo iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -t nat -F POSTROUTING
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE