fail2ban protects SSH by reading sshd authentication logs and blocking IPs that fail too often at the firewall. For SSH you only need /etc/fail2ban/jail.local with the [sshd] jail enabled, a low maxretry and a sensible bantime. You then verify the result with fail2ban-client status sshd.

How fail2ban blocks SSH brute-force attempts

fail2ban reads sshd authentication logs and runs them through a regex filter. When an IP collects maxretry failures within findtime, fail2ban runs an action that blocks it at the firewall for bantime. When that time runs out, the rule is removed automatically.

Be clear about the scope. fail2ban cuts the noise and cost of automated attempts against TCP port 22, but it does not replace key-based authentication. If the server accepts weak passwords, an attacker who spreads attempts across many IPs can keep trying below each address's threshold.

Configuring the fail2ban sshd jail in jail.local

The basic rule is never to edit /etc/fail2ban/jail.conf, because package upgrades overwrite it. Your changes belong in /etc/fail2ban/jail.local, which fail2ban reads afterwards and which takes precedence.

On Debian or Ubuntu, install the package and open the file:

sudo apt install fail2ban
sudo nano /etc/fail2ban/jail.local

A minimal, realistic SSH configuration:

[DEFAULT]
ignoreip = 127.0.0.1/8 ::1
bantime  = 1h
findtime = 10m
maxretry = 3
bantime.increment = true

[sshd]
enabled = true
port    = ssh
backend = systemd

port = ssh means port 22. If sshd listens elsewhere, put the real number (port = 2222); otherwise bans land on a port nobody uses. backend = systemd makes fail2ban read the journal on distributions that do not write /var/log/auth.log. If your system does write that file, you can drop the line. Enable the service, then restart it after every change:

sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban

On a phone, saving and quitting nano takes Ctrl+O and Ctrl+X. The keyboard in termi1, an SSH terminal for iOS and Android, has Ctrl, Esc, Tab and arrow keys, so editing jail.local does not send you hunting for a laptop.

What bantime, findtime and maxretry control in fail2ban

maxretry is the number of failures allowed, findtime is the window they are counted in, and bantime is how long the block lasts. The last two accept time suffixes such as m, h or d.

  • maxretry = 3: with key authentication, a legitimate user almost never fails three times in a row; a bot does.
  • findtime = 10m: three failures spread over an hour do not count; three within ten minutes do.
  • bantime = 1h: long enough for the bot to move on, short enough not to lock you out for a whole day if you slip up yourself.
  • bantime.increment = true: every repeat offence from the same IP lengthens the next ban.

A negative bantime (-1) makes bans permanent. It is tempting, but the list grows without limit, and a dynamic IP later reassigned to a legitimate user stays blocked forever.

Checking fail2ban with fail2ban-client status sshd

sudo fail2ban-client status sshd prints the jail state. The Filter section shows current and total failures; the Actions section shows currently banned IPs, total bans and the list of addresses.

sudo fail2ban-client status
sudo fail2ban-client status sshd
sudo fail2ban-client get sshd bantime
sudo fail2ban-client set sshd unbanip 203.0.113.45
  • status with no arguments lists active jails. If sshd is missing, enabled = true is absent or there is a syntax error; check sudo journalctl -u fail2ban.
  • status sshd showing zero total failures while the journal shows failed logins usually means the wrong backend or log path.
  • get sshd bantime confirms the value fail2ban actually loaded, not the one you think you typed.
  • set sshd unbanip lifts the ban on a single IP.

In termi1 you can dictate "unban 203.0.113.45 in fail2ban": the AI types the command at the prompt without pressing Enter, and in Review mode it never runs it for you. On a connection tagged as production, Production Guard holds back anything that stops or changes something, such as systemctl restart, before it reaches the prompt.

fail2ban versus other SSH defences: comparison table

The defence that stops password brute-forcing outright is PasswordAuthentication no, documented in the official sshd_config reference. fail2ban is a complementary layer.

MeasureWhat it stopsWhat it does not stopRisk of locking yourself out
fail2ban sshd jailBots retrying from a single IPAttacks spread across many IPsMedium if you fail several times in a row
Keys only (PasswordAuthentication no)Password guessingNoise in the logsLow if you keep a second authorised key
Moving SSH off port 22The most basic automated scansA full port scanLow; remember to update port in jail.local
Mesh private network, no public portAny direct attempt from the internetAccess from inside the meshLow while the mesh is working

Stopping fail2ban from banning your own SSH connection from a phone

ignoreip lists addresses fail2ban never bans, and it is your main guard against banning yourself. The catch on a phone is that the IP changes: office wifi may have a fixed address, but you cannot whitelist mobile data in advance.

  1. Keep a fallback path before touching the config. Your provider's web console or another network lets you run unbanip if you get locked out.
  2. Use keys, not passwords. No password to type means no typos adding up to failures. termi1 generates the key on the phone itself, stores it in the system's secure storage, and the private key never leaves the device.
  3. Come in over a mesh private network. With a mesh private network without open ports such as Prialo, or with Tailscale, you arrive from a mesh IP that does not change. Add its range to ignoreip, or stop exposing port 22 to the internet at all.
  4. Understand how Mosh connects. Mosh starts mosh-server through an initial SSH connection, and failures in that login count towards the sshd jail. After that, traffic runs over UDP ports 60000-61000, which the jail does not watch. termi1 keeps the Mosh session alive when you switch from wifi to mobile data, which means fewer reconnects and fewer chances to fail.
  5. Run long jobs inside tmux. If a rule cuts you off, whatever was running is still there when you get back in.

Frequently asked questions

Where do you configure fail2ban for SSH?

fail2ban for SSH is configured in /etc/fail2ban/jail.local, inside an [sshd] section with enabled = true. Leave jail.conf alone, because package upgrades overwrite it. After every change, restart the service with sudo systemctl restart fail2ban so the new values are loaded.

What bantime should you use for SSH?

A one-hour bantime combined with bantime.increment = true balances protection and convenience well. Bots move on, repeat offenders get progressively longer bans, and a mistaken self-ban does not cost you days of access. Setting bantime to -1 makes every ban permanent.

How do you unban an IP in fail2ban?

You unban an IP in fail2ban with sudo fail2ban-client set sshd unbanip followed by the address. First confirm with sudo fail2ban-client status sshd that it appears in the banned IP list. If the banned address is yours, run it from another network or your provider's console.

Does fail2ban also protect Mosh connections?

fail2ban protects the SSH stage of Mosh, because mosh-server is started through an initial SSH connection whose failures sshd logs. Subsequent traffic travels over UDP ports 60000-61000, which the sshd jail does not watch, so restrict that range in your firewall instead.