<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.townx.org" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>townx - Simple firewall for Ubuntu using iptables - Comments</title>
 <link>http://www.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>Steve</title>
 <link>http://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comment-41648</link>
 <description>&lt;p&gt;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. Thank you.&lt;/p&gt;</description>
 <pubDate>Mon, 21 Nov 2011 20:27:08 -0600</pubDate>
 <dc:creator>Steve</dc:creator>
 <guid isPermaLink="false">comment 41648 at http://www.townx.org</guid>
</item>
<item>
 <title>Steve</title>
 <link>http://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comment-41643</link>
 <description>&lt;p&gt;I think this is nice. I try your script which I thought it got something to do with my error, but still fail. Thank you for you iptables script. I have used your script every time I format my Linux machines. Its very handy. &lt;/p&gt;</description>
 <pubDate>Thu, 17 Nov 2011 22:57:44 -0600</pubDate>
 <dc:creator>Steve</dc:creator>
 <guid isPermaLink="false">comment 41643 at http://www.townx.org</guid>
</item>
<item>
 <title>Your iptables script</title>
 <link>http://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comment-40860</link>
 <description>&lt;p&gt;Thanks for your script, very nice. I especially like the part about update-rc.d. Your iptables script and the info on update-rc.d has given me a starting point and inspired me to delve deeper into iptables and start-up procedures in general. &lt;/p&gt;

&lt;p&gt;All the best,&lt;br /&gt;
James&lt;/p&gt;</description>
 <pubDate>Fri, 06 Aug 2010 02:50:30 -0500</pubDate>
 <dc:creator>James</dc:creator>
 <guid isPermaLink="false">comment 40860 at http://www.townx.org</guid>
</item>
<item>
 <title>Hi,I have some problems</title>
 <link>http://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comment-40852</link>
 <description>&lt;p&gt;Hi,I have some problems about iptables rules under Ubuntu .&lt;br /&gt;
Is it necessary to create some script llike before to build the firewall,Will the rules got lost when I restart the server.&lt;/p&gt;

&lt;p&gt;I create a simple firewall with the function of &lt;span class=&quot;caps&quot;&gt;NAT,&lt;/span&gt;then restart the server.&lt;br /&gt;
type commands below in the terminal:&lt;br /&gt;
iptables -L -v&lt;/p&gt;

&lt;p&gt;But display nothing.&lt;br /&gt;
And it seemd that the &lt;span class=&quot;caps&quot;&gt;NAT &lt;/span&gt;function and the rules made last time are still available to use.&lt;/p&gt;


&lt;p&gt;Hoping for your email.&lt;br /&gt;
Thanks.&lt;/p&gt;</description>
 <pubDate>Sun, 01 Aug 2010 03:45:59 -0500</pubDate>
 <dc:creator>Guest</dc:creator>
 <guid isPermaLink="false">comment 40852 at http://www.townx.org</guid>
</item>
<item>
 <title>That might be the case now</title>
 <link>http://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comment-40534</link>
 <description>&lt;p&gt;That might be the case now (I notice Fedora now comes with default firewall rules, which I&#039;m pretty sure it never used to). It wasn&#039;t the case on Ubuntu a few years ago when I wrote this article, however.&lt;/p&gt;

&lt;p&gt;Time passes, things change :)&lt;/p&gt;</description>
 <pubDate>Fri, 23 Apr 2010 17:21:53 -0500</pubDate>
 <dc:creator>elliot</dc:creator>
 <guid isPermaLink="false">comment 40534 at http://www.townx.org</guid>
</item>
<item>
 <title>Is this necessary?</title>
 <link>http://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comment-40532</link>
 <description>&lt;p&gt;As far as I was aware, the default ubuntu firewall rules block all unsolicited incoming traffic.&lt;/p&gt;

&lt;p&gt;What does this do that is not already happening?&lt;/p&gt;

&lt;p&gt;Or am I missing something obvious?&lt;/p&gt;</description>
 <pubDate>Thu, 22 Apr 2010 12:46:00 -0500</pubDate>
 <dc:creator>Ian</dc:creator>
 <guid isPermaLink="false">comment 40532 at http://www.townx.org</guid>
</item>
<item>
 <title>My example got formatted</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>This worked perfectly for my</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>I was once try out the</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>firewall init.d script</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>Thanks. That does look like</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>Ooops! Forgot the LocalHost...</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>Simpler Alternative (?)</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>Only if you&#039;re running a web</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>firewall</title>
 <link>http://www.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://www.townx.org</guid>
</item>
<item>
 <title>Simple firewall for Ubuntu using iptables</title>
 <link>http://www.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://www.townx.org/simple_firewall_for_ubuntu_using_iptables#comments</comments>
 <category domain="http://www.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://www.townx.org</guid>
</item>
</channel>
</rss>

