{"id":10817,"date":"2024-12-12T06:57:52","date_gmt":"2024-12-12T06:57:52","guid":{"rendered":"https:\/\/blog.trustedhosting.in\/?p=10817"},"modified":"2024-12-12T06:57:52","modified_gmt":"2024-12-12T06:57:52","slug":"how-to-configure-ip-firewall-in-linux","status":"publish","type":"post","link":"https:\/\/www.webystrata.com\/blog\/how-to-configure-ip-firewall-in-linux\/","title":{"rendered":"How to Configure IP Firewall in Linux"},"content":{"rendered":"<figure id=\"attachment_10818\" aria-describedby=\"caption-attachment-10818\" style=\"width: 2560px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.trustedhosting.in\/reseller-hosting.html\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-10818 size-full\" title=\"How to Configure IP Firewall in Linux\" src=\"https:\/\/blog.trustedhosting.in\/wp-content\/uploads\/2024\/12\/3797936_10001-scaled.jpg\" alt=\"How to Configure IP Firewall in Linux\" width=\"2560\" height=\"2560\" srcset=\"https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-scaled.jpg 2560w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-300x300.jpg 300w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-1024x1024.jpg 1024w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-150x150.jpg 150w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-768x768.jpg 768w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-1536x1536.jpg 1536w, https:\/\/www.webystrata.com\/blog\/wp-content\/uploads\/2024\/12\/3797936_10001-2048x2048.jpg 2048w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/a><figcaption id=\"caption-attachment-10818\" class=\"wp-caption-text\">\u00a0<\/figcaption><\/figure>\n<h1>How to Configure IP Firewall in Linux<\/h1>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">iptables<\/span><\/code>\u00a0is the name of the software firewall bundled with most UKFast Linux <a href=\"https:\/\/www.trustedhosting.in\/reseller-hosting.html\" target=\"_blank\" rel=\"noopener\">servers.<\/a><\/p>\n<p>Whilst we recommend that you use your hardware firewall for most things, it may still be helpful \/ necessary to be able to use\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">iptables<\/span><\/code>\u00a0for quickly blocking IP addresses or closing ports.<\/p>\n<p>As with most Linux packages, the man pages are a good place to start to learn more about the package and its usage, but if you\u2019re just looking to get up and running quickly, here are a few examples:<\/p>\n<section id=\"command-structure\">\n<h2>Command Structure<\/h2>\n<div class=\"highlight-console notranslate\">\n<div class=\"highlight\">\n<pre><span class=\"go\">   iptables -I OUTPUT -p tcp -s 192.168.0.1 --dport 2020 -j ACCEPT<\/span>\r\n<\/pre>\n<\/div>\n<\/div>\n<p>The individual elements of the above commands are explained below<\/p>\n<ul>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">iptables<\/span><\/code><\/strong>\n<p>The command itself.<\/li>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">-I<\/span><\/code><\/strong>\n<p>Insert the new rule at the top of the chain.<\/p>\n<p>IPTables rules are read in sequentially, so the order the rules are in is very important. If the rule you\u2019re adding doesn\u2019t need to be the first, can you alternatively use\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-A<\/span><\/code>\u00a0to append it to the bottom of the chain or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-I<\/span>\u00a0<span class=\"pre\">{CHAIN}<\/span>\u00a0<span class=\"pre\">{LINE<\/span>\u00a0<span class=\"pre\">NUMBER}<\/span><\/code>\u00a0to insert it at a particular line number.<\/li>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">OUTPUT<\/span><\/code><\/strong>\n<p>The chain name.<\/p>\n<p>There are 2 main chains that you\u2019ll likely be interested in,\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">INPUT<\/span><\/code>\u00a0and\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">OUTPUT<\/span><\/code>. Simply put, rules in\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">INPUT<\/span><\/code>\u00a0affect inbound traffic and rules in\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">OUTPUT<\/span><\/code>\u00a0affect outbound.<\/li>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">-p<\/span><\/code><\/strong>\n<p>The protocol that the rule relates to.<\/p>\n<p>Valid protocols are\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">tcp<\/span><\/code>,\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">udp<\/span><\/code>\u00a0or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">icmp<\/span><\/code><\/li>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">-s<\/span><\/code><\/strong>\n<p>Source IP address that the rule pertains to.<\/p>\n<p>Can also be written longhand as\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--source<\/span><\/code>\u00a0or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--src<\/span><\/code>.<\/p>\n<p>Inversely, destination IP addresses can be specified with\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">-d<\/span><\/code>,\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--destination<\/span><\/code>\u00a0or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--dst<\/span><\/code>.<\/p>\n<p>Can also be given a range such as\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">192.168.0.0\/24<\/span><\/code>\u00a0or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">192.168.0.0\/255.255.255.0<\/span><\/code>.<\/li>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">--dport<\/span><\/code><\/strong>\n<p>Destination port that the rule relates to.<\/p>\n<p>Can also be written as\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--destination-port<\/span><\/code>.<\/p>\n<p>Alternatively, source port can be specified with\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--sport<\/span><\/code>\u00a0or\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">--source-port<\/span><\/code>.<\/p>\n<p>Ranges of ports can be specified with a\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">:<\/span><\/code>, for example\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">1096:2999<\/span><\/code>.<\/li>\n<li><strong><code class=\"docutils literal notranslate\"><span class=\"pre\">-j<\/span><\/code><\/strong>\n<p>Chain to jump to when previous elements have been matched.<\/p>\n<p>Basic chains that can be used here are\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">ACCEPT<\/span><\/code>,\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">DROP<\/span><\/code>\u00a0and\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">REJECT<\/span><\/code>.<\/p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">ACCEPT<\/span><\/code>\u00a0will accept packet,\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">REJECT<\/span><\/code>\u00a0will deny packet and\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">DROP<\/span><\/code>\u00a0will result in it being silently dropped.<\/li>\n<\/ul>\n<\/section>\n<section id=\"list-current-rules\">\n<h2>List current rules<\/h2>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>iptables -vnL\r\n<\/pre>\n<\/div>\n<\/div>\n<\/section>\n<section id=\"blocking-a-port\">\n<h2>Blocking a port<\/h2>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>iptables -I INPUT -p tcp --dport <span class=\"m\">6667<\/span> -j REJECT\r\n<\/pre>\n<\/div>\n<\/div>\n<\/section>\n<section id=\"blocking-an-ip\">\n<h2>Blocking an IP<\/h2>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>iptables -I INPUT -p tcp -s <span class=\"m\">1<\/span>.2.3.4 -j DROP\r\n<\/pre>\n<\/div>\n<\/div>\n<\/section>\n<section id=\"blocking-a-port-for-a-particular-ip\">\n<h2>Blocking a port for a particular IP<\/h2>\n<div class=\"highlight-bash notranslate\">\n<div class=\"highlight\">\n<pre>iptables -I OUTPUT -p tcp -s <span class=\"m\">192<\/span>.168.0.1 --dport <span class=\"m\">25<\/span> -j DROP\r\n<\/pre>\n<\/div>\n<\/div>\n<div class=\"admonition note\">\n<blockquote><p>The site\u00a0http:\/\/iptabl.es\u00a0can be used to generate IPTables rules without having to memorise the above syntax. Usage is explained on the frontpage, but a few examples are below:<\/p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">curl<\/span>\u00a0<span class=\"pre\">iptabl.es\/drop\/2020\/8.8.8.8<\/span><\/code><\/p>\n<p>http:\/\/iptabl.es\/accept\/25<\/p><\/blockquote>\n<\/div>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>How to Configure IP Firewall in Linux iptables\u00a0is the name of the software firewall bundled with most UKFast Linux servers.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":10818,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-10817","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts\/10817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/comments?post=10817"}],"version-history":[{"count":2,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts\/10817\/revisions"}],"predecessor-version":[{"id":10821,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/posts\/10817\/revisions\/10821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/media\/10818"}],"wp:attachment":[{"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/media?parent=10817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/categories?post=10817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webystrata.com\/blog\/wp-json\/wp\/v2\/tags?post=10817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}