<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://townx.org" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>townx - Simple firewall for Ubuntu using iptables - Comments</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables</link>
 <description>Comments for &quot;Simple firewall for Ubuntu using iptables&quot;</description>
 <language>en</language>
<item>
 <title>My example got formatted</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-40405</link>
 <description>&lt;p&gt;My example got formatted queerly.  The 1&#039;s are actually script comments.  Cheers!&lt;/p&gt;</description>
 <pubDate>Fri, 05 Mar 2010 22:55:14 -0600</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 40405 at http://townx.org</guid>
</item>
<item>
 <title>This worked perfectly for my</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-40404</link>
 <description>&lt;p&gt;This worked perfectly for my needs.  Thanks!&lt;/p&gt;

&lt;p&gt;For those who want to &lt;span class=&quot;caps&quot;&gt;ACCEPT &lt;/span&gt;specific ports for services like &lt;span class=&quot;caps&quot;&gt;HTTPD, &lt;/span&gt;be sure to put those rules before the &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;rules.  I fiddled with this for an hour before I figured out that the first rule that matches is the one that gets applied.&lt;/p&gt;

&lt;p&gt;eg.&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;Allow specific &lt;span class=&quot;caps&quot;&gt;TCP &lt;/span&gt;inputs&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i eth0 -p tcp -m tcp --dport 22  -j &lt;span class=&quot;caps&quot;&gt;ACCEPT &lt;/span&gt;# ssh&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i eth0 -p tcp -m tcp --dport 80  -j &lt;span class=&quot;caps&quot;&gt;ACCEPT &lt;/span&gt;# http&lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i eth1 -p tcp --dport 3306 -s 10.176.84.219 -j &lt;span class=&quot;caps&quot;&gt;ACCEPT&lt;/span&gt;&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i eth1 -p tcp --dport 3306 -s 10.176.85.10 -j &lt;span class=&quot;caps&quot;&gt;ACCEPT&lt;/span&gt;&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;drop everything else&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i eth+ -p udp -j &lt;span class=&quot;caps&quot;&gt;DROP&lt;/span&gt;&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i eth+ -p tcp -m tcp --syn -j &lt;span class=&quot;caps&quot;&gt;DROP&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
 <pubDate>Fri, 05 Mar 2010 22:51:27 -0600</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 40404 at http://townx.org</guid>
</item>
<item>
 <title>I was once try out the</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38824</link>
 <description>&lt;p&gt;I was once try out the ubuntu, but end up keep getting error on the eth0, in which I think it is related to the network card that I have. I try your script which I thought it got something to do with my error, but still fail.  Would a firewall cause my eth0(network card) to have such error? I&#039;ve tried changed few cards which is still the same.&lt;/p&gt;</description>
 <pubDate>Sat, 07 Feb 2009 10:39:14 -0600</pubDate>
 <dc:creator>WeightLoss Coach</dc:creator>
 <guid isPermaLink="false">comment 38824 at http://townx.org</guid>
</item>
<item>
 <title>firewall init.d script</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38698</link>
 <description>&lt;p&gt;if you change the &lt;span class=&quot;caps&quot;&gt;POLICIES &lt;/span&gt;to &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;and run the init.d script with anything other than a  &quot;start&quot; argument, you will flush your rules, but since the policy is &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;, you will  not be able to access &lt;/p&gt;</description>
 <pubDate>Mon, 05 Jan 2009 16:57:46 -0600</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 38698 at http://townx.org</guid>
</item>
<item>
 <title>Thanks. That does look like</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38531</link>
 <description>&lt;p&gt;Thanks. That does look like an improvement. Like I said in the post, I&#039;m not really an iptables expert!&lt;/p&gt;</description>
 <pubDate>Tue, 21 Oct 2008 13:26:59 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 38531 at http://townx.org</guid>
</item>
<item>
 <title>Ooops! Forgot the LocalHost...</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38525</link>
 <description>&lt;p&gt;&lt;code&gt;iptables -A INPUT -i lo -j ACCEPT&lt;/code&gt;&lt;/p&gt;</description>
 <pubDate>Sun, 19 Oct 2008 18:03:15 -0500</pubDate>
 <dc:creator>Lifenstein</dc:creator>
 <guid isPermaLink="false">comment 38525 at http://townx.org</guid>
