CSF / LFD Firewall and Security on Debian 9

firewallFor my work I had to setup a DirectAdmin server (which was a great pain to setup perfect) and I was bothered by enormous amounts of brute force attempts, mostly on Exim ports. First thing on the agenda was of course look for a way to block these fuckers and in comes the CSF /LFD Firewall and Security.

I went experimenting on non-DirectAdmin servers with CSF / LFD and was pleased. After some debating and testing I decided to drop firewalld6 and go with CSF / LFD. This guide is to get it running on a Debian 9 server without DirectAdmin (but will make a post in week or two for that one specifically).

Installing our new firewall

Here we go.

# cd /tmp/

# wget https://download.configserver.com/csf.tgz

# tar xzf csf.tgz

# cd csf/

Check if everything should be ready to run CSF / LFD:

# ./csftest.pl

If everything is OK you can install CSF / LFD.

# ./install.sh

Setting it up

Now configure your firewall. A good place to start are the man pages and this site. For a quick setup follow the steps below.

First make sure that management of the server is only possible from your WAN IP address.

# vi /etc/csf/csf.allow

Add: # Your WAN IP

Then the configuration file is very big, it has a lot of commented text, but stripped down its still pretty huge. From top to bottom the changed lines:


We’ll be making our own cron for automatic updates.

The below is important to understand. You will NOT put management ports (like 22 and 2222) in the allow sections. That is why we’ve allowed our WAN IP in the csf.allow file.

# Allow incoming TCP ports
TCP_IN = "80,443"
# Allow outgoing TCP ports
TCP_OUT = "25,80,443,587"
# Allow incoming UDP ports
UDP_IN = ""
# Allow outgoing UDP ports
UDP_OUT = "53,123"

Use the same settings for IPv6. Continuing:

LF_ALERT_TO = "email@yourdomain.com"
LF_ALERT_FROM = "email@yourdomain.com"
URLGET = "1"
PT_LIMIT = "0"
UI = "0"

That should do it. Restart services and test your settings.

# csf -r

# systemctl restart csf

# systemctl restart lfd


Create a cron for automatic updates in /etc/cron.d/csfupdate. Contents:


# Automatic updates for CSF / LFD security and firewall
30 5 * * * root /usr/sbin/csf -u

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.