{"id":6290,"date":"2019-08-09T05:20:16","date_gmt":"2019-08-09T05:20:16","guid":{"rendered":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?p=6290"},"modified":"2019-08-09T05:39:08","modified_gmt":"2019-08-09T05:39:08","slug":"learn-to-configure-firewall-using-iptables","status":"publish","type":"post","link":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/","title":{"rendered":"Learn to configure Firewall using iptables"},"content":{"rendered":"<p>This article guides on how to configure a basic firewall using iptables. By using an iptables program, you can explicitly grant or deny access to selected services that run on your server as well as on selected IP addresses.<\/p>\n<h2>What is iptables?<\/h2>\n<p>The iptables program allows you to view and modify the Linux kernel&#8217;s built-in network packet filtering capabilities. You have the opportunity to grant and deny access to specific network services such as SSH, HTTP, etc. Also, you can permit or block specific IP addresses from connecting to the server.<\/p>\n<p>To perform all these actions, first, you need to define a set of rules that are grouped into chains. By default iptables uses three chains :<\/p>\n<p><strong>(i) INPUT (for incoming packets)<\/strong><\/p>\n<p><strong>(ii) FORWARD (for forwarding packets)<\/strong><\/p>\n<p><strong>(iii) OUTPUT (for outgoing packets)<\/strong><\/p>\n<p>This article will cover only the INPUT chain to selectively block and accept an incoming packet to the server.<br \/>\nMost of the major Linux distributors are already incorporated with iptables program by default, including Debian, Ubuntu, CentOS and Fedora.<\/p>\n<h2># How to add rules?<\/h2>\n<p>iptables does not have any rules defined and to verify this, you can type the following command :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -L<\/span><\/div>\n<p>&nbsp;<\/p>\n<pre class=\"trim-whitespace:false 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>You can see that there are no targets and no destinations defined. So, to add some basic rules, type the following commands:<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -A INPUT -i lo -j ACCEPT<\/span><\/div>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -A INPUT -m state &#8211;state RELATED, ESTABLISHED -j ACCEPT<\/span><\/div>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -A INPUT -p tcp &#8211;dport 7822 -j ACCEPT<\/span><\/div>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -A INPUT -j DROP<\/span><\/div>\n<p><strong>-A<\/strong> in all the above comments, instructs iptables to append the rule to the end of the specified chain (here, the INPUT chain). Let&#8217;s see what each command specifies :<\/p>\n<p>\u2022 The first command is used to permit all packets for the local loopback interface. The loopback interface is used by many programs, so it is a good option to accept packets on it.<\/p>\n<p>\u2022 The second command is used because it uses <strong>-m<\/strong> option to load the state module. This module can determine and monitor a packet&#8217;s state, which can be <strong>NEW<\/strong>, <strong>ESTABLISHED<\/strong>, or <strong>RELATED<\/strong>. Using this rule, we accept incoming packets that belong to a connection that has already been established.<\/p>\n<p>\u2022 The third command is used to accept incoming TCP connection on port 7288 (SSH).<\/p>\n<p>\u2022 The last command is used to drop (reject) incoming packets that do not match any of the preceding rules.<\/p>\n<p>Now, if you type the <strong>iptables -L<\/strong> command, you will get the following output :<\/p>\n<pre class=\"trim-whitespace:false lang:default decode:true \">Chain INPUT (policy ACCEPT)\r\ntarget prot opt source destination\r\nACCEPT all -- anywhere anywhere\r\nACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED\r\nACCEPT tcp -- anywhere anywhere tcp dpt:7822\r\nDROP all -- anywhere anywhere\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>You can test the configuration by connecting to the server using SSH. It will allow you to connect. Connections that are on any other ports like HTTP connection on port 80 will get rejected.<\/p>\n<h2># Inserting the rules<\/h2>\n<p>The set of rules that are defined above are limited. If you want to allow only SSH as the incoming connection then you are all set. But, most likely, you will need to add access to services as you configure your server.<\/p>\n<p>If we add a rule using <strong>-A<\/strong> option as shown above, then it will be the last rule in the chain, right after the <strong>DROP<\/strong> rule. This is because iptables works through the sequence of rules. That means it will never get to the new rule as the packet have already been dropped. Thus, we need to have a way to insert new rules into the chain.<\/p>\n<p>The <strong>-I<\/strong> option allows us to insert a new rule anywhere in the chain. Let&#8217;s see how to insert a rule that allows incoming TCP connections on port 80(HTTP). So we will want a rule to come just before the <strong>DROP<\/strong> rule which is currently the fourth rule in the chain :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -I INPUT 4 -p tcp -m tcp &#8211;dport 80 -j ACCEPT<\/span><\/div>\n<p>This command will insert our HTTP rule in the fourth line and will push the <strong>DROP<\/strong> rule down to the fifth line. Now after typing the <strong>iptables -L<\/strong> command, you will get the following output :<\/p>\n<pre class=\"trim-whitespace:false lang:default decode:true \">Chain INPUT (policy ACCEPT)\r\ntarget prot opt source destination\r\nACCEPT all -- anywhere anywhere\r\nACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED\r\nACCEPT tcp -- anywhere anywhere tcp dpt:7822\r\nACCEPT tcp -- anywhere anywhere tcp dpt:http\r\nDROP all -- anywhere anywhere\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<div style=\"background-color: #e0ffff;padding: 10px\">\n<p><strong>Note:<\/strong> To check the line numbers for all of the rules in a chain, type the following command :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -L &#8211;line-numbers<\/span><\/div>\n<\/div>\n<h2># To Block an IP Address<\/h2>\n<p>The rules explained above define access by service (SSH, HTTP, etc.). Similarly, you can also set the rules that permit or block specific IP addresses.<br \/>\nFor example, if you find server log files that there are repeated SSH login attempts from a particular IP address in the server log files. So, to block all subsequent SSH connections from the IP address, you need to type the following command:<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -I INPUT <span style=\"color: #ff0000\">rulenum<\/span> -s <span style=\"color: #ff0000\">xxx.xxx.xxx.xxx<\/span> -p tcp -m tcp &#8211;dport 7822 -j DROP<\/span><\/div>\n<p>In this command, replace <span style=\"color: #ff0000\">rulenum<\/span> with the rule number and also replace <span style=\"color: #ff0000\">xxx.xxx.xxx.xxx<\/span> with the IP address to block.<\/p>\n<p>To <strong>block all the traffic<\/strong> from an IP address regardless of the service that has been requested, type the following command :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -I INPUT rulenum -s xxx.xxx.xxx.xxx -j DROP<\/span><\/div>\n<h2># Deleting Rules<\/h2>\n<p>To delete the rule, you need to use the -D option. Also, you need to know the number of the rule that you want to delete. For example to delete the fourth rule from the INPUT chain, use the following command :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -D INPUT 5<\/span><\/div>\n<p>To delete all the rules at once, type the following command :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables -F<\/span><\/div>\n<h2># Saving Rules<\/h2>\n<p>Once you reboot the server now, all the rules that you have defined will be erased. So, to maintain rules across system restarts, you need to save them. The steps to do this depend on the Linux distribution that you are running.<\/p>\n<h3>For Debian and Ubuntu<\/h3>\n<p>Perform the following steps to save the iptables rules on a server running Debian or Ubuntu :<\/p>\n<p>1. In the command prompt, type the following command :<\/p>\n<p>apt-get install iptables-persistent<\/p>\n<p>2. During the process of package installation, at the <strong>Save current IPv4 rules?<\/strong> prompt, press on <strong>Enter<\/strong>.<\/p>\n<p>3. At the prompt for the <strong>Save current IPv6 rules?<\/strong>, press Tab, and then press <strong>Enter<\/strong>.<\/p>\n<div style=\"background-color: #e0ffff;padding: 10px\">\n<p><strong>Note:<\/strong> Above steps 2 and 3 will only appear once during initial package installation. So, if you make any changes to iptables rules, then type the following command to save them :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">iptables-save &gt; \/etc\/iptables\/rules.v4<\/span><\/div>\n<\/div>\n<h3>For CentOS and Fedora<\/h3>\n<p>Enter the following command, to save the iptables rules on a server running CentOS or Fedora.<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">\/sbin\/service iptables save<\/span><\/div>\n<p>To know more information about iptables type the following command :<\/p>\n<div style=\"background-color: #000000;padding: 10px\"><span style=\"color: #ffffff\">man iptables<\/span><\/div>\n<div><\/div>\n<div>\n<p><strong>Also Read :<\/strong><\/p>\n<\/div>\n<div><strong>1) <a href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/how-to-open-ports-in-windows-firewall\/\">How to Open Ports in Windows Firewall?<\/a><\/strong><\/div>\n<div><\/div>\n<div><strong>2) <a href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-how-to-allow-an-ftp-server-through-windows-firewall\/\">Learn how to allow an FTP server through Windows Firewall<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article guides on how to configure a basic firewall using iptables. By using an iptables program, you can explicitly grant or deny access to selected services that run on your server as well as on selected IP addresses. What is iptables? The iptables program allows you to view and modify the Linux kernel&#8217;s built-in [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[867],"tags":[868,927],"class_list":["post-6290","post","type-post","status-publish","format-standard","placeholder-for-hentry","category-firewall","tag-firewall","tag-iptables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Learn to configure Firewall using iptables - Web Hosting FAQs by MilesWeb<\/title>\n<meta name=\"description\" content=\"This article guides you on how to configure firewall using iptables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn to configure Firewall using iptables - Web Hosting FAQs by MilesWeb\" \/>\n<meta property=\"og:description\" content=\"This article guides you on how to configure firewall using iptables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Hosting FAQs by MilesWeb\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-09T05:20:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-08-09T05:39:08+00:00\" \/>\n<meta name=\"author\" content=\"Sonam Wagh\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sonam Wagh\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/\",\"name\":\"Learn to configure Firewall using iptables - Web Hosting FAQs by MilesWeb\",\"isPartOf\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\"},\"datePublished\":\"2019-08-09T05:20:16+00:00\",\"dateModified\":\"2019-08-09T05:39:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/dc645d02823c86e07e53798ebe02c6f4\"},\"description\":\"This article guides you on how to configure firewall using iptables.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Learn to configure Firewall using iptables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/\",\"name\":\"Web Hosting FAQs by MilesWeb\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/dc645d02823c86e07e53798ebe02c6f4\",\"name\":\"Sonam Wagh\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9cb2cf6ae11d7625ef6417ef8e84ba25?s=96&d=blank&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/9cb2cf6ae11d7625ef6417ef8e84ba25?s=96&d=blank&r=g\",\"caption\":\"Sonam Wagh\"},\"description\":\"With an interest in doing something creative daily, Sonam works as a Digital Marketing Executive. She likes to write technical blogs related to web hosting, digital marketing, and other IT topics. She also likes to spend her leisure time on social media to find different strategies for client engagement.\",\"url\":\"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/sonam\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn to configure Firewall using iptables - Web Hosting FAQs by MilesWeb","description":"This article guides you on how to configure firewall using iptables.","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:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/","og_locale":"en_GB","og_type":"article","og_title":"Learn to configure Firewall using iptables - Web Hosting FAQs by MilesWeb","og_description":"This article guides you on how to configure firewall using iptables.","og_url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/","og_site_name":"Web Hosting FAQs by MilesWeb","article_published_time":"2019-08-09T05:20:16+00:00","article_modified_time":"2019-08-09T05:39:08+00:00","author":"Sonam Wagh","twitter_misc":{"Written by":"Sonam Wagh","Estimated reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/","name":"Learn to configure Firewall using iptables - Web Hosting FAQs by MilesWeb","isPartOf":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website"},"datePublished":"2019-08-09T05:20:16+00:00","dateModified":"2019-08-09T05:39:08+00:00","author":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/dc645d02823c86e07e53798ebe02c6f4"},"description":"This article guides you on how to configure firewall using iptables.","breadcrumb":{"@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/learn-to-configure-firewall-using-iptables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/"},{"@type":"ListItem","position":2,"name":"Learn to configure Firewall using iptables"}]},{"@type":"WebSite","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#website","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/","name":"Web Hosting FAQs by MilesWeb","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/dc645d02823c86e07e53798ebe02c6f4","name":"Sonam Wagh","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/9cb2cf6ae11d7625ef6417ef8e84ba25?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9cb2cf6ae11d7625ef6417ef8e84ba25?s=96&d=blank&r=g","caption":"Sonam Wagh"},"description":"With an interest in doing something creative daily, Sonam works as a Digital Marketing Executive. She likes to write technical blogs related to web hosting, digital marketing, and other IT topics. She also likes to spend her leisure time on social media to find different strategies for client engagement.","url":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/author\/sonam\/"}]}},"views":573,"_links":{"self":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/6290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/comments?post=6290"}],"version-history":[{"count":3,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/6290\/revisions"}],"predecessor-version":[{"id":6293,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/posts\/6290\/revisions\/6293"}],"wp:attachment":[{"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/media?parent=6290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/categories?post=6290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.milesweb.co.uk\/hosting-faqs\/wp-json\/wp\/v2\/tags?post=6290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}