How do I build a Simple Linux Firewall for DSL/Dial-up connection?

by on October 28, 2005 · 6 comments· LAST UPDATED December 6, 2007

in , ,

If you're new to Linux, here's a simple firewall that can be setup in minutes. Especially those coming from a Windows background, often lost themselves while creating linux firewall.
This is the most common question asked by Linux newbies (noobs). How do I install a personal firewall on a standalone Desktop Linux computer. In other words "I wanna a simple firewall that allows or permits me to visit anything from my computer but it should block everything from outside world".
Well that is pretty easy first remember INPUT means incoming and OUTPUT means outgoing connection/access. With following little script and discussion you should able to setup your own firewall.

Step # 1: Default Firewall policy

Set up default access policy to drop all incoming traffic but allow all outgoing traffic. This will allow you to make unlimited outgoing connections from any port but not incoming traffic/ports are allowed.
iptables -p INPUT DROP
iptables -p OUTPUT ACCEPT

Step # 2: Allow unlimited traffic from loopback (lo) device

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -i lo -j ACCEPT

Step # 3: Setup connection oriented access

Some protocol such as a FTP, DNS queries and UDP traffic needs an established connection access. In other words you need to allow all related connection using iptables state modules.
iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT

Step # 4: Drop everything else and log it

iptables -A INPUT -j LOG
iptables -A INPUT -j REJECT

But wait you cannot type all above commands at a shell command prompt. It is a good idea to create a script called fw.start as follows (copy and paste following script in fw.start file):

#!/bin/sh
# A simple
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
modprobe ip_conntrack
modprobe ip_conntrack_ftp
# Setting default filter policy
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
# Unlimited access to loop back
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
# Allow UDP, DNS and Passive FTP
iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
# DROP everything and Log it
iptables -A INPUT -j LOG
iptables -A INPUT -j DROP

You can enhance your tiny firewall with

  • Create a script to stop a firewall
  • This is optional, if you wish to start a firewall automatically as soon as Debian Linux boots up use the instruction outlined here
  • Finally if you wanna open incoming ssh (port 22) or http (port 80) then insert following two rules before #DROP everything and Log it line in above script:

iptables -A INPUT -p tcp -i eth0 --dport 22 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp -i eth0 --dport 80 -m state --state NEW -j ACCEPT

Easy to use Linux firewall programs/tools

  • GUI tools - firestarter :: A graphical interfaced Open Source firewall for Linux. (highly recommended for Linux desktop users)
  • IPCop Firewall and SmoothWall :: Setup a dedicated firewall box. (highly recommended for Linux server and LAN/WAN users)
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 6 comments… read them below or add one }

1 Anonymous November 2, 2005 at 9:49 pm

There are many programs that automate the iptables process quite well and still allow for advanced rules. Check out shorewall.

Reply

2 LinuxTitli November 3, 2005 at 9:58 am

Agreed. Howerver, they are only useful if you understand basis of iptables otherwise you can harm yourself more than the protecting yourself. And that is why you need to understand the iptables, once done it you can go for tools or ready to use scripts out there. Besides writing your own script saves more time in long run, IMPO.

Reply

3 ganesh November 16, 2005 at 11:24 pm

I must agree with LinuxTitli, with this samll script one can understand how linux firewall works. Good work, IMPO

Reply

4 Chris November 6, 2007 at 9:21 pm

Code in step 2 is incorrect:
iptables -A OUTPUT -i lo -j ACCEPT

cannot use -i with OUTPUT,
should read:
iptables -A OUTPUT -o lo -j ACCEPT

Reply

5 Blingbling April 8, 2008 at 7:40 am

Why oh why do you first decide to drop all incoming packages that no other rule take care of (the policy) and then later on, just forget about this and set a “reject” for everything in step 4?

Reply

6 Paul July 31, 2009 at 5:53 am

Standard practice with firewall rules, is to setup a default reject (fail safe) – and then an explicit reject rule. That way, if your rules don’t load properly, things get dropped by default. And when you’re looking over your rules – you can see your explicit reject, and not rely on the default (which is usually much earlier in the file, and not visible), just in case you forgot it, or it got changed on an upgrade or something silly like that…

It’s about safety/security, which is what firewalls are for.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , ,

Previous post:

Next post: