<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Green Sea Turtle on Medium]]></title>
        <description><![CDATA[Stories by Green Sea Turtle on Medium]]></description>
        <link>https://medium.com/@greenseaturtle30?source=rss-1e9a40024f8d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bfHc46PsRGFgAZRtFIKDyg.png</url>
            <title>Stories by Green Sea Turtle on Medium</title>
            <link>https://medium.com/@greenseaturtle30?source=rss-1e9a40024f8d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 01:11:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@greenseaturtle30/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Set Up a Secure Firewall Using iptables (IPv4)]]></title>
            <link>https://medium.com/@greenseaturtle30/how-to-setup-a-secure-firewall-using-iptables-ipv4-7ad5c83896e9?source=rss-1e9a40024f8d------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ad5c83896e9</guid>
            <category><![CDATA[iptables]]></category>
            <category><![CDATA[ipv4]]></category>
            <category><![CDATA[computer-networking]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[firewall]]></category>
            <dc:creator><![CDATA[Green Sea Turtle]]></dc:creator>
            <pubDate>Mon, 09 Nov 2026 20:58:09 GMT</pubDate>
            <atom:updated>2026-11-10T01:34:17.143Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Firewall" src="https://cdn-images-1.medium.com/max/474/1*tvg6cWMibaYDtf0C5hG3Tg.jpeg" /></figure><h3>Prerequisites:</h3><ul><li>A computer running Linux</li><li>sudo privileges</li><li>Console or remote access</li></ul><h3>iptables Overview:</h3><p>A firewall is a piece of software or hardware that regulates the network traffic to and from a computer. <em>iptables</em> is a command-line utility for configuring the built-in Linux kernel firewall, allowing administrators to control incoming and outgoing network traffic.</p><p><em>iptables</em> is made up of tables (shocking), which are collections of groups of similar rules called chains. There are five default tables used to manage rule chains. The one we will use is the filter table, which is the default packet filtering table, deciding which packets are allowed to enter and leave the network.</p><p>Chains are rule lists within tables. There are multiple types of chains in <em>iptables</em>, but the three we will see in the filter table are:</p><ol><li>INPUT: handles incoming packets with a local destination</li><li>OUTPUT: handles outgoing packets with a local source</li><li>FORWARD: handles packets that pass through the system between network interfaces (this is useful if you’re using <em>iptables</em> to set up your own NAT router).</li></ol><p>After a rule in a chain is evaluated, the packet is sent to a target. Targets decide what happens to a packet after it’s matched to a rule criteria. The three targets we will use are:</p><ol><li>ACCEPT: allow the packet through the firewall</li><li>DROP: don’t allow the packet and don’t inform the sender</li><li>REJECT: don’t allow the packet and return an error response to the sender</li><li>LOG: record packet info into a log file, then continue evaluating the packet against the current rule set</li></ol><p>This walkthrough handles only IPv4 addressing and routing. If your machine is using IPv6, then it is best to use <em>ip6tables</em> to create a firewall instead of <em>iptables. </em>If your machine does use IPv6, then these rules will only apply to IPv4 traffic and leave IPv6 open. IPv6 also requires ICMPv6 to function, so a default deny policy can break it unless you allow it.</p><h3>Incoming Traffic</h3><p>The default policy for incoming traffic will be set to deny all. Before doing this, however, we need to ensure we don’t lock ourselves and others out of the machine (if this is done through remote access).</p><p>First, enable loopback traffic on the system so applications are able to communicate with the localhost interface. To do this, run:</p><pre>sudo iptables -A INPUT -i lo -j ACCEPT</pre><p>Then, allow remote access. The SSH protocol is used to access machines remotely over TCP, with default port 22. To allow incoming SSH traffic, run:</p><pre>sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT</pre><p>Next, allow both established and related connections. <em>ESTABLISHED</em> packets come from a connection that has been seen going in both directions. We need to allow <em>ESTABLISHED</em> packets so that existing connections don’t get dropped. <em>RELATED</em> packets are any packets that are related to an existing connection. We need to also allow incoming related packets to make sure nothing breaks.</p><pre>sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</pre><p>At a bare minimum, this is all that is needed before changing the default policy. However, if your system is running services on it, you might need to expose more ports so that users can access the services. For example, if you are running a web server using HTTPS, then you would need to allow TCP traffic on port 443.</p><p>To set the default policy to deny all incoming traffic, run:</p><pre>sudo iptables -P INPUT DROP</pre><h3>Outgoing Traffic</h3><p>When it comes to deciding rules for outgoing traffic, the most basic thing to allow is internet access. How do you access the internet? You’ll want to allow outgoing HTTP and HTTPS requests. Your browser uses HTTP and HTTPS to make GET and POST requests to webpages, but if this traffic is blocked from your computer, webpages will never load. The other protocol we’ll need to access the internet is DNS (Domain Name System); otherwise, you’d have to remember 142.251.211.206 instead of just google.com.</p><p>The HTTP protocol runs on port 80, and the HTTPS protocol runs on port 443. To allow outgoing HTTP and HTTPS traffic, run:</p><pre>sudo iptables -A OUTPUT -p tcp --dport 80 -j ACCEPT<br>sudo iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT</pre><p>The DNS protocol runs on port 53. Unlike the other protocols we’ve looked at, DNS mainly runs over UDP instead of TCP (though DNS does use TCP as a backup). To allow outgoing DNS traffic, run:</p><pre>sudo iptables -A OUTPUT -p udp --dport 53 -j ACCEPT<br>sudo iptables -A OUTPUT -p tcp --dport 53 -j ACCEP</pre><p>The Network Time Protocol (NTP) is used for synchronizing the computer’s clocks over a network. If NTP traffic isn’t allowed, it is possible the system time won’t stay accurate. To allow NTP, which also runs over UDP, run:</p><pre>sudo iptables -A OUTPUT -p udp --dport 123 -j ACCEPT</pre><p>Finally, allow outgoing loopback and <em>ESTABLISHED</em> and <em>RELATED</em> connections, just like in the INPUT chain.</p><pre>sudo iptables -A OUTPUT -o lo -j ACCEPT<br>sudo iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT</pre><p>This is the bare minimum of what outgoing traffic needs to be allowed before changing the default policy. However, just like with incoming traffic, you might need to allow further traffic depending on what’s running on your system.</p><p>To set the default policy to deny all outgoing traffic, run:</p><pre>sudo iptables -P OUTPUT DROP</pre><h3>Logging</h3><p>Because we are using a default deny all policy, a lot of network traffic will be dropped by <em>iptables</em>. It would be nice if we could see what is being dropped by our firewall. This could let us know if there are any packets being blocked that should be allowed, or if someone is trying to break into our system.</p><p>Luckily, <em>iptables</em> has the LOG target, which can be used to log specific packets and assign a key to them, making them easier to search for.</p><p>To log dropped packets and prepend the prefix “Dropped: ”, use the following command:</p><pre>sudo iptables -A INPUT -j LOG --log-prefix &quot;Dropped: &quot;<br>sudo iptables -A OUTPUT -j LOG --log-prefix &quot;Dropped: &quot;</pre><p>We can then search through the logs using the assigned prefix. There are two commands we can use to view the logs:</p><pre>sudo dmesg | grep &quot;Dropped&quot; # view system logs<br>sudo tail -f /var/log/syslog | grep &quot;Dropped&quot; # view tail of syslog file</pre><h3>Saving Rules</h3><p>Now we have a default deny-all firewall that still lets our computer remain usable and logs all dropped packets. But what happens if the machine gets rebooted? Rules in <em>iptables</em> are not persistent, so all our work would be reset. To fix this, we can use the persistent package for <em>iptables</em>.</p><p>For Debian-based distributions, install and enable the persistent package with the following command:</p><pre>sudo apt install iptables-persistent # install persistent package<br>sudo systemctl enable netfilter-persistent # enables start on reboot</pre><p>Then, after adding your rules, you can save them using:</p><pre>sudo netfilter-persistent save</pre><p>For RedHat-based distributions, install and enable the persistent package with the following command:</p><pre>sudo yum install iptables-services # install persistent package<br>sudo systemctl enable iptables # enables iptables rules on reboot</pre><p>Then, save the rules using:</p><pre>sudo service iptables save</pre><p>Now, if your computer is ever rebooted, your firewall won’t be wiped and your network traffic will continue to be filtered.</p><h3>Next Steps</h3><p>Your firewall is now fully set up! However, that doesn’t mean we’re done. Setting a firewall isn’t a one-and-done thing; it’s something that needs to be maintained. Services running on your computer might change, forcing you to allow or drop certain packets. Maybe you need to allow all traffic from a specific IP subnet, or block traffic coming from a certain IP address. Maybe you added a rule you didn’t mean to and want to delete it. Below are some examples of <em>iptable</em>s commands that you might find helpful in the future:</p><p>View rules with line numbers:</p><pre>sudo iptables -L --line-numbers</pre><p>Delete a rule:</p><pre>sudo iptables -D INPUT [line-number]</pre><p>Allow traffic from an IP address:</p><pre>sudo iptables -A INPUT -s [IP-address] -j ACCEPT</pre><p>Drop traffic from an IP address:</p><pre>sudo iptables -I INPUT 1 -s [IP-address] -j DROP</pre><p>Reject traffic from an IP address range:</p><pre>sudo iptables -I INPUT 1 -m iprange --src-range [IP-address-range] -j REJECT</pre><p>Allow ICMP (ping) echo reply and request traffic for debugging or testing:</p><pre># allow ping from outside to inside<br>sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT<br>sudo iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT<br><br># allow ping from inside to outside<br>sudo iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT<br>sudo iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT</pre><p>Allow outbound SSH (if you use it for GitHub or other similar sources)</p><pre>sudo iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ad5c83896e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>