Complete Guide · 2026

How to secure a Linux server in 2026

A practical, step-by-step guide covering SSH hardening, firewalls, updates, WAF, malware scanning, monitoring, and auditing. Every step includes real commands you can copy and run.

Updated April 2026·~15 min read·8 steps

Why every Linux server needs hardening

Every Linux server connected to the internet is under attack. This is not hypothetical — it is measurable. Botnets continuously scan the entire IPv4 address space, probing for open SSH ports, exposed databases, unpatched web applications, and default credentials.

4,200
attacks per server per day
Based on Defensia telemetry across production servers
22 min
time to first attack after deploy
Median across new VPS deployments
96%
of web servers run Linux
Making it the #1 target for automated attacks

The majority of these attacks are fully automated. Bots do not care about your website traffic, your company size, or your content. They probe every IP looking for low-hanging fruit: default passwords, known CVEs, and misconfigured services. A fresh VPS on DigitalOcean or any other provider will already have SSH brute force attempts in its auth log within minutes.

The good news: most attacks are preventable with a few hours of hardening. This guide walks through every step, from SSH lockdown to continuous monitoring. Each step includes the exact commands for Ubuntu/Debian and RHEL/Rocky/AlmaLinux. For a condensed version, see the VPS security checklist.

1

SSH hardening

SSH is the most attacked service on any Linux server. Brute force bots try thousands of username/password combinations per hour. The first step is to reduce your SSH attack surface to near zero.

Disable root login

Root is the most commonly targeted username. Disabling direct root login forces attackers to guess both a username and a password.

/etc/ssh/sshd_config

PermitRootLogin no

Disable password authentication

SSH keys are cryptographically stronger than any password. Once you have your SSH key set up, disable password authentication entirely.

/etc/ssh/sshd_config

PasswordAuthentication no

PubkeyAuthentication yes

Change the default port (optional)

Changing the SSH port from 22 to a non-standard port reduces noise from automated scanners. It is not a security measure — a targeted attacker will find your SSH port in seconds. But it does reduce log noise by 90%+, making it easier to spot real threats. If you use it, pick a port above 1024:

/etc/ssh/sshd_config

Port 2222

Apply changes and install brute force protection

Terminal

# Restart SSH to apply changes

$ sudo systemctl restart sshd

 

# Rate limit SSH with iptables (manual approach)

$ sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW \

-m recent --set --name SSH

$ sudo iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW \

-m recent --update --seconds 60 --hitcount 4 --name SSH -j DROP

The iptables rules above limit new SSH connections to 3 per minute per IP. For more comprehensive protection, install a dedicated tool:

Option A: fail2ban

$ sudo apt install fail2ban -y

$ sudo systemctl enable fail2ban

Option B: Defensia (15 SSH patterns, zero config)

$ curl -fsSL https://defensia.cloud/install.sh | sudo bash

Defensia detects 15 SSH attack patterns including failed passwords, invalid users, pre-auth disconnects, PAM failures, and kex negotiation drops. See all SSH detection patterns →

2

Firewall configuration

A firewall controls which ports accept connections. The principle is simple: deny everything by default, then allow only the ports you need.

UFW (Ubuntu / Debian)

Terminal

$ sudo ufw default deny incoming

$ sudo ufw default allow outgoing

$ sudo ufw allow 22/tcp # SSH

$ sudo ufw allow 80/tcp # HTTP

$ sudo ufw allow 443/tcp # HTTPS

$ sudo ufw enable

$ sudo ufw status verbose

firewalld (RHEL / Rocky / AlmaLinux)

Terminal

$ sudo systemctl enable --now firewalld

$ sudo firewall-cmd --permanent --add-service=ssh

$ sudo firewall-cmd --permanent --add-service=http

$ sudo firewall-cmd --permanent --add-service=https

$ sudo firewall-cmd --permanent --set-default-zone=drop

$ sudo firewall-cmd --reload

$ sudo firewall-cmd --list-all

Raw iptables (advanced)

Terminal

$ sudo iptables -P INPUT DROP

$ sudo iptables -P FORWARD DROP

