How do I build a Simple Linux Firewall for DSL/Dial-up connection?
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)
E-mail this to a Friend
Printable Version
You may also be interested in other helpful articles:
- Traceroute to Bypass the Firewall filters and Personal Firewalls policy for home user
- How do I use Iptables connection tracking feature?
- Linux Iptables open Bittorrent tcp ports 6881 to 6889
- Tutorial simple Linux firewall configuration using NetFilter / iptables
- FreeBSD setup Internet connections sharing with NAT firewall
Discussion on This Article:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: access policy, default filter, desktop linux, dns queries, firewall policy, fw, incoming traffic, iptables state, linux computer, linux firewall, linux firewall howto, loop back, mangle, modprobe, outgoing connections, outgoing traffic, shell command, udp traffic, unlimited traffic, windows background



There are many programs that automate the iptables process quite well and still allow for advanced rules. Check out shorewall.
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.
I must agree with LinuxTitli, with this samll script one can understand how linux firewall works. Good work, IMPO
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
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?