{"id":151,"date":"2017-10-21T06:53:00","date_gmt":"2017-10-21T06:53:00","guid":{"rendered":"https:\/\/linuxadmin.melberi.com\/uncategorized\/iptables-nat-how-to-port-forwarding"},"modified":"2017-10-31T07:03:19","modified_gmt":"2017-10-31T07:03:19","slug":"iptables-nat-port-forwarding","status":"publish","type":"post","link":"https:\/\/www.melberi.com\/linuxadmin\/iptables\/iptables-nat-port-forwarding","title":{"rendered":"Linux iptables NAT Port forwarding Masquerade Port Redirection Configuration"},"content":{"rendered":"<p>Linux iptables NAT, Port forwarding, Masquerade, Port Redirection Configuration for SendOS Fedora Redhat Suse Ubuntu<\/p>\n<p>If your have two network cards eth0, eth1<br \/>\nlets assume<br \/>\neth0 is connected to local network<br \/>\neth1 is connected to public network(or ppp0)<\/p>\n<h3><u><b>Masquerading<\/b><\/u><\/h3>\n<p><b>1)To enable nat on eth0 for all local network users<\/b><\/p>\n<p><b>If you are running iptables service,\u00a0 use the following method <\/b><\/p>\n<p><b>#echo 1 &gt; \/proc\/sys\/net\/ipv4\/ip_forward<br style=\"background-color: #cccccc;\" \/>#iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE <\/b><br \/>\nTo save this changes<br \/>\n<b>#iptables-save &gt; \/etc\/sysconfig\/iptables<\/b><br \/>\n<b>or <\/b><b>\u00a0<\/b><br \/>\n<b>#service iptables save <\/b><b> <\/b><br \/>\n<b>#service iptables restart <\/b><\/p>\n<p>This will enable Masquerade. Now you can configure eth0 as the gateway for local network.<\/p>\n<p><b>If you are not running iptables service<\/b><br \/>\nedit \/etc\/rc.local and add the below lines<\/p>\n<p><b>echo 1 &gt; \/proc\/sys\/net\/ipv4\/ip_forward<br style=\"background-color: #cccccc;\" \/>iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE <\/b><\/p>\n<p>Then\u00a0 execute the .\/rc.local file<\/p>\n<p><b>2) To enable nat on eth0 for specified network user ips<\/b><\/p>\n<p><b>#echo 1 &gt; \/proc\/sys\/net\/ipv4\/ip_forward<br style=\"background-color: #cccccc;\" \/>#iptables -t nat -A POSTROUTING -s x.x.x.x(ip of local user system1) -o eth1 -j MASQUERADE<br style=\"background-color: #cccccc;\" \/>#iptables -t nat -A POSTROUTING -s x.x.x.x(ip of local user system2) -o eth1 -j MASQUERADE <br style=\"background-color: #cccccc;\" \/>#iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\u00a0 <\/b><\/p>\n<h3><u><b>Port Redirection<\/b><\/u><\/h3>\n<p><b>1)To redirect external 80 port traffic to 3128 port<\/b><\/p>\n<p><b style=\"background-color: #cccccc;\">#iptables -t nat -A PREROUTING -i eth1 -p tcp &#8211;dport 80 -j REDIRECT &#8211;to-port 3128 <\/b><\/p>\n<p><b>2)To redirect external rdp port traffic to any local systems rdp port. <\/b><\/p>\n<p><b>#iptables -t nat -A PREROUTING -t nat -p tcp -d x.x.x.x(eth1 ip) &#8211;dport 3389 -j DNAT &#8211;to x.x.x.x(ip of any local network system):3389 <br style=\"background-color: #cccccc;\" \/>\u00a0or<br style=\"background-color: #cccccc;\" \/>#iptables -t nat -A PREROUTING -t nat -p tcp -d x.x.x.x(eth1 ip) &#8211;dport 3382 -j DNAT &#8211;to x.x.x.x(ip of any local network system):3389<\/b><\/p>\n<p>Based on your requirement, you can change the port and ip<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux iptables NAT, Port forwarding, Masquerade, Port Redirection Configuration for SendOS Fedora Redhat Suse Ubuntu If your have two network cards eth0, eth1 lets assume eth0 is connected to local&#8230; <a href=\"https:\/\/www.melberi.com\/linuxadmin\/iptables\/iptables-nat-port-forwarding\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104,140],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/posts\/151"}],"collection":[{"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":3,"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/posts\/151\/revisions\/779"}],"wp:attachment":[{"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.melberi.com\/linuxadmin\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}