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

Debian12.13 : Anti-virus(Clamav) , Mail Server

1. Antivirus software Clamav installed

Install Clam AntiVirus, a free anti-virus software for Linux, as an anti-virus measure.
By installing this anti-virus software, you can not only scan the entire server for viruses, but also scan incoming and outgoing mail for viruses if you build and configure a mail server.。

1.1 Install

The clamav-related configuration files are installed in the /etc/clamav/ folder.

1.2 Virus Definition Update

Edit configuration file

Automatic virus definition update confirmation
Ensure that the service is registered for automatic virus definition updates.

It appears as follows

Logs are recorded in the file /var/log/clamav/freshclam.log.

1.3 Virus Check Confirmation

①Running manual virus checks

Infected files: 0, so no virus

②Virus detection by downloading test viruses
Download a harmless virus and test it for detection.

As you can see, it notifies me of the virus by displaying “FOUND” and “Removed.” and “Infected files: 1”. The “--remove” option was added, so the test virus was removed.

1.4 Create a script file to do a full scan

①Create a script file storage directory (/opt/script) in advance.

②Create script file

Contents of clam-full.sh (new)

③execute authorization

④Create a folder for virus quarantine
If not, a runtime error will occur because the above script specifies it as an excluded directory.

⑤Script Execution

akes quite a long time to complete.
Logs are recorded in the /var/log/clamav/clamscan.log file.

⑤Scheduled virus scan execution with cron

上記例では、毎週月曜日の夜中2時に定期的に実行。

2. Mail server installation

Postfix was developed as a Mail Transport Agent (MTA) to replace sendmail, and is a mail server that is highly compatible with sendmail, secure, easy to maintain, and fast.
In addition, since Postfix only functions as an SMTP server for sending mail, the POP server Dovecot for receiving mail will be installed separately in the latter half.

2.1 Postfix : Installation Configuration

Install Postfix
Install Postfix and build an SMTP server; SMTP uses 25/TCP.
To prevent unauthorized mail relay, use the SASL function of Dovecot, described below, and configure Postfix so that authentication is also required for sending.

You will be asked to select general configuration settings, select "No configuration " to set them manually later

Edit configuration file (main.cf)

[main.cf] Edit Content

# Line 82:Uncomments
mail_owner = postfix

# Line 100:Add Hostname Specification
myhostname = mail.[Domain]

# Per Line 107:Domain Name Specification Addition
mydomain = [Domain]

# Per Line 127:Uncomments
myorigin = $mydomain

# Per Line 141:Uncomments
inet_interfaces = all

# Per Line 189:Uncomments
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

# Per Line 232:Uncomments
local_recipient_maps = unix:passwd.byname $alias_maps

# Per Line 277:Uncomments
mynetworks_style = subnet

# Per Line 294:Self-Network Addendum
mynetworks = 127.0.0.0/8, 192.168.11.0/24

# Per Line 416:Uncomments
alias_maps = hash:/etc/aliases

# Per Line 427:Uncomments
alias_database = hash:/etc/aliases

# Per Line 449:Uncomments
home_mailbox = Maildir/

# Per Line 585:Add a comment and append it below
#smtpd_banner = $myhostname ESMTP $mail_name (Debian/GNU)
smtpd_banner = $myhostname ESMTP

# Per Line 659:Add
sendmail_path = /usr/sbin/postfix

# Per Line 664:Add
newaliases_path = /usr/bin/newaliases

# Per Line 669:Add
mailq_path = /usr/bin/mailq

# Per Line 675:Add
setgid_group = postdrop

#Per Line 679:Commenting
#html_directory =

# Per Line 683:Commenting
#manpage_directory =

# Per Line 688:Commenting
#sample_directory =

# Per Line 692:Commenting
#readme_directory =

#Add to the last line
#Disable the SMTP VRFY command
disable_vrfy_command = yes

#Request the HELO command from the client
smtpd_helo_required = yes

#Limit sent and received email size to 10MB
message_size_limit = 10485760

#Limit mailbox size to 1GB
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 = $myhostname
smtpd_recipient_restrictions = permit_mynetworks, permit_auth_destination, permit_sasl_authenticated, reject

Edit configuration file (master.cf)

[master.cf] Edit Content