</item>
<item>
 <title>Simpler Alternative (?)</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38524</link>
 <description>&lt;p&gt;For a personal home computer (running no services for the outside world),  here is a simpler version :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;#!/bin/bash&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;############################################################&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#---- Script to setup a simple firewall using iptables -----&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;###&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;# * Blocks all incoming connections, except those opened by&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;#   me, or related to already open connections&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;# * Blocks all forward requests&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;# * Allows all outgoing connections&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;###&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;############################################################&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# Clearing all previous rules&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;iptables -F&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;# Setting Default Policies&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;iptables -P INPUT DROP&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;iptables -P OUTPUT ACCEPT&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;iptables -P FORWARD DROP&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;# Allowing already-established and related-incoming connections&lt;/code&gt;&lt;br /&gt;
&lt;code&gt;iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT&lt;/code&gt;&lt;/p&gt;</description>
 <pubDate>Sun, 19 Oct 2008 17:48:21 -0500</pubDate>
 <dc:creator>Lifenstein</dc:creator>
 <guid isPermaLink="false">comment 38524 at http://townx.org</guid>
</item>
<item>
 <title>Only if you&#039;re running a web</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38371</link>
 <description>&lt;p&gt;Only if you&#039;re running a web server :)&lt;/p&gt;</description>
 <pubDate>Sat, 02 Aug 2008 15:40:41 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 38371 at http://townx.org</guid>
</item>
<item>
 <title>firewall</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38131</link>
 <description>&lt;p&gt;Hi,&lt;br /&gt;
Great script - I&#039;ve used it as the starter for my setup.&lt;/p&gt;

&lt;p&gt;ShieldsUP notes that your script shows ports 0 and 1 as closed rather than in stealth mode, and also doesn&#039;t drop &lt;span class=&quot;caps&quot;&gt;ICMP &lt;/span&gt;packets - meaning that if the machines are directly attached to the internet via ppp, or with an &lt;span class=&quot;caps&quot;&gt;ADSL &lt;/span&gt;modem with no firewall, then they can be discovered.&lt;/p&gt;

&lt;p&gt;Also your script doesn&#039;t handle dialup connections.&lt;/p&gt;

&lt;p&gt;The following changes mitigates against these:&lt;/p&gt;


&lt;ol&gt;
&lt;li&gt;drop everything else on ppp&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i ppp+ -p udp -j &lt;span class=&quot;caps&quot;&gt;DROP&lt;/span&gt;&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i ppp+ -p tcp -m tcp --syn -j &lt;span class=&quot;caps&quot;&gt;DROP&lt;/span&gt;&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-i ppp+ -p icmp -j &lt;span class=&quot;caps&quot;&gt;DROP&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;Explcitly deal with port 0&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p tcp --sport 0&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p udp --sport 0&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p tcp --dport 0&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p udp --dport 0&lt;/li&gt;
&lt;li&gt;Explcitly deal with port 1&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p tcp --sport 1&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p udp --sport 1&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p tcp --dport 1&lt;br /&gt;
iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-j &lt;span class=&quot;caps&quot;&gt;DROP &lt;/span&gt;-p udp --dport 1&lt;/li&gt;
&lt;/ol&gt;



&lt;p&gt;regards&lt;/p&gt;

&lt;p&gt;Colin&lt;/p&gt;</description>
 <pubDate>Thu, 08 May 2008 03:01:53 -0500</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 38131 at http://townx.org</guid>
</item>
<item>
 <title>Thx ;)</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-38122</link>
 <description>&lt;p&gt;thanks!though  iptables -A &lt;span class=&quot;caps&quot;&gt;INPUT &lt;/span&gt;-p tcp -s 0/0 --dport 80 -j &lt;span class=&quot;caps&quot;&gt;ACCEPT &lt;/span&gt;is needed!&lt;/p&gt;</description>
 <pubDate>Wed, 30 Apr 2008 18:18:12 -0500</pubDate>
 <dc:creator>Prakash</dc:creator>
 <guid isPermaLink="false">comment 38122 at http://townx.org</guid>
</item>
<item>
 <title>What, the ports became</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-14521</link>
 <description>&lt;p&gt;What, the ports became unavailable? Or the services stopped running? When you stopped the firewall? If you set it up as above, stopping the firewall shouldn&#039;t touch other services, and should definitely not block them.&lt;/p&gt;</description>
 <pubDate>Sun, 18 Nov 2007 18:07:54 -0600</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14521 at http://townx.org</guid>
