<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yeshraj B.</title>
    <description>The latest articles on DEV Community by Yeshraj B. (@yeshraj_b).</description>
    <link>https://dev.to/yeshraj_b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F917997%2F7986bc60-3e5c-422f-b975-e5a82302468b.png</url>
      <title>DEV Community: Yeshraj B.</title>
      <link>https://dev.to/yeshraj_b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yeshraj_b"/>
    <language>en</language>
    <item>
      <title>Managing traffic using iptables firewall</title>
      <dc:creator>Yeshraj B.</dc:creator>
      <pubDate>Wed, 11 Dec 2024 14:12:52 +0000</pubDate>
      <link>https://dev.to/yeshraj_b/managing-traffic-using-iptables-firewall-4i9f</link>
      <guid>https://dev.to/yeshraj_b/managing-traffic-using-iptables-firewall-4i9f</guid>
      <description>&lt;p&gt;&lt;code&gt;iptables&lt;/code&gt; is a powerful firewall that allows you to set up rules to control incoming, outgoing, and forwarded traffic on your networks. It is a default firewall in many linux systems including server instances provided on oracle cloud.&lt;/p&gt;

&lt;p&gt;In this post, we’ll cover the following topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Basics of iptables&lt;/li&gt;
&lt;li&gt;Installing and Viewing Rules&lt;/li&gt;
&lt;li&gt;Common iptables Commands&lt;/li&gt;
&lt;li&gt;Working with Chains and Rules&lt;/li&gt;
&lt;li&gt;Best Practices&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Basics of iptables
&lt;/h2&gt;

&lt;p&gt;iptables works by examining network packets and applying rules to determine how to handle them. The main component of iptables is the &lt;strong&gt;Chains&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Chains: A chain is a list of rules that are examined in order. The three default chains in iptables are:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INPUT: For packets coming into our server aka incoming traffic.&lt;/li&gt;
&lt;li&gt;OUTPUT: For packets going out of our server aka outgoing traffic.&lt;/li&gt;
&lt;li&gt;FORWARD: For packets being routed through the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installing and Viewing Rules
&lt;/h2&gt;

&lt;p&gt;On Debian/Ubuntu, you can install iptables with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install iptables
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed you can use this command to view it's current state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -L -n -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;-L: List the rules.&lt;/li&gt;
&lt;li&gt;-n: Show numerical addresses instead of resolving hostnames.&lt;/li&gt;
&lt;li&gt;-v: Provide verbose output.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common iptables Commands
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Allow input traffic on a port:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will accept incoming traffic on the port &lt;strong&gt;80&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block input traffic on a port:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -A INPUT -p tcp --dport 443 -j DROP
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will disallow input traffic on the port &lt;strong&gt;443&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reject a Connection (Send a response):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -A INPUT -p tcp --dport 23 -j REJECT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Flush All Rules:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -F
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Delete a Specific Rule:
To delete a rule, you first need to find out its sequence number:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -L --line-numbers
sudo iptables -D INPUT {line_number}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Working with Chains and Rules
&lt;/h2&gt;

&lt;p&gt;Before adding or deleting a rules it's important to understand the order in which the rules of a chain are executed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;iptables evaluates rules in a chain sequentially, from top to bottom. If a rule does not match the packet, only then the packet is passed to the next rule&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;When a packet arrives, it's checked against each rule in the chain, one by one, until a match is found. Once a matching rule is identified, the corresponding action (such as ACCEPT, DROP or REJECT) is applied, and processing stops. &lt;strong&gt;No further rules in the chain are evaluated for that packet.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So if you have a REJECT rule at line 14 which rejects all incoming traffic&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REJECT     all  --  *      *       0.0.0.0/0    0.0.0.0/0      reject-with icmp-host-prohibited
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you have a rule at line 15 which accepts incoming traffic on port 3000&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ACCEPT     tcp  --  *      *       0.0.0.0/0    0.0.0.0/0      tcp dpt:3000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;there will be no incoming traffic on port 3000 as iptables will stop on line 14 and the ACCEPT rule on line 15 will not be evaluated.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add Rule to End of Chain:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -A INPUT -s 192.168.1.2 -j ACCEPT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a Rule at a Specific Position:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -I INPUT 4 -s 192.168.1.2 -j ACCEPT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;strong&gt;4&lt;/strong&gt; is the line number where this rule will be added&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a incoming traffic rule at Position &lt;strong&gt;6&lt;/strong&gt; :
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo iptables -I INPUT 6 -p tcp --dport 8000 -j ACCEPT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Backup Your Rules: Always take a backup before making changes.&lt;/li&gt;
&lt;li&gt;Test Rules: Apply and test your rules carefully, especially when implementing complex configurations.&lt;/li&gt;
&lt;li&gt;Use DESCRIPTIVE Comments: Document your rules by using comments to explain the purpose of complex rules.&lt;/li&gt;
&lt;li&gt;Monitor Logs: Regularly check the logs to identify any potential issues or blocked traffic.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>security</category>
      <category>firewall</category>
    </item>
  </channel>
</rss>
