ClamAV installation and daily scan + report on Ubuntu
Published: 01-12-2012 | Author: Remy van Elst | Text only version of this article
❗ This post is over eleven years old. It may no longer be up to date. Opinions may have changed.
Table of Contents
ClamAV is a virus scanner for Linux/Unix like systems. This tutorial will show you how to install it and how to set it up to auto-update and do a daily scan. It will only report if threats are found. It is tested on Ubuntu 10.04 and up. It also covers if, and why you might need a virus scanner for Linux/Unix. As an extra, it also covers scans for specific folders which remove infected files automatically.
Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:
I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!
Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.
You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.
Do you need a virus scanner on Linux/Unix?
Short answer, it depends on your situation.
Long answer: You might want to run a virus scanner on a server where files are uploaded, or where users login and manipulate files. Lets say you host a file upload site, you want to run a scan on the upload folder which auto removes infected files. It can also be that your mailserver runs Linux, but people sometimes mail executables or infected pdf files. Then you also want a scanner. Or you might run a source control server (gitolite, mercurial) where sometimes binary files are checked in. (although ClamAV might not find things in bare-git repositories, only in working directories.)
If you however only run a static HTML website, or an rsync backup server, or a Rougelike via telnet, where there are no uploads or like, you might be wasting resources.
My advice is to look at your situation and then decide if you need ClamAV, and if you only need to scan a few folders, the entire system and if you want to auto-remove the virusses or not.
First we update the package repository:
sudo apt-get update
Now we install the clamav scanner, the daemon and the database updater.
sudo apt-get install clamav clamav-daemon clamav-freshclam
You can also install the clamav-data package, but the clamav-freshclam package is another (easier) way to update your virus definitions.
On RHEL, CentOS or Scientific Linux, you only need to install the
yum install clamav
Update the database
The below two commands will restart the freshclam daemon (which auto updates the database) and then does a manual update of the definitions.
sudo /etc/init.d/clamav-freshclam restart sudo /usr/bin/freshclam
The daily scan
The below cronjob will run a virus database definition update (so that the scan always has the most recent definitions) and afterwards run a full scan which will only report when there are infected files on the system. It also does not remove the infected files automatically, you have to do this manually. This way you make sure that it does not delete /bin/bash by accident.
## This should be a root cronjob. 30 01 * * * / usr/bin/freshclam --quiet; /usr/bin/clamscan --recursive --no-summary --infected / 2>/dev/null
2>/dev/null options keeps the /proc and such access denied errors out of
the report. The infected files however are still found and reported.
Also make sure that your cron is configured so that it mails you the output of the cronjobs. The manual page will help you with that.
This is how a sample email looks if you have an infection:
/home/remy/eicar.zip: Eicar-Test-Signature FOUND /home/remy/eicar.com: Eicar-Test-Signature FOUND
Extra: the targeted scan
The below cronjob is an example and you should adapt it. It updates the virus
definitions and scans the folder
html/uploads/ two times per hour, and if it found any files it removes them.
## This should be a root cronjob. */29 * * * * /usr/bin/freshclam --quiet; /usr/bin/clamscan --recursive --no-summary --infected --remove /var/www/sites/uploader.com/public-html/uploads 2>/dev/null
This is how a sample email might look like:
Tags: anti-virus , clamav , malware , tutorials , ubuntu , virus
/var/www/sites/uploader.com/public-html/uploads/eicar.zip: Eicar-Test-Signature FOUND /var/www/sites/uploader.com/public-html/uploads/eicar.zip: Removed. /var/www/sites/uploader.com/public-html/uploads/eicar.com: Eicar-Test-Signature FOUND /var/www/sites/uploader.com/public-html/uploads/eicar.com: Removed.