How to Configure a Linux Firewall.

There are so many ways to configure Linux firewall so i will explain my favorite and easiest one.

I use script that is called ConfigServer Security & Firewall (csf).This script is released under ConfigServer Services script license that can be obtained from . It is released free of charge and is compatible with any Linux server running 2.4.x and 2.6.x kernel with packet filtering framework enabled. Also can be configured via Web interface when installed on servers running cpanel/webmin control panels.You will need iptables installed and LWP perl module (libwww-perl).
The suite of scripts provides:

  • Straight-forward SPI iptables firewall script
  • Daemon process that checks for login authentication failures for:
  • courier imap and pop3
  • ssh
  • cpanel / whm / webmail (cPanel servers only)
  • pure-pftd
  • password protected web pages (htpasswd)
  • mod_security failures
  • suhosin failures
  • POP3/IMAP login tracking to enforce logins per hour
  • SSH login notification
  • SU login notification
  • Excessive connection blocking
  • WHM configuration interface (cPanel servers only) or through Webmin
  • WHM iptables report log (cPanel servers only)
  • Easy upgrade between versions from within WHM (cPanel servers only) or through Webmin
  • Easy upgrade between versions from shell
  • A standard Webmin Module to configure csf is included in the distribution ready to install into Webmin - csfwebmin.tgz
  • Pre-configured to work on a cPanel server with all the standard cPanel ports open (cPanel servers only)
  • Auto-configures the SSH port if it's non-standard on installation
  • Block traffic on unused server IP addresses - helps reduce the risk to your server
  • Alert when end-user scripts sending excessive emails per hour - for identifying spamming scripts
  • Suspicious process reporting - reports potential exploits running on the server
  • Excessive user processes reporting
  • Excessive user process usage reporting and optional termination
  • Suspicious file reporting - reports potential exploit files in /tmp and similar directories
  • Directory and file watching - reports if a watched directory or a file changes
  • Block traffic on the DShield Block List and the Spamhaus DROP List
  • BOGON packet protection
  • Pre-configured settings for Low, Medium or High firewall security (cPanel servers only)
  • Works with multiple ethernet devices
  • Server Security Check - Performs a basic security and settings check on the server (cPanel servers only)
  • Allow Dynamic DNS IP addresses - always allow your IP address even if it changes whenever you connect to the internet
  • Alert sent if server load average remains high for a specified length of time
  • mod_security log reporting (if installed)
  • Email relay tracking - tracks all email sent through the server and issues alerts for excessive usage (cPanel servers only)
  • IDS (Intrusion Detection System) - the last line of detection alerts you to changes to system and application binaries
  • SYN Flood protection
  • Ping of death protection
  • Port Scan Tracking and blocking
  • Permanent and Temporary (with TTL) IP blocking
  • Exploit checks

So here is how you install the script.

  1. Login to your server via ssh with your favorite ssh client .You will need to have full root access in order to complete the install.
  2. Download the script in your root directory " wget"
  3. Download libwww-perl "wget"
  4. Uncompress libwww-perl "tar xzvf libwww-perl-5.812.tar.gz"
  5. Prepare to install libwww-perl "cd libwww-perl-5.812; perl -y Makefile.PL". Answer "yes" on all of the questions and type "make ;make install "
  6. Uncompress the installation script via "tar -xzf csf.tgz" and run the installer "cd csf; sh"
  7. You should not run any other iptables firewall configuration script. For example, if you previously used APF+BFD you can remove the combination (which you will need to do if you have them installed otherwise they will conflict horribly) so you run "sh /etc/csf/"
  8. You should ensure that kernel logging daemon (klogd) is enabled.Some times VDS servers have syslog disabled.

That's it.You have good working Linux firewall.

By default your firewall is set to allow connections to "ftp,ssh,smtp,dns,http,pop3,imap,https,smtps,rndc, whm/cpanel
and secure whm/cpanel". If you like to add/remove any services you will need to edit /etc/csf/csf.conf file.
Just and add/remove the service port under TCP_IN(for inbound TCP )/TCP_OUT(for outbound TCP)/UDP_IN( for inbound UDP)
/UDP_OUT(for outbound UDP)

Here is example of how to add IRC to your existing configuration
TCP_IN = "20,21,22,25,53,80,110,143,443,465,953,993,995,207 7,2078,2082,2083,2086,2087,2095,2096,6667"
TCP_OUT = "20,21,22,25,53,80,110,143,443,465,953,993,995,207 7,2078,2082,2083,2086,2087,2095,2096,6667"

You can also allow/deny access to your server base on ip or subnet.
To deny access.
csf -d Because of spam .
To allow access.
csf -a Is my workstation.