Marcio Cunha

How to block suspicious connections in Linux with iptables and fail2ban

Learn how to configure Linux firewall rules and real-time log scanning to mitigate automated brute-force attacks on exposed servers.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Iptables acts as a digital building doorman, filtering network packets based on predefined rules.
  • Brute-force attacks exploit weak passwords by repeatedly trying to guess remote SSH access in automated ways.
  • Fail2ban monitors system log files and bans IP addresses after multiple consecutive authentication failures.
  • Restrictive firewall rules prevent unwanted traffic before it consumes vital application resources.
  • Combining active monitoring and dynamic blocking drastically reduces the risk of successful intrusions.

Why servers on the internet face constant attacks

Any server connected directly to the public internet starts receiving intrusion attempts just minutes after activation. Automated bots scan entire ranges of IP addresses looking for open ports, especially the default remote access port, SSH. In practice, this means hundreds of malicious computers try to guess your system password day and night without stopping.

To protect your infrastructure against this wave of unwanted access, we need to rely on native Linux operating system tools. The kernel firewall and log monitoring utilities form an insurmountable barrier against brute-force bots. If you have ever had to debug incidents involving stuck containers inspecting Docker logs, you know that log analysis is vital for understanding failures and attacks.

Understanding the role of iptables in network security

Iptables is the traditional program used in Linux to configure the kernel's network packet filtering tables. In practice, it works as a set of strict rules determining which connections can enter, which must leave, and which need to be silently dropped. When a packet arrives at the network interface, the system evaluates it line by line against these guidelines.

Configuring iptables requires care to avoid locking yourself out of the server. For instance, before blocking any incoming traffic, it is crucial to ensure that established connections and your own terminal port remain open. To deepen infrastructure hardening strategies, it is worth checking methods on how to block suspicious traffic with nftables in modern environments.

How to create basic firewall rules to block access

The practical application of firewall rules involves commands executed directly in the terminal with superuser privileges. Let us structure a simple procedure to close unnecessary ports and protect your Linux server's remote access against invaders.

Execute the following steps to configure a restrictive default policy and allow only the essentials:

  1. Set the default policy to drop all incoming traffic that is not explicitly authorized:
    iptables -P INPUT DROP
  2. Allow packets from already established or related connections to keep flowing normally without interruptions:
    iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -J ACCEPT
  3. Open access to your custom or default SSH port so you do not lose remote control of the machine:
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT

Detecting anomalous behavior with fail2ban

Although iptables is excellent for blocking ports, it lacks native intelligence to identify when a specific IP address is repeatedly trying to guess passwords. This is where fail2ban comes in, a program written in Python that watches system log files in real time. Whenever it detects a suspicious pattern—such as five login failures in under two minutes—the program automatically adds a temporary rule to the firewall to ban that IP.

This automated approach lightens human labor and responds with impressive speed to distributed attacks. For specific scenarios focused on web applications, you can also apply similar strategies by checking the guide on how to block malicious IPs in Nginx with Fail2Ban.

Step-by-step guide to installing and configuring fail2ban

Configuring fail2ban is straightforward and relies on cleanly organized text configuration files. Let us perform the installation and initial adjustments to secure the SSH service against automated brute-force attacks.

Follow the steps below on your server:

  1. Install the fail2ban package using your Linux distribution's package manager:
    sudo apt update && sudo apt install fail2ban -y
  2. Create a copy of the default configuration file to prevent your changes from being lost during future updates:
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  3. Open the local configuration file and enable the SSH jail by adjusting the ban time and retry limit parameters:
    sudo nano /etc/fail2ban/jail.local

Monitoring bans and validating rule effectiveness

After getting the dynamic blocking system running, it is important to know how to inspect its daily behavior. Fail2ban maintains detailed logs of all actions taken against malicious IP addresses. You can verify which hosts are currently banned by checking the status of the jail configured for the remote access service.

The status command displays the total number of blocked attempts and the exact list of temporary intruders isolated from your network. Keeping up this frequent monitoring ensures false positives do not lock out legitimate users and proves that the security barrier is performing its role efficiently.

Final considerations on perimeter security in Linux

Securing an internet-exposed server requires an active posture and the combined use of complementary tools. Iptables establishes the static perimeter fence, while fail2ban acts as an automated watchman responding instantly to ongoing threats. Keeping these defenses active drastically reduces your infrastructure's attack surface, ensuring stability and peace of mind for daily operations.

Investing time in proper firewall configuration and log monitoring is an indispensable step for any engineer or tech enthusiast managing their own systems. With these practices implemented, your server will be much better prepared to resist the constant automated scans populating the world wide web.