{"id":10414,"date":"2021-09-14T23:39:36","date_gmt":"2021-09-14T20:39:36","guid":{"rendered":"https:\/\/kifarunix.com\/?p=10414"},"modified":"2024-03-18T14:00:28","modified_gmt":"2024-03-18T11:00:28","slug":"install-clamav-on-debian-11","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-clamav-on-debian-11\/","title":{"rendered":"Install ClamAV on Debian 11"},"content":{"rendered":"\n
In this tutorial, we are going to learn how to Install ClamAV on Debian 11. ClamAV<\/a> is an open source antivirus engine for detecting trojans, viruses, malware, adwares, rootkits and other malicious threats.<\/p>\n\n\n\n Some of the features of ClamAV include;<\/p>\n\n\n\n ClamAV includes a multi-threaded scanner daemon, command line utilities for on demand file scanning and automatic signature updates. One of its main uses is on mail servers as a server-side email virus scanner.<\/p>\n\n\n\n To begin with, update system package cache.<\/p>\n\n\n\n As of this writing, the default Debian 11 repositories provides ClamAV version: 0.103.2.<\/p>\n\n\n\n The current stable release is version 0.104.0<\/a><\/strong>.<\/p>\n\n\n\n Thus, in order to ensure that you are running the latest version of ClamAV, you need to build and install from sources.<\/p>\n\n\n\n Install required package dependencies and build tools.<\/p>\n\n\n\n Create ClamAV service account;<\/p>\n\n\n\n Navigate to clamav.net downloads page<\/a> and download source code. You can run the command below to download it the current stable release version as of this writing.<\/p>\n\n\n\n Extract the source code, compile and install ClamAV.<\/p>\n\n\n\n Build and install ClamAV;<\/p>\n\n\n\n Sample output;<\/p>\n\n\n\n Once the installation is done, you need to configure ClamAV.<\/p>\n\n\n\n There are three main configs;<\/p>\n\n\n\n From our installation, we set ClamAV to store the configs on Update these configs as follows;<\/p>\n\n\n\n Since we didn't compile ClamAV with mail filtering support, then the configs above are enough.<\/p>\n\n\n\n Create the log, database and socket directories for FreshClam.<\/p>\n\n\n\n Set the ownership of the log, database and socket directories to clamav<\/strong> user.<\/p>\n\n\n\n For scanning to work, you need am updated virus database. There are two options for updating ClamAV database:<\/p>\n\n\n\n If you have internet access, you can use Then update the virus database;<\/p>\n\n\n\n The command will pull database updates and you might get an output similar to below.<\/p>\n\n\n\n Create FreshClam service;<\/p>\n\n\n\n Create and start ClamAV daemon service;<\/p>\n\n\n\n Reload systemd daemon and ensure the services are enabled to run on system boot;<\/p>\n\n\n\n Clamscan is used to scan files and directories for viruses. From the man pages, the clamscan command syntax is:<\/p>\n\n\n\n Some of the clamscan command options and their example usage is illustrated below;<\/p>\n\n\n\n Print help information using Note:<\/strong> Options marked with [=yes\/no(*)] can be optionally followed by =yes <\/strong>or =no. <\/strong>If they get called without the boolean argument the scanner will assume \u2018yes\u2019. The asterisk marks the default internal setting for a given option.<\/p>\n\n\n\n Scan specific directory or file;<\/p>\n\n\n\n Sample results<\/p>\n\n\n\n Do not display summary at the end of scanning.<\/p>\n\n\n\n Print infected files only ( Skip printing OK files ( Sound a bell on virus detection ( Scan directories recursively ( Save scan report to FILE ( Scan files listed line by line in FILE ( Remove infected files ( Move infected files into DIRECTORY Copy infected files into DIRECTORY (\u2013copy=DIRECTORY). Directory must be writable for the user or unprivileged user running clamscan.<\/p>\n\n\n\n There is quite long list of options for various usage of clamscan. Consult The following are the exit return codes for ClamAV.<\/p>\n\n\n\n To limit the clamscan CPU time to certain levels, you can use two tools;<\/p>\n\n\n\n To use nice command,<\/p>\n\n\n\n As long as no other process requires cputime, clamscan will maximize it. But as soon as another process with a higher priority needs cputime, clamscan will lost it.<\/p>\n\n\n\n Using cpulimit;<\/p>\n\n\n\n Limits clamscan cpu time to 15% when scanning the entire root directory.<\/p>\n\n\n\n ClamAV User Manual<\/a><\/p>\n\n\n\n\n
Installing ClamAV on Debian 11<\/h2>\n\n\n\n
Update System Package Cache<\/h3>\n\n\n\n
apt update<\/code><\/pre>\n\n\n\n
Install ClamAV<\/h3>\n\n\n\n
apt-cache policy clamav clamav-daemon<\/code><\/pre>\n\n\n\n
\nclamav:\n Installed: 0.103.2+dfsg-2\n Candidate: 0.103.2+dfsg-2\n Version table:\n *** 0.103.2+dfsg-2 500\n 500 http:\/\/deb.debian.org\/debian bullseye\/main amd64 Packages\n 100 \/var\/lib\/dpkg\/status\nclamav-daemon:\n Installed: 0.103.2+dfsg-2\n Candidate: 0.103.2+dfsg-2\n Version table:\n *** 0.103.2+dfsg-2 500\n 500 http:\/\/deb.debian.org\/debian bullseye\/main amd64 Packages\n 100 \/var\/lib\/dpkg\/status\n\n<\/code><\/pre>\n\n\n\n
Install ClamAV from Sources on Debian 11<\/h4>\n\n\n\n
apt install -y gcc make pkg-config python3 python3-pip python3-pytest valgrind \\\ncheck libbz2-dev libcurl4-openssl-dev libjson-c-dev libmilter-dev sudo \\\nlibncurses5-dev libpcre2-dev libssl-dev libxml2-dev zlib1g-dev cmake<\/code><\/pre>\n\n\n\n
useradd -r -M -d \/var\/lib\/clamav -s \/bin\/false -c \"Clam Antivirus\" clamav<\/code><\/pre>\n\n\n\n
VER=0.104.0<\/strong><\/code><\/pre>\n\n\n\n
wget https:\/\/www.clamav.net\/downloads\/production\/clamav-$VER.tar.gz<\/code><\/pre>\n\n\n\n
tar xzf clamav-$VER.tar.gz<\/code><\/pre>\n\n\n\n
cd clamav-$VER<\/code><\/pre>\n\n\n\n
mkdir build && cd build<\/code><\/pre>\n\n\n\n
cmake .. \\\n -D CMAKE_INSTALL_PREFIX=\/usr \\\n -D CMAKE_INSTALL_LIBDIR=lib \\\n -D APP_CONFIG_DIRECTORY=\/etc\/clamav \\\n -D DATABASE_DIRECTORY=\/var\/lib\/clamav \\\n -D ENABLE_JSON_SHARED=OFF<\/code><\/pre>\n\n\n\n
cmake --build .<\/code><\/pre>\n\n\n\n
ctest<\/code><\/pre>\n\n\n\n
\nTest project \/root\/clamav-0.104.0\/build\n Start 1: libclamav\n 1\/10 Test #1: libclamav ........................ Passed 8.22 sec\n Start 2: libclamav_valgrind\n 2\/10 Test #2: libclamav_valgrind ............... Passed 67.93 sec\n Start 3: clamscan\n 3\/10 Test #3: clamscan ......................... Passed 2.64 sec\n Start 4: clamscan_valgrind\n 4\/10 Test #4: clamscan_valgrind ................ Passed 35.01 sec\n Start 5: clamd\n 5\/10 Test #5: clamd ............................ Passed 10.02 sec\n Start 6: clamd_valgrind\n 6\/10 Test #6: clamd_valgrind ................... Passed 46.44 sec\n Start 7: freshclam\n 7\/10 Test #7: freshclam ........................ Passed 2.03 sec\n Start 8: freshclam_valgrind\n 8\/10 Test #8: freshclam_valgrind ............... Passed 20.16 sec\n Start 9: sigtool\n 9\/10 Test #9: sigtool .......................... Passed 0.46 sec\n Start 10: sigtool_valgrind\n10\/10 Test #10: sigtool_valgrind ................. Passed 1.23 sec\n\n100% tests passed, 0 tests failed out of 10\n\nTotal Test time (real) = 194.14 sec\n<\/code><\/pre>\n\n\n\n
cmake --build . --target install<\/code><\/pre>\n\n\n\n
Configuring ClamAV<\/h3>\n\n\n\n
\n
freshclam.conf<\/code> required for FreshClam<\/li>\n\n\n\n
clamd.conf<\/code> required by ClamD<\/li>\n\n\n\n
clamav-milter.conf<\/code> required by ClamAV-Milter if you enabled ClamAV support for mail filtering (we didnt do that in this setup).<\/li>\n<\/ul>\n\n\n\n
\/etc\/clamav<\/code>.<\/p>\n\n\n\n
ls -1 \/etc\/clamav\/<\/code><\/pre>\n\n\n\n
clamav-milter.conf.sample\nclamd.conf.sample\nfreshclam.conf.sample<\/code><\/pre>\n\n\n\n
\ncat > \/etc\/clamav\/freshclam.conf << 'EOL'\nDatabaseOwner clamav\nUpdateLogFile \/var\/log\/clamav\/freshclam.log\nLogVerbose false\nLogSyslog false\nLogFacility LOG_LOCAL6\nLogFileMaxSize 0\nLogRotate true\nLogTime true\nForeground false\nDebug false\nMaxAttempts 5\nDatabaseDirectory \/var\/lib\/clamav\nDNSDatabaseInfo current.cvd.clamav.net\nConnectTimeout 30\nReceiveTimeout 0\nTestDatabases yes\nScriptedUpdates yes\nCompressLocalDatabase no\nBytecode true\nNotifyClamd \/etc\/clamav\/clamd.conf\nChecks 24\nDatabaseMirror db.local.clamav.net\nDatabaseMirror database.clamav.net\nEOL\n<\/code><\/pre>\n\n\n\n
\ncat > \/etc\/clamav\/clamd.conf << 'EOL'\nLocalSocket \/var\/run\/clamav\/clamd.ctl\nFixStaleSocket true\nLocalSocketGroup clamav\nLocalSocketMode 666\nUser clamav\nScanMail true\nScanArchive true\nArchiveBlockEncrypted false\nMaxDirectoryRecursion 15\nFollowDirectorySymlinks false\nFollowFileSymlinks false\nReadTimeout 180\nMaxThreads 12\nMaxConnectionQueueLength 15\nLogSyslog false\nLogRotate true\nLogFacility LOG_LOCAL6\nLogClean false\nLogVerbose false\nPreludeEnable no\nPreludeAnalyzerName ClamAV\nDatabaseDirectory \/var\/lib\/clamav\nOfficialDatabaseOnly false\nSelfCheck 3600\nForeground false\nDebug false\nScanPE true\nMaxEmbeddedPE 10M\nScanOLE2 true\nScanPDF true\nScanHTML true\nMaxHTMLNormalize 10M\nMaxHTMLNoTags 2M\nMaxScriptNormalize 5M\nMaxZipTypeRcg 1M\nScanSWF true\nExitOnOOM false\nLeaveTemporaryFiles false\nAlgorithmicDetection true\nScanELF true\nIdleTimeout 30\nCrossFilesystems true\nPhishingSignatures true\nPhishingScanURLs true\nPhishingAlwaysBlockSSLMismatch false\nPhishingAlwaysBlockCloak false\nPartitionIntersection false\nDetectPUA false\nScanPartialMessages false\nHeuristicScanPrecedence false\nStructuredDataDetection false\nCommandReadTimeout 30\nSendBufTimeout 200\nMaxQueue 100\nExtendedDetectionInfo true\nOLE2BlockMacros false\nAllowAllMatchScan true\nForceToDisk false\nDisableCertCheck false\nDisableCache false\nMaxScanTime 120000\nMaxScanSize 100M\nMaxFileSize 25M\nMaxRecursion 16\nMaxFiles 10000\nMaxPartitions 50\nMaxIconsPE 100\nPCREMatchLimit 10000\nPCRERecMatchLimit 5000\nPCREMaxFileSize 25M\nScanXMLDOCS true\nScanHWP3 true\nMaxRecHWP3 16\nStreamMaxLength 25M\nLogFile \/var\/log\/clamav\/clamav.log\nLogTime true\nLogFileUnlock false\nLogFileMaxSize 0\nBytecode true\nBytecodeSecurity TrustSigned\nBytecodeTimeout 60000\nOnAccessMaxFileSize 5M\nEOL\n<\/code><\/pre>\n\n\n\n
mkdir \/var\/log\/clamav\/ \/var\/lib\/clamav \/var\/run\/clamav\/<\/code><\/pre>\n\n\n\n
chown clamav: \/var\/log\/clamav\/ \/var\/lib\/clamav \/var\/run\/clamav\/<\/code><\/pre>\n\n\n\n
Update the ClamAV Signature Database<\/h3>\n\n\n\n
\n
clamav-freshclam<\/strong><\/code>: updates the database from Internet. This is recommended with Internet access.<\/li>\n\n\n\n
Offline update<\/strong><\/code> for systems with no direct internet access.<\/li>\n<\/ul>\n\n\n\n
Update Signature Database with
clamav-freshclam<\/code><\/h4>\n\n\n\n
clamav-freshclam<\/code><\/strong> to update the ClamAV virus signature database.<\/p>\n\n\n\n
sudo -u clamav freshclam<\/code><\/pre>\n\n\n\n
\nTue Sep 14 22:29:35 2021 -> ClamAV update process started at Tue Sep 14 22:29:35 2021\nTue Sep 14 22:29:35 2021 -> daily database available for download (remote version: 26294)\nTime: 1m 18s, ETA: 0.0s [========================>] 55.56MiB\/55.56MiB\nTue Sep 14 22:30:55 2021 -> Testing database: '\/var\/lib\/clamav\/tmp.6d40b4137a\/clamav-ec762c472f7babc64cd6617646a05aa9.tmp-daily.cvd' ...\nTue Sep 14 22:31:02 2021 -> Database test passed.\nTue Sep 14 22:31:02 2021 -> daily.cvd updated (version: 26294, sigs: 1972718, f-level: 90, builder: raynman)\nTue Sep 14 22:31:02 2021 -> main database available for download (remote version: 61)\nTime: 4m 00s, ETA: 0.0s [========================>] 160.41MiB\/160.41MiB\nTue Sep 14 22:35:05 2021 -> Testing database: '\/var\/lib\/clamav\/tmp.6d40b4137a\/clamav-f2c7c5efaa09e11ebb5085cbd4abe3f3.tmp-main.cvd' ...\nTue Sep 14 22:35:13 2021 -> Database test passed.\nTue Sep 14 22:35:13 2021 -> main.cvd updated (version: 61, sigs: 6607162, f-level: 90, builder: sigmgr)\nTue Sep 14 22:35:13 2021 -> bytecode database available for download (remote version: 333)\nTime: 2.0s, ETA: 0.0s [========================>] 286.79KiB\/286.79KiB\nTue Sep 14 22:35:15 2021 -> Testing database: '\/var\/lib\/clamav\/tmp.6d40b4137a\/clamav-60d878c5920160c4068f0a9a43fc214e.tmp-bytecode.cvd' ...\nTue Sep 14 22:35:15 2021 -> Database test passed.\nTue Sep 14 22:35:15 2021 -> bytecode.cvd updated (version: 333, sigs: 92, f-level: 63, builder: awillia2)\nTue Sep 14 22:35:15 2021 -> ^Clamd was NOT notified: Can't connect to clamd through \/var\/run\/clamav\/clamd.ctl: No such file or directory\n<\/code><\/pre>\n\n\n\n
freshclam<\/strong><\/code> downloads the ClamAV databases, CVDs, and place them on under,
\/var\/lib\/clamav\/<\/code>.<\/p>\n\n\n\n
ls -1 \/var\/lib\/clamav\/<\/code><\/pre>\n\n\n\n
bytecode.cvd\ndaily.cvd\nfreshclam.dat\nmain.cvd<\/code><\/pre>\n\n\n\n
\ncat > \/etc\/systemd\/system\/clamav-freshclam.service << EOL\n[Unit]\nDescription=ClamAV virus database updater\nDocumentation=man:freshclam(1) man:freshclam.conf(5) https:\/\/www.clamav.net\/documents\n# If user wants it run from cron, don't start the daemon.\nConditionPathExists=!\/etc\/cron.d\/clamav-freshclam\nWants=network-online.target\nAfter=network-online.target\n\n[Service]\nUser=clamav\nGroup=clamav\nExecStart=\/usr\/bin\/freshclam -d --foreground=true\nStandardOutput=syslog\n\n[Install]\nWantedBy=multi-user.target\nEOL\n<\/code><\/pre>\n\n\n\n
\ncat > \/etc\/systemd\/system\/clamav-daemon.service << EOL\n[Unit]\nDescription=Clam AntiVirus userspace daemon\nDocumentation=man:clamd(8) man:clamd.conf(5) https:\/\/www.clamav.net\/documents\/\n# Check for database existence\nConditionPathExistsGlob=\/var\/lib\/clamav\/main.{c[vl]d,inc}\nConditionPathExistsGlob=\/var\/lib\/clamav\/daily.{c[vl]d,inc}\n\n[Service]\nUser=clamav\nGroup=clamav\nExecStart=\/usr\/sbin\/clamd --foreground=true\n# Reload the database\nExecReload=\/bin\/kill -USR2 $MAINPID\nStandardOutput=syslog\nTimeoutStartSec=420\n\n[Install]\nWantedBy=multi-user.target\nEOL\n<\/code><\/pre>\n\n\n\n
systemctl daemon-reload<\/code><\/pre>\n\n\n\n
systemctl enable --now clamav-daemon<\/code><\/pre>\n\n\n\n
systemctl enable --now clamav-freshclam<\/code><\/pre>\n\n\n\n
ClamAV Scanning<\/h3>\n\n\n\n
Clamscan CLI Options and Example Usage<\/h4>\n\n\n\n
clamscan [options] [file\/directory\/-]<\/code><\/pre>\n\n\n\n
-h<\/code> or
--help<\/code> option.<\/p>\n\n\n\n
clamscan -h<\/code><\/pre>\n\n\n\n
clamscan \/home\/<\/code><\/pre>\n\n\n\n
\nLoading: 16s, ETA: 0s [========================>] 8.56M\/8.56M sigs \nCompiling: 3s, ETA: 0s [========================>] 41\/41 tasks \n\n\n----------- SCAN SUMMARY -----------\nKnown viruses: 8564637\nEngine version: 0.104.0\nScanned directories: 1\nScanned files: 0\nInfected files: 0\nData scanned: 0.00 MB\nData read: 0.00 MB (ratio 0.00:1)\nTime: 20.375 sec (0 m 20 s)\nStart Date: 2021:09:14 23:14:15\nEnd Date: 2021:09:14 23:14:35\n<\/code><\/pre>\n\n\n\n
clamscan \/home\/filename.docx<\/code><\/pre>\n\n\n\n
clamscan --no-summary \/home\/<\/code><\/pre>\n\n\n\n
-i<\/code><\/strong>,
--infected<\/strong><\/code>);<\/p>\n\n\n\n
clamscan -i \/<\/code><\/pre>\n\n\n\n
-o<\/code>,
--suppress-ok-results<\/code><\/strong>);<\/p>\n\n\n\n
clamscan -o<\/strong> \/home\/<\/code><\/pre>\n\n\n\n
--bell<\/code><\/strong>);<\/p>\n\n\n\n
clamscan --bell<\/strong> -i \/home<\/code><\/pre>\n\n\n\n
-r<\/code><\/em>,
--recursive<\/code><\/em>).<\/p>\n\n\n\n
clamscan --bell -i -r<\/strong> \/home<\/code><\/pre>\n\n\n\n
-l FILE<\/code>,
--log=FILE<\/code><\/strong><\/em>);<\/p>\n\n\n\n
clamscan --bell -i -r<\/strong> \/home -l home-scan.txt<\/strong><\/code><\/pre>\n\n\n\n
-f FILE<\/code>,
--file-list=FILE<\/code><\/strong>).<\/p>\n\n\n\n
clamscan -i -f \/tmp\/scan<\/strong><\/code><\/pre>\n\n\n\n
--remove[=yes\/no(*)]<\/strong><\/code>). Be careful as this removes file completely.<\/p>\n\n\n\n
clamscan -r --remove \/home\/USER<\/code><\/pre>\n\n\n\n
(--move=DIRECTORY<\/code>). Directory must be writable for the user or unprivileged user running clamscan.<\/p>\n\n\n\n
clamscan -r -i --move=\/home\/USER\/infected \/home\/<\/code><\/pre>\n\n\n\n
clamscan -r -i --copy=\/home\/USER\/infected \/home\/<\/code><\/pre>\n\n\n\n
man clamscan<\/code> for more details.<\/p>\n\n\n\n
ClamAV Return Codes<\/h3>\n\n\n\n
\n
Limiting Clamscan CPU Usage<\/h3>\n\n\n\n
clamscan<\/code> can be CPU intensive especially if it scanning a large directory.<\/p>\n\n\n\n
\n
nice<\/code><\/strong>: lowers the priority of clamscan (limits relative cpu time).<\/li>\n\n\n\n
cpulimit<\/code><\/strong>: limits absolute cpu time.<\/li>\n<\/ul>\n\n\n\n
nice -n 15 clamscan && clamscan -ir \/<\/code><\/pre>\n\n\n\n
cpulimit -z -e clamscan -l 20 & clamscan -ir \/<\/code><\/pre>\n\n\n\n
Further Reading<\/h3>\n\n\n\n
Related Tutorials<\/h3>\n\n\n\n