Public SSH servers are constantly scanned and brute-forced. Changing the SSH port is not real security by itself, but it reduces background noise. fail2ban adds an actual defensive layer by watching failed login attempts and temporarily banning abusive IP addresses.
This note records a minimal setup for SSH protection with fail2ban and systemd logs.
Install Fail2ban
Arch Linux:
sudo pacman -S fail2banDebian/Ubuntu:
sudo apt install fail2banEnable the service:
sudo systemctl enable --now fail2banOptional: Change SSH Port
Edit the SSH daemon config:
sudo vim /etc/ssh/sshd_configSet a non-default port. This article uses 22222 as an example; replace it with your own value.
Port 22222Before restarting SSH, keep your current SSH session open. A bad config or firewall mistake can lock you out.
Validate the config:
sudo sshd -tRestart SSH:
sudo systemctl restart sshdSome distributions use ssh instead of sshd as the service name:
sudo systemctl restart sshTest login from another terminal before closing the old session:
ssh -p 22222 user@example.comConfigure Fail2ban For SSH
Create a local jail config instead of editing the packaged defaults:
sudo vim /etc/fail2ban/jail.d/sshd.localMinimal config:
[sshd]enabled = trueport = 22222backend = systemdIf you keep SSH on the default port, use:
port = sshRestart fail2ban:
sudo systemctl restart fail2banCheck Status
List enabled jails:
sudo fail2ban-client statusCheck the SSH jail:
sudo fail2ban-client status sshdView logs:
sudo journalctl -u fail2ban -eOn systems where fail2ban logs to a file:
sudo less /var/log/fail2ban.logUnban An IP
If you accidentally ban yourself, unban the IP from another trusted session:
sudo fail2ban-client set sshd unbanip 203.0.113.10203.0.113.10 is a documentation example address. Replace it with the real IP you need to unban.
Safer SSH Baseline
Fail2ban is only one layer. These SSH settings are usually worth enabling too:
PermitRootLogin noPasswordAuthentication noPubkeyAuthentication yesAfter changing SSH config, always validate and restart:
sudo sshd -tsudo systemctl restart sshdNotes
- Do not rely on port changes as the only protection.
- Prefer SSH keys over passwords.
- Keep an existing SSH session open while changing SSH and firewall settings.
- If a firewall is enabled, allow the SSH port before restarting SSH.
- Use
/etc/fail2ban/jail.d/*.localfiles for local overrides so package updates do not overwrite your changes.