Line 19,22 : Uncomments
submission inet n - y - - smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
# -o smtpd_tls_auth_only=yes

Reflect settings, restart

2.2 Dovecot : Installation Configuration

Install Dovecot
Install Dovecot and build a POP/IMAP server, using 110/TCP for POP and 143/TCP for IMAP.

Configure Dovecot to provide SASL functionality for Postfix

Reflect settings, reboot

2.3 Email User Account Registration

egister a user account for e-mail.
This setting is for when a user account on the OS is also used for e-mail.
If you want to use mail with a user account on the OS, no additional configuration is required, just register the OS user

Mail client installed

Mailboxes are set to refer to Maildir

2.4 Opening Ports

2.5 operation check ①

①Send test mail to yourself [mail (user name)@(host name)].
user name : huong

Ctrl + D key to exit the main text

Check incoming mail

2.6 operation check ②

Set up and confirm your account in Mozilla Thunderbird (for OS user huong as mail user)
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>
Enter the information and click "Continue".

Click on [EDIT CONFIGURATION]

Incoming Server Settings
Set [Connection security] to [None] and click [Continue].

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

Click [Continue]

Enter the [user's password] in the "Password" field and click "Continue"

Click [Continue]

Click "Finish" when the email account has been successfully created.

3 Applied ClamAV to mail server Postfix

Set up Postfix and Clamav to work together to scan incoming and outgoing mail in real time.

Install Amavisd and Clamav Daemon and start Clamav Daemon

If the server is not using a fully qualified domain name (FQDN) as the hostname, Amavis may fail to start. Also, since the OS hostname may change, set a valid hostname directly in the Amavis configuration file

Virus Scan Enable

Register your own domain name

Edit configuration file (Main.cf)

Edit configuration file (master.cf)

Editorial content

Settings reflect

When you send an e-mail to yourself on Thuderbird, etc., it is successful if you see the following message in the header of the incoming e-mail.

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 localhost (localhost [127.0.0.1])
   by mail.xxxxxxx.com (Postfix) with ESMTP id D2C87A002F
   for xxxxx@xxxxxxx.com; Fri, 23 Jan 2026 14:52:21 +0900 (JST)
X-Virus-Scanned: Debian amavis at xxxxxxx.com
Received: from mail.xxxxxxx.com ([127.0.0.1])
 by localhost (mail.xxxxxxx.com [127.0.0.1]) (amavis, port 10024) with ESMTP
 id pdZKQYgBYANB for xxxxx@xxxxxxx.com;
 Fri, 23 Jan 2026 14:52:21 +0900 (JST)

Received: from [192.168.11.8] (buffalo.setup [192.168.11.1])
   by mail.xxxxxxx.com (Postfix) with ESMTPA id B9081A001F
   for xxxxx@xxxxxxx.com; Fri, 23 Jan 2026 14:52:21 +0900 (JST)
Message-ID: c8d2d46a-313a-46b6-a95e-ba341e819f7f@xxxxxxx.com
Date: Fri, 23 Jan 2026 14:52:22 +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

Send yourself an email with the body text "X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*" and verify that the email is discarded and does not arrive.

4 Apply spam checks to the mail server

Using SpamAssassin and procmail for spam checking

4.1 spamassassin installation

①Install

②SpamAssassin Configuration

➂SpamAssassin Configuration File Update Script

[spamassassin-update.sh] Contents

#!/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

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

Grant the spamassassin-update script execution permissions and run it.

Verify that the SpamAssassin configuration file (local.cf) has been created with the current date in the /etc/mail/spamassassin directory.
Install unzip beforehand

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

SpamAssassin Milter Startup and Automatic Startup Configuration

  • Create a directory named ".Spam" in Maildir format for storing spam emails
  • Creation must be performed by the target user.

4.2 Preparing Procmail

①Installing Procmail

If installed, the following result will be returned.

②Procmail Configuration
Entering settings in /etc/procmailrc, which controls all mail filtering, will apply them to all users.
When written in the individual user's mail filter file, /home/[user]/.procmailrc, the filter applies only to that user.
This time, to apply it to all users, we will write it in /etc/procmailrc.

Content recorded in procmailrc (new creation)

#Set the path
PATH=/bin:/usr/bin:/usr/local/bin
#Mailbox Settings
MAILDIR=$HOME/Maildir
DEFAULT=$MAILDIR/
#Specify the output location for Procmail log files
LOGFILE=$MAILDIR/procmaillog
#Specify the path to the lock file
LOCKFILE=$HOME/.lockmail
#If the mail header does not contain an "X-Spam-***" entry, SpamAssassin will be launched.
:0fw
*!^X-Spam.*
|spamassassin
#If the email header contains the entry "X-Spam-Status: Yes", the email will be stored in the ".Spam" directory.
:0
*^X-Spam-Status: Yes
$MAILDIR/.Spam/

4.3 Postfix Configuration

① Editing main.cf

②Reflecting Settings and Launching

4.4 Learning about spam emails

Teach the system to recognize all contents within every user's ".Spam" directory as spam email.
①Learning about spam emails

②Learning regular emails

③Create a script and register it with Cron
Name the file something like "spam-learns.sh" and place it under /opt/scripts.
After saving the script, grant it executable permissions using chmod 750 spam-learns.sh.

[spam-learns.sh]Content

#! /bin/sh
#Learning about spam emails
/usr/bin/sa-learn --spam /home/*/Maildir/.Spam/cur
#Learning regular emails
/usr/bin/sa-learn --ham /home/*/Maildir/cur

#If you want to force the deletion of the contents of the spam email storage directory, add the following line:
/bin/rm -f /home/*/Maildir/.Spam/cur

Once spam-learns.sh is created, create the definition file directly under /lib/systemd/system. Name it with a .service extension, such as spam-learns.service.
Define the Type as simple.

[spam-learns.service]Content
[Unit]
Description=demo sample node.js program

[Service]
Type=simple
ExecStart= /opt/script/spam-learns.sh
Restart=always

[Install]
WantedBy=multi-user.target

④If you send an empty email to yourself using Thunderbird or similar software and see a message like the following in the received email header, it means the process was successful.

X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Return-Path: xxxxx@xxxxxxx.com
X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on Lepard
X-Spam-Level: **
X-Spam-Status: No, score=2.3 required=13.0 tests=ALL_TRUSTED,
  CONTENT_TYPE_PRESENT,EMPTY_MESSAGE autolearn=no autolearn_force=no
  version=4.0.1

X-Original-To: xxxxx@xxxxxxx.com
Delivered-To: xxxxx@xxxxxxx.com
Received: from localhost (localhost [127.0.0.1])
  by mail.xxxxxxx.com (Postfix) with ESMTP id A92E760609
  for xxxxx@xxxxxxx.com; Fri, 23 Jan 2026 16:05:19 +0900 (JST)
X-Virus-Scanned: Debian amavis at xxxxxxx.com
Received: from mail.xxxxxxx.com ([127.0.0.1])
 by localhost (mail.xxxxxxx.com [127.0.0.1]) (amavis, port 10024) with ESMTP
 id QG9ltnq7dRlj for xxxxx@xxxxxxx.com;
 Fri, 23 Jan 2026 16:05:19 +0900 (JST)
-----------------------------------------------------------------------
Subject:

⑤Spam Check Confirmation
Confirm that when you send an email with the subject line "XJSC4JDBQADN1.NSBN32IDNENGTUBE-STANDARD-ANTI-UBE-TEST-EMAILC.34X" to yourself, the email is not delivered and is instead filtered into the Spam folder.
The header displays as follows:
Check the emails under /home/[user]/Maildir/.Spam/new/

Return-Path: xxxxx@xxxxxxx.com
X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on Lepard
X-Spam-Flag: YES
X-Spam-Level:*********************************************
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=4.0.1
X-Spam-Report:
     * 0.1 ALL_TRUSTED Passed through trusted hosts only via SMTP
     * 1000 GTUBE BODY: Generic Test for Unsolicited Bulk Email
     * 0.1 MULTIPART_ALTERNATIVE Multipart/alternative
     * -0.1 CONTENT_TYPE_PRESENT exists:Content-Type
     * 1.0 HTML_MESSAGE BODY: HTML included in message
     * 0.7 MPART_ALT_DIFF BODY: HTML and text parts are different

X-Original-To: xxxxx@xxxxxxx.com
Delivered-To: xxxxx@xxxxxxx.com

Copied title and URL