Learning how to secure SSH properly is one of the highest-return jobs on a new server. SSH is the front door to your machine, it listens on a public port, and automated scanners probe it within minutes of a server going online. The good news is that a handful of changes close off nearly all of the common attacks.
This guide walks through the three moves that matter most: switching to key-only authentication, moving SSH off the default port, and adding Fail2ban to block brute-force attempts. They work together, and each one is quick to apply.
How to Secure SSH Step by Step
Before changing anything, open a second SSH session to the server and leave it connected. If a config change locks you out, that open session is your way back in. Test every change from a new connection before closing the old one.
Step 1: Switch to SSH Key Authentication
Password authentication is the weakest part of a default SSH setup. Passwords can be guessed, reused, or leaked. SSH keys come in a pair: a private key that stays on your computer and a public key that sits on the server. Logging in proves you hold the private key, and nobody can guess a 4096-bit key the way they can guess a password.
Generate a key on your local machine if you do not have one:
ssh-keygen -t ed25519 -C "[email protected]"
Then copy the public key to the server:
ssh-copy-id user@your-server-ip
Confirm you can log in with the key, then disable password authentication in /etc/ssh/sshd_config:
PasswordAuthentication no
ChallengeResponseAuthentication no
PubkeyAuthentication yes
Restart the SSH service and verify from a new session that key login works and password login is refused. If you manage several servers, keep your private key protected with a passphrase and back it up somewhere safe, losing it means losing access. For a broader hardening pass beyond SSH, the same discipline applies server-wide, as covered in a fresh Ubuntu server hardening checklist.
Step 2: Disable Root Login and Limit Who Can Connect
Attackers always try root first, so do not let root log in over SSH at all. Set this in the same config file:
PermitRootLogin no
Then narrow access further with AllowUsers, listing only the accounts that should ever connect:
AllowUsers deploy admin
Anyone not on the list gets refused before authentication even starts. This pairs well with a host firewall: if you have not set one up yet, a UFW and Fail2ban setup guide walks through allowing only the ports you actually need.
Step 3: Change the Default SSH Port
Moving SSH from port 22 to a custom port does not make your server unhackable, and anyone who says otherwise is overselling it. What it does is cut out nearly all of the automated noise: most scanners only knock on port 22. Fewer failed login attempts means cleaner logs and less load from brute-force bots.
Pick a port above 1024, for example 2222 or 22022, and set it in sshd_config. A custom SSH port also keeps your logs cleaner, since most automated scanners never knock there.
Port 2222
Update your firewall to allow the new port before restarting SSH, or you will lock yourself out. Then connect with the port flag:
ssh -p 2222 user@your-server-ip
Update any deployment scripts, monitoring tools, and CI jobs that connect over SSH, because they will all need the new port. Port changes are a filter, not a lock, so treat them as one layer among several.
Step 4: Install Fail2ban to Block Brute-Force Attempts
Fail2ban watches your authentication logs and temporarily bans IP addresses that rack up failed logins. It turns a persistent brute-force campaign into a series of short bans that never get anywhere.
Install it and enable the SSH jail:
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
On Ubuntu, Fail2ban ships with an SSH jail enabled by default. Check banned addresses with:
sudo fail2ban-client status sshd
If you moved SSH to a custom port, make sure the jail watches the right port, either by setting the port in the jail configuration or by using the default SSH jail with your port listed. A jail pointed at port 22 while SSH listens on 2222 bans nobody. For production servers, the Ubuntu 24.04 production hardening checklist covers Fail2ban alongside the other controls worth enabling before a server takes real traffic.
Step 5: Keep SSH Itself Updated and Tight
A few smaller settings round out the setup. Disable X11 forwarding and agent forwarding unless you actually use them, since both extend trust beyond the session. Turn off empty passwords entirely. And keep the system updated: SSH vulnerabilities do appear, and an unpatched daemon undermines every other control.
X11Forwarding no
AllowAgentForwarding no
PermitEmptyPasswords no
Review your authentication logs occasionally. A sudden spike in failed attempts from new networks is worth a look, and Fail2ban’s ban list tells you whether the bans are working. If your team connects from varied locations, a private VPN can shrink the exposed surface further, for example by running WireGuard on your own VPS and only allowing SSH from the VPN network.
A Quick Checklist Before You Call It Done
- Key-only authentication works from a fresh session, and password login is refused.
- Root login over SSH is disabled.
- Only named users can connect.
- SSH listens on a custom port, and the firewall allows it.
- Fail2ban is running and its SSH jail watches the correct port.
- Deployment scripts, CI pipelines, and monitoring tools use the new port.
These five steps close the attacks that actually happen to real servers: credential guessing, root targeting, and automated brute force. They take under an hour on a fresh machine, and they are worth repeating on every server you bring online, including the ones running your CI/CD pipeline, where a compromised SSH key can reach far beyond one box.