ClamAV and maldet commands cheat sheet

This cheat sheet brings together the commands of ClamAV, the open-source antivirus engine, and of Linux Malware Detect (maldet), a scanner built to find malicious code on hosting servers: installing and updating signatures, scanning website folders, quarantining and restoring files, fast scanning with clamd, file monitoring, and scheduled scans. After the commands, you'll find what to do when a scan finds a suspicious file. You can search for a command, filter by topic, and copy it with one click.

ClamAV: installing and updating signatures

  • sudo apt install clamav clamav-daemon clamav-freshclam

    On Debian and Ubuntu: installs the clamscan scanner, the clamd service, and the freshclam update tool.

  • sudo dnf install epel-release

    On AlmaLinux: enables the EPEL repository, which the ClamAV packages are installed from. On RHEL, enable EPEL by following Red Hat's instructions.

  • sudo dnf config-manager --set-enabled crb

    Enables the CRB repository, which many EPEL packages depend on.

  • sudo dnf install clamav clamd clamav-update

    On RHEL-based distributions, with EPEL: installs clamscan, the clamd service, and the freshclam update tool.

  • sudo freshclam

    Downloads the signature database, or updates an existing one. Run it right after installation, since clamd won't start without a signature database. If the update service is already running in the background, a manual run may fail with a lock error: stop the service with sudo systemctl stop clamav-freshclam, run freshclam, and then start the service again.

  • sudo systemctl enable --now clamav-freshclam

    On Debian and Ubuntu: enables the update service, which checks for new signatures several times a day.

  • sudo freshclam -d --checks=12

    Runs freshclam in the background with 12 checks a day (the default). The number can be between 1 and 50, and you can also set it with the Checks setting in freshclam.conf.

  • #Example

    A freshclam.conf or clamd.conf file created from the sample file contains the line Example. Until you comment it out, as shown here, the tools won't run with that file.

ClamAV: scanning with clamscan

  • sudo clamscan -r -i /var/www

    Scans the folder and all its subfolders (-r) and shows only infected files (-i). A scan summary is shown at the end.

  • sudo clamscan -r -i -l /root/clamscan.log /var/www

    The same scan, with the report saved to a file. Useful for long scans and scheduled scans.

  • sudo clamscan -r -i --include="\.php$" /var/www

    Scans only files whose names match the regular expression, here PHP files. Faster than a full scan when you're looking for malicious scripts on a website.

  • sudo clamscan -r -i --exclude-dir="/cache/" /var/www

    Skips folders whose paths match the regular expression, for example cache folders. You can repeat the option several times, and the same goes for --exclude for files.

  • sudo clamscan -r -i --max-filesize=50M --max-scansize=200M /var/www

    Sets the maximum size of a file to scan and the maximum amount of data scanned from each file. Files over the limit aren't fully scanned and aren't reported as infected, so don't set it too low.

  • sudo clamscan -r -i /home

    Scans the home folders of all users on the server. Scanning a large folder takes a long time, so run it during quiet hours.

  • echo $?

    Run right after a scan, it shows the clamscan exit code: 0 when nothing was found, 1 when an infected file was found, and 2 when an error occurred. This is how a script or a cron job knows whether there's a finding.

ClamAV: quarantine and removal

  • sudo mkdir -p /root/clamav-quarantine

    Creates a quarantine folder outside the website folders, where infected files will be moved.

  • sudo clamscan -r -i --move=/root/clamav-quarantine /var/www

    Moves each infected file to the quarantine folder. This is the recommended approach: the file is taken out of the website, and if it turns out to be a false positive, you can put it back. Caution: moving a file the website loads may cause an error on the site.

  • sudo clamscan -r -i --copy=/root/clamav-quarantine /var/www

    Copies each infected file to the quarantine folder and leaves the original in place. Useful for reviewing the findings before you decide what to do.

  • sudo clamscan -r -i --remove /var/www

    Deletes each infected file. Use extreme caution: deletion is permanent, a false positive deletes a valid file, and deleting a core file of the website may take it down. It's better to move the files to a quarantine folder.

ClamAV: fast scanning with clamd

  • sudo systemctl enable --now clamav-daemon

    On Debian and Ubuntu: enables clamd, the service that keeps the signature database loaded in the background. clamscan loads the database again on every run, so clamdscan starts scanning faster.

  • sudo systemctl status clamav-daemon

    Shows whether clamd is running and its latest messages.

  • sudo clamdscan --multiscan --fdpass /var/www

    Sends the scan to clamd. The --multiscan option scans several files in parallel, and the --fdpass option passes the open files to clamd, so the scan works even when clamd runs as a different user. The --fdpass option works only over a local connection, not over TCP.

  • sudo clamdscan -i -l /root/clamdscan.log /var/www

    Shows only infected files and saves the report to a file. Most other clamscan options don't apply to clamdscan, because the scan follows the clamd settings in clamd.conf.

  • sudo clamdscan --reload

    Tells clamd to reload the signature database.

  • NotifyClamd /etc/clamav/clamd.conf

    A setting in freshclam.conf: after every update, freshclam tells clamd to load the new signatures. The path is the clamd configuration file, which is different on RHEL-based distributions.

  • clamdtop

    Shows the status of clamd, including its memory usage.

