Most server breaches I investigate start with the same handful of misconfigurations: default SSH ports, root login enabled, no rate limiting on authentication. The good news? A methodical hardening checklist stops nearly all of them.
This guide walks through twelve specific hardening steps that address the attacks I see every week in production environments. No theory—just the configurations that actually matter when someone targets your server.
1. Lock Down SSH Access
SSH is the front door. Every unpatched server on the internet gets hammered with brute-force attempts within hours of going live.
Disable root login entirely:
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
Change the default port if your threat model includes automated scanners (they mostly hit port 22). Pick something above 1024:
Port 2849
Restart SSH carefully—open a second session first so you don't lock yourself out:
sudo systemctl restart sshd
Allow only specific users:
AllowUsers deploy admin
In practice, key-based auth plus user restrictions stops almost every SSH compromise I've tracked down.
2. Configure Fail2Ban for Brute-Force Protection
Even with keys, you want fail2ban watching your logs. It blocks IPs after repeated failed attempts.
Install it:
sudo apt install fail2ban # Debian/Ubuntu
sudo yum install fail2ban # RHEL/CentOS
Create /etc/fail2ban/jail.local:
[DEFAULT]
bantime = 3600
findtime = 600
maxretry = 3
[sshd]
enabled = true
port = 2849
logpath = /var/log/auth.log
Adjust bantime and maxretry to match your environment. Three strikes in ten minutes is reasonable for most setups.
Enable and start:
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
Check banned IPs:
sudo fail2ban-client status sshd
I've seen fail2ban block thousands of attempts per day on publicly exposed servers. Set it up once and forget it.
3. Keep the Kernel and Packages Updated
Unpatched systems are the easiest targets.
Automate security updates on Debian/Ubuntu:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
On RHEL/CentOS, enable automatic updates with dnf-automatic:
sudo yum install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
For production systems that need controlled maintenance windows, at minimum set up weekly email notifications when updates are available. Then schedule a recurring task to apply them.
Check what needs updating right now:
sudo apt update && apt list --upgradable # Debian/Ubuntu
sudo yum check-update # RHEL/CentOS
4. Disable Unused Services and Ports
Every running service is an attack surface.
List what's listening:
sudo ss -tulpn
You'll see output like this:
Netid State Local Address:Port
tcp LISTEN 0.0.0.0:22
tcp LISTEN 0.0.0.0:80
tcp LISTEN 0.0.0.0:3306
If you see MySQL listening on all interfaces but only your application needs it locally, bind it to localhost. Edit /etc/mysql/my.cnf:
[mysqld]
bind-address = 127.0.0.1
Disable services you genuinely don't need:
sudo systemctl disable cups
sudo systemctl stop cups
Common candidates: print services, Bluetooth daemons on headless servers, old RPC services.
5. Configure a Firewall (iptables or firewalld)
Default-deny is the baseline.
For ufw (simplest option on Ubuntu):
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2849/tcp comment 'SSH'
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
For firewalld (RHEL/CentOS):
sudo firewall-cmd --set-default-zone=drop
sudo firewall-cmd --zone=public --add-service=ssh --permanent
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=https --permanent
sudo firewall-cmd --reload
Verify what's open:
sudo ufw status numbered # ufw
sudo firewall-cmd --list-all # firewalld
Only open what you need. I've seen compromises happen because someone opened a temporary port for testing and forgot to close it.
6. Harden Kernel Parameters with sysctl
The kernel has security knobs most people never touch.
Edit /etc/sysctl.conf or create /etc/sysctl.d/99-hardening.conf:
# Disable IP forwarding
net.ipv4.ip_forward = 0
# Disable ICMP redirect acceptance
net.ipv4.conf.all.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
# Enable SYN cookies (DoS protection)
net.ipv4.tcp_syncookies = 1
# Ignore ICMP ping requests
net.ipv4.icmp_echo_ignore_all = 1
# Log suspicious packets
net.ipv4.conf.all.log_martians = 1
# Disable source routing
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0
Apply immediately:
sudo sysctl -p
These settings protect against common network-level attacks: SYN floods, ICMP redirects, IP spoofing.
7. Set Up Centralized Logging
When something breaks—or gets broken into—you need logs.
Configure rsyslog to forward logs to a remote server or a local aggregator:
# /etc/rsyslog.conf
*.* @@logserver.example.com:514
For single-server setups, at minimum rotate logs and keep enough history:
# /etc/logrotate.d/syslog
/var/log/syslog {
rotate 14
daily
compress
missingok
notifempty
}
Watch auth logs actively:
sudo tail -f /var/log/auth.log
In support tickets I handled, the usual culprit was a full disk because logs weren't rotated. Check disk usage regularly:
df -h /var/log
8. Enforce Strong Password Policies
Even if you use keys for SSH, local user passwords still matter for sudo and local escalation.
Install libpam-pwquality:
sudo apt install libpam-pwquality
Edit /etc/security/pwquality.conf:
minlen = 14
minclass = 3
maxrepeat = 2
Set password expiration in /etc/login.defs:
PASS_MAX_DAYS 90
PASS_MIN_DAYS 1
PASS_WARN_AGE 7
For existing users:
sudo chage -M 90 username
9. Use AppArmor or SELinux
Mandatory access control limits what processes can do even if compromised.
On Ubuntu, AppArmor is active by default. Check status:
sudo aa-status
If profiles are in complain mode, enforce them:
sudo aa-enforce /etc/apparmor.d/*
On RHEL/CentOS, SELinux should be enforcing:
sudo getenforce
If it returns Permissive or Disabled, edit /etc/selinux/config:
SELINUX=enforcing
Reboot after changing SELinux mode. Expect to troubleshoot denials—check /var/log/audit/audit.log and use audit2allow to generate policies when legitimate operations get blocked.
MAC systems are a pain to configure at first. They're worth it.
10. Scan for Rootkits and Malware
Even hardened servers can get compromised. Regular scans catch problems early.
Install rkhunter and chkrootkit:
sudo apt install rkhunter chkrootkit
Run initial scans:
sudo rkhunter --update
sudo rkhunter --check --skip-keypress
sudo chkrootkit
Schedule weekly scans via cron:
sudo crontab -e
Add:
0 3 * * 0 /usr/bin/rkhunter --cronjob --report-warnings-only
Configure email alerts in /etc/rkhunter.conf:
[email protected]
False positives happen. Review warnings manually the first few times to understand what's normal for your system.
11. Secure Shared Memory
Shared memory can be exploited to run malicious code.
Edit /etc/fstab and add:
tmpfs /run/shm tmpfs defaults,noexec,nodev,nosuid 0 0
Remount immediately:
sudo mount -o remount /run/shm
Verify:
mount | grep shm
You should see noexec, nodev, and nosuid in the options.
12. Enable Automatic Security Notifications
You can't fix vulnerabilities you don't know about.
Subscribe to security mailing lists for your OS:
- Debian: debian-security-announce
- Ubuntu: ubuntu-security-announce
- RHEL: redhat-watch-list
For real-time monitoring, tools like apticron (Debian/Ubuntu) email you when updates are available:
sudo apt install apticron
Edit /etc/apticron/apticron.conf:
EMAIL="[email protected]"
Set up monitoring for unusual login attempts:
sudo apt install logwatch
Logwatch emails daily summaries of system activity. Configure /etc/logwatch/conf/logwatch.conf:
MailTo = [email protected]
Detail = Med
What to check first
Start with SSH and the firewall. Those two alone stop the majority of automated attacks. Then add fail2ban and updates. Everything else builds on that foundation.
Walk through this checklist once per server. Document what you changed—future you will thank current you when something breaks at 2 AM. Hardening isn't a one-time task; plan to review these settings quarterly as your infrastructure and threat landscape evolve.
FAQ
How often should I update the server?
Apply security updates within 48 hours of release. Schedule a monthly maintenance window for kernel updates that require reboots.
Will changing the SSH port really help?
It drastically reduces log noise from automated scanners. It won't stop a targeted attack, but it's free and takes thirty seconds.
Should I disable IPv6?
Only if you're certain you don't need it. Many modern applications expect it. If you disable it, do so completely—half-disabled IPv6 causes weird networking bugs.
What if SELinux breaks my application?
Start in permissive mode, reproduce the issue, read the audit log, and create a policy. Don't just disable SELinux—that defeats the purpose.
Can I automate this entire checklist?
Yes, with Ansible, Puppet, or even a bash script. Automate everything except the initial SSH key setup and firewall rules (do those manually to avoid lockouts).
Start with the basics
Hardening feels overwhelming when you look at the full list. Pick three items: SSH lockdown, firewall rules, and automatic updates. Get those right and you're ahead of most servers on the internet.
Schedule an hour this week to work through the rest. Your logs will thank you.
