Base Server Hardening
These are the minimum security operations to perform on every new server before putting it into production.
02
Quick Checklist
| Operation | Priority |
|---|---|
| Update system | π΄ Critical |
| Change root password | π΄ Critical |
| Configure SSH keys | π΄ Critical |
| Enable firewall | π΄ Critical |
| Install Fail2ban | π High |
| Disable SSH password login | π High |
| Create non-root user | π High |
| Change SSH port | π‘ Medium |
| Configure automatic updates | π‘ Medium |
03
1. Update the System
bash
apt update && apt upgrade -y # Debian/Ubuntu
dnf update -y # CentOS/AlmaLinux
04
2. Configure SSH Keys and Disable Passwords
bash
# On your computer: copy your public key
ssh-copy-id root@SERVER_IP
# On the server: disable password login
nano /etc/ssh/sshd_config
Set:
PasswordAuthentication no
PermitRootLogin prohibit-password
bash
systemctl restart sshd
05
3. Enable the Firewall
bash
ufw default deny incoming
ufw default allow outgoing
ufw allow ssh
ufw allow http
ufw allow https
ufw enable
06
4. Install Fail2ban
bash
apt install fail2ban -y
systemctl enable --now fail2ban
See the complete guide: Fail2ban
07
5. Create a Non-Root User
bash
adduser deploy
usermod -aG sudo deploy
08
6. Configure Automatic Security Updates
bash
# Debian/Ubuntu
apt install unattended-upgrades -y
dpkg-reconfigure --priority=low unattended-upgrades
09
Complete Recommended SSH Configuration
bash
nano /etc/ssh/sshd_config
# Change the port (optional)
Port 2222
# Disable root login with password
PermitRootLogin prohibit-password
# Disable password authentication
PasswordAuthentication no
# Disable interactive keyboard authentication
ChallengeResponseAuthentication no
# Disable X11 forwarding if not needed
X11Forwarding no
# Limit authentication attempts
MaxAuthTries 3
# Timeout for idle sessions (in seconds)
ClientAliveInterval 300
ClientAliveCountMax 2
# Disable login for users without password
PermitEmptyPasswords no
bash
systemctl restart sshd
10
Verify SSH Configuration
bash
sshd -T | grep -E 'passwordauth|permitroot|port|maxauthtries'
11
Monitor Access
Regularly check who has accessed your server:
bash
# Last successful logins
last | head -20
# Failed login attempts
lastb | head -20
# SSH logs in real-time
journalctl -u sshd -f
12
Automatic Security Audit Tool
You can use Lynis for automated security audit:
bash
apt install lynis -y
lynis audit system
Lynis analyzes your server configuration and suggests improvements with a security score.
Related articles
Security
Fail2ban: Brute Force Protection
How to install and configure Fail2ban to protect your server from SSH and web brute force attacks
2 min read
Security
Change SSH Port
How to change SSH port to reduce automatic brute force attempts from bots and scanners on the internet
2 min read
Security
Users and Permissions
How to manage Linux users, groups and file permissions on your server
3 min read