$ sudo iptables -A INPUT -i lo -j ACCEPT

$ sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

$ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

$ sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

$ sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Defensia note: Defensia manages its own iptables/ipset rules for dynamic IP blocking. It never modifies your existing firewall rules (UFW, firewalld, or manual iptables). They work at different layers and complement each other.

3

Keep software updated

Unpatched software is one of the top attack vectors. When a CVE is published, exploit code often follows within hours. Automated scanners then sweep the internet for vulnerable versions. The window between disclosure and exploitation is shrinking — in 2025 it averaged under 5 days for critical CVEs.

Manual updates

Ubuntu / Debian

$ sudo apt update

$ sudo apt upgrade -y

RHEL / Rocky / Alma

$ sudo dnf update -y

Automatic security updates

Ubuntu / Debian

$ sudo apt install unattended-upgrades -y

$ sudo dpkg-reconfigure -plow \

unattended-upgrades

RHEL / Rocky / Alma

$ sudo dnf install dnf-automatic -y

$ sudo systemctl enable --now \

dnf-automatic-install.timer

Defensia note: Defensia's CVE scanner reads your installed packages and matches them against the NVD (National Vulnerability Database) daily. Each CVE is scored with EPSS (exploit probability) and flagged if it appears in the CISA Known Exploited Vulnerabilities catalog. You get alerted before an attacker finds the vulnerability.

4

Web server security

If you run nginx or Apache, your web server is the second-most attacked surface after SSH. Hardening it involves two layers: configuration hardening and a web application firewall.

Security headers

Add these headers to protect against common browser-based attacks:

nginx

add_header X-Frame-Options "SAMEORIGIN" always;

add_header X-Content-Type-Options "nosniff" always;

add_header X-XSS-Protection "1; mode=block" always;

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

add_header Referrer-Policy "strict-origin-when-cross-origin" always;

add_header Content-Security-Policy "default-src 'self'" always;

Hide server version

nginx

server_tokens off;

Apache

ServerTokens Prod

ServerSignature Off

Web Application Firewall (WAF)

A WAF detects and blocks application-level attacks: SQL injection, XSS, path traversal, remote code execution, and more. There are two main approaches:

ModSecurity + OWASP CRS (complex)

Industry standard, but requires compiling modules, maintaining YAML config files, tuning hundreds of rules to avoid false positives, and restarting your web server for every change. Typical setup time: 4-8 hours.

Defensia WAF (zero config)

Reads nginx/Apache access logs and detects 15+ OWASP attack types automatically. No inline proxy, no module compilation, no rule files. The WAF runs as a separate Go process — zero impact on web server performance. Learn more →

5

Malware scanning

Even with SSH hardening and a firewall, web application vulnerabilities can lead to uploaded web shells, cryptominers, and backdoors. Regular malware scanning catches what preventive measures miss.

ClamAV (free, signature-based)

Terminal

$ sudo apt install clamav clamav-daemon -y

$ sudo freshclam # Update signatures

$ sudo clamscan -r /var/www --infected # Scan web directories

ClamAV is effective for known malware but slow on large directories and does not detect obfuscated PHP web shells or encoded backdoors well.

rkhunter + chkrootkit (rootkit detection)

Terminal

$ sudo apt install rkhunter chkrootkit -y

$ sudo rkhunter --update

$ sudo rkhunter --check --skip-keypress

$ sudo chkrootkit

These tools check for known rootkit files, hidden processes, and modified system binaries. Run them weekly via cron.

Defensia malware scanner (automatic)

Defensia combines all three approaches into a single scanner: 64,000+ hash signatures, 684 dynamic detection patterns (obfuscated PHP, encoded backdoors, suspicious eval chains), rootkit checks (ld.so.preload, hidden processes, /tmp executables), credential scanning (.env exposure, .git directories, world-readable SSH keys), and WordPress database scanning (malicious posts, rogue admin accounts).

Scans run on schedule and results appear in the dashboard with one-click quarantine. No separate tools to install, no cron jobs to configure.

6

Monitor and respond

