# sshd_config and Fail2ban

> How to switch off password and root logins in sshd_config. How to check the change took hold, and ban guessers with fail2ban.

**Course:** [SSH Keys and Logins](https://learn.benibix.com/learn/infrastructure-ssh)  
**Time:** 14 min

**After this lesson you can:**

- Switch off password login safely
- Check the setting sshd really uses
- Install fail2ban to ban guessers

## How to shut out password guessers

Any server on the internet gets password guesses on port 22 within minutes of starting. The fix is in sshd_config, the SSH settings file in /etc/ssh. Two lines matter most. One switches off all password logins, so only keys work: PasswordAuthentication no. The other keeps root out: PermitRootLogin no.

Password authentication is the old way to log in, by typing a password. A bot can try thousands of them a day. With it switched off, the bot has nothing to guess, because no password will ever be accepted.

The file belongs to root, so you edit it with sudo. Before you reload sshd, check the file with sudo sshd -t. It prints nothing if all is well, and names the line if not.

Keep your current login open while you test. Open a second window and log in again. If that works, you are safe. If it fails, you still have the first window to undo the change.

Ubuntu has a trap here. Small files in /etc/ssh/sshd_config.d are read first, and the first value sshd finds for each setting wins. A file left by the host can say PasswordAuthentication yes and beat your line. sudo sshd -T prints the values sshd really uses.

Fail2ban adds a guard. It reads the login log and counts failures from each address. By default, 5 failures within 10 minutes get that address banned for 10 minutes. On Ubuntu it watches SSH as soon as it is installed.

A common mistake is to switch off passwords before your key works. Then nobody can get in. Log in with the key first, in a second window, and only then change the setting.

```console
$ sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin)'
passwordauthentication no
permitrootlogin no
$ sudo systemctl reload ssh
```
Ask sshd which values it really uses, then reload it. The current login stays open.

![Two bots on the left ask password? at a server's SSH door, while a laptop with a key gets in. Inside the server, the file sshd_config shows PasswordAuthentication no and PermitRootLogin no. A guard marked fail2ban reads the log, counts failures and bans the guessing address. Key login is allowed.](https://learn.benibix.com/diagrams/inf3-ssh-hardening.svg)

### Example: Emma checks her log

Emma's new server is 2 days old. Its login log already shows 4,300 failed password guesses from 160 addresses.

She logs in with her key in a second window, sets PasswordAuthentication no, and confirms it with sudo sshd -T. Then she installs fail2ban. The guesses still arrive, but none can ever work.

**Words in this lesson:** [sshd_config](https://learn.benibix.com/term/infrastructure/sshd-config), [SSH Password Authentication](https://learn.benibix.com/term/infrastructure/ssh-password-authentication), [sudo](https://learn.benibix.com/term/infrastructure/sudo), [Fail2ban](https://learn.benibix.com/term/infrastructure/fail2ban)

**Question:** You set PasswordAuthentication no, but passwords still work. What do you check?

**Answer:** sudo sshd -T, to see if another file set it first. sshd keeps the first value it reads, and files in sshd_config.d are read first. sshd -T prints the value it really uses.

---

Source: https://learn.benibix.com/learn/infrastructure-ssh/sshd-config-and-fail2ban
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
