How ClamAV actually works on your desktop
If you install ClamAV on a Linux desktop, you do not get one program. You get a small toolkit: a scanning library, two ways to scan, a database updater and an optional on-access scanner. Each part has its own job, its own configuration and sometimes its own user account. This post goes through them one by one.
What ClamAV is, and what it is not
Section titled “What ClamAV is, and what it is not”ClamAV is an open source (GPLv2) anti-virus toolkit published by Cisco. Its own documentation says it was “designed especially for e-mail scanning on mail gateways”, and also that it “is not a traditional anti-virus or endpoint security suite”.
That shapes what you should expect from it on a desktop:
- It is signature based. ClamAV compares files against a database of known malware signatures. It also has some heuristic checks, but the database does most of the work. It does not watch how running programs behave.
- It does not protect you in real time by default. A plain installation scans when you ask it to. Real-time scanning exists on Linux, but it is a separate component that you have to configure and run yourself.
- It needs a current database. With old signatures, it can miss newer threats. Keeping the database up to date is part of running ClamAV.
That makes it a good fit for checking downloads, USB drives, mail attachments or shared folders on demand, not a replacement for a full security suite.
The parts
Section titled “The parts”libclamav
Section titled “libclamav”The scanning engine lives in a shared library, libclamav. It loads the signature database, unpacks archives and other containers, and matches content against signatures. Every scanner below uses this library. You never run it directly.
clamscan: simple, but slow to start
Section titled “clamscan: simple, but slow to start”clamscan is the standalone command line scanner. You point it at a file or folder and it prints what it found.
The ClamAV documentation says clamscan “will create a new engine and load in the virus database each time it is run”. The database is large, so every clamscan run spends a noticeable amount of time and memory before it scans the first file. For an occasional scan this is fine. For many small scans in a row it adds up.
clamd and clamdscan: load once, scan often
Section titled “clamd and clamdscan: load once, scan often”clamd is the ClamAV daemon. It is a multi-threaded background service built on libclamav. It loads the database once, keeps it in memory, and waits for scan requests on a Unix socket or a TCP socket. Its settings live in clamd.conf.
clamdscan is a small client that sends the scan request to the running daemon and prints the result. If clamd is not running, clamdscan cannot do anything.
Because the database is already loaded, a new scan request does not have to load it first. The cost is that clamd holds the database in RAM the whole time it runs. ClamAV’s own Docker documentation mentions upwards of 1.2 GiB of RAM just to load the signatures, and briefly about twice that while the daemon reloads a new database next to the old one. The exact numbers depend on the version and the database, but it matters on a machine with little memory.
There is one more detail that matters later: how files get to the daemon. With a local daemon on a Unix socket, clamdscan passes paths by default, and the daemon opens the files itself. So clamd, which usually runs as its own user, needs read access to what you scan. With --fdpass, clamdscan opens the file with your permissions and hands the open file to the daemon instead. This only works over a Unix socket. With --stream, or with a daemon on another machine over TCP, clamdscan sends the file content.
freshclam and the virus database
Section titled “freshclam and the virus database”The database files
Section titled “The database files”ClamAV’s official signatures come from Cisco Talos in three databases:
- main: the large base set of signatures. It changes rarely.
- daily: the frequent additions and fixes.
- bytecode: small programs that the engine can run for detections that plain signatures cannot express.
Each comes as a .cvd file (ClamAV Virus Database), a digitally signed container. After incremental updates you may see a .cld file instead, which holds the same kind of content.
Where they live depends on how ClamAV was installed. A build from source uses /usr/local/share/clamav by default. Distribution packages usually choose their own path. Debian and Ubuntu, for example, use /var/lib/clamav. The DatabaseDirectory setting in freshclam.conf and clamd.conf tells you the path on your system. If it is not set, the build default applies.
Without a readable database, neither clamscan nor clamd can scan.
freshclam
Section titled “freshclam”freshclam is the updater. It downloads the official databases and, where possible, fetches small incremental updates instead of the whole file again. If a database file is damaged, it downloads a fresh copy.
You can run freshclam by hand, from cron, or as a daemon with freshclam -d that checks for updates at a fixed interval. Many distributions ship it as a systemd service, often called clamav-freshclam, that does exactly that. If clamd is running, freshclam can tell it to reload the new database (the NotifyClamd option).
Why sudo freshclam can fail
Section titled “Why sudo freshclam can fail”The update service is running, you want an update right now, you type sudo freshclam, and you get this:
ERROR: Failed to lock the log file /var/log/clamav/freshclam.log: Resource temporarily unavailableERROR: Problem with internal logger (UpdateLogFile = /var/log/clamav/freshclam.log).ERROR: initialize: libfreshclam init failed.ERROR: Initialization error!freshclam locks its log file (UpdateLogFile in freshclam.conf) while it runs. The service already holds that lock, so a second freshclam process with the same log file cannot start. The error does not tell you whether the service itself updates successfully. Its log does. Rather than starting a second freshclam, you can let the service do its job, restart it, or ask it to check now. ClamUI does the last one: when it sees the freshclam service running, it signals the service to start an update check. If the daily database was out of date, ClamUI then waits up to 60 seconds for a newer version to appear, and warns you if none does.
On-access scanning with clamonacc
Section titled “On-access scanning with clamonacc”If you want ClamAV to scan files when they are opened, you need clamonacc, the on-access scanner. It is Linux only and needs kernel 3.8 or newer, because it relies on the kernel’s fanotify interface. Some distributions build their kernels without fanotify’s permission events (CONFIG_FANOTIFY_ACCESS_PERMISSIONS). clamonacc then still scans and alerts, but it cannot block access to a file.
How it fits together:
- clamonacc runs as root, because it has to register with the kernel for file events.
- It does not scan anything itself. It passes each event to clamd. So on-access scanning always means running clamd as well, with the memory cost described above.
- With a local clamd, the daemon must run as a user that clamonacc excludes. Otherwise clamd opening a file would trigger another scan of that same file.
- You choose which paths to watch with
OnAccessIncludePathinclamd.conf. ClamAV refuses/as a value, because watching the whole system can lock it up.
By default clamonacc only notifies. It reports infected files, but it does not stop the program from opening them. With OnAccessPrevention yes, it blocks access to an infected file, and the program trying to open it gets “Operation not permitted”.
That blocking mode has a real cost. A file access in a watched path can now wait for the scan result. Excluded users and paths and size limits narrow this down, but the waiting stays. ClamAV’s documentation warns that prevention “will seriously impact performance if used on commonly accessed directories”. In practice that means: watch the folders where new files arrive, such as Downloads, and not your whole home folder.
File permissions
Section titled “File permissions”File permissions can stop ClamAV from opening a file or from loading its database.
Files you cannot read. A scanner can only check files it is allowed to open. With clamscan, that is you. With clamd, it is the daemon’s user, unless the file is passed with --fdpass. Files the scanner cannot open lead to warnings or a scan error. If files were skipped, a clean result only covers what was actually scanned.
A database you cannot read. On some installations, the database files are readable only by the clamav user and group. Then a plain clamscan as your normal user fails before it scans anything, with an error like this:
LibClamAV Error: cli_load(): Can't open file /var/lib/clamav/main.cvdclamd is not affected as long as its service account can read the database, which is the usual setup. For clamscan you have two clean options: use clamd instead, or give your user read access, for example through the clamav group if that group can read the files on your system.
Configuration files in unusual places. Distributions also differ in where they put the daemon configuration. Fedora, for example, uses /etc/clamd.d/scan.conf and a service called clamd@scan, not the /etc/clamav/clamd.conf that many guides assume.
Where ClamUI fits in
Section titled “Where ClamUI fits in”ClamUI is a desktop front end for the ClamAV that is already installed on your system. It does not bring its own copy of ClamAV or its own database. The engine, the tools and the services come from your distribution’s packages, and the signatures are updated by freshclam as usual.
On top of that, ClamUI does a few specific things:
- It picks a scan backend. In Auto mode it checks whether clamd answers, and uses it if it does. Otherwise it falls back to clamscan. You can also force one or the other. Daemon scans normally pass files with
--fdpass, so clamd can scan what you can read. For some locations, such as FUSE mounts, ClamUI streams the content instead. - It reports database problems plainly. When clamscan fails because it cannot read the database, ClamUI says so in plain words and suggests the daemon backend or read access, for example through the
clamavgroup. The original ClamAV message stays in the scan output. - It works with the freshclam service. As described above, it asks a running update service for a check instead of colliding with it.
- It changes system settings through a small helper. ClamUI runs as your normal user. To change system ClamAV settings such as scanner, database or on-access options, it uses a separate helper that you approve through polkit, the standard Linux mechanism for one-off administrator prompts.
- The Flatpak talks to the host. The Flatpak build runs inside a sandbox but needs the ClamAV on your system. Its manifest therefore declares access to the host file system, and it runs the host’s ClamAV tools through
flatpak-spawn --host. It reads the database location from your host’s ClamAV configuration.
If you want to try it, these pages are a good start:
Comments
Comments use giscus and a GitHub account. They are stored as GitHub Discussions in linx-systems/clamui-blog-comments.