Iptables setup masquerading for Linux firewall

Q. How do I setup masquerading for my Linux iptables firewall? What rules I need to apply or write?

ADVERTISEMENTS

A. Iptables is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. Several different tables may be defined. Each table contains a number of built-in chains and may also contain user-defined chains. However, to setup masquerading you need to use special target called MASQUERADE.
This target is only valid in the nat table, in the POSTROUTING chain. It should only be used with dynamically assigned IP (dialup) connections: if you have a static IP address, you should use the SNAT target.

You need to type following rule (or add to your script):

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

Also set net.ipv4.ip_forward to 1:

net.ipv4.ip_forward=1

See this previous article for more step by step instructions.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
2 comments… add one
  • Dipendra Apr 4, 2008 @ 19:18

    Hi

    i just want to know , how i configure firewall on RHEL 4 , can u give me step by step configuration .
    Looking forward

    Thanks & Regards
    Dipendra

  • amit Jul 19, 2013 @ 11:14

    can u send me links where i can get more knowlege about masquerading??????

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.