{"id":4304,"date":"2019-12-11T06:40:39","date_gmt":"2019-12-11T06:40:39","guid":{"rendered":"https:\/\/cloudkul.com\/blog\/?p=4304"},"modified":"2019-12-11T06:51:32","modified_gmt":"2019-12-11T06:51:32","slug":"introduction-to-iptables-in-linux","status":"publish","type":"post","link":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/","title":{"rendered":"Introduction to IPtables in Linux"},"content":{"rendered":"<p>Managing network traffic is one of the toughest jobs a system administrator has to deal with. He must configure the firewall in such a way that it will meet the system and user&#8217;s requirements for both incoming and outgoing connections, without leaving the system vulnerable to attacks and all this can be done with the help of IPtables.<\/p>\n<p>&nbsp;<\/p>\n<p>IPtables is a Linux command line firewall that allows system administrators to manage incoming and outgoing traffic with the help of configurable table rules.<\/p>\n<p>&nbsp;<\/p>\n<p>There are 3 types of Iptables:<\/p>\n<p>&nbsp;<\/p>\n<ol>\n<li><strong>\u00a0FILTER<\/strong><\/li>\n<li><strong>\u00a0NAT<\/strong><\/li>\n<li><strong>\u00a0MANGLE<\/strong><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ul>\n<li>FILTER \u2013 It is the default table and it contains the following built-in chains:<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ol>\n<li style=\"text-align: left;\">INPUT<\/li>\n<li style=\"text-align: left;\">FORWARD<\/li>\n<li style=\"text-align: left;\">OUTPUT<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ul>\n<li>NAT \u2013 a table that is consulted when a packet tries to create a new connection. It has the following built-in chains:<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ol>\n<li>PREROUTING<\/li>\n<li>OUTPUT<\/li>\n<li>POSTROUTING<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<ul>\n<li>MANGLE \u2013 this table is used for packet altering. Until kernel version <strong>2.4<\/strong> this table had only two chains, but they are now 5:<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<ol>\n<li>PREROUTING<\/li>\n<li>OUTPUT<\/li>\n<li>INPUT<\/li>\n<li>POSTROUTING<\/li>\n<li>FORWARD<\/li>\n<\/ol>\n<h3><\/h3>\n<p>&nbsp;<\/p>\n<p>Mostly we play around with FILTER type of IPtables, Now, let\u2019s see some useful commands:<\/p>\n<p>&nbsp;<\/p>\n<p>List the currently configured IPtables rules:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">iptables -L<\/pre>\n<p>&nbsp;<\/p>\n<p>Sample output:-<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">Chain INPUT (policy ACCEPT)\r\ntarget     prot opt source               destination         \r\n\r\nChain FORWARD (policy ACCEPT)\r\ntarget     prot opt source               destination         \r\n\r\nChain OUTPUT (policy ACCEPT)\r\ntarget     prot opt source               destination<\/pre>\n<p>&nbsp;<\/p>\n<h2><\/h2>\n<p>&nbsp;<\/p>\n<h2><strong><span id=\"Step-2-Defining-Chain-Rules\">Defining Chain Rules<\/span><\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>Defining a rule means appending it to the chain. To do this, you need to insert the\u00a0<strong>-A<\/strong>\u00a0option (Append) right after the<strong>\u00a0iptables<\/strong>\u00a0command, like so:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"\">sudo iptables -A<\/pre>\n<p>&nbsp;<\/p>\n<p>It will alert\u00a0<strong>iptables<\/strong>\u00a0that you are adding new rules to a chain. Then, you can combine the command with other options, such as:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li><strong>-i<\/strong>\u00a0(<strong>interface<\/strong>) \u2014 the network interface whose traffic you want to filter, such as\u00a0<strong>eth0<\/strong>,\u00a0<strong>lo<\/strong>,\u00a0<strong>ppp0,<\/strong>\u00a0etc.<\/li>\n<li><strong>-p<\/strong>\u00a0(<strong>protocol<\/strong>) \u2014 the network protocol where your filtering process takes place. It can be either\u00a0<strong>tcp<\/strong>,\u00a0<strong>udp<\/strong>,\u00a0<strong>udplite<\/strong>,\u00a0<strong>icmp<\/strong>,\u00a0<strong>sctp<\/strong>,\u00a0<strong>icmpv6<\/strong>, and so on. Alternatively, you can type\u00a0<strong>all<\/strong>\u00a0to choose every protocol.<\/li>\n<li><strong>-s<\/strong>\u00a0(<strong>source<\/strong>) \u2014 the address from which traffic comes from. You can add a hostname or IP address.<\/li>\n<li><strong>\u2013dport<\/strong>\u00a0(<strong>destination port<\/strong>) \u2014 the destination port number of a protocol, such as\u00a0<strong>22<\/strong>\u00a0(SSH),\u00a0<strong>443\u00a0<\/strong>(https), etc.<\/li>\n<li><strong>-j<\/strong>\u00a0(<strong>target<\/strong>) \u2014 the target name (<strong>ACCEPT<\/strong>,\u00a0<strong>DROP<\/strong>,\u00a0<strong>RETURN<\/strong>). You need to insert this every time you make a new rule.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>1. Block Specific IP Address in IPtables Firewall<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If you want to block all outgoing connections to a specific IP address on all the ports with the following rule, <strong>-s\u00a0<\/strong>option used here specifies the source, the command will look like this:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"\">iptables -A INPUT -s xxx.xxx.xxx.xxx -j DROP<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>2. Unblock IP Address in IPtables Firewall<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If you want to unblock requests from specific IP address, you can delete the blocking rule with the following command:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">iptables -D INPUT -s xxx.xxx.xxx.xxx -j DROP<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>3. Block Specific Port on IPtables Firewall<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If you want to block incoming or outgoing connections on a specific port, below you can find such rule for both incoming and outgoing connections:<\/p>\n<p>&nbsp;<\/p>\n<p>To block outgoing connections on a specific port use:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">iptables -A OUTPUT -p tcp --dport xxx -j DROP<\/pre>\n<p>&nbsp;<\/p>\n<p>To allow incoming connections use:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables -A INPUT -p tcp --dport xxx -j ACCEPT<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>4. Allow Multiple Ports on IPtables using Multiport<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>We can allow or block multiple ports at once, by using <strong>multiport<\/strong>, the blocking rule with the following commands is given below:<\/p>\n<p>&nbsp;<\/p>\n<p>To allow incoming connections:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables -A INPUT  -p tcp -m multiport --dports 22,80,443 -j ACCEPT<\/pre>\n<p>&nbsp;<\/p>\n<p>To block incoming connections:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">iptables -A OUTPUT -p tcp -m multiport --sports 22,80,443 -j DROP<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>5. Block any specific website on IPtables Firewall<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>First, find the IP addresses used by that website:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">host cloudkul.com\r\ncloudkul.com has address 104.18.59.180<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">whois 104.18.59.180 | grep CIDR\r\nCIDR:           104.16.0.0\/12<\/pre>\n<p>&nbsp;<\/p>\n<p>You can then block that cloudkul.com network with:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables -A OUTPUT -p tcp -d 104.16.0.0\/12 -j DROP<\/pre>\n<p>&nbsp;<\/p>\n<p>Now, when you will try to access cloudkul.com in your browser, it will not be accessible.<\/p>\n<h2><\/h2>\n<p>&nbsp;<\/p>\n<h2><strong>6. Block Outgoing Mails through IPTables<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>if you want to block outgoing emails, you can block outgoing ports on SMTP ports, , the command will look like this:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables -A OUTPUT -p tcp -m multiport --dports 25,465,587 -j REJECT<\/pre>\n<p>&nbsp;<\/p>\n<h2><strong>7. Block Incoming Ping Requests through IPtables<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>if you want to block incoming ping requests, you can use the following command if you are connected with ethO network interface, in my case I am connected with wi-fi so I replaced ethO with wlp2s0 i.e wireless network interface.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables -A INPUT -p icmp -i eth0 -j DROP<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>8. Flush IPtables Firewall Chains<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If you want to flush your firewall chains, you can use:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables -F<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>9. Save IPtables Rules to a File<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If you want to save your firewall rules you can use the following to save and store your rules in a file:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">iptables-save &gt; iptablesbackup.rules<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h2><strong>10. Restore IPtables Rules from a File<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<p>If you want to restore your firewall rules you can use the following to restore your rules from a file:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">iptables-restore &lt; iptablesbackup.rules<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Iptables<\/strong> is a powerful firewall and important for every linux administrator to learn at least the basics of iptables. If you want to find more detailed information about iptables and its options it is highly recommended to read it\u2019s manual:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">man iptables<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 In case of any help or query, please <a href=\"http:\/\/cloudkul.com\/contact\/\" target=\"_blank\" rel=\"noopener noreferrer\">contact us<\/a>.<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Managing network traffic is one of the toughest jobs a system administrator has to deal <a class=\"text-primary\" title=\"read more\" href=\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[245,86],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Introduction to IPtables in Linux - Cloudkul<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introduction to IPtables in Linux - Cloudkul\" \/>\n<meta property=\"og:description\" content=\"Managing network traffic is one of the toughest jobs a system administrator has to deal [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloudkul\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-11T06:40:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-11T06:51:32+00:00\" \/>\n<meta name=\"author\" content=\"Nitin Kaushik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/\",\"url\":\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/\",\"name\":\"Introduction to IPtables in Linux - Cloudkul\",\"isPartOf\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\"},\"datePublished\":\"2019-12-11T06:40:39+00:00\",\"dateModified\":\"2019-12-11T06:51:32+00:00\",\"author\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/99e46a85b2ad2848198f6de2c90bcbc4\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cloudkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Introduction to IPtables in Linux\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#website\",\"url\":\"https:\/\/cloudkul.com\/blog\/\",\"name\":\"Cloudkul\",\"description\":\"Host your eCommerce Store on AWS with Optimized Performance\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cloudkul.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/99e46a85b2ad2848198f6de2c90bcbc4\",\"name\":\"Nitin Kaushik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fdc703824bce4bae43a834dfdf641f46?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fdc703824bce4bae43a834dfdf641f46?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g\",\"caption\":\"Nitin Kaushik\"},\"url\":\"https:\/\/cloudkul.com\/blog\/author\/nitin-kaushik680\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Introduction to IPtables in Linux - Cloudkul","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/","og_locale":"en_US","og_type":"article","og_title":"Introduction to IPtables in Linux - Cloudkul","og_description":"Managing network traffic is one of the toughest jobs a system administrator has to deal [...]","og_url":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/","og_site_name":"Cloudkul","article_published_time":"2019-12-11T06:40:39+00:00","article_modified_time":"2019-12-11T06:51:32+00:00","author":"Nitin Kaushik","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/","url":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/","name":"Introduction to IPtables in Linux - Cloudkul","isPartOf":{"@id":"https:\/\/cloudkul.com\/blog\/#website"},"datePublished":"2019-12-11T06:40:39+00:00","dateModified":"2019-12-11T06:51:32+00:00","author":{"@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/99e46a85b2ad2848198f6de2c90bcbc4"},"breadcrumb":{"@id":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/cloudkul.com\/blog\/introduction-to-iptables-in-linux\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cloudkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Introduction to IPtables in Linux"}]},{"@type":"WebSite","@id":"https:\/\/cloudkul.com\/blog\/#website","url":"https:\/\/cloudkul.com\/blog\/","name":"Cloudkul","description":"Host your eCommerce Store on AWS with Optimized Performance","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cloudkul.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/99e46a85b2ad2848198f6de2c90bcbc4","name":"Nitin Kaushik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cloudkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fdc703824bce4bae43a834dfdf641f46?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdc703824bce4bae43a834dfdf641f46?s=96&d=https%3A%2F%2Fs.gravatar.com%2Favatar%2F6148c37469011bc2f8e491ca8f5de495%3Fs%3D80&r=g","caption":"Nitin Kaushik"},"url":"https:\/\/cloudkul.com\/blog\/author\/nitin-kaushik680\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/4304"}],"collection":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/comments?post=4304"}],"version-history":[{"count":99,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/4304\/revisions"}],"predecessor-version":[{"id":5218,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/posts\/4304\/revisions\/5218"}],"wp:attachment":[{"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/media?parent=4304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/categories?post=4304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudkul.com\/blog\/wp-json\/wp\/v2\/tags?post=4304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}