Tous les articles

security

How to Set Up Fail2ban to Stop Brute-Force Attacks

A plain-English guide to using fail2ban to block repeated login attempts before they become a real problem.

  • security
  • ssh
  • fail2ban
A Fail2ban shield blocks repeated SSH login attempts before they reach a VPS server and adds them to a ban list.

You check your server logs and see the same thing over and over: strangers trying passwords on your SSH login all day long.

Short version: to set up fail2ban, install the fail2ban package, enable an SSH jail, choose how many failed logins are allowed, and restart the service. fail2ban watches login logs like a doorman watching a guest list; when an address fails too many times, it blocks that address for a while.

What does fail2ban actually do?

fail2ban is a small security tool that watches log files for repeated failures. The common target is SSH, the remote login service you use to administer a server.

Think of SSH as the front door. A firewall decides which doors exist at all. fail2ban watches the people knocking on an allowed door and throws out anyone who keeps guessing wrong.

A repeated password-guessing attack is called a brute-force attack. It is not clever. It is more like someone trying every key on a huge keyring. fail2ban helps by slowing that down and banning noisy attackers automatically.

It does not replace strong passwords, SSH keys, updates, or a firewall. If you have not handled the basic network doorways yet, start with setting up a firewall and then add fail2ban as the next layer.

How do you set up fail2ban for SSH?

On most Debian or Ubuntu servers, the basic install is simple:

bashsudo apt update
sudo apt install fail2ban

After installation, fail2ban reads rules called jails. A jail is just a watch rule: look at this service, count failures this way, and ban addresses that cross the line.

Do not edit the default jail.conf file directly. Treat it like the printed manual that came in the box. Put your local changes in jail.local, so future updates do not overwrite your choices.

Create or edit the file:

bashsudo nano /etc/fail2ban/jail.local

For a basic SSH setup, add:

ini[sshd]
enabled = true
port = ssh
maxretry = 5
findtime = 10m
bantime = 1h

Here is what those lines mean in normal language:

  • enabled = true turns on protection for SSH.
  • port = ssh tells fail2ban to protect the normal SSH port.
  • maxretry = 5 allows five failed attempts.
  • findtime = 10m counts those failures within ten minutes.
  • bantime = 1h blocks the address for one hour.

Then restart fail2ban:

bashsudo systemctl restart fail2ban
sudo systemctl enable fail2ban

You can check that the service is alive with:

bashsudo systemctl status fail2ban

The exact entity you want to see running is fail2ban.service.

What settings keep you from locking yourself out?

The biggest beginner fear with fail2ban is fair: you do not want your own address banned because you mistyped a password while tired.

That is what ignoreip is for. It is a small allowlist of addresses fail2ban should never ban. If you have a stable home or office IP address, you can add it:

ini[DEFAULT]
ignoreip = 127.0.0.1/8 your.ip.address.here

Use this carefully. Adding the whole internet would be like hiring a doorman and then telling them everyone is family.

You should also avoid making bans too aggressive at first. A one-hour bantime and five tries in ten minutes is a reasonable starting point for many small servers. Once you understand your real traffic, you can tighten it.

If you changed your SSH port, make sure port = ssh still matches your system. Otherwise fail2ban may be watching the wrong doorway.

And remember the bigger safety net: a backup is what saves you when a security change goes badly or a server needs to be rebuilt. If you have not tested restores yet, read how to back up your server before you stack on more changes.

How do you know fail2ban is working?

The main check is:

bashsudo fail2ban-client status

You should see sshd listed as an active jail. Then inspect that jail:

bashsudo fail2ban-client status sshd

This shows how many failures fail2ban has seen and which addresses are currently banned.

If the banned list is empty, that is not automatically bad. It may simply mean nobody has crossed your limit since fail2ban started. The useful sign is that the sshd jail exists, the service is running, and your logs are being read.

On many systems, SSH failures appear in /var/log/auth.log. On some newer setups, they live in the system journal instead. If fail2ban is running but never sees attempts, the issue is often the log path. That is like pointing a security camera at the wrong hallway.

The shortcut

Server Manager helps by keeping these security layers understandable instead of scattered across old notes, half-remembered files, and one-off terminal history.

For the problems in this post, the outcome is simple: you are less likely to leave SSH exposed without rate-limiting, less likely to forget which server has fail2ban active, and less likely to lose track of why a rule was added months later. The setup stays legible, which matters when you come back under pressure.

The real benefit is not avoiding every technical detail. It is having a server that still makes sense after the initial setup day, so security changes do not turn into mystery machinery.

FAQ

Does fail2ban stop all brute-force attacks?

No. It reduces repeated guessing by banning noisy addresses. You still need strong authentication, updates, and a firewall.

Should I use SSH keys with fail2ban?

Yes. SSH keys are usually safer than password login. fail2ban is an extra guard, not the main lock.

Can fail2ban block real users?

Yes, if they fail login too many times. Use sensible limits and add trusted fixed addresses to ignoreip when appropriate.

What is the command to check fail2ban?

Use sudo fail2ban-client status to see active jails, then sudo fail2ban-client status sshd for SSH details.

How do you stay safer after fail2ban is running?

Once fail2ban is active, your server is no longer politely accepting endless wrong guesses without consequence. That is a good step.

Keep the rest simple: use SSH keys, keep packages updated, run a firewall, and make sure you can restore from backup. fail2ban is the doorman, not the whole building.

Your win is a quieter login surface and a setup you can understand later. Fewer repeated attacks get to keep knocking, and you are not relying on memory alone to know how your server is protected.