ClamAV is a free, open-source antivirus software designed to detect viruses, trojans, and malware on your VPS. This guide explains step by step how to install ClamAV, configure it to start automatically, and schedule daily security scans.
Important: ClamAV can be resource-intensive. It is recommended that your VPS has at least 1 GB of free RAM. You can check available memory using the top command.
sudo dnf -y update
Updating ensures that all system packages and dependencies are up to date before installing ClamAV.
ClamAV is not included in the default CentOS/AlmaLinux repositories. You must first install the EPEL (Extra Packages for Enterprise Linux) repository:
sudo dnf -y install epel-release
Once EPEL is enabled, install ClamAV and its necessary components:
sudo dnf -y install clamav clamd clamav-data
This installs the main ClamAV package, the daemon (clamd), and the virus definition database.
Run the command below to check whether SELinux is active:
sudo sestatus
If SELinux is enabled, grant ClamAV the necessary permissions to scan system files:
sudo setsebool -P antivirus_can_scan_system 1 sudo setsebool -P clamd_use_jit 1
Modify the ClamAV configuration to enable the socket and disable the “Example” line:
sudo sed -i -e "s/^Example/#Example/" /etc/clamd.d/scan.conf sudo sed -i -e "s/#LocalSocket /LocalSocket /" /etc/clamd.d/scan.conf
Freshclam keeps ClamAV’s virus definitions up to date. Enable it by commenting out the “Example” line in its configuration:
sudo sed -i -e "s/^Example/#Example/" /etc/freshclam.conf
Run the following command to download the latest virus database:
sudo freshclam
If the update completes without errors, ClamAV is now ready to use.
Create a new systemd service file for Freshclam:
sudo nano /usr/lib/systemd/system/freshclam.service
Add the following content:
[Unit] Description=freshclam scanner After=network.target [Service] Type=forking ExecStart=/usr/bin/freshclam -d -c 1 Restart=on-failure PrivateTmp=true [Install] WantedBy=multi-user.target
You can change -c 1 to -c 2 to perform two updates per day.
sudo systemctl enable clamd@scan sudo systemctl enable freshclam sudo systemctl start clamd@scan sudo systemctl start freshclam
ClamAV will now run automatically and update itself regularly.
If you want to reduce memory usage, you can use a cron job instead of keeping the daemon active at all times. This allows you to schedule ClamAV scans at specific times.
sudo nano /etc/cron.d/clamav-scan
mkdir /var/log/clamav touch /var/log/clamav/cron-scan.log
MAILTO=root 0 2 * * * root /usr/bin/clamscan \ --quiet --infected --recursive / \ --exclude-dir=/proc/* \ --exclude-dir=/sys/* \ >> /var/log/clamav/cron-scan.log 2>&1
This command schedules a system-wide scan every day at 2:00 AM. You can change the time, for example to 6:00 PM, by replacing 0 2 * * * with 0 18 * * *.
Note that CPU usage may temporarily spike during a scan, so it’s best to schedule scans during off-peak hours.
You can perform manual scans using either Clamscan (command-line mode) or ClamD (daemon mode).
Run a basic scan using:
clamscan --exclude-dir=/proc/* --exclude-dir=/sys/* -i -r /
-i: Show only infected files-r: Perform a recursive scan--exclude-dir: Exclude virtual directoriesTo scan a specific directory using the ClamD service:
clamdscan --fdpass -i /folder
--fdpass: Allows scanning with the user’s permissions (e.g., root)-i: Shows only infected files/folder: The directory you wish to scanClamD configuration settings can be modified here:
sudo nano /etc/clamav/clamd.conf
From this file, you can define scan parameters, exclusions, and logging preferences.
You have now successfully installed and configured ClamAV on AlmaLinux, Rocky Linux, or CentOS Stream. Your server is now protected with a reliable antivirus system that can perform both automatic and scheduled scans. It is recommended to regularly check your logs and ensure your virus definitions are kept up to date.
Contact our experts, they will be happy to help!
Contact us