How to secure your Linux server
A new server is scanned by bots within minutes. These steps block most attacks:
- Use SSH keys. Create a key pair (
ssh-keygen), copy the public key to~/.ssh/authorized_keys, then setPasswordAuthentication noin/etc/ssh/sshd_configand restart SSH. Test a second session before closing the first one. - Disable direct root login (
PermitRootLogin no) after creating a sudo user. - Enable a firewall. Ubuntu:
ufw allow OpenSSH && ufw enable. AlmaLinux:firewall-cmd --permanent --add-service=sshthen--reload. Open only the ports your services use (80, 443, ...). - Install fail2ban to block repeated login attempts.
- Keep everything updated and enable automatic security updates where suitable.
- Take regular backups and test restoring them.
Changing the default SSH port reduces noise in the logs but is not a replacement for keys and a firewall.