ClamAV: false positives

  • sudo clamscan /var/www/example.com/file.php

    Scans a single file. The output shows the name of the signature that detected the file, which you use to check whether it's a false positive.

  • /var/lib/clamav/local.ign2

    A file where you list the names of signatures for ClamAV to ignore, one name per line. The file is kept in the signature database folder, which is /var/lib/clamav by default.

    Example: Php.Malware.Example-1

  • sudo sigtool --sha256 /var/www/example.com/file.php | sudo tee -a /var/lib/clamav/false-positives.sfp

    Adds this specific file to the list of allowed files by its hash, so only this file won't be detected. If the file changes, its hash changes and the exclusion no longer applies.

maldet: installation and updates

  • git clone https://github.com/rfxn/linux-malware-detect.git && cd linux-malware-detect && sudo ./install.sh

    Downloads maldet from the official repository and installs it in the /usr/local/maldetect folder. The installation creates the maldet command and a daily cron job.

  • sudo maldet -u

    Updates the maldet signatures. The --force option downloads them again even when there's no update.

  • sudo maldet -d

    Updates maldet itself to the latest version.

  • /usr/local/maldetect/conf.maldet

    The main maldet configuration file: alerts, automatic quarantine, the scan engine, and monitoring.

  • /etc/cron.daily/maldet

    The daily job the installation adds: it updates the signatures and scans recently changed files in the website folders of well-known control panels.

maldet: scans and reports

  • sudo maldet -a /home/?/public_html

    Scans all files in the path. The ? character stands for any folder name, so the public_html folders of all users are scanned.

  • sudo maldet -a /var/www

    Scans all files in the /var/www folder and its subfolders.

  • sudo maldet -r /home/?/public_html 2

    Scans only files created or changed in the last two days. Much faster than a full scan, and a good fit for a daily check.

  • sudo maldet -e list

    Lists the scans that were run, with the scan ID (SCANID) of each one.

  • sudo maldet -e latest

    Shows the report of the latest scan. To see the report of a specific scan, write its ID instead of latest.

  • sudo maldet -l

    Shows the maldet event log: scans, updates, and files moved to quarantine.

maldet: quarantine, cleaning, and restore

  • sudo maldet -q 050910-1534.21135

    Quarantines all files found in a scan, by the scan ID shown at the end of the scan and in maldet -e list.

  • sudo maldet -n 050910-1534.21135

    Tries to clean the malicious code from the files found in the scan. After cleaning, check that the website works and that the files are intact.

  • sudo maldet -s 050910-1534.21135

    Restores all files quarantined in the scan to their original locations. Instead of the ID, you can write the path of a single quarantined file.

  • /usr/local/maldetect/quarantine/

    The maldet quarantine folder. Files are kept there with 000 permissions, so they can't be run.

maldet: file monitoring

  • sudo maldet -m users

    Starts monitoring users' home folders: every file that's created, changed, or moved is scanned. In home folders, only the subfolders listed in the inotify_docroot setting are monitored, along with /tmp, /var/tmp, and /dev/shm. Monitoring requires the inotifywait tool from the inotify-tools package.

  • sudo maldet -m /home/site1,/home/site2

    Starts monitoring specific paths, separated by commas. Instead of the paths, you can write a file that lists one path per line.

  • sudo maldet -k

    Stops monitoring.

  • inotify_docroot="public_html,public_ftp"

    A setting in conf.maldet: the subfolders in each home folder that are monitored in users mode.

conf.maldet: key settings

  • quarantine_hits="1"

    Automatically quarantines every file found in a scan. By default (0), findings are only reported.

  • quarantine_clean="1"

    Tries to clean the files that were quarantined. Works together with quarantine_hits.

  • scan_clamscan="auto"

    Uses the ClamAV engine when it's installed on the server, with the maldet signatures. maldet prefers clamd when it's running, and clamscan is the slower option. The value 1 turns on the ClamAV engine, and 0 turns it off.

  • scan_ignore_root="1"

    Skips files owned by root, which is the default. To scan them too, change the value to 0.

  • scan_max_filesize="2048k"

    The maximum size of a file that's scanned.

  • email_alert="1"

    Emails a report after every scan to the address in the email_addr setting. The alert works only when the server sends email.

  • /usr/local/maldetect/ignore_paths

    Paths that maldet won't scan, one path per line. In the same folder: ignore_sigs for signatures to ignore, ignore_file_ext for file extensions, and ignore_inotify for paths that won't be monitored.

