{"id":9251,"date":"2025-08-09T14:28:44","date_gmt":"2025-08-09T08:58:44","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=9251"},"modified":"2026-08-25T09:36:15","modified_gmt":"2026-08-25T04:06:15","slug":"blocking-an-ip-address-using-iptables","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/blocking-an-ip-address-using-iptables\/","title":{"rendered":"Blocking an IP Address using iptables"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>1. Introduction<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>iptables<\/code> is a powerful firewall utility available in Linux systems that allows administrators to control incoming and outgoing network traffic. One common use case is <strong>blocking traffic from a specific IP address<\/strong> to protect a server from malicious activity, repeated failed login attempts, or excessive requests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This document outlines the process to:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Check if an IP is already blocked.<\/li>\n\n\n\n<li>Add a new firewall rule to block it.<\/li>\n\n\n\n<li>Save the configuration to make it persistent after a reboot.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Prerequisites<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before performing these steps, ensure the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Root or Sudo Access<\/strong>: You must have administrative privileges on the server.<\/li>\n\n\n\n<li><strong>iptables Installed<\/strong>: Most Linux distributions (CentOS, RHEL, Fedora, Debian, Ubuntu) come with <code>iptables<\/code> pre-installed.<\/li>\n\n\n\n<li><strong>Basic Command Line Knowledge<\/strong>: Familiarity with Linux terminal commands.<\/li>\n\n\n\n<li><strong>The IP Address to Block<\/strong>: Ensure you have identified the correct IP to avoid blocking legitimate traffic.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Procedure<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Check if the IP is already blocked<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">iptables -L -n | grep &lt;IP_address&gt;\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>-L<\/code> lists all current rules.<\/li>\n\n\n\n<li><code>-n<\/code> avoids DNS lookups and shows raw IP addresses.<\/li>\n\n\n\n<li><code>grep<\/code> filters the output for the specific IP.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If no output appears, the IP is not currently blocked.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Block the IP Address<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">iptables -A INPUT -s &lt;IP_address&gt; -j DROP\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>-A INPUT<\/code> appends the rule to the <strong>INPUT chain<\/strong> (incoming traffic).<\/li>\n\n\n\n<li><code>-s &lt;IP_address&gt;<\/code> specifies the source IP to block.<\/li>\n\n\n\n<li><code>-j DROP<\/code> tells iptables to silently discard packets from that IP.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Save the iptables Rules<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">iptables-save &gt; \/etc\/sysconfig\/iptables\n<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Explanation<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>iptables-save<\/code> outputs the current firewall rules.<\/li>\n\n\n\n<li>Redirecting to <code>\/etc\/sysconfig\/iptables<\/code> ensures the rules are applied automatically after a reboot.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Notes &amp; Best Practices<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To <strong>remove<\/strong> a blocked IP: <code>iptables -D INPUT -s &lt;IP_address&gt; -j DROP<\/code><\/li>\n\n\n\n<li>Always <strong>double-check the IP<\/strong> before blocking, to avoid disrupting legitimate connections.<\/li>\n\n\n\n<li>Consider using <code>fail2ban<\/code> for automated blocking of repeated offenders.<\/li>\n\n\n\n<li>For firewalld-based systems (CentOS 7+), use <code>firewall-cmd<\/code> instead of direct <code>iptables<\/code> edits.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Blocking specific IP addresses using <code>iptables<\/code> is an effective way to mitigate certain types of malicious traffic and protect server resources. By following the above steps, administrators can quickly identify, block, and persist these rules, maintaining better control over inbound traffic.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction iptables is a powerful firewall utility available in Linux systems that allows administrators to control incoming and outgoing [&hellip;]<\/p>\n","protected":false},"author":508,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1024,1048,1041],"tags":[],"class_list":["post-9251","post","type-post","status-publish","format-standard","hentry","category-security","category-iptables","category-network","psol-cat-security"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2pd","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/508"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=9251"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9251\/revisions"}],"predecessor-version":[{"id":11024,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/9251\/revisions\/11024"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=9251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=9251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=9251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}