{"id":1352,"date":"2011-08-29T18:49:47","date_gmt":"2011-08-29T10:49:47","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=1352"},"modified":"2023-04-28T09:50:02","modified_gmt":"2023-04-28T09:50:02","slug":"how-to-drop-or-block-incoming-access-from-specific-ip-address-using-iptables","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-drop-or-block-incoming-access-from-specific-ip-address-using-iptables\/","title":{"rendered":"How to Drop or Block Incoming Access From Specific IP Address Using Iptables"},"content":{"rendered":"<h2>Introduction:<\/h2>\n<p>Iptables is a powerful tool used to manage the Linux firewall. It is used to set up, maintain, and inspect the tables of IP packet filter rules in the Linux kernel. One of the most common use cases of iptables is to block incoming access from specific IP addresses.<\/p>\n<pre>\r\n--append  -A Append to chain\r\n--delete  -D Delete matching rule from chain\r\n--delete  -D Delete rule rulenum (1 = first) from chain\t\r\n--insert  -I Insert in chain as rulenum (default 1=first)\r\n--replace -R Replace rule rulenum (1 = first) in chain\r\n--list    -L List the rules in a chain or all chains\r\n\r\n--source      -s [!] address[\/mask] source specification\r\n--destination -d [!] address[\/mask] destination specification                         \r\n--jump        -j target\r\n\r\nINPUT = Incoming Access\r\nOUTPUT = Outgoing Access\r\n-I = Insert\r\n-D = Delete\r\n-s = Source Ip Address\r\n-j = Target Action\r\nDROP = Block action\r\n<\/pre>\n<p>In this guide, we will show you how to drop or block incoming access from a specific IP address using iptables.<\/p>\n<h2>Step 1: Check Current Iptables Rules<\/h2>\n<p>Before we begin, let&#8217;s check the current iptables rules by running the following command:<\/p>\n<pre>\r\nsudo iptables -L\r\n<\/pre>\n<p>This will display the current iptables rules.<\/p>\n<h2>Step 2: Add a Rule to Block Incoming Access from a Specific IP Address<\/h2>\n<p>To block incoming access from a specific IP address, we need to add a new rule to iptables. The rule will drop all incoming traffic from the IP address.<\/p>\n<p>To add the rule, run the following command:<\/p>\n<pre>\r\nsudo iptables -A INPUT -s 192.168.1.100 -j DROP\r\n<\/pre>\n<p>In this command, we are adding a new rule to the INPUT chain to drop all traffic from IP address 192.168.1.100.<\/p>\n<h2>Step 3: Save the Rule<\/h2>\n<p>To ensure that the new rule persists after a reboot, we need to save the iptables rules.<\/p>\n<p>To save the current iptables rules, run the following command:<\/p>\n<pre>\r\nsudo sh -c \"iptables-save > \/etc\/iptables.rules\"\r\n<\/pre>\n<p>This command will save the current iptables rules to the file \/etc\/iptables.rules.<\/p>\n<h2>Step 4: Load the Rule on Boot<\/h2>\n<p>To ensure that the new rule is loaded on boot, we need to modify the \/etc\/rc.local file.<\/p>\n<p>Open the file in a text editor:<\/p>\n<pre>\r\nsudo nano \/etc\/rc.local\r\n<\/pre>\n<p>Add the following line to the file before the exit 0 line:<\/p>\n<pre>\r\niptables-restore < \/etc\/iptables.rules\r\n<\/pre>\n<p>This command will load the saved iptables rules from the file \/etc\/iptables.rules on boot.<\/p>\n<h2>Step 5: Test the Rule<\/h2>\n<p>To test the new rule, try to access your server from the blocked IP address. You should not be able to establish a connection.<\/p>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">iptables<\/span> - tool used to manage the Linux firewall<\/li>\n<li><span class=\"fw-bold\">iptables-save<\/span> - saves the current iptables rules to a file<\/li>\n<li><span class=\"fw-bold\">iptables-restore<\/span> - loads the saved iptables rules from a file<\/li>\n<\/ul>\n<h2>Conclusion:<\/h2>\n<p>In this guide, we have shown you how to drop or block incoming access from a specific IP address using iptables. By adding a new rule to iptables, we can drop all traffic from the specified IP address. Remember to save the iptables rules and load them on boot to ensure that the new rule persists after a reboot.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Iptables is a powerful tool used to manage the Linux firewall. It is used to set up, maintain, and inspect the tables of IP packet filter rules in the&#8230;<\/p>\n","protected":false},"author":6,"featured_media":1312,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1158],"tags":[1246,2089,1975,1536,1744],"class_list":["post-1352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iptables","tag-centos-5-5","tag-configuration","tag-install","tag-linux","tag-security"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/1352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=1352"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/1352\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/1312"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=1352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=1352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=1352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}