Secure a new Linux server in this order: install updates, create a normal user with sudo, log in with an SSH key, turn off root and password logins, and allow only the ports you use with a firewall. Then add Fail2ban and automatic security updates. Most of it takes under half an hour. For each command explained step by step, see First Steps on a New Linux Server.
Do These First
1. Update Everything
apt update && apt full-upgrade -y # Debian and Ubuntu
dnf update -y # AlmaLinux and Rocky Linux
2. Create a Normal User With Sudo
adduser deploy
usermod -aG sudo deploy # "wheel" instead of "sudo" on AlmaLinux and Rocky Linux
3. Log In With an SSH Key
On your own computer:
ssh-keygen -t ed25519 -C "my-laptop"
ssh-copy-id deploy@YOUR_SERVER_IP
More in Managing SSH Keys.
4. Turn Off Root and Password Logins
Put the settings in their own file. The 00- prefix matters: SSH uses the first value it reads, and some images ship a 50-cloud-init.conf that turns password logins back on.
cat > /etc/ssh/sshd_config.d/00-hardening.conf <<'CONF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AllowUsers deploy
MaxAuthTries 3
CONF
sshd -t && systemctl restart ssh # "sshd" on AlmaLinux and Rocky Linux
sshd -T | grep -Ei '^(passwordauthentication|permitrootlogin)'
Keep your current session open and test a new login as deploy before you close it. If you lock yourself out, the browser console on a VPS still accepts the root password.
5. Allow Only What You Use
On a VPS, turn on the firewall in the dashboard: add SSH (22), limited to your own address if you can, plus the ports your services need. Inside the server you can add UFW as well (Configuring a Firewall Inside the Server With UFW).
Highly Recommended
6. Fail2ban
apt install -y fail2ban python3-systemd
printf '[sshd]\nenabled = true\nbackend = systemd\n' > /etc/fail2ban/jail.d/sshd.local
systemctl enable --now fail2ban
systemctl restart fail2ban
Fail2ban watches the login logs and blocks addresses after repeated failures.
7. Automatic Security Updates
apt install -y unattended-upgrades
dpkg-reconfigure -plow unattended-upgrades # Debian and Ubuntu
dnf install -y dnf-automatic && systemctl enable --now dnf-automatic-install.timer # AlmaLinux and Rocky Linux
8. Turn Off What You Do Not Use
systemctl list-units --type=service --state=running
systemctl disable --now SERVICE_NAME
For Production
9. Backups
Add Automated Backups & Restore on a VPS, and keep a second copy off the server, for example on a storage box.
10. Logs Off the Server
Send logs somewhere else so you still have them if the server is compromised.
11. An Audit
Lynis is a free, open-source security audit tool:
apt install -y lynis
lynis audit system
Deal with the items marked [WARNING] first.
Your Virteche Account
Protect the account that controls the server too: turn on two-factor authentication. See Account Security and Two-Factor Authentication.