Management Guides Troubleshooting

My VPS Got Hacked: What to Do First, and Whether to Clean or Rebuild

An incident runbook for a compromised Linux VPS: the first hour, commands to see how far the intruder got, a clean-or-rebuild decision table, and how to rebuild safely.

You have found a process you did not start, your provider has sent an abuse notice, or the site is redirecting visitors somewhere else. The next hour matters more than the next week. This runbook covers what to do first, how to judge how far the intruder got, and how to decide between cleaning the server and rebuilding it.

This is general guidance for small self-managed servers. If the server holds payment data, health data or personal data of customers, you may have legal duties to report a breach within a fixed time; involve a professional incident responder and legal advice early.

Signs that a VPS is compromised

  • CPU at 100% from a process with an unfamiliar or random name: usually a cryptocurrency miner.
  • An abuse report from the provider about outgoing spam, port scans or attacks.
  • Outgoing traffic far above normal.
  • New user accounts, SSH keys or cron jobs you did not create.
  • Website files changed, unknown PHP files in upload folders, redirects or injected links.
  • Logins from addresses or at times that are not yours.
  • Security tools, logs or shell history disabled or emptied.

One sign is a reason to investigate. It is not yet proof: a runaway backup job also uses 100% CPU.

First hour

  1. Do not delete anything yet, and do not reboot. Killing the visible process and removing a file feels like progress and destroys the evidence of how the intruder got in. A reboot clears what is only in memory.
  2. Cut the server off from the internet, except for you. In the provider's cloud firewall, allow SSH from your own IP and block everything else, in and out. This stops the damage (spam, attacks on others, data leaving) without changing the disk.
  3. Take a snapshot. It preserves the compromised state for later analysis and is your fallback if the investigation goes wrong.
  4. Change credentials from a clean device: provider account, domain registrar, DNS, email and anything whose password or API key was stored on the server. Turn on two-factor authentication on the provider account.
  5. Tell the people who need to know: your team, and the provider if they contacted you. Providers are more patient with customers who respond.

Find out how far it went

Run these as root and save the output off the server. A capable intruder with root access can make these tools lie, so treat clean results with caution and suspicious results as real.

# who is and was logged in
w
last -a | head -30
sudo lastb -a | head -20

# accounts and keys
awk -F: '$3 == 0 {print $1}' /etc/passwd          # should print only root
getent passwd | awk -F: '$3 >= 1000 {print $1}'
sudo find / -xdev -name authorized_keys -exec ls -l {} + 2>/dev/null

# what is running and listening
ps auxf
sudo ss -tulpn

# persistence: scheduled jobs and services
for u in $(cut -d: -f1 /etc/passwd); do sudo crontab -l -u "$u" 2>/dev/null | sed "s/^/$u: /"; done
ls -la /etc/cron.d /etc/cron.hourly /etc/cron.daily
systemctl list-unit-files --type=service --state=enabled
systemctl list-timers --all

# recently changed files
sudo find /etc /usr/local /root /home -xdev -type f -mtime -14 2>/dev/null | head -100
sudo find /var/www -type f -name "*.php" -mtime -14 2>/dev/null | head -100
ls -la /tmp /var/tmp /dev/shm

For a suspicious process, find out where it lives before touching it:

sudo ls -l /proc/PID/exe
sudo cat /proc/PID/cmdline | tr '\0' ' '; echo
sudo ls -l /proc/PID/cwd

Then look for the way in: web server access logs around the time the first bad file appeared, SSH logins in journalctl -u ssh (sshd on AlmaLinux and Rocky Linux), and the versions of the CMS, its plugins and any control panel compared with their recent security fixes.

Clean or rebuild?

SituationDecision
The intruder had root: a root login you did not make, changes in /etc, system binaries or services, new accounts with UID 0Rebuild. Nothing on the system can be trusted
You cannot tell how they got in or what they changedRebuild
The compromise came through the provider's or a control panel's own flawRebuild, on a patched platform
Damage is confined to one website's files and its own unprivileged user, and you found the entry point (for example one vulnerable plugin)Cleaning that site can be enough: restore its files from a backup made before the compromise or reinstall from official sources, patch the hole, rotate its passwords
The same infection returns after cleaningRebuild. Something was missed

When in doubt, rebuild. Security guides disagree on how much cleaning in place is acceptable; they agree that a server where an attacker had root cannot be proven clean. A rebuild takes hours with good backups. A second compromise costs more.

How to rebuild

  1. Create a new server from a fresh operating system image. Do not restore the compromised snapshot.
  2. Harden it before putting anything on it: updates, key-only SSH, firewall. Follow the initial server setup and the hardening checklist. Use new SSH keys.
  3. Install software from official sources, at current versions.
  4. Restore data, not programs. Bring back databases and uploaded content. Reinstall the application code, themes and plugins from their publishers instead of copying them from the old server. Uploaded folders should contain no executable files: search them for .php and anything else that should not be there.
  5. Choose the backup carefully. Use the most recent one from before the compromise began, if you can date it. If you can only use a later one, treat everything in it as suspect and inspect it.
  6. Change every secret the old server knew: database passwords, application keys and salts, API tokens, mail passwords, and the passwords of the application's admin users. Force a password reset for users if the user table may have been read.
  7. Close the hole you identified. If you never found it, assume the cause was the most outdated component and check that everything is now current.
  8. Switch DNS to the new server, as in the migration runbook, and keep the old one isolated for a while in case you need to look something up. Then destroy it.

The special case of a miner

Crypto miners are the most common find on small servers because they are installed by automated attacks. Killing the process is never enough: they return through a cron job, a systemd service, a modified shell profile or a second hidden process that restarts the first. Finding the miner means someone was able to run commands on your server. Work out whether that was as a website's user or as root, and apply the table above.

Afterwards

  • Write down what happened, how it was found and what was changed. Short notes are enough.
  • Ask the provider to close the abuse case and to remove the IP from any blocklist it reported.
  • If the server sent spam, check the IP against public blocklists before sending mail from it again.
  • Fix what made it possible: turn on automatic security updates, add monitoring for CPU and outgoing traffic, and set up backups the server cannot delete.

Frequently asked questions

Can I just restore last night's backup?

Only if the compromise started after that backup was made and you close the hole straight away. Otherwise you restore the intruder's access along with your data.

How do attackers usually get into a VPS?

Through an outdated web application or plugin, a weak or reused password on SSH or a control panel, a service that should not have been exposed (a database, Redis, a Docker API), or stolen credentials from a developer's computer.

Should I pay someone to clean it?

For a business site with customer data, a professional is worth it, mainly for the investigation. For a personal site with backups, a rebuild following the steps above is within reach.

Will my provider help?

On an unmanaged plan, usually only by suspending the server if abuse continues. Managed plans vary; some include clean-up, many charge for it. See Managed vs Unmanaged VPS.

Share story:

Continue reading other guides on How VPS.