NewCode CLOUD — 20% off your first month on all cloud VPSClaim

How to Scan a Linux Server for Malware: ClamAV, Linux Malware Detect and rkhunter

Linux servers do get malware — not usually the desktop viruses people think of, but web shells uploaded through vulnerable plugins, cryptocurrency miners, spam scripts and occasionally rootkits. Regular scanning catches infections early, often before they cause blacklisting or abuse reports. This guide sets up three complementary free tools and explains how to interpret what they find.

Tool Best at
ClamAV General malware signatures, infected uploads, email attachments
Linux Malware Detect (maldet) Web shells and PHP malware on shared and web servers
rkhunter Rootkits, changed system binaries, suspicious system settings

None of them is perfect; together they cover the common cases.

ClamAV

Install

# Ubuntu / Debian
sudo apt install clamav clamav-daemon

# AlmaLinux / Rocky (EPEL)
sudo dnf install epel-release
sudo dnf install clamav clamav-update clamd

Update signatures

sudo systemctl stop clamav-freshclam      # Debian/Ubuntu: the service holds a lock
sudo freshclam
sudo systemctl enable --now clamav-freshclam

On AlmaLinux/Rocky, comment out the Example line in /etc/freshclam.conf if present, then run sudo freshclam.

Scan

sudo clamscan -ri /var/www /home /tmp --exclude-dir="^/sys" -l /var/log/clamscan.log
Flag Meaning
-r recursive
-i print only infected files
-l write a log
--move=/root/quarantine move infected files (use with care)

clamscan loads the whole signature database for every run and uses around 1 GB of RAM. On small VPS plans, run it at quiet times or use the daemon (clamdscan), which keeps signatures loaded.

Linux Malware Detect (maldet)

maldet focuses on threats found on web servers, and can use ClamAV’s engine for speed.

Install

cd /tmp
wget https://www.rfxn.com/downloads/maldetect-current.tar.gz
tar xzf maldetect-current.tar.gz
cd maldetect-*/
sudo ./install.sh
sudo maldet -u                     # update signatures

Configure

Edit /usr/local/maldetect/conf.maldet:

email_alert="1"
email_addr="you@example.com"       # needs working mail on the server
scan_clamscan="1"                  # use ClamAV engine if installed
quarantine_hits="0"                # 1 = quarantine automatically
quarantine_clean="0"

Start with automatic quarantine off until you trust the results.

Scan

sudo maldet -a /var/www                 # scan everything
sudo maldet -r /var/www 7               # only files changed in the last 7 days
sudo maldet --report list
sudo maldet --report SCANID

maldet installs a daily cron job that scans recently changed files in common web directories.

rkhunter

Install and baseline

sudo apt install rkhunter              # or: sudo dnf install rkhunter
sudo rkhunter --update
sudo rkhunter --propupd                # record current system file properties

Run --propupd on a clean system, ideally right after installation, and again after every system update. Otherwise rkhunter will warn about every file that legitimately changed.

Check

sudo rkhunter --check --sk --rwo

--sk skips the “press Enter” prompts and --rwo reports warnings only. Details are in /var/log/rkhunter.log.

Reading rkhunter warnings

rkhunter is noisy. Common warnings that are usually harmless:

  • File properties changed after a package update (run --propupd after updates).
  • Hidden files in /dev that belong to systemd or udev.
  • “SSH root access allowed” — a configuration warning, not malware (and worth fixing; see SSH hardening).

Investigate a warning before acting. Compare a changed binary with the package database (debsums or rpm -V) — if the package manager says the file is original, it is fine.

On Debian/Ubuntu, if rkhunter --update fails, set UPDATE_MIRRORS=1, MIRRORS_MODE=0 and WEB_CMD="" in /etc/rkhunter.conf.

Scheduling scans

A weekly full scan plus maldet’s daily scan of changed files works well for most servers:

sudo crontab -e
# Sunday 04:00: ClamAV scan of web and home directories
0 4 * * 0 /usr/bin/clamscan -ri /var/www /home -l /var/log/clamscan-weekly.log >/dev/null 2>&1

# Daily 05:00: rkhunter check
0 5 * * * /usr/bin/rkhunter --check --sk --rwo --cronjob >> /var/log/rkhunter-cron.log 2>&1

Make sure someone reads the results — an email report or a monitoring check that alerts when the log contains “FOUND” or “Warning”. See cron jobs on Linux.

What to do when a scan finds something

  1. Do not panic, and do not mass-delete. Look at each file. Is it in a plugin you use? Was it uploaded recently?
  2. Check for false positives. Some legitimate files (security tools, obfuscated commercial plugins, test files with malware signatures such as EICAR) trigger alerts.
  3. For web shells and infected PHP files: the file is a symptom; the cause is usually an outdated plugin, theme or CMS. Replace the application’s code with a fresh copy from the official source, keep only your content and uploads (after scanning them), update everything, and change admin and database passwords.
  4. For miners, rootkits or changed system binaries: treat the server as fully compromised. Follow has my server been hacked? and rebuild.
  5. Find the entry point with access logs: look for POST requests to the infected file and what came just before.
grep "suspicious-file.php" /var/log/nginx/access.log | head

Limits of signature scanners

Scanners only recognise known malware. New, custom or obfuscated code can slip through, and a rootkit with root access can hide from tools running on the same system. Scanning is a useful early-warning layer, not a guarantee. The bigger wins are prevention:

  • Keep software updated: automatic security updates.
  • Make web files non-writable by the web server user except upload and cache folders: see file permissions.
  • Disable PHP execution in upload directories (Nginx):
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}
  • Keep off-server backups so you can restore a known-good copy.