Responding to a hacked dedicated server
An intrusion is stressful, but the order of work is always the same: contain, record, rebuild, then fix the cause. This guide walks through each stage on bare metal.
How do I know the machine has really been broken into?
Treat the server as compromised when you find a process, account or file you did not create and cannot explain. Typical signs are a CPU pinned at full load by an unknown binary (often a coin miner), outbound connections to ports such as 6667 or 3333, a new user with UID 0, unexpected entries in /root/.ssh/authorized_keys, cron jobs pointing at /tmp or /dev/shm, and a sudden flood of outgoing mail. A slow site alone is not proof; an unknown listening socket usually is.
ss -tupna | grep -v 127.0.0.1
ps -eo pid,user,%cpu,lstart,cmd --sort=-%cpu | head -n 15
awk -F: '$3 == 0 {print $1}' /etc/passwd
ls -la /etc/cron.d /var/spool/cron/crontabs 2>/dev/null
Run those checks from a trusted session and write the results down with timestamps. If ps or ss output looks too clean while the load stays high, the tools themselves may have been replaced, which is a strong reason to move to the rescue system for the next steps.
What should I do in the first ten minutes?
Contain first: stop the attacker from doing more, without destroying what they left behind. Pulling the plug feels decisive but erases memory evidence and gives you nothing to learn from. A better sequence on a rented machine is:
- Block outgoing traffic except your own SSH session, so a miner or spam bot stops working and any data theft halts.
- Note the time, your own IP address and every command you run, in a file on your laptop, not on the server.
- Rotate credentials stored elsewhere that the server could read: database passwords, API tokens, deploy keys and any SSH key that lived on it.
- Tell the people whose data is on the box, if personal data may have left it.
MYIP=203.0.113.24 iptables -I OUTPUT 1 -m state --state ESTABLISHED -j ACCEPT iptables -I OUTPUT 2 -d $MYIP -j ACCEPT iptables -A OUTPUT -j DROP
Replace the example address with the one you connect from. Because this is a bare metal machine and no neighbour shares it, cutting egress affects only your own services.
How do I keep evidence before wiping the disks?
Boot the machine into rescue mode from the console and copy what you need from the untouched drives. In rescue mode the installed system is not running, so a rootkit cannot hide files from you. Mount the root filesystem read-only and archive the logs, the web root and the home directories to another host you control.
mkdir -p /mnt/sysroot mount -o ro /dev/md2 /mnt/sysroot tar -C /mnt/sysroot -czf - var/log etc home root | ssh audit@198.51.100.7 "cat > case-2026-12.tgz"
The device name depends on the layout: check lsblk and cat /proc/mdstat first. Save a hash of the archive so you can show later that it was not altered. If you plan to involve the police, keep a full disk image too; a Storage line machine with large drives is a convenient place to park one.
Can I just clean the infection and carry on?
No: once someone has had root, reinstall. Removing the obvious malware leaves you guessing about the modules, timers and patched binaries you did not find. The console reinstall wipes the drives and lays down a fresh system in minutes, so rebuilding is usually faster than a forensic clean-up. Restore application data from a backup taken before the first sign of trouble, and never restore executables, cron files or authorised keys from the compromised copy.
How do I find out how they got in?
Read the archived logs on your own machine before you put the service back online. The usual entry points on a public server are, in order of how often they appear:
| Entry point | Where the trace shows up | Fix after the rebuild |
|---|---|---|
| Guessed SSH password | auth.log or secure: many failures, then an accepted login | Keys only, no password login |
| Outdated web application or plugin | Web server access log: POST requests to odd paths, then a new PHP file | Update, remove unused plugins, read-only code directory |
| Exposed database or cache port | Service log entries from foreign addresses | Bind to localhost or filter the port |
| Leaked key or token | A clean login with valid credentials from an unknown address | Rotate everything, shorten key lifetimes |
If you cannot find the cause, assume the weakest of these and fix all four before going live again.
What should the rebuilt server do differently?
Harden before the first public request. Install SSH keys at reinstall time, disable password logins, enable automatic security updates, filter every port that does not need to be public, and run each application under its own unprivileged user. Our Hardening a new dedicated server article lists the full checklist, and the Build an iptables firewall guide shows a default-deny rule set that fits in twenty lines. Finally, set up off-machine backups with a retention long enough to cover an intrusion that went unnoticed for weeks.
Does the data centre help during an incident?
Every machine sits behind DDoS filtering, and the network team will look at traffic to your address if you open a ticket with the time window. The operating system and everything inside it are yours, though: with full root access you decide what runs, and you carry out the clean-up yourself. If your server was used to attack others, reply to any abuse notice with what you found and what you changed, because a clear answer closes the matter quickly.
Related pages
Hardening a new dedicated server
The first hour on a fresh machine: SSH keys, a firewall, automatic updates, brute force blocking and a habit of checking what listens.
Read moreBuild an iptables firewall
A default-deny host firewall with iptables or nftables: allow SSH and web, rate-limit new connections, cover IPv6 and keep the rules across reboots.
Read moreIPMI and rescue mode
What out-of-band management is, and how console power control, reinstall and rescue boot cover the same needs on our machines.
Read moreQuestions and answers
Should I power off a hacked server immediately?
Usually not. Blocking outbound traffic stops the damage while keeping the machine available for inspection. Power it down only if it is actively attacking others and you cannot filter it quickly.
Is a reinstall really necessary after a break-in?
Yes, whenever an intruder had root access. A fresh install from the console is the only way to be sure no modified binary or hidden service survives, and it takes minutes on a dedicated server.
Can I keep a copy of the hacked disks?
Yes. Boot rescue mode, mount the file systems read-only and stream an archive or a full image to another server before you reinstall.
Will someone else on the machine be affected?
No. A dedicated server has a single tenant, so containment rules and the rebuild affect only your own services.
How do I stop it happening again?
Log in with keys only, keep the system and applications updated automatically, expose only the ports you need and keep off-machine backups that go back several weeks.