SSH is the front door to a Linux server, and bots try to guess SSH passwords on every public IP all day. Locking it down takes about fifteen minutes and removes the most common way servers get compromised. This checklist pulls together the individual guides in the right order.
Before you start
- Root or sudo access to the VPS. See connect to a Linux VPS with SSH.
- Your provider's web console (VNC) as a backup way in.
- Keep one SSH session open the whole time and test every change from a second session.
Use SSH keys
- Create a key pair on your computer and copy the public key to the server, following set up SSH keys.
- Log in with the key from a new terminal to confirm it works before going further.
Disable password and root login
- Create a normal user with sudo rights if you do not have one, and give it your key.
- Edit the SSH configuration:
nano /etc/ssh/sshd_config - Set these values:Use
PasswordAuthentication no PermitRootLogin prohibit-password MaxAuthTries 3 LoginGraceTime 30 X11Forwarding noPermitRootLogin noif you always log in as your sudo user. Details: disable root SSH login. - Check the syntax and reload:
sshd -t && systemctl reload sshd
Warning: On cPanel servers, WHM may need root key login for some tasks. Keep prohibit-password rather than no, and also check WHM → Security Center → SSH Password Authorization Tweak.
Limit SSH in the firewall
The strongest step is allowing port 22 only from IPs you trust:
| Firewall | How |
|---|---|
| UFW | ufw allow from 203.0.113.10 to any port 22 proto tcp – see UFW allow port by IP |
| firewalld | Rich rule – see firewalld rich rules |
| CSF | tcp|in|d=22|s=203.0.113.10 in csf.allow – see CSF ports |
If your IP changes often, rate limit instead, for example ufw limit 22/tcp, or connect through a VPN.
Ban brute-force attempts
Install fail2ban on plain Linux servers (install fail2ban), or rely on LFD on CSF servers (tune LFD).
Optional extras
- Move SSH to another port to cut log noise: change the SSH port safely. This is not real security on its own.
- Remove old algorithms: disable weak SSH ciphers.
- Restrict who may log in with
AllowUsers deploy admininsshd_config. - Keep the system patched: automatic security updates.
Check afterwards
- From another machine,
ssh -o PreferredAuthentications=password user@servershould be refused. journalctl -u sshd --since today(or/var/log/secure) should show failed bots being cut off.- If you are locked out, use the console and follow firewall lockout recovery.
