Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iptables NAT and Masquerade rules - what do they do?

i've followed a tutorial (in german) on setting up a WiFi Router (Access Point) on a Raspberry Pi. Following the tutorial i had to add the following iptable rules:

iptables -A FORWARD -o eth0 -i wlan0 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Unfortunately i dont have any experience with iptables and would like to know what the rules mean/do?

like image 832
Darellon Avatar asked Oct 30 '25 03:10

Darellon


1 Answers

I go through the rules, and explain each at once: for understanding the flow, refer to the iptables chart

iptables -A FORWARD -o eth0 -i wlan0 -m conntrack --ctstate NEW -j ACCEPT

In the FORWARD chain, you appended a rule which says: if any packet comes newly, from wlan0 to eth0, the filter lets it pass, and tracks that connection as NEW (which means: follows its change of state).

iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

For any packets coming, tracked as ESTABLISHED or RELATED, the filter lets it pass

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

For the NAT table (which contains the FORWARD chain), in the POSROUTING chain, any packet leaving eth0 forgets its inner IP address (so, stays behind a NAT), and gets the one of eth0: MASQUERADE stands for masking the address.

like image 84
Ariel Otilibili Avatar answered Nov 03 '25 00:11

Ariel Otilibili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!