How to Use iptables on a Linux Cloud Server to Block a High Number of Concurrent Connections in a Short Time?

11-01-2024 02:46:43

iptables is a firewall software that operates on the Linux operating system, and it is compatible with various Linux distributions, such as CentOS and Ubuntu. This article explains how to use iptables to block a large number of concurrent connections within a short period. The methods described can also be utilized to defend against some basic forms of DDoS attacks.

Step 1: Install iptables

Most Linux operating systems come with iptables pre-installed. Use the following command to verify if iptables is installed:

which iptables

If it returns a path similar to /sbin/iptables, it indicates that iptables has been successfully installed. If no path is returned, use the following commands to install:

For CentOS:

yum install iptables

For Debian/Ubuntu:

apt-get install iptables iptables-persistent

Step 2: Create iptables Rules

Monitor incoming connections on the eth0 interface and port 80:

iptables -I INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set

If there are more than 10 new incoming connections within 60 seconds, drop them:

iptables -I INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 -j DROP

Step 3: Save iptables Rules

After creating iptables rules, it is necessary to save and load iptables to ensure that the rules are permanently effective.

service iptables-persistent save
service iptables-persistent reload

This concludes the method for using iptables on a Linux cloud server environment to block a high number of concurrent connections in a short time.