Why Most Servers Get Compromised
Breaches happen because defaults stay in place. SSH runs on port 22 with password auth enabled, Windows RDP sits on 3389 accepting any valid credential, and patch cycles slip from weekly to quarterly. Attackers know this.
The good news: you don't need enterprise SIEM or a security team to lock down a server. You need seven focused changes that address how attackers actually get in—brute force, unpatched CVEs, lateral movement, and privilege abuse. These steps work whether you're running Ubuntu on a VPS or Windows Server in a data center, and they layer together into a zero-trust posture where nothing is trusted by default.
Let's walk through each one.
Step 1: Disable Password Authentication for SSH and RDP
Password auth is the easiest attack vector to automate. Botnets hammer SSH and RDP endpoints with credential lists 24/7, and a weak password will fall in hours.
For SSH, switch to key-based authentication:
# Generate an ED25519 key pair on your local machine
ssh-keygen -t ed25519 -C "[email protected]"
# Copy the public key to your server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server-ip
Once the key is in place, edit /etc/ssh/sshd_config:
PasswordAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
ChallengeResponseAuthentication no
Restart SSH:
sudo systemctl restart sshd
For Windows RDP, enforce Network Level Authentication and disable password logons in Group Policy. Better yet, require certificate-based smart card auth or use Windows Hello for Business if you're in an Active Directory environment. At minimum, create a local account with a 20+ character random password and disable the default Administrator account entirely.
Step 2: Move Remote Access Off Default Ports and Add Rate Limiting
Changing the SSH port from 22 or RDP from 3389 doesn't stop a determined attacker, but it drops automated scan traffic to near zero. I've seen auth logs go from thousands of failed attempts per day to single digits.
For SSH, pick a high port (above 1024, below 65535) and update sshd_config:
Port 52847
Then allow it in your firewall:
sudo ufw allow 52847/tcp
sudo ufw delete allow 22/tcp
For RDP on Windows, change the port in the registry:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\PortNumber
Set it to a high port, restart the Remote Desktop service, and update your firewall rule.
Next, add rate limiting. On Linux, use fail2ban:
sudo apt install fail2ban
sudo systemctl enable fail2ban
Create /etc/fail2ban/jail.local:
[sshd]
enabled = true
port = 52847
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
On Windows, enable account lockout policies in Local Security Policy: lock after three failed attempts, reset the counter after fifteen minutes, and keep the lockout duration at thirty minutes. For public-facing RDP, put it behind a VPN or use Remote Desktop Gateway instead.
Step 3: Implement Automated Patch Management
Unpatched servers are how ransomware spreads laterally. A single outdated kernel or missing Windows security update can hand an attacker root.
On Debian/Ubuntu, enable unattended-upgrades:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
Edit /etc/apt/apt.conf.d/50unattended-upgrades to auto-reboot if needed:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";
On RHEL/CentOS/Rocky, use dnf-automatic:
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
Edit /etc/dnf/automatic.conf and set apply_updates = yes.
For Windows, enable automatic updates in Settings > Windows Update > Advanced options. In a domain, use Group Policy to enforce update schedules and automatic restart outside business hours. Check eventvwr.msc under Windows Logs > System for update success events regularly.
Step 4: Enforce Least Privilege and Audit Sudo/Admin Access
Root and Administrator accounts should never be used for daily tasks. Every service, every human operator, gets only the permissions required for their job.
On Linux, create a non-root user for admin work and grant granular sudo access. Edit /etc/sudoers with visudo:
adminuser ALL=(ALL) NOPASSWD: /usr/bin/systemctl, /usr/bin/apt
Never put ALL=(ALL) ALL in production. Log every sudo command:
Defaults log_output
Defaults!/usr/bin/sudoreplay !log_output
Defaults!/sbin/reboot !log_output
On Windows, use standard user accounts and elevate with UAC only when necessary. In Active Directory, create tiered admin accounts (T0 for domain controllers, T1 for servers, T2 for workstations) and never use a T0 account to log into a member server. Enable command-line auditing in Group Policy:
Computer Configuration > Policies > Windows Settings > Security Settings > Advanced Audit Policy > Detailed Tracking > Audit Process Creation
Check Event ID 4688 in Security logs to see every command executed with elevated rights.
So What If Access Is Locked but the Network Isn't?
Zero-trust architecture assumes breach. Even with SSH keys and patched kernels, an attacker who compromises one service should not be able to pivot freely.
Step 5: Segment Networks and Apply Host-Based Firewalls
Default-allow firewall rules let compromised web servers talk to database servers, file shares, and each other. Segment by function.
On Linux, use ufw or iptables to allow only required ports:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 52847/tcp # SSH
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw enable
For internal services (databases, memcached), bind to 127.0.0.1 or a private IP and restrict by source:
sudo ufw allow from 10.0.1.0/24 to any port 3306 proto tcp
On Windows, use Windows Defender Firewall with Advanced Security. Create inbound rules for each service and set the default action to Block. For domain-joined servers, deploy firewall profiles via Group Policy and enforce them with IPsec connection security rules.
If you run multiple VMs or containers on a hypervisor, use VLANs or software-defined networking to isolate workloads. A compromised WordPress container should not see your monitoring stack or backup server.
Step 6: Enable Centralized Logging and Alerting
You can't detect what you don't log. When an attacker lands on a server, you have a narrow window to spot the anomaly before they establish persistence.
Forward logs to a central syslog server or a SaaS aggregator. On Linux, configure rsyslog to send auth and kernel logs:
# /etc/rsyslog.d/50-remote.conf
auth,authpriv.* @10.0.1.5:514
kern.* @10.0.1.5:514
Restart rsyslog:
sudo systemctl restart rsyslog
On Windows, forward Security and System event logs with Windows Event Forwarding or install a log shipper (Winlogbeat, NXLog). Focus on:
- Failed logon attempts (Event ID 4625)
- Successful logons (4624)
- Account creation or modification (4720, 4722, 4738)
- Service installation (7045)
- PowerShell execution (4104)
Set up alerts for:
- More than five failed SSH or RDP attempts from a single IP in ten minutes
- Successful login from an unexpected country or IP range
- New user account created
- Root or Administrator login
- Unusual outbound connection (database server initiating HTTP to the internet)
You don't need a fancy SIEM to start. A cron job that greps /var/log/auth.log and emails you can catch low-hanging fruit.
Step 7: Implement Integrity Monitoring for Critical Files
Rootkits and backdoors modify system binaries, cron jobs, SSH config, and authorized_keys files. Catch changes as they happen.
Install aide (Advanced Intrusion Detection Environment) on Linux:
sudo apt install aide
sudo aideinit
sudo mv /var/lib/aide/aide.db.new /var/lib/aide/aide.db
Run a daily check:
sudo aide --check | mail -s "AIDE Report" [email protected]
Add it to cron:
0 2 * * * /usr/bin/aide --check | mail -s "AIDE Report" [email protected]
On Windows, enable File Integrity Monitoring in Windows Defender or use a third-party tool. At minimum, turn on auditing for critical directories:
C:\Windows\System32
C:\Program Files
C:\Users\Administrator
Check Event ID 4663 (object access) in Security logs. Any unexpected write to System32 or modification of a scheduled task should trigger an alert.
Testing Your Hardening
Once you've applied these steps, validate them. Run an external port scan with nmap from a different network:
nmap -sS -p- your-server-ip
You should see only the ports you explicitly allowed. Try logging in with a password over SSH—it should fail. Check your logs to confirm events are being captured and forwarded.
For Windows, use Microsoft Baseline Security Analyzer or a CIS benchmark scanner to identify remaining gaps.
What to Check Every Month
Security hardening is not one-and-done. Schedule these monthly:
- Review sudo/admin audit logs for unexpected commands
- Check firewall rules for cruft (old exceptions that are no longer needed)
- Scan for outdated packages:
apt list --upgradableoryum check-update - Verify fail2ban or account lockout is still active
- Test your backup restoration process (if you can't restore, you don't have a backup)
Quarterly, rotate SSH keys and service account passwords. Annually, review your network segmentation and privilege model as your infrastructure grows.
Start With Remote Access
If you only have time for one hardening step today, disable password authentication and move SSH or RDP off the default port. That change alone will eliminate the majority of automated attacks hitting your server.
Then layer in the rest: patches, least privilege, segmentation, logging, and integrity checks. Each step multiplies the effort an attacker needs to compromise your system. No single control is perfect, but seven working together turn your server from an easy target into one that's not worth the trouble.