Prevention is only half the equation. You need visibility into what is happening on your server right now. Without monitoring, you will not know about attacks until the damage is done.

Key log files to monitor

Log filePurpose
/var/log/auth.logSSH login attempts, sudo usage (Ubuntu/Debian)
/var/log/secureSSH login attempts (RHEL/CentOS)
/var/log/nginx/access.logWeb requests, attack patterns
/var/log/apache2/access.logWeb requests (Apache)
/var/log/syslogSystem events, kernel messages
/var/log/mail.logPostfix/Dovecot authentication events
/var/log/fail2ban.logBan events (if fail2ban installed)

Monitoring tools

Logwatch

Sends daily email summaries of log activity. Good for small servers but not real-time.

GoAccess

Real-time web log analyzer with terminal and HTML output. Great for traffic analysis, but not security-focused.

OSSEC

Host-based intrusion detection system with log analysis and file integrity monitoring. Powerful but complex to configure.

Defensia

Real-time web dashboard showing every attack, ban, and security event across all your servers. Live charts, event feed, ban timeline, geographic distribution, and Slack/email/Discord alerts. All logs in one place, no terminal required.

7

Database security

Databases are high-value targets. An exposed MySQL or PostgreSQL instance can be discovered, brute-forced, and dumped in minutes. The fix is simple: never expose database ports to the internet.

Bind to localhost

MySQL (my.cnf)

bind-address = 127.0.0.1

PostgreSQL (postgresql.conf)

listen_addresses = 'localhost'

Remove default users and set strong passwords

MySQL

mysql> DROP USER IF EXISTS ''@'localhost';

mysql> DROP DATABASE IF EXISTS test;

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'strong_password_here';

mysql> FLUSH PRIVILEGES;

Block database ports in firewall

Terminal

# Make sure these ports are NOT open in your firewall:

# 3306 (MySQL), 5432 (PostgreSQL), 27017 (MongoDB), 6379 (Redis)

$ sudo ufw deny 3306

$ sudo ufw deny 5432

$ sudo ufw deny 27017

$ sudo ufw deny 6379

If you need remote database access, use SSH tunnels instead of exposing ports. Full database security guide →

8

Regular audits

Security is not a one-time task. Run audits regularly to catch configuration drift, new vulnerabilities, and misconfigurations that accumulate over time.

Lynis (security auditing)

Terminal

$ sudo apt install lynis -y

$ sudo lynis audit system

Lynis scans your system configuration and outputs a hardening index from 0-100 with specific recommendations. Run it monthly.

OpenSCAP (compliance)

Terminal

$ sudo apt install libopenscap8 -y # Ubuntu/Debian

$ sudo dnf install openscap-scanner -y # RHEL/Rocky

$ sudo oscap xccdf eval --profile xccdf_org.ssgproject.content_profile_cis \

/usr/share/xml/scap/ssg/content/ssg-ubuntu2204-ds.xml

OpenSCAP checks your system against CIS benchmarks and produces compliance reports. Ideal for regulated environments.

Defensia alternative: Defensia's security posture score (0-100, A-F grade) continuously checks SSH configuration, firewall state, file permissions, credential exposure, and system integrity. No manual scans needed — the score updates automatically and shows up in your dashboard.

The automated alternative: secure your server in 30 seconds

The 8 steps above take 2-4 hours to implement manually, and they require ongoing maintenance: updating rules, reviewing logs, running scans, and checking for new CVEs. If you manage multiple servers, multiply that time accordingly.

Defensia automates steps 1, 5, 6, 7, and 8 completely, and enhances steps 2, 3, and 4 with automated detection and alerts. One command installs everything:

$ curl -fsSL https://defensia.cloud/install.sh | sudo bash

What you get in 30 seconds:

SSH brute force protection (15 detection patterns)
Web Application Firewall (15+ OWASP attack types)
Malware scanner (64K+ hashes, 684 patterns)
CVE scanning (NVD + EPSS + CISA KEV)
Real-time web dashboard for all servers
Security posture score (0-100, A-F grade)
Geoblocking at the firewall level
Bot management (70+ fingerprints)
Rootkit and credential scanning
Slack, email, and Discord alerts

