Every server with a public IP address faces constant automated probing. Within hours of bringing a fresh Linux virtual machine online, automated bots start scanning port 22, attempting thousands of common username and password combinations. These are not targeted attacks by skilled hackers; they are automated scripts sweeping millions of IP addresses at random.
You can eliminate nearly all of this automated noise using two straightforward layers: changing your default SSH port to a non-standard number (so blind scanners miss you entirely) and installing Fail2Ban, an automated watcher that bans IP addresses after repeated failed logins.
Step 1: Update Your System Packages
Before modifying security configurations, update your system repositories and packages to ensure you have the latest patches:
# On Debian / Ubuntu
sudo apt update && sudo apt upgrade -y
# On AlmaLinux / Rocky Linux / RHEL / Fedora
sudo dnf upgrade -y
Step 2: Change the Default SSH Port
SSH defaults to port 22. You can switch this to any unused port between 1024 and 65535 (for example, port 21348).
Open the SSH daemon configuration file in a text editor:
sudo nano /etc/ssh/sshd_config
Find the line that says #Port 22. Remove the # symbol to uncomment it, and change the number to your chosen port:
Port 21348
Press Ctrl+O to save, then Ctrl+X to exit.
Note for Ubuntu 22.10 and 24.04 LTS users: Modern Ubuntu uses systemd socket activation for SSH. If your system runs ssh.socket, editing sshd_config alone will not change the listening port. To ensure your custom port is respected, disable the socket and run the standard service:
sudo systemctl disable --now ssh.socket
sudo systemctl enable --now ssh.service
Step 3: Update Your Firewall BEFORE Restarting SSH
Never restart the SSH service without first opening your new port in the firewall. Otherwise, you will lock yourself out of your server immediately.
If you use UFW (Ubuntu / Debian):
sudo ufw allow 21348/tcp comment 'Custom SSH Port'
sudo ufw status
If you use firewalld (AlmaLinux / Rocky / CentOS):
sudo firewall-cmd --permanent --add-port=21348/tcp
sudo firewall-cmd --reload
Step 4: Install and Configure Fail2Ban
Fail2Ban monitors authentication log files and creates temporary firewall rules to block IPs that repeatedly fail authentication.
Install Fail2Ban:
# Debian / Ubuntu
sudo apt install fail2ban -y
# RHEL / AlmaLinux / Rocky (requires EPEL repository)
sudo dnf install epel-release -y
sudo dnf install fail2ban -y
Fail2Ban includes a default configuration file named jail.conf. Because package updates can overwrite this file, create a local copy named jail.local where your custom rules will live:
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Scroll to the [DEFAULT] section to define lockouts, and configure your [sshd] jail. Ensure the port matches your newly chosen SSH port:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 3
[sshd]
enabled = true
port = 21348
backend = systemd
In this configuration, any IP address that fails login 3 times within a 10-minute window is banned from connecting for 1 hour. Save the file with Ctrl+O and exit with Ctrl+X.
Step 5: Restart Services and Test (Do Not Close Your Session!)
Apply the changes by restarting both services:
sudo systemctl restart ssh || sudo systemctl restart sshd
sudo systemctl restart fail2ban
sudo systemctl enable fail2ban
Check that Fail2Ban is actively monitoring your jail:
sudo fail2ban-client status sshd
Crucial Rule: Do not close your current terminal window. Open a completely new terminal window on your local machine and test connecting with the new port flag (-p):
ssh -p 21348 yourusername@your_server_ip
If you can log in successfully, everything is configured properly. If the connection hangs or refuses, switch back to your open first window to correct any typos or firewall blocks.
Next Level: Switch to SSH Keys and Turn Off Passwords
Changing ports and running Fail2Ban stops automated sweeps. To achieve near-complete immunity against brute-force attacks, replace passwords with cryptographic keys.
- Generate an Ed25519 key pair on your local computer:
ssh-keygen -t ed25519 - Copy your public key to the server:
ssh-copy-id -p 21348 yourusername@your_server_ip - Verify that you can log in without typing your server user password.
- Once verified, open
/etc/ssh/sshd_configon the server, setPasswordAuthentication no, and restart SSH.
With password authentication disabled, bots cannot brute-force your accounts regardless of how many guesses they make.