Back to articles
Security & Anti-DDoS

Basic VPS DDoS Protection with Fail2Ban, Cloudflare, and iptables

VPS owners must protect their servers from L3/L4 and L7 DDoS attacks using tools like Fail2Ban, Cloudflare, and iptables. This guide provides a basic security setup, including...

DPTCloud Engineering5 min read
Basic VPS DDoS Protection with Fail2Ban, Cloudflare, and iptables

Image credit: 81349 · Pixabay Content License

VPS owners must protect their servers from L3/L4 and L7 DDoS attacks using tools like Fail2Ban, Cloudflare, and iptables. This guide provides a basic security setup, including firewall configuration, connection limiting, and leveraging high-level network security services such as Cloudflare to filter malicious traffic while hiding the server's real IP. Steps are performed on Linux systems and are compatible with DPTCloud's VPS services, which offer Anycast L3/L4/L7 network defense and a 99.98% SLA infrastructure.

Overview of VPS DDoS Protection

DDoS (Distributed Denial of Service) attacks are among the most common threats to VPS servers, especially when the real IP is exposed. To mitigate risks, a layered defense strategy is essential: server-level firewall (iptables), access limitation (Fail2Ban), and network-level protection (Cloudflare). Cloudflare filters traffic at the edge, reducing server load and concealing the real IP address. iptables manages packets at the operating system level, while Fail2Ban automatically blocks IP addresses exhibiting suspicious behavior. These tools work together to provide comprehensive protection, suitable for DPTCloud's VPS offerings, which include a robust Network Security & 99.98% SLA Infrastructure.

Setting Up Cloudflare to Protect VPS IP

To begin, transfer your domain to Cloudflare to leverage its L3/L4 and L7 protection features. The basic steps include:

  1. Create an account at Cloudflare and add your domain.
  2. Update your domain registrar’s DNS settings to use Cloudflare’s DNS servers.
  3. In the Cloudflare dashboard, configure the "Proxied" mode for records you want to protect (e.g., A, CNAME).
  4. Enable security features: Firewall rules, DDoS protection, and Web Application Firewall (WAF) to block malicious requests.

Cloudflare processes incoming traffic before it reaches the VPS, reducing load and blocking attacks such as SYN floods, UDP floods, or HTTP floods. The server's real IP remains hidden, protecting it from direct attacks. This service is well-suited for DPTCloud’s Cloud VPS with L3/L4/L7 Anycast Anti-DDoS solutions.

Configuring iptables and Fail2Ban on VPS

On the VPS server, iptables is the default firewall for controlling traffic. Below are basic rules to block common attacks:

iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT
iptables -A INPUT -p tcp --dport 80 -m limit --limit 50/minute --limit-burst 100 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m limit --limit 50/minute --limit-burst 100 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -j DROP

The rules above:

  • Limit SSH connections to 3 simultaneous connections to prevent brute-force attacks.
  • Limit 50 requests per minute on ports 80 and 443 to reduce HTTP flood load.
  • Accept established connections and drop all remaining packets.

Fail2Ban automates IP blocking for malicious behavior. Install and configure it as follows:

sudo apt update
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

In the `/etc/fail2ban/jail.local` file, configure bans for services such as SSH, Nginx, or Apache. For example:

[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

Fail2Ban monitors logs and blocks IPs after three failed login attempts within one hour. This configuration helps reduce risks from brute-force attacks and L7 attacks.

Testing and Troubleshooting

To verify the effectiveness of your protection, perform the following steps:

  1. Check iptables status: sudo iptables -L.
  2. Check Fail2Ban status: sudo fail2ban-client status sshd.
  3. Test access from another machine and review logs at /var/log/fail2ban.log.
  4. Run tools such as test-ipv6.com or Digicert SSL checker to verify connectivity.

If protection is not effective, check:

  • Whether Cloudflare is active (DNS records are set to Proxied).
  • If iptables rules are loaded correctly (check with sudo iptables -L).
  • If Fail2Ban is running (check with sudo systemctl status fail2ban).
  • If logs are being overwritten (check /var/log/auth.log or /var/log/nginx/error.log).

Missing or incorrect configuration in the config files may prevent protection layers from working. For more detailed guidance on self-hosting high-security services, refer to the Guide to Self-Hosting Moodle on High-Security Docker Container Clusters for Enterprises.

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478