Finding suspicious files and scheduling scans

  • sudo find /var/www -type f -name "*.php" -mtime -2

    Lists PHP files changed in the last two days. After a finding, this is how you find other files created at the same time.

  • sudo find /var/www -path "*/uploads/*" -name "*.php"

    Finds PHP files in upload folders. The uploads folder of a content management system, such as WordPress, usually holds images and documents, so a PHP file there is suspicious.

  • sudo grep -rlE "base64_decode|gzinflate" --include="*.php" /var/www

    Finds PHP files that use functions common in obfuscated code. Legitimate code uses them too, so this is a starting point for a review, not proof.

  • command -v clamscan

    Shows the full path of clamscan. In a cron line, write the full path.

  • 30 3 * * * /usr/bin/clamscan -r -i -l /root/clamscan-daily.log /var/www

    An example line in root's crontab: a scan every night at 3:30 AM, with the report saved to a file. maldet adds its own daily job during installation, so it doesn't need a line of its own.

Who develops ClamAV and maldet

  • ClamAV: an open-source antivirus engine for detecting trojans, viruses, and malware, under the GPLv2 license. ClamAV is developed by Cisco, and its code is published on GitHub in the Cisco Talos repository. The Talos team also handles reports of undetected malware and of false positives.
  • Linux Malware Detect (maldet): a malware scanner for Linux under the GPLv2 license, built around the threats found on shared hosting servers. It's developed and supported on a volunteer basis by Ryan MacDonald, on rfxn.com and in a GitHub repository. The project is actively updated, and its latest major version includes a rewritten scan engine that's much faster.
  • Using them together: when ClamAV is installed on the server, maldet can scan with its engine, together with the maldet signatures, which specialize in malicious code on websites.

What to do when a scan finds a suspicious file

  • Quarantine before you delete: move the file to quarantine with --move in ClamAV or with maldet -q, rather than deleting it right away. This way, you can review it and put it back if it turns out to be clean.
  • Check for a false positive: check the signature name and the contents of the file. You can report a ClamAV false positive through the form on clamav.net, and in the meantime exclude the signature or the file on the server.
  • Find the point of entry: a malicious file is usually the result of the breach, not its cause. Look for other files changed at the same time, and check the web server access logs for requests from those hours.
  • Update and fix: update the content management system, plugins, and themes, and remove plugins you don't use. Check whether any software your website uses appears on the list of known exploited vulnerabilities.
  • Change passwords: change the passwords for the website's admin dashboard, FTP, SSH, and the database, and do it from a computer that has been scanned and found free of malware.
  • Scan again: after cleaning, scan all the website folders again. When a site is extensively compromised, restoring from a clean backup is faster and safer than cleaning it file by file.

Real-time scanning and memory requirements

  • Real-time scanning: on Linux, the ClamAV clamonacc tool scans files as they're accessed, together with clamd. It runs with root privileges and by default only reports without blocking, and the documentation warns that blocking mode greatly slows down frequently accessed folders. maldet offers its own monitoring, with maldet -m.
  • Memory: the ClamAV documentation recommends at least 3 GiB of memory for running clamscan or clamd with the standard signature database.
  • Scheduling: you can build and test the cron line for the nightly scan with the Cron expression generator.

The paths on this page, such as /var/www and /home/?/public_html, are examples, so replace them with the website folders on your server. The scan ID 050910-1534.21135 is an example from the maldet documentation.

Did you know?

On a Linux VPS, you connect over SSH with full root access, install ClamAV and maldet, and schedule scans of your website folders with the commands on this page. Server resources are guaranteed, so clamd can keep the signature database loaded in the background.

Linux VPS plans

ClamAV and maldet FAQ

What's the difference between ClamAV and maldet?

ClamAV is a general-purpose antivirus engine that detects viruses, trojans, and malware in any type of file, and freshclam updates it several times a day. maldet (Linux Malware Detect) was built specifically for hosting servers, and its signatures focus on malicious code planted on websites, such as PHP scripts that allow remote control. The two complement each other: when ClamAV is installed on the server, maldet scans with its engine and adds its own signatures.

How do I scan a WordPress site for malicious code?

Run maldet on the website folder, for example sudo maldet -a /var/www, or ClamAV with sudo clamscan -r -i /var/www. To quickly scan only PHP files, add the --include="\.php$" option to clamscan. It's also a good idea to check for PHP files in the wp-content/uploads folder, which usually holds only images and documents.

How do I set up regular automatic scans?

The maldet installation adds a daily cron job that updates the signatures and scans recently changed files in the website folders. For ClamAV, add a line to root's crontab, for example a nightly scan with clamscan -r -i and a report saved to a file with -l. You can build and test the cron line with the Cron expression generator.

What do I do with a file the scan detected?

Move it to quarantine, with --move in ClamAV or with maldet -q, and check the signature name to make sure it isn't a false positive. Then find out how the file got onto the website: update the content management system and plugins, change passwords, and scan again. This way, your site is back up and running, clean and up to date.

Can maldet use the ClamAV engine?

Yes. The scan_clamscan setting in conf.maldet sets the scan engine, and by default (auto), maldet uses ClamAV when it's installed on the server. maldet prefers clamd when the service is running, and the maldet signatures are loaded into the ClamAV engine too.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement