#!/bin/bash# Automated script to install and set up ClamAV on Fedora.## Installs ClamAV if needed, configures and starts the clamd@scan daemon and the# freshclam signature updater, and adds:# sys-scan - manual scan of /home, /usr/bin and /usr/local/bin# clamsig - force a signature update# a nightly scan at 2:00 AM (clamav-nightly-scan.timer)set-euopipefail
if["$EUID"-ne0];thenecho"This setup script requires root privileges. Run with sudo."exit1fiSCAN_CONF="/etc/clamd.d/scan.conf"FRESHCLAM_CONF="/etc/freshclam.conf"SOCKET="/run/clamd.scan/clamd.sock"# -----------------------------------------------------------------------------# 0. Install ClamAV if it is missing# -----------------------------------------------------------------------------echo"Checking for Fedora ClamAV packages..."if!command-vclamdscan>/dev/null2>&1||!command-vfreshclam>/dev/null2>&1||[!-f"$SCAN_CONF"];thenecho"Installing clamav, clamav-freshclam and clamd..."dnfinstall-yclamavclamav-freshclamclamd
fiecho"ClamAV is installed. Proceeding with Fedora configuration setup..."# -----------------------------------------------------------------------------# 1. Configure the scan daemon and the updater# -----------------------------------------------------------------------------# Fedora ships these files with an "Example" line that stops the programs from# starting until it is commented out, and with the local socket disabled.forconfin"$SCAN_CONF""$FRESHCLAM_CONF";doif[-f"$conf"]&&grep-q'^Example'"$conf";thensed-i's/^Example/#Example/'"$conf"echo"Enabled $conf (commented out the 'Example' line)."fidoneifgrep-q"^LocalSocket ""$SCAN_CONF";thenecho"clamd local socket already configured."elifgrep-q"^#LocalSocket ""$SCAN_CONF";thensed-i"s|^#LocalSocket .*|LocalSocket $SOCKET|""$SCAN_CONF"echo"Enabled clamd local socket at $SOCKET."elseecho"LocalSocket $SOCKET">>"$SCAN_CONF"echo"Added clamd local socket at $SOCKET."fi# Let ClamAV read system and home directories under SELinuxifcommand-vgetenforce>/dev/null2>&1&&["$(getenforce)"="Enforcing"];thenecho"SELinux is Enforcing. Enabling antivirus_can_scan_system boolean..."setsebool-Pantivirus_can_scan_system1fi# clamd will not start without a signature database, so fetch one firstif!ls/var/lib/clamav/*.c[lv]d>/dev/null2>&1;thenecho"Downloading the first signature database (this can take a minute)..."freshclam
fiecho"Enabling the signature updater and the scan daemon..."systemctlenable--nowclamav-freshclam.service
systemctlenable--nowclamd@scan.service
# -----------------------------------------------------------------------------# 2. Nightly scan script# -----------------------------------------------------------------------------echo"Creating nightly scan script at /usr/local/bin/clamav-nightly-scan.sh..."cat<<'EOF' >/usr/local/bin/clamav-nightly-scan.sh#!/bin/bash# Nightly ClamAV scan of /home and system binaries, run by clamav-nightly-scan.timer.# Installed by setup-clamav-fedora.sh as /usr/local/bin/clamav-nightly-scan.sh.LOGFILE="/var/log/clamav/daily_scan.log"mkdir -p /var/log/clamav# Fedora's clamd@scan uses /etc/clamd.d/scan.conf; other distros use the default configCONF_ARGS=()[ -f /etc/clamd.d/scan.conf ] && CONF_ARGS=(--config-file=/etc/clamd.d/scan.conf)echo "=== Scan Started: $(date) ===" >>"$LOGFILE"clamdscan "${CONF_ARGS[@]}" --fdpass --infected --multiscan --log="$LOGFILE" /home /usr/bin /usr/local/binrc=$?echo "=== Scan Finished: $(date) (exit code $rc: 0 = clean, 1 = infected files found, 2 = error) ===" >>"$LOGFILE"exit $rcEOFchmod755/usr/local/bin/clamav-nightly-scan.sh
# -----------------------------------------------------------------------------# 3. systemd service + timer# -----------------------------------------------------------------------------echo"Creating systemd service at /etc/systemd/system/clamav-nightly-scan.service..."cat<<'EOF' >/etc/systemd/system/clamav-nightly-scan.service# systemd service that runs the nightly ClamAV scan (clamav-nightly-scan.sh).# Installed by setup-clamav-fedora.sh as /etc/systemd/system/clamav-nightly-scan.service.[Unit]Description=Trigger multi-threaded ClamAV system scanWants=clamd@scan.serviceAfter=clamd@scan.service[Service]Type=oneshotExecStart=/usr/local/bin/clamav-nightly-scan.shNice=10IOSchedulingClass=idleEOFecho"Creating systemd timer at /etc/systemd/system/clamav-nightly-scan.timer..."cat<<'EOF' >/etc/systemd/system/clamav-nightly-scan.timer# systemd timer that runs clamav-nightly-scan.service every day at 2:00 AM.# Installed by setup-clamav-fedora.sh as /etc/systemd/system/clamav-nightly-scan.timer.[Unit]Description=Run ClamAV Scan Automatically[Timer]OnCalendar=*-*-* 02:00:00Persistent=true[Install]WantedBy=timers.targetEOF# -----------------------------------------------------------------------------# 4. Manual scan command: sys-scan# -----------------------------------------------------------------------------echo"Creating manual scan script at /usr/local/bin/sys-scan..."cat<<'EOF' >/usr/local/bin/sys-scan#!/bin/bash# Manual multi-threaded ClamAV daemon scan of /home and system binaries.# Installed by setup-clamav-fedora.sh as /usr/local/bin/sys-scan.CONF_ARGS=()[ -f /etc/clamd.d/scan.conf ] && CONF_ARGS=(--config-file=/etc/clamd.d/scan.conf)echo "========================================="echo " Starting Manual ClamAV System Scan "echo "========================================="echo "Scanning: /home, /usr/bin, /usr/local/bin"echo "Please wait..."echo ""clamdscan "${CONF_ARGS[@]}" --fdpass --multiscan /home /usr/bin /usr/local/binrc=$?echo ""echo "========================================="case $rc in 0) echo " Scan complete: no threats " ;; 1) echo " Scan complete: INFECTED FILES FOUND " ;; *) echo " Scan failed (is clamd@scan running?) " ;;esacecho "========================================="exit $rcEOFchmod755/usr/local/bin/sys-scan
# -----------------------------------------------------------------------------# 5. Manual signature update command: clamsig# -----------------------------------------------------------------------------echo"Creating manual updater script at /usr/local/bin/clamsig..."cat<<'EOF' >/usr/local/bin/clamsig#!/bin/bash# Force a ClamAV signature update with freshclam.# Installed by setup-clamav-fedora.sh as /usr/local/bin/clamsig.if [ "$EUID" -ne 0 ]; then echo "This script needs root privileges to stop/start systemd services." echo "Re-running script with sudo..." exec sudo "$0" "$@"fiecho "========================================="echo " Forcing ClamAV Signature Update "echo "========================================="echo ""echo "[1/3] Stopping clamav-freshclam service to release file locks..."systemctl stop clamav-freshclam.service# Always restart the background updater, even if the manual update failstrap 'systemctl start clamav-freshclam.service' EXITecho "[2/3] Downloading latest malware definitions from Cisco Talos..."echo "------------------------------------------------------------"freshclamecho "------------------------------------------------------------"echo "[3/3] Restarting clamav-freshclam for automated background checks..."echo ""echo "========================================="echo " Update Sequence Complete! "echo "========================================="EOFchmod755/usr/local/bin/clamsig
# -----------------------------------------------------------------------------# 6. Activate the timer# -----------------------------------------------------------------------------echo"Reloading systemd daemon to recognize new units..."systemctldaemon-reload
echo"Enabling and starting clamav-nightly-scan.timer..."systemctlenable--nowclamav-nightly-scan.timer
echo"========================================="echo"Fedora Setup complete! The following system-wide commands are available:"echo"1. sys-scan (Run a manual scan)"echo"2. clamsig (Force virus definitions update)"echo"Note: The nightly scan will trigger automatically via systemd at 2:00 AM."echo"Check status any time with: systemctl status clamd@scan clamav-nightly-scan.timer"echo"========================================="