a copy of my answer to the identical in essence, but a different question:
if on the computers in the subnet the default gateway indicates the use of the server you are configuring, then it is sufficient:
Allow packet transfer between interfaces on this server:
$ echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
in order for this setting to be applied even after a reboot, uncomment (or add, if not) the line in /etc/sysctl.conf :
net.ipv4.ip_forward=1
- check that these packets are not distorted or blocked by netfilter (see the output of
$ sudo iptables-save ). add a netfiler rule for replacing the sender's address for packets arriving from the “internal” interface and going to the external interface:
$ sudo iptables -t nat -A POSTROUTING -o внешний_интерфейс -j MASQUERADE
if, on these computers, the default gateway is another machine (or no one is specified at all), then, in addition to allowing the transfer of packets between interfaces, you will have to “distort” these packets.
at a minimum, a source address must be substituted for packets arriving at the “external” interface and addressed to computers on the subnet, as well as a reverse substitution of the destination address for return packets.
Both of these actions can be performed by a netfilter directive called snat ( source nat ). An example of its addition using the iptables program:
$ sudo iptables -t nat -A POSTROUTING -o внутренний_интерфейс -j SNAT --to-source ip-адрес
where ip-адрес is the address assigned to the “internal” interface.
In order not to enter one or two commands mentioned above ( iptables ... ) after each restart of the configured server, you can add them, for example, at the end of the /etc/rc.local file, but before the line exit 0 , which is usually present there (if not, just add to the end of the file). the addition of sudo , of course, is not needed in this file.