Linux server security checklist

Quick reference: every hardening step with the manual command and whether Defensia covers it automatically.

StepManual commandDefensia
Disable root SSH loginPermitRootLogin noManual
Use SSH keys onlyPasswordAuthentication noManual
Configure firewall (UFW/firewalld)ufw enableManual
Update all packagesapt upgrade / dnf updateManual
Enable automatic updatesunattended-upgrades / dnf-automaticManual
Block SSH brute forcefail2ban / iptables rules
Add security headersnginx/Apache configManual
Hide server versionserver_tokens offManual
Install WAFModSecurity + CRS
Scan for malwareClamAV + rkhunter + cron
Monitor logsLogwatch / OSSEC
Secure database portsbind-address = 127.0.0.1
Scan for CVE vulnerabilitiesManual apt audit
Run security auditLynis / OpenSCAP
Set up alertsCustom scripts

Frequently asked questions

What is the first thing to do after deploying a Linux server?

Three things immediately: (1) Update all packages with apt upgrade or dnf update. (2) Disable root SSH login and switch to SSH keys. (3) Enable a firewall (UFW or firewalld) and allow only ports 22, 80, and 443. These three steps eliminate the majority of your attack surface. After that, install intrusion detection (fail2ban or Defensia), set up automatic updates, and configure a WAF if you run a web server.

Is Linux secure by default?

Linux has strong security fundamentals: user permissions, file system permissions, process isolation, and SELinux/AppArmor mandatory access controls. However, a default installation is not hardened. SSH accepts password login as root, no firewall rules are active, no intrusion detection is running, and services bind to all interfaces. Linux provides the tools for security — you still need to configure them.

How often should I update my server?

Security updates should be applied as soon as possible — ideally automatically via unattended-upgrades (Ubuntu) or dnf-automatic (RHEL). For non-security updates, a weekly or biweekly schedule is reasonable. The median time between CVE disclosure and active exploitation was under 5 days in 2025 for critical vulnerabilities, so delays matter.

Do I need a WAF on Linux?

If you run a web server (nginx, Apache, or any application on ports 80/443), yes. A firewall blocks ports but cannot inspect HTTP request content. SQL injection, XSS, and path traversal attacks arrive on port 443 — your firewall allows them through. A WAF inspects the request payload and blocks malicious content. Options include ModSecurity (complex), Defensia (zero-config), or cloud WAFs like Cloudflare (DNS change required).

What's the best free Linux security tool?

It depends on what you need. For SSH brute force protection: fail2ban (requires jail config) or Defensia free tier (zero config). For malware scanning: ClamAV. For rootkit detection: rkhunter + chkrootkit. For security auditing: Lynis. For a combination of SSH protection, dashboard, and bot detection with zero configuration: Defensia free plan (1 server). No single tool covers everything — which is why tools like Defensia that combine multiple layers are increasingly popular.

How do I check if my server has been hacked?

Look for: (1) Unexpected CPU spikes, especially during off-hours (cryptominers). (2) Unknown processes in /tmp or /dev/shm. (3) New SSH authorized_keys entries you did not add. (4) Unexpected cron jobs. (5) Outbound connections to unknown IPs (check with ss -tnp or netstat -tnp). (6) Modified system binaries (verify with dpkg -V on Debian or rpm -Va on RHEL). (7) PHP files in upload directories. Defensia checks all of these continuously and surfaces them in a security posture score.

Sources

  • Defensia telemetry: 4,200 average attacks/server/day across 9 monitored production servers (2026).
  • W3Techs: Linux usage statistics for web servers (96.3% as of 2026).
  • Mandiant M-Trends 2025: median time from CVE disclosure to exploitation.
  • NIST National Vulnerability Database (NVD): CVE data and scoring.
  • CISA Known Exploited Vulnerabilities (KEV) catalog.
  • CIS Benchmarks for Linux hardening.

Start protecting your server now

One command. Under 30 seconds. Free for one server.

$ curl -fsSL https://defensia.cloud/install.sh | sudo bash
Create Free Account

No credit card required.