</item>
<item>
 <title>shit</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-14520</link>
 <description>&lt;p&gt;when i typed /etc/init.d/firewall stop ssh, apache gone away .. :(&lt;br /&gt;
why ?&lt;/p&gt;</description>
 <pubDate>Sun, 18 Nov 2007 11:09:12 -0600</pubDate>
 <dc:creator>alex rose</dc:creator>
 <guid isPermaLink="false">comment 14520 at http://townx.org</guid>
</item>
<item>
 <title>Thanks for that, Roy. I&#039;ve</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-14403</link>
 <description>&lt;p&gt;Thanks for that, Roy. I&#039;ve used some of the &lt;span class=&quot;caps&quot;&gt;GUI&lt;/span&gt;s before, but the simple ones can&#039;t cope with the scenario outlined in the article. While fwbuilder probably could, I like the simplicity of my script, and have used it succesfully without touching it for about 3 years. I&#039;m very happy with it.&lt;/p&gt;</description>
 <pubDate>Tue, 02 Oct 2007 13:20:28 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14403 at http://townx.org</guid>
</item>
<item>
 <title>Great, glad it was helpful.</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-14402</link>
 <description>&lt;p&gt;Great, glad it was helpful.&lt;/p&gt;</description>
 <pubDate>Tue, 02 Oct 2007 13:19:19 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 14402 at http://townx.org</guid>
</item>
<item>
 <title>short and sweet, works as</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comment-14401</link>
 <description>&lt;p&gt;short and sweet, works as advertised! happy customer here. &lt;/p&gt;</description>
 <pubDate>Mon, 01 Oct 2007 22:57:55 -0500</pubDate>
 <dc:creator>Reid</dc:creator>
 <guid isPermaLink="false">comment 14401 at http://townx.org</guid>
</item>
<item>
 <title>Simple firewall for Ubuntu using iptables</title>
 <link>http://townx.org/simple_firewall_for_ubuntu_using_iptables</link>
 <description>&lt;p&gt;Linux&#039;s built-in firewall iptables is very useful, but pretty hard to configure. I used to use lokkit, but this caused problems when moving between different networks. I was also having problems with the network configuration tools in Ubuntu, which work but aren&#039;t automatic enough for me. And I wanted to be able to switch the firewall and the network configuration simultaneously.&lt;/p&gt;

&lt;p&gt;In the end, I bit the bullet and worked out how to write a simple iptables script. Here it is:&lt;/p&gt;



&lt;pre&gt;
#!/bin/bash
# flush all chains
iptables -F
# set the default policy for each of the pre-defined chains
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
# allow establishment of connections initialised by my outgoing packets
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
# drop everything else
iptables -A INPUT -i eth+ -p udp -j DROP
iptables -A INPUT -i eth+ -p tcp -m tcp --syn -j DROP
# accept anything on localhost
iptables -A INPUT -i lo -j ACCEPT
&lt;/pre&gt;



&lt;p&gt;I have network interfaces on eth0 and eth1, so this script has rules which cover both; if your interfaces have different names, you will need to edit the rules to cover that. This drops everything incoming, except for connections which were initially established by my outgoing packets (thanks Luke! - see comments); which means it&#039;s no good for servers.&lt;/p&gt;

&lt;p&gt;I put this script in /opt/scripts/iptables.script and made it executable. Once you run it, you can find out whether it has worked by displaying your current iptables rules with:&lt;/p&gt;



&lt;pre&gt;
sudo iptables -L -v
&lt;/pre&gt;



&lt;p&gt;I then created a simple init script to start/stop the firewall (in /etc/init.d/firewall):&lt;/p&gt;



&lt;pre&gt;#!/bin/bash
if [[ $1 == start ]] ; then
  sudo /opt/scripts/iptables.script
else
  sudo iptables -F
fi
&lt;/pre&gt;



&lt;p&gt;Then I symlinked this into my /etc/rc.* directories using the update-rc.d tool, so the firewall starts before the network comes up:&lt;/p&gt;



&lt;pre&gt;
update-rc.d firewall start 20 2 3 4 5 . stop 99 0 1 6 .
&lt;/pre&gt;



&lt;p&gt;I find having this script helps me a lot. I have it integrated with a start/stop script with my network, so I can easily switch network and firewall configuration from the command line.&lt;/p&gt;</description>
 <comments>http://townx.org/simple_firewall_for_ubuntu_using_iptables#comments</comments>
 <category domain="http://townx.org/howtos">howtos</category>
 <pubDate>Wed, 05 Apr 2006 09:36:10 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">259 at http://townx.org</guid>
</item>
</channel>
</rss>
