Building a Firewall with IP Chains

A quick introduction to the program ipchains.

Today, one of the most important topics in the computation world is security. How to improve security in a single or interconnected machine is sometimes hard to understand and difficult to implement. In this article, I will discuss how to implement a simple firewall on a Linux machine using IP chains.


IP chains could be new to users who upgraded their 2.0.36 kernels to the 2.2.x series, but old to those who worked in the 2.1.x series. ipchains is a rewrite of the well-known ipfwadm, which was a rewrite of BSD's ipfw, and was used to build firewalls in 2.0.x kernels. The are many reasons for this rewrite but perhaps the most important is ipfwadm couldn't allow protocols other than TCP, UDP or ICMP and it didn't handle fragments.

What It Is

Linux IP firewall chaining software is a program that uses the kernel IP packet filtering capability. A packet filter looks at the header of a packet and decides the fate of the entire packet. It can decide to DENY the packet (discard the packet as if it had never received it), ACCEPT (let the packet pass through), or REJECT (like deny, but notify the source of the packet).

Why You Want It

When you build your firewall you are looking for control and security of your network, and good firewall scripts are the key to this objective's success. If you are constantly receiving a ping flood from a specific IP address, you can deny all packets received from that IP, by creating a chain with this policy. ipchains is able to read the policies of the scripts and give instructions to the IP packet filtering as to how to handle the incoming/outcoming packets.


First, your kernel must be able to use IP chains. Look for the file /proc/net/ip_fwchains, if it exists, everything is okay. If not, you need to recompile your kernel and set these options:


Next you need to know the syntax of ipchains necessary to create functional scripts. Let's imagine a hypothetical file called scriptf with some rules :

ipchains -N ippolicy
ipchains -I input -j ippolicy
ipchains -A ippolicy -p icmp -s -j\
ipchains -A ippolicy -p TCP -t
This script will DENY every packet with the ICMP protocol from the specific source addresses (in our example: and also DENY every packet with the TCP protocol where the target is the choosen address (again in our example: Here's a step-by-step explanation:
  • ipchains -N ippolicy: this line creates a new chain with the name ippolicy.

  • ipchains -I input -j ippolicy: this line says all packets will be verified by ippolicy rules.

  • ipchains -A ippolicy -p icmp -s -j DENY: this line appends the rule ippolicy to the ICMP protocol packets, with a source address of and denies them. Options are:

  • -A: append one or more rules to the selected chain.

  • -p: specify the protocol.

  • -s: specify the source address (0/0 means all addresses).

  • -j: specify the target of the rule, i.e., what to do if the packet matches it.

  • ipchains -A ippolicy -p TCP -t -j DENY: This line will append the rule ippolicy to the TCP protocol packets with a target address of and denies them.

Now, you will want to inform the system that these rules are to be initialized at boot time. Remembering all information about system initialization is in the /etc/rc.d/init.d directory, copy the scriptf file to this directory and add a line like:
in the file /etc/rc.d/rc.sysinit to start it. An important option that could help you in the future is the -F flag, which is used when you want to create new rules and override all previous rules, that is:
ipchains -F ippolicy


White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState