業務用エアコン関連の技術情報、エラーコード、環境問題対策に関する別サイト「エアコンの安全な修理・適切なフロン回収」

RockyLinux9.7 : Anti-virus(Clamav) , Mail Server

Clamav (anti-virus) Install

1.Clamav Install

2.Clam AntiVirus  Configuration File Edit

Edited content

●Line 14:
#Default: disabled
LogFile /var/log/clamd.scan ← Uncomments
●Line 77:
#Default: disabled
PidFile /run/clamd.scan/clamd.pid ← Uncomments
●Add to line 103
#Path to a local socket file the daemon will listen on.
#Default: disabled (must be specified by a user)
#LocalSocket /run/clamd.scan/clamd.sock
LocalSocket /var/run/clamd.scan/clamd.sock
●Line 233:
#Run as another user (clamd must be started by root for this option to work)
#Default: don't drop privileges
#User clamscan ← Add a # at the beginning of the line to comment it out (to enable operation with root privileges).

3.Virus definition file update settings

Edited content

●Line 78: Comment it out and add it below.
#DatabaseMirror database.clamav.net
DatabaseMirror db.jp.clamav.net
●Around line 154: Addition
#NotifyClamd /path/to/clamd.conf
NotifyClamd /etc/clamd.d/scan.conf

4.Virus definition file update

5.Clam AntiVirus start

6.Conducted virus scan

■Download a test virus and perform a virus scan

/root/eicar.com.txt: Win.Test.EICAR_HDB-1 FOUND
/root/eicar.com.txt: Removed.
Infected files: 1
The above is displayed and the virus file has been detected and deleted

7.Deployment of virus scan auto-execution scripts

Create script save directory

Creating an Automatic Virus Scan Script

Script Content

#!/bin/bash
PATH=/usr/bin:/bin
#excludeopt setup
excludelist=/var/www/system/clamscan.exclude
if [ -s $excludelist ]; then
for i in cat $excludelist
do
if [ $(echo "$i"|grep \/$) ]; then
i=echo $i|sed -e 's/^\([^ ]*\)\/$/\1/p' -e d
excludeopt="${excludeopt} --exclude-dir=$i"
else
excludeopt="${excludeopt} --exclude=$i"
fi
done
fi
#signature update
freshclam
#virus scan
clamscan --recursive --remove ${excludeopt} /

8.Virus scan exclusion directory settings

Exclude the sys directory and proc directory.

Try to implement a trial script

9.Scheduled virus scan execution

Mail Server Install

1. Postfix Install

1.1 Install
Install Postfix and build an SMTP server

Confirm Postfix is installed

1.2 Register Postfix to the service

1.3 Backup postfix configuration files, main.cf and master.cf files

1.4 Edit postfix configuration file
To prevent unauthorized mail relay, configure Postfix to require authentication for outgoing mail as well, using Dovecot's SASL function.

Edited content

●Line 96: Added
#myhostname = virtual.domain.tld
myhostname = mail.<domain>
●Line 103: Add your domain name
#mydomain = domain.tld
mydomain = <domain>
●1Line 18: Uncomment
myorigin = $mydomain
●Line 135: Change
inet_interfaces = all
●Line 183: Commented out
#mydestination = $myhostname, localhost.$mydomain, localhost
●Line 184: Uncomment
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
●Around line 286 : Add your own server network (adjust 192.168.11.0/24 to match your environment)
mynetworks = 192.168.11.0/24, 127.0.0.0/8
●Line 438: Uncomment (Configures the email storage format)
home_mailbox = Maildir/ 
●Line 445 : Uncomment
mail_spool_directory = /var/spool/mail 
●Add around line 593
smtpd_banner = $myhostname ESMTP unknown

●Add the following content to the last line
#Limit sent and received email size to 10MB
message_size_limit = 10485760
#Limit mailbox size to 1 GB
mailbox_size_limit = 1073741824
#SMTP Authentication Settings
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $mydomain
smtpd_recipient_restrictions = permit_mynetworks, permit_auth_destination, permit_sasl_authenticated, reject

1.5 Allow SMTP port (port 25)

1.6 Start Postfix

2.Dovecot

2.1 dovecot Install

2.2 Edit dovecot.conf file

Edited content

●Line 25: Append
#protocols = imap pop3 lmtp
protocols = imap pop3
●Line 30: Uncomment (Remove [::] if listening only on IPv4)
listen = *

2.3 Edit 10-auth.conf file

Edited content

●Line 10: Uncomment and modify
When plaintext authentication is also permitted
disable_plaintext_auth = no
●Line 100: Postscript
auth_mechanisms = plain login

2.3 Edit 10-mail.conf file

2.4 Edit 10-master.conf file

2.5 Edit 10-ssl.conf file

2.6 Register dovecot as a service and start it

2.7 Allow POP/IMAP services in Firewalld Port Allow
POP is [110/TCP], IMAP is [143/TCP].

3.Create mail user and check operation

3.1 advance preparation
new user
When a new user is added, set up the system to automatically send and receive e-mail.

Existing users
Configure the already created users to be able to send and receive e-mail.

3.2 User Account Creation

3.3 operation check ①
①Log in as an email user and send a test email.

Email Confirmation

3.4 Operation check ②
Set up and confirm your account in Mozilla Thunderbird. This time we will set it up with the general user "huong".
①Start Thunderbird and click [New Account][Email] in the [Three] Button-menu in the upper right corner.

Full name : Any name
Email address : huong@<domain-name>
「Continue」click

[EDIT CONFIGURATION] click

Incoming server settings
Set [Connection security] to [None] and click [Continue].

Sending Server Settings
Set [Connection security] to [None] and click [Test].

[Continue] click

Enter the user's password in the “Password” field and click "Continue"

[Continue] click

Click “Finish” when the email account has been successfully created.

Mail : Postfix + Clamav + clamav-milter+SpamAssassin

1.Real-time scanning of e-mails

①clamav-milter Install
Virus checks are performed on the mail server side using Clam AntiVirus.
The mail server and Clam AntiVirus are linked using clamav-milter.

clamav-milter configuration

Editing the clamav-milter configuration file

Edited content

●Line 6: Commented out
#Example
●Line 23: Addition
MilterSocket /var/run/clamav-milter/clamav-milter.socket ← Specify the UNIX socket name for clamav-milter
MilterSocketMode 660 ← Specify the access permissions for the UNIX socket of clamav-milter
●Per Line 98: Addition
ClamdSocket unix:/var/run/clamd.scan/clamd.sock ← Specify the Clam AntiVirus UNIX socket name (matching the LocalSocket option in /etc/clamd.d/scan.conf)
●Per Line 171: Addition
OnInfected Blackhole ← Delete virus-infected emails
●Per Line 193 : Addition
AddHeader Yes ← Output a message in the email header indicating that a virus check has been performed.
●Per Line 253 : Uncomments
LogFacility LOG_MAIL ← Set the log output destination to email logs

clamav-milter startup and auto-startup settings

Postfix and clamav-milter integration settings
Edit Postfix configuration file

Add postfix user to clamilt group

Postfix Settings Reflected

Check with Thunderbird
I sent a blank email to myself in Thunderbird, and in the header display of the received email, I see the following
「X-Virus-Scanned: clamav-milter 1.4.3 at Lepard」

X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Return-Path: xxxxx@xxxxxxx.com
X-Original-To: xxxxx@xxxxxxx.com
Delivered-To: xxxxx@xxxxxxx.com
Received: from [192.168.11.6] (buffalo.setup [192.168.11.1])
by mail.xxxxxxx.com (Postfix) with ESMTPA id 7383B113B57D
for xxxxx@xxxxxxx.com; Sat, 27 Dec 2025 10:14:30 +0900 (JST)
Message-ID: f21a16cf-b64f-4245-b99c-efc54d4f9244@xxxxxxx.com
Date: Sat, 27 Dec 2025 10:14:30 +0900
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
From: xxxxx xxxxx@xxxxxxx.com
Content-Language: en-US
To: xxxxx xxxxx@xxxxxxx.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Virus-Scanned: clamav-milter 1.4.3 at Lepard
X-Virus-Status: Clean

Try sending yourself an email with the body of the email "X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*" and confirm that the email is discarded and never received

2.Email spam protection

2.1 SpamAssassin・procmail・postfix settings

SpamAssassin・procmail Install

➁SpamAssassin start

SpamAssassin Settings

SpamAssassin configuration file modernization script

Script Content

#!/bin/bash

cd /etc/mail/spamassassin
wget -q https://github.com/kittyfreak/spamassassin_user_prefs/archive/refs/heads/main.zip
[ $? -ne 0 ] && exit
unzip main.zip >/dev/null 2>&1
[ $? -ne 0 ] && exit
rm -f main.zip
mv spamassassin_user_prefs-main/user_prefs .
rm -rf spamassassin_user_prefs-main
diff user_prefs user_prefs.org > /dev/null 2>&1
if [ $? -ne 0 ]; then
cp user_prefs local.cf
echo "report_safe 0" >> local.cf
echo "rewrite_header Subject SPAM" >> local.cf

#SpamAssassin restart
if [ -f /etc/rc.d/init.d/spamassassin ]; then
/etc/rc.d/init.d/spamassassin restart > /dev/null
else
systemctl restart spamassassin > /dev/null
fi
fi
cp user_prefs user_prefs.org

Grant execute permission to the spamassassin-update script and run it

Confirm that the SpamAssassin configuration file (local.cf) has been created in the /etc/mail/spamassassin directory with the date of the day

Set up cron to automatically run a script daily that updates the SpamAssassin configuration file

spamass-milter startup and automatic startup settings

Postfix and spamass-milter integration settings

Additional Edits

milter_default_action = tempfail
smtpd_milters = unix:/var/run/clamav-milter/clamav-milter.socket,unix:/run/spamass-milter/postfix/sock ※If clamav-milter is already defined
non_smtpd_milters = unix:/var/run/clamav-milter/clamav-milter.socket,unix:/run/spamass-milter/postfix/sock ※If clamav-milter is already defined

Restart postfix

⑧Procmail Settings
Create procmail configuration file

Create procmail log rotation configuration file

Postfix and Procmail integration settings

Postfix Settings Reflected

2.2 Spam mail sorting settings

Mail marked as spam in the mail header by SpamAssassin will be delivered to a spam-only mailbox, while other mail will be delivered to a normal mailbox.

Create a dedicated spam mailbox
For existing users, add a spam-only mailbox to their mailboxes.
For new users, a spam-only mailbox will be automatically created when a user is added.
Created script to create spam-only mailboxes

Script Content

#!/bin/bash

for user in `ls /home`
do
id -u $user > /dev/null 2>&1
if [ $? -eq 0 ] && [ ! -d /home/$user/Maildir/.Spam/new ]; then
mkdir -p /home/$user/Maildir/.Spam/new
mkdir -p /home/$user/Maildir/.Spam/cur
mkdir -p /home/$user/Maildir/.Spam/tmp
chmod -R 700 /home/$user/Maildir/.Spam
chown -R $user. /home/$user/Maildir/.Spam
echo $user
fi
done

Run script to create spam-only mailboxes

Measures against new users
Automatic spam-only mailbox creation when adding new users

➁spam mail sorting
Emails identified as spam by SpamAssassin are delivered to a spam-only mailbox.

spamass-milter confirmation
When you send a blank email to yourself, the following message is appended to the header of the received email
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on Lepard

X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Return-Path: xxxxx@xxxxxxx.com
X-Original-To: xxxxx@xxxxxxx.com
Delivered-To: xxxxx@xxxxxxx.com
Received: from [192.168.11.6] (buffalo.setup [192.168.11.1])
by mail.xxxxxxx.com (Postfix) with ESMTPA id 1D223113CA46
for xxxxx@xxxxxxx.com; Sat, 27 Dec 2025 11:20:46 +0900 (JST)
Message-ID: 80a9c3df-bd66-44ca-8723-b76da9415b04@xxxxxxx.com
Date: Sat, 27 Dec 2025 11:20:45 +0900
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
From: xxxxx xxxxx@xxxxxxx.com
Content-Language: en-US
To: xxxxx xxxxx@xxxxxxx.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Virus-Scanned: clamav-milter 1.4.3 at Lepard
X-Virus-Status: Clean
X-Spam-Status: No, score=5.3 required=13.0 tests=ALL_TRUSTED,
CONTENT_TYPE_PRESENT,EMPTY_MESSAGE,PDS_TONAME_EQ_TOLOCAL_SHORT,
PDS_TONAME_EQ_TOLOCAL_VSHORT autolearn=no autolearn_force=no
version=3.4.6
X-Spam-Level: *
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on Lepard

④Spam check confirmation
Send yourself an email with the body of the email "XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X" and make sure the email is not delivered and is sorted into the Spam folder
The header states

X-Spam-Flag: YES
X-Spam-Status: Yes, score=1001.8 required=13.0 tests=ALL_TRUSTED,
CONTENT_TYPE_PRESENT,GTUBE,HTML_MESSAGE,MPART_ALT_DIFF,
MULTIPART_ALTERNATIVE autolearn=no autolearn_force=no version=3.4.6
X-Spam-Report:
* 0.1 ALL_TRUSTED Passed through trusted hosts only via SMTP
* -0.1 CONTENT_TYPE_PRESENT exists:Content-Type
* 0.1 MULTIPART_ALTERNATIVE Multipart/alternative
* 1000 GTUBE BODY: Generic Test for Unsolicited Bulk Email
* 0.7 MPART_ALT_DIFF BODY: HTML and text parts are different
* 1.0 HTML_MESSAGE BODY: HTML included in message
X-Spam-Level:************************************************
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on Lepard

This is a multi-part message in MIME format.
--------------APlZDSJdHKxON0aRHopyNhC9
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

XJSC4JDBQADN1.NSBN32IDNENGTUBE-STANDARD-ANTI-UBE-TEST-EMAILC.34X

Copied title and URL