{"id":1903,"date":"2018-10-28T14:45:00","date_gmt":"2018-10-28T14:45:00","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1903"},"modified":"2018-10-31T03:09:39","modified_gmt":"2018-10-31T03:09:39","slug":"iptables-introduction-and-examples-linuxadmin-io","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/28\/iptables-introduction-and-examples-linuxadmin-io\/","title":{"rendered":"Iptables Introduction and Examples &#8211; LinuxAdmin.io"},"content":{"rendered":"<p>Iptables is a firewall service included in CentOS, in CentOS 7 its offered as a alternative firewalld is offered as well. Iptables uses netfilter to filter chains. Essentially you create a chain of filter rules to process how incoming and outgoing data is handled. You can view more about iptables on <a href=\"http:\/\/ipset.netfilter.org\/iptables.man.html\">Netfilter<\/a><\/p>\n<h2>Iptables rule format<\/h2>\n<p>The iptables rules format is pretty simplistic when using basic rules to allow or deny traffic.<\/p>\n<p>iptables -t &lt;type&gt; &lt;direction&gt; &lt;pattern&gt; -j &lt;action&gt;<\/p>\n<h3>Type<\/h3>\n<p>for -t &lt;type&gt; there are two basic type options filter and nat<\/p>\n<p>filter \u2013 creates a rule for filtering traffic.<br \/>\nnat \u2013 this creates a nat(network address translation) rule.<\/p>\n<h3>Direction<\/h3>\n<p>\u2013append \u2013 Adds a rule to the end of the chain. You also want to specify INPUT (incoming packets) or OUTPUT (outgoing packets) when appending rules.<br \/>\n\u2013delete \u2013 Deletes a rule from the chain. You also want to specify INPUT (incoming packets) or OUTPUT (outgoing packets) when deleting rules.<br \/>\n\u2013list \u2013 lists the current rules<br \/>\n\u2013flush \u2013 flushes all the rules<\/p>\n<h3>Pattern<\/h3>\n<p>\u2013source &lt;ip_address&gt; \u2013 Rule only applies to packets coming from this source IP address.<br \/>\n\u2013destination &lt;ip_address&gt; \u2013 Rule only applies for packets going to this destination IP address.<\/p>\n<h3>Action<\/h3>\n<p>DROP \u2013 packets are dropped<br \/>\nREJECT \u2013 packets are dropped and a error message sent back<br \/>\nACCEPT \u2013 packets are allowed<\/p>\n<h2>Iptables Service Management<\/h2>\n<p>How to manage the IPtables service itself.<\/p>\n<h4>To start iptables:<\/h4>\n<p>service iptables start<\/p>\n<h4>To stop iptables:<\/h4>\n<p>service iptables stop<\/p>\n<h4>To ensure iptables starts on reboot:<\/h4>\n<p>CentOS 6:<\/p>\n<p>chkconfig &#8211;add iptableschkconfig iptables on<\/p>\n<p>Centos 7:<\/p>\n<p>systemctl enable iptables<\/p>\n<h4>Restore saved ruleset:<\/h4>\n<p>iptables-restore &lt; \/etc\/sysconfig\/iptables<\/p>\n<h4>Save new rules permamently:<\/h4>\n<p>iptables-save &gt; \/etc\/sysconfig\/iptables<\/p>\n<h3>Example rules:<\/h3>\n<p>Samples of different functions you can perform to block or accept traffic based on IP addresses and Ports.<\/p>\n<p>Block a IP with Iptables:<\/p>\n<p>iptables -A INPUT -s 1.2.3.4 -j DROP<\/p>\n<p>Allow a IP<\/p>\n<p>iptables -A INPUT -s 1.2.3.4 -j ACCEPT<\/p>\n<p>Block a PORT:<\/p>\n<p>iptables -A INPUT -p tcp &#8211;dport 21 -j DROP<\/p>\n<p>Allow a IP to a specific port:<\/p>\n<p>iptables -A INPUT -s 1.2.3.4 -p tcp &#8211;dport 21 -j ACCEPT<\/p>\n<p>There is much more that you can do with iptables this is just a basic introduction.<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/iptables\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Iptables is a firewall service included in CentOS, in CentOS 7 its offered as a alternative firewalld is offered as well. Iptables uses netfilter to filter chains. Essentially you create a chain of filter rules to process how incoming and outgoing data is handled. You can view more about iptables on Netfilter Iptables rule format &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/28\/iptables-introduction-and-examples-linuxadmin-io\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Iptables Introduction and Examples &#8211; LinuxAdmin.io&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1903","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1903"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1903\/revisions"}],"predecessor-version":[{"id":2026,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1903\/revisions\/2026"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}