Skip to content

Scanning with ClamAV

In this article

ClamAV is a free, cross‑platform, open‑source antivirus designed to detect trojans, viruses, malware, and other threats. It is most commonly used on servers and mail gateways.

Installation

Ubuntu / Debian

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

Note

The clamav-daemon package (the clamd daemon) is only needed for background scanning via clamdscan or for integration with other services (for example, a mail server). For one-off scans with clamscan, the clamav package is sufficient.

CentOS / AlmaLinux / Rocky Linux

ClamAV packages are located in the EPEL repository.

For CentOS 7:

sudo yum install -y epel-release
sudo yum install -y clamav clamav-update

For AlmaLinux / Rocky Linux 8–10, CentOS 8 Stream (using dnf):

sudo dnf install -y epel-release
sudo dnf install -y clamav clamav-update

If you need the clamd daemon, additionally install the clamd package (sudo dnf install -y clamd). On RPM-based distributions, the daemon service is called clamd@scan, and its configuration is located in /etc/clamd.d/scan.conf.

Note

On systems with systemd (all modern distributions), the clamav-freshclam service manages automatic database updates. You can enable it with sudo systemctl enable --now clamav-freshclam.

Updating Signatures

Before first use, be sure to update the virus signature databases.

Standard method (when freshclam is running)

sudo freshclam

If you encounter an error ERROR: Can't open /var/lib/clamav/main.cvd: Permission denied or ERROR: Database lock file exists, the clamav-freshclam service may already be running and blocking the update. In that case, use:

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

Resource Requirements

  • RAM: For full scans, at least 1–1.5 GB of free RAM is recommended.
  • Swap: If no swap partition or file exists, set it up, especially on VPS with limited memory. The procedure is described in the article Managing swap: creation and resizing.

Scanning Examples

Task Command
Scan a single file clamscan /path/to/file
Recursive folder scan (infected only) clamscan -r -i /folder
Move infected files clamscan -r --move=/quarantine /folder
Automatically delete infected files clamscan -r --remove /folder
Output report to file clamscan -r -i /folder > scan_report.txt
Use the clamd daemon (faster on repeated scans) clamdscan /folder

Note

The --remove option permanently deletes files. Use it only after testing and with a backup.

To speed up repeated scans, it is recommended to use clamdscan (runs via the clamd daemon), after starting the service:

sudo systemctl start clamav-daemon    # Ubuntu / Debian
sudo systemctl start clamd@scan       # CentOS / AlmaLinux / Rocky Linux
sudo clamdscan /folder

clamdscan scans directories recursively by default. Note that the daemon runs as a separate user and must have read access to the files being scanned (or use the --fdpass option).

Useful Tips

  • To check if clamd is running:
    sudo systemctl status clamav-daemon
    
  • Scanning logs (by default) are located in /var/log/clamav/.
  • To schedule regular scans, set up cron:
    # Daily check at 3:30 am
    30 3 * * * /usr/bin/clamscan -r --exclude-dir="^/sys\|^/proc\|^/dev" /home --log=/var/log/clamav/daily_scan.log
    
question_mark
Is there anything I can help you with?
question_mark
AI Assistant ×