ClamAV installation and daily scan + report on Ubuntu
Published: 01-12-2012 | Author: Remy van Elst | Text only version of this article
Table of Contents
ClamAV is a virus scanner for Linux/Unix like systems. This tutorial will showyou 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 anextra, it also covers scans for specific folders which remove infected filesautomatically.
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 areuploaded, or where users login and manipulate files. Lets say you host a fileupload site, you want to run a scan on the upload folder which auto removesinfected files. It can also be that your mailserver runs Linux, but peoplesometimes mail executables or infected pdf files. Then you also want a scanner.Or you might run a source control server (gitolite, mercurial) where sometimesbinary files are checked in. (although ClamAV might not find things in bare-gitrepositories, only in working directories.)
If you however only run a static HTML website, or an rsync backup server, or aRougelike via telnet, where there are no uploads or like, you might be wastingresources.
My advice is to look at your situation and then decide if you need ClamAV, andif you only need to scan a few folders, the entire system and if you want toauto-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 packageis 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 thedatabase) and then does a manual update of the definitions.
sudo /etc/init.d/clamav-freshclam restartsudo /usr/bin/freshclam
The daily scan
The below cronjob will run a virus database definition update (so that the scanalways has the most recent definitions) and afterwards run a full scan whichwill only report when there are infected files on the system. It also does notremove the infected files automatically, you have to do this manually. This wayyou 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 ofthe 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 ofthe 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 virusdefinitions and scans the folder
/var/www/sites/uploader.com/public-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.