A freshly installed server starts receiving automated login attempts within minutes of going online. Linux server hardening means reducing what attackers can reach and making the rest hard to break. Work through this checklist on every new VPS or dedicated server before you put real data on it.
Before you start
- Root SSH access and a way back in if you lock yourself out (the provider's VNC/console or rescue mode).
- Commands are shown for Ubuntu/Debian (
apt) and AlmaLinux/Rocky (dnf).
Warning: Keep your current SSH session open while you change SSH or firewall settings, and test a new login in a second window before closing it.
Update the system
apt update && apt full-upgrade -yor
dnf upgrade -yReboot if a new kernel was installed.
Create a sudo user
- Create the user:
adduser deploy(on AlmaLinux useuseradd -m deploy && passwd deploy). - Give it sudo:
usermod -aG sudo deployon Ubuntu orusermod -aG wheel deployon AlmaLinux.
Use SSH keys and lock down SSH
- From your computer copy your public key:
ssh-copy-id deploy@your-server-ip. - Test key login in a new window.
- In
/etc/ssh/sshd_config(or a file in/etc/ssh/sshd_config.d/) set:PermitRootLogin no PasswordAuthentication no KbdInteractiveAuthentication no MaxAuthTries 3 - Check and restart:
sshd -t && systemctl restart ssh(the service issshdon AlmaLinux).
Enable a firewall
Allow only what you serve. On Ubuntu with UFW:
ufw default deny incoming
ufw allow OpenSSH
ufw allow 80,443/tcp
ufw enableOn AlmaLinux with firewalld:
firewall-cmd --permanent --add-service=ssh --add-service=http --add-service=https
firewall-cmd --reloadOn cPanel servers use CSF instead of UFW or firewalld.
Block brute-force attempts
Install Fail2Ban (apt install fail2ban or dnf install epel-release fail2ban), then enable the SSH jail in /etc/fail2ban/jail.local:
[sshd]
enabled = true
maxretry = 5
bantime = 1hStart it with systemctl enable --now fail2ban and check with fail2ban-client status sshd.
Turn on automatic security updates
- Ubuntu/Debian:
apt install unattended-upgrades && dpkg-reconfigure -plow unattended-upgrades. - AlmaLinux/Rocky:
dnf install dnf-automatic, setupgrade_type = securityandapply_updates = yesin/etc/dnf/automatic.conf, thensystemctl enable --now dnf-automatic.timer.
Close services you do not need
- List listening ports:
ss -tulpn. - Disable anything unused:
systemctl disable --now servicename. - Bind databases and caches (MySQL, Redis, Memcached) to
127.0.0.1unless another server must reach them.
Protect logs, time and backups
- Keep time accurate with
timedatectl set-ntp trueso logs and certificates line up. - Review authentication logs:
journalctl -u sshor/var/log/secure. - Set up automated, off-server backups and test a restore.
- Keep SELinux enforcing on RHEL-family systems instead of disabling it.
What to check afterwards
- Root login and password login are refused.
- An external port scan (for example
nmap your-server-ipfrom another machine) shows only the ports you intend. - Fail2Ban is banning repeat offenders.
FAQ: Linux server hardening
Should I change the SSH port?
It reduces log noise from bots but is not real security. Keys, no root login and Fail2Ban matter far more.
Is a firewall needed if only a few services run?
Yes. It protects you if a service you forgot about starts listening later.
How often should I review a hardened server?
Check updates and logs weekly and review users, open ports and backups at least monthly.
