<\/span><\/h2>\nAfter you have successfully updated your server, you can run the following commands to change the current directory to \/opt, download the latest CSF source code, and unpack it to the same directory.<\/p>\n
cd \/opt\/\nwget http:\/\/download.configserver.com\/csf.tgz\ntar xzf csf.tgz<\/pre>\nBefore proceeding with the installation, make sure that you are not using another firewall such as UFW. The following two commands will change the directory to \/opt\/csf\/ and execute the installation script.<\/p>\n
cd \/opt\/csf\nsh install.sh<\/pre>\nThe installation of the firewall is complete, but it is best to check if the iptables modules are available which are required for proper functioning on CSF. You can do that with the command:<\/p>\n
perl \/usr\/local\/csf\/bin\/csftest.pl<\/pre>\nOutput:<\/p>\n
Testing ip_tables\/iptable_filter...OK\nTesting ipt_LOG...OK\nTesting ipt_multiport\/xt_multiport...OK\nTesting ipt_REJECT...OK\nTesting ipt_state\/xt_state...OK\nTesting ipt_limit\/xt_limit...OK\nTesting ipt_recent...OK\nTesting xt_connlimit...OK\nTesting ipt_owner\/xt_owner...OK\nTesting iptable_nat\/ipt_REDIRECT...OK\nTesting iptable_nat\/ipt_DNAT...OK\n\nRESULT: csf should function on this server<\/pre>\nIf no fatal errors have been reported from your testing, the CSF firewall will work properly.<\/p>\n
<\/span>Step 3: Basic Configuration<\/span><\/h2>\nIf you want to configure the CSF firewall, you need to edit the configuration file ‘csf.conf’. In this tutorial, we will use nano as our editor, but you can freely use your favorite editor to edit the configuration file.<\/p>\n
nano \/etc\/csf\/csf.conf<\/pre>\nThe first thing we need to check in the configuration files are the open ports on your server. Typically, by default, the most commonly used ports are already open, but sometimes we need to open a specific port on the server. The following list is of the default opened ports by CSF:<\/p>\n
# Allow incoming TCP ports\nTCP_IN = \"20,21,22,25,53,80,110,143,443,465,587,993,995\"\n\n# Allow outgoing TCP ports\nTCP_OUT = \"20,21,22,25,53,80,110,113,443,587,993,995\"\n\n# Allow incoming UDP ports\nUDP_IN = \"20,21,53\"\n\n# Allow outgoing UDP ports\n# To allow outgoing traceroute add 33434:33523 to this list\nUDP_OUT = \"20,21,53,113,123\"<\/pre>\nIf some of the default listed services are not being used on your server, the best practice is to close them. The less access there is to ports on your server, the better the security of your server becomes.<\/p>\n
CSF offers many different options in the configuration files. But we will only cover some of the most commonly used settings.<\/p>\n
ICMP_IN<\/strong> – This option is to control the availability of your server from PING. By default, this setting is set to 1 so that it can allow all incoming PING requests. If you choose to disable this option, you will not be able to use an external monitoring system.<\/p>\nIGNORE_ALLOW<\/strong> – If you enable this option, LFD will ignore all IP addresses that are listed in the csf.allow<\/code> file. This option can be useful if you use a static IP address at home or in your office and you can be sure that this IP address will never be blocked from the server’s firewall.<\/p>\nSYNFLOOD<\/strong>, SUNFLOOD_RATE<\/strong> and SYNFLOOD_BURST<\/strong> – This option should be used only if your server is under a SYN flood attack. Please note that it is not recommended to enable this option because it will significantly slow down your server, and some visitors may have a connection problem.<\/p>\nLF_ALERT_TO<\/strong> and LF_ALERT_FROM<\/strong> – This is where you define the email addresses that you want to use for alerts.<\/p>\nTESTING<\/strong> – By default, TESTING is set to 1. This means that CSF enables a CRON job that clears your iptables configuration in case of configuration problems when you start CSF. This is a precautionary measure that should help you avoid getting locked out of your server. Once you are completely sure that all the settings are OK, you can set the value to zero which will enable the LFD service.<\/p>\nOnce you define all the settings you prefer, you can save the configuration file and restart and enable the CSF and LFD services, which causes them to start automatically upon boot.<\/p>\n
systemctl restart csf\nsystemctl enable csf\nsystemctl restart lfd\nsystemctl enable lfd<\/pre>\n<\/span>Step 4: CSF\/LFD notifications<\/span><\/h2>\nOne of the many options of the CSF LFD are the various notifications that can help you monitor the events on your server. In this section of the article, we’ll show you how to activate or disable some of these notifications.<\/p>\n
We will start with the system integrity alerts that check for changes in certain system files. These notifications help to detect compromised files, but you will also receive emails when changes are made with legitimate system updates.<\/p>\n
You can leave these types of notifications if you want to track changes made to your system. If you have decided not to receive these notifications, you can disable them by finding the LF_INTEGRITY<\/strong> parameter in the csf.conf file and set its value to 0, or you can simply run the following command:<\/p>\nsed -i 's\/LF_INTEGRITY = \"3600\"\/LF_INTEGRITY = \"0\"\/g' \/etc\/csf\/csf.conf<\/pre>\nThe second type of notification that we would like to talk about is the excessive resource usage alert. These types of notifications are probably the most common. LFD has a feature that can monitor processes running on your server and send you emails if they use too many resources. The purpose of these notifications is to track the most intensive processes which can cause loading issues on the server. If you have decided not to receive these notifications, you can disable them by finding the PT_USERMEM<\/strong> and PT_USERTIME<\/strong> parameter in the csf.conf file and set its value to 0 or you can simply run the following commands:<\/p>\nsed -i 's\/PT_USERTIME = \"1800\"\/PT_USERTIME = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/PT_USERMEM = \"512\"\/PT_USERMEM = \"0\"\/g' \/etc\/csf\/csf.conf<\/pre>\nThe Process Tracking option examines suspicious executable files or opened network ports on your server. These notifications help to detect potentially exploitative processes, even if these processes appear as system services. If you have decided to not receive these notifications, you can disable them by finding the PT_LIMIT<\/strong> parameter in the csf.conf file and set its value to 0 or you can simply run the following SSH command:<\/p>\nsed -i 's\/PT_LIMIT = \"60\"\/PT_LIMIT = \"0\"\/g' \/etc\/csf\/csf.conf<\/pre>\nThe last type of notifications that we will cover in this article are the alerts about the IP block.<\/p>\n
CSF\/LFD has the power to block IP addresses for certain reasons. Each time the system blocks an IP address, you will be notified by email containing the IP address that was blocked and the reason why it was blocked. If you want to manage these types of notification you can open your csf.conf file and find the following options:<\/p>\n
LF_EMAIL_ALERT<\/strong> – Send an email alert if an IP address is blocked by one of the [*] triggers<\/p>\nLF_PERMBLOCK_ALERT<\/strong> – If an IP address was blocked more than a few times (to configure, use LF_PERMBLOCK_COUNT<\/strong>) then this option will send an email notification when the IP address is permanently blocked.<\/p>\nLF_NETBLOCK_ALERT<\/strong> – If an IP network class was blocked, you will receive an email notification.<\/p>\nLF_DISTFTP_ALERT<\/strong> – If LF_DISTFTP<\/strong> is triggered, you will receive an email notification with IP addresses that have been blocked with a reason for being involved in an FTP distributed attack.<\/p>\nLF_DISTSMTP_ALERT<\/strong> – If LF_DISTSMTP<\/strong> is triggered, you will receive an email notification with IP addresses that have been blocked with a reason for being involved in an SMTP distributed attack.<\/p>\nLT_EMAIL_ALERT<\/strong> – If an account exceeds the number of daily logins per IP address, then you will receive an email alert.<\/p>\nCT_EMAIL_ALERT<\/strong> – If an IP address is blocked due to connectivity tracking, then you will receive an email alert. These types of email alerts are triggered by the CT_LIMIT<\/strong> feature. The\u00a0CT_LIMIT<\/strong> option is meant to protect your server from DOS attacks.<\/p>\nIf you choose not to receive these notifications, you can turn them off by finding them in the csf.conf file and setting their values to 0, or you can simply run the following commands:<\/p>\n
sed -i 's\/LF_EMAIL_ALERT = \"1\"\/LF_EMAIL_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/LF_PERMBLOCK_ALERT = \"1\"\/LF_PERMBLOCK_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/LF_NETBLOCK_ALERT = \"1\"\/LF_NETBLOCK_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/LF_DISTFTP_ALERT = \"1\"\/LF_DISTFTP_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/LF_DISTSMTP_ALERT = \"1\"\/LF_DISTSMTP_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/LT_EMAIL_ALERT = \"1\"\/LT_EMAIL_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf\nsed -i 's\/CT_EMAIL_ALERT = \"1\"\/CT_EMAIL_ALERT = \"0\"\/g' \/etc\/csf\/csf.conf<\/pre>\nOnce the changes have been made, you need to restart CSF and LFD using the following systemctl commands:<\/p>\n
systemctl restart csf \nsystemctl restart lfd<\/pre>\n<\/span>Step 5: Manage CSF from the Command Line<\/span><\/h2>\nYou can manage CSF from the command line or through the built-in Web User Interface. In this part of the tutorial, we will make a short list of useful commands that can help you manage CSF from your command line.<\/p>\n
Start, Stop and Restart\/Reload CSF:<\/p>\n
csf -s : Start csf<\/pre>\ncsf -f : Stop csf<\/pre>\ncsf -r : Reload\/Restart csf<\/pre>\nAllow an IP and add it to csf.allow (Whitelist an IP):<\/p>\n
csf -a 123.45.67.89<\/pre>\nOutput:<\/p>\n
Adding 123.45.67.89 to csf.allow and iptables ACCEPT...\nACCEPT all opt -- in !lo out * 123.45.67.89 -> 0.0.0.0\/0 \nACCEPT all opt -- in * out !lo 0.0.0.0\/0 -> 123.45.67.89<\/pre>\nRemove and delete an IP from csf.allow:<\/p>\n
csf -ar 123.45.67.89<\/pre>\nOutput:<\/p>\n
Removing rule...\nACCEPT all opt -- in !lo out * 123.45.67.89 -> 0.0.0.0\/0 \nACCEPT all opt -- in * out !lo 0.0.0.0\/0 -> 123.45.67.89<\/pre>\nDeny an IP and add to csf.deny (Blacklist an IP):<\/p>\n
csf -d 123.45.67.89<\/pre>\nOutput:<\/p>\n
Adding 123.45.67.89 to csf.deny and iptables DROP...\nDROP all opt -- in !lo out * 123.45.67.89 -> 0.0.0.0\/0 \nLOGDROPOUT all opt -- in * out !lo 0.0.0.0\/0 -> 123.45.67.89<\/pre>\nRemove and delete an IP from csf.deny (Unblock IP address):<\/p>\n
csf -dr 123.45.67.89<\/pre>\nOutput:<\/p>\n
Removing rule...\nDROP all opt -- in !lo out * 123.45.67.89 -> 0.0.0.0\/0 \nLOGDROPOUT all opt -- in * out !lo 0.0.0.0\/0 -> 123.45.67.89<\/pre>\nRemove and Unblock all IP addresses from csf.deny:<\/p>\n
csf -df<\/pre>\nOutput:<\/p>\n
DROP all opt -- in !lo out * 123.45.67.88 -> 0.0.0.0\/0 \nLOGDROPOUT all opt -- in * out !lo 0.0.0.0\/0 -> 123.45.67.88 \nDROP all opt -- in !lo out * 123.45.67.89 -> 0.0.0.0\/0 \nLOGDROPOUT all opt -- in * out !lo 0.0.0.0\/0 -> 123.45.67.89 \ncsf: all entries removed from csf.deny<\/pre>\nSearch for a pattern match in iptables e.g: IP, CIDR, Port Number:<\/p>\n
csf -g 123.45.67.89<\/pre>\nOutput:<\/p>\n
Table Chain num pkts bytes target prot opt in out source destination\nfilter DENYIN 1 0 0 DROP all -- !lo * 123.45.67.89 0.0.0.0\/0\nfilter DENYOUT 1 0 0 LOGDROPOUT all -- * !lo 0.0.0.0\/0 123.45.67.89<\/pre>\nDisplays the current list of temporary allow and deny IP entries with their TTL and comment:<\/p>\n
csf -t<\/pre>\nCheck for updates to csf and upgrade if available:<\/p>\n
csf -u<\/pre>\nDisable CSF and LFD completely:<\/p>\n
csf -x<\/pre>\nEnable CSF and LFD if previously disabled:<\/p>\n
csf -e<\/pre>\nShow the CSF version:<\/p>\n
csf -v<\/pre>\nOutput:<\/p>\n
csf: v13.04 (generic)<\/pre>\nFor more options you can check the following command:<\/p>\n
csf -h<\/pre>\n