How to Scan a Linux Server for Malware with ClamAV and rkhunter

How to Scan a Linux Server for Malware with ClamAV and rkhunter

To scan a Linux server for malware, use two tools that look in different places: ClamAV checks files against known malware signatures, and rkhunter checks the system itself for rootkits and signs of tampering. Neither tool alone proves a server is clean, but together they catch the cases each one misses. This guide installs both, runs a first scan, reads the reports without panic, and handles what the scan finds.

Start with expectations. A scanner reports what it recognizes and what looks odd. It does not prove the absence of new malware, and a clean report on a server that already shows strange behavior should not override that behavior. If you already see warning signs, contain the server first, because a live intrusion needs containment before a routine scan. For a healthy server, a scheduled scan is a useful second opinion.

Install ClamAV and Update Its Signatures

Install ClamAV and the daemon package on Ubuntu with:

sudo apt update
sudo apt install -y clamav clamav-daemon

Before the first scan, update the signature database. The freshclam service usually does this in the background, but run it by hand once to confirm it works:

sudo systemctl stop clamav-freshclam
sudo freshclam
sudo systemctl start clamav-freshclam

A stale database misses new samples, so a scan with signatures from months ago gives false comfort. Check the database age with ls -l /var/lib/clamav/ and confirm the main and daily files are recent. If freshclam fails, check outbound network access and DNS first, because signature updates need to reach the ClamAV mirrors, and a server with no outbound HTTP will fail silently in the background.

Scan a Linux Server for Malware with ClamAV: Read the Results

Scan the whole filesystem, excluding the virtual folders that are not real files:

sudo clamscan -r --exclude-dir=^/sys --exclude-dir=^/proc --exclude-dir=^/dev / 2>/dev/null | tee /root/clamscan-report.txt

On a large server this takes a while, so run it in a screen or tmux session, or scan one area at a time, starting with web roots, upload folders, and temp folders where malware most often lands. The summary at the end shows files scanned, infected files, and errors. Infected files are listed with a signature name, such as a PHP webshell pattern or a known trojan family.

Do not delete a flagged file at once. First look at it: check its path, its owner, its modification time with ls -l, and its content if it is a script. A flag on a file you recognize, such as a security test tool you installed, may be a false positive. A flag on a PHP file in an uploads folder that you never wrote is almost certainly real. Move suspect files to quarantine with mv into a root-only folder instead of deleting them, so you keep evidence and can restore a false positive.

ClamAV Scan Commands for Targeted Checks

Full scans are slow, so targeted scans cover the risky areas between full runs. Scan a web root with:

sudo clamscan -r --infected /var/www/

The --infected flag prints only infected files, which keeps the output readable. Scan uploads and temp folders the same way, and add a weekly full scan for the rest. If you run WordPress, also review plugin and theme folders, because compromised plugins are a common entry point, and our comparison of WordPress security plugins shows what application-level scanning adds on top of file scanning.

For faster repeat scans, the daemon mode with clamdscan reuses a loaded engine instead of starting fresh each time. It needs the daemon running and the right socket permissions, so keep the simple clamscan route until you run scans often enough for the setup to pay off.

Install rkhunter and Run a Rootkit Check

rkhunter looks for rootkits, changed system binaries, hidden files, and suspicious kernel modules. Install it with:

sudo apt install -y rkhunter
sudo rkhunter --update
sudo rkhunter --propupd

The --propupd step builds a baseline of file hashes for your system as it is now. That baseline matters: rkhunter compares current files against it, so a baseline built after a compromise would bless the compromised files. Build the baseline on a fresh, trusted install, or right after you have verified the server by other means. Then run the check:

sudo rkhunter --check --skip-keypress

Read the report at /var/log/rkhunter.log. Warnings fall into groups: changed binaries, suspect files, hidden directories, and network checks. A changed binary after a legitimate system update is expected, and you rebuild the baseline after such updates with --propupd. A changed binary with no update behind it is not expected, and that is the case to investigate.

Rkhunter Rootkit Check Results Without False Alarms

Most first runs show warnings that turn out to be harmless. Common ones include hidden files in temp folders, allowed SSH protocol settings, and scripts in system folders that belong to installed packages. For each warning, check whether a package owns the file with dpkg -S /path/to/file and whether the file changed recently. A warning you can explain with a package and a date is a candidate for the allowlist in /etc/rkhunter.conf, using settings such as ALLOWHIDDENFILE for known hidden files.

Do not silence warnings by disabling whole test groups. Each disabled group is a blind spot, and the point of the tool is to force a decision per warning. Document each allowlist entry with a comment naming the package or reason, so a future admin can tell a considered exception from a lazy one. Rebuild the baseline after every system update, or the next check will drown in expected changes and hide a real one.

Schedule Scans and Act on What They Find

Put ClamAV on a weekly full scan and rkhunter on a weekly check, with reports mailed or logged where you will see them, and pair them with alerts from our Uptime Kuma Telegram alerts setup. A scan nobody reads is decoration. Use cron entries that write dated reports, and check them on a fixed day. Pair scanning with the layers that stop malware from landing in the first place: our SSH hardening guide closes the most common entry, and our WordPress security basics guide covers the app layer where many infections start.

When a scan finds real malware, the job shifts from scanning to cleanup. For malware removal Linux admins should quarantine first, find the entry point second, and delete last, so a false positive never costs a good file. Isolate the affected site or service, find how the file got in from logs and timestamps, remove the entry point, and restore clean files from a known-good backup where you cannot verify a file by hand. A scan that finds the same file again after cleanup means the entry point is still open, and cleaning without closing it only buys time until the next infection.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.