Contents
1. clamav ( アンチウィルスソフト )のインストール
1.1 Clam AntiVirusインストール
| 1 | # dnf -y install clamav clamd clamav-update | 
1.2 Clam AntiVirus設定ファイル編集
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # vi /etc/clamd.d/scan.conf 14行目 # Default: disabled LogFile /var/log/clamd.scan ← コメント解除 77行目 # Default: disabled PidFile /run/clamd.scan/clamd.pid ← コメント解除 96行目 # 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 ← コメント解除 219行目 # Run as another user (clamd must be started by root for this option to work) # Default: don't drop privileges #User clamscan ← 行頭に#を追加してコメントアウト(root権限で動作するようにする) | 
1.3 ウイルス定義ファイル更新設定
| 1 2 3 4 5 6 7 8 9 10 11 12 | # vi /etc/freshclam.conf 75行目 「DatabaseMirror database.clamav.net」の行頭に「#」を挿入し、 「DatabaseMirror db.jp.clamav.net」を追加 #DatabaseMirror database.clamav.net DatabaseMirror db.jp.clamav.net 151 行目あたり 「NotifyClamd /etc/clamd.d/scan.conf」を追加 #NotifyClamd /path/to/clamd.conf NotifyClamd /etc/clamd.d/scan.conf | 
1.4 ウイルス定義ファイル最新化
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # freshclam ClamAV update process started at Wed Aug  9 15:27:16 2023 daily database available for download (remote version: 26994) Time:    1.3s, ETA:    0.0s [========================>]   58.82MiB/58.82MiB Testing database: '/var/lib/clamav/tmp.aab5f1004b/clamav-7d6dbe853cf7d769199009e8d0f70ca3.tmp-daily.cvd' ... Database test passed. daily.cvd updated (version: 26994, sigs: 2039812, f-level: 90, builder: raynman) main database available for download (remote version: 62) Time:    2.8s, ETA:    0.0s [========================>]  162.58MiB/162.58MiB Testing database: '/var/lib/clamav/tmp.aab5f1004b/clamav-5689b04d4cf704ce444fd99f66210516.tmp-main.cvd' ... Database test passed. main.cvd updated (version: 62, sigs: 6647427, f-level: 90, builder: sigmgr) bytecode database available for download (remote version: 334) Time:    0.3s, ETA:    0.0s [========================>]  285.12KiB/285.12KiB Testing database: '/var/lib/clamav/tmp.aab5f1004b/clamav-55bc60cc992d3fb838262971a2eaf8fc.tmp-bytecode.cvd' ... Database test passed. bytecode.cvd updated (version: 334, sigs: 91, f-level: 90, builder: anvilleg) WARNING: Clamd was NOT notified: Can't connect to clamd through /run/clamd.scan/clamd.sock: No such file or directory | 
1.5 Clam AntiVirus起動
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # systemctl start clamd@scan ← clamd起動 # systemctl enable clamd@scan ← clamd自動起動設定 Created symlink /etc/systemd/system/multi-user.target.wants/clamd@scan.service → /usr/lib/systemd/system/clamd@.service. # systemctl is-enabled clamd@scan enabled # systemctl status clamd@scan ← 動作確認 ● clamd@scan.service - clamd scanner (scan) daemon    Loaded: loaded (/usr/lib/systemd/system/clamd@.service; enabled; vendor pres>    Active: active (running) since Fri 2023-08-18 11:06:28 JST; 21s ago      Docs: man:clamd(8)            man:clamd.conf(5)            https://www.clamav.net/documents/  Main PID: 77380 (clamd)     Tasks: 2 (limit: 22884)    Memory: 1.2G    CGroup: /system.slice/system-clamd.slice/clamd@scan.service            mq77380 /usr/sbin/clamd -c /etc/clamd.d/scan.conf Aug 18 11:06:28 Lepard clamd[77380]: ELF support enabled. Aug 18 11:06:28 Lepard clamd[77380]: Mail files support enabled. Aug 18 11:06:28 Lepard clamd[77380]: OLE2 support enabled. Aug 18 11:06:28 Lepard clamd[77380]: PDF support enabled. Aug 18 11:06:28 Lepard clamd[77380]: SWF support enabled. Aug 18 11:06:28 Lepard systemd[1]: Started clamd scanner (scan) daemon. Aug 18 11:06:28 Lepard clamd[77380]: HTML support enabled. Aug 18 11:06:28 Lepard clamd[77380]: XMLDOCS support enabled. Aug 18 11:06:28 Lepard clamd[77380]: HWP3 support enabled. Aug 18 11:06:28 Lepard clamd[77380]: Self checking every 600 seconds. | 
1.6 ウィスルスキャンを実施
テスト用ウィルスをダウンロードして、ウィスルスキャンを実施
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | # wget http://www.eicar.org/download/eicar.com # clamscan --infected --remove --recursive /root/eicar.com: Win.Test.EICAR_HDB-1 FOUND /root/eicar.com: Removed. ----------- SCAN SUMMARY ----------- Known viruses: 8671786 Engine version: 0.103.8 Scanned directories: 1 Scanned files: 10 Infected files: 1 Data scanned: 0.02 MB Data read: 0.01 MB (ratio 2.00:1) Time: 14.699 sec (0 m 14 s) Start Date: 2023:08:18 11:07:33 End Date:   2023:08:18 11:07:48 | 
1.7 ウィルススキャン自動実行スクリプトの導入
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # mkdir -p /var/www/system # cd /var/www/system clamscan.sh を次の内容で/var/www/systemに作成 # vi /var/www/system/clamscan.sh #!/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} / | 
スクリプトに実行権限付与
| 1 | # chmod 700 clamscan.sh | 
1.8 ウイルススキャン除外ディレクトリー設定
| 1 2 | # echo "/sys/" >> /var/www/system/clamscan.exclude # echo "/proc/" >> /var/www/system/clamscan.exclude | 
sys ディレクトリ、proc ディレクトリは対象外にする
1.9 ウィルススキャンの定期実行
| 1 2 | # crontab -e 0 1 * * * /var/www/system/clamscan.sh > /dev/null 2>&1 | 
試しにスクリプトを実行し、フルスキャンする(時間がかかります)
| 1 | # /var/www/system/clamscan.sh | 
2. メールサーバー (Postfix)
2.1 Postfixのインストール
Postfix をインストールして SMTP サーバーを構築します
| 1 | # dnf -y install postfix | 
Postfix がインストールされているか確認
| 1 2 3 4 | # rpm -qa | grep postfix postfix-3.5.8-4.el8.x86_64 postfix-perl-scripts-3.5.8-4.el8.x86_64 pcp-pmda-postfix-5.3.7-17.0.1.el8_8.x86_64 | 
2.2 Postfix をサービスへ登録
| 1 2 3 4 | # systemctl enable postfix.service Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service. # systemctl is-enabled postfix.service enabled | 
2.3 postfix の設定ファイル編集
postfix の設定ファイル、main.cf とmaster.cf ファイルをバックアップ
| 1 2 | # cp -p /etc/postfix/main.cf `date '+/etc/postfix/main.cf.%Y%m%d'` # cp -p /etc/postfix/master.cf `date '+/etc/postfix/master.cf.%Y%m%d'` | 
メール不正中継防止のため、Dovecot の SASL 機能を利用して、送信にも認証を要求するように Postfix を設定します
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # vi /etc/postfix/main.cf 96 行目あたりに追加 #myhostname = virtual.domain.tld myhostname = mail.<ドメイン> 103 行目あたりに自ドメイン名を追加 #mydomain = domain.tld mydomain = <ドメイン> 118 行目あたりコメントアウト削除 myorigin = $mydomain 135 行目あたり変更 inet_interfaces = all 183 行目あたりをコメントアウトし、184 行目に追加します。 #mydestination = $myhostname, localhost.$mydomain, localhost mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain 287 行目あたりに追加 #mynetworks = 168.100.189.0/28, 127.0.0.0/8 #mynetworks = $config_directory/mynetworks #mynetworks = hash:/etc/postfix/network_table mynetworks = 192.168.11.0/24, 127.0.0.0/8 ←192.168.11.0/24は各自環境に合わす 440 行目あたりコメント削除 メールの格納形式の設定をします。 #home_mailbox = Mailbox home_mailbox = Maildir/  447 行目あたりに追加 #mail_spool_directory = /var/mail mail_spool_directory = /var/spool/mail  593 行目あたりに追加 #smtpd_banner = $myhostname ESMTP $mail_name #smtpd_banner = $myhostname ESMTP $mail_name ($mail_version) smtpd_banner = $myhostname ESMTP unknown 以下の内容を最終行に追加 # 送受信メールサイズを 10M に制限 message_size_limit = 10485760 # メールボックスサイズを 1G に制限 mailbox_size_limit = 1073741824 # SMTP-Auth 設定 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 | 
2.4 SMTP ポート(25 番ポート) の解放
| 1 2 3 4 | # firewall-cmd --add-service=smtp --permanent success # firewall-cmd --reload success | 
2.5 Postfix を起動
| 1 2 | # systemctl enable --now postfix # systemctl start postfix | 
3. メールサーバー (Dovecot)
3.1 Dovecotインストール
| 1 | # dnf -y install dovecot | 
3.2 dovecot.conf ファイルを編集
| 1 2 3 4 5 6 7 8 9 10 11 | # cp -p /etc/dovecot/dovecot.conf `date '+ /etc/dovecot/dovecot.conf.%Y%m%d'` # vi /etc/dovecot/dovecot.conf 25 行目あたりに追加 # protocols = imap pop3 lmtp protocols = imap pop3 30行目 : コメント解除 # IPv4 のみリスンする場合は [::] を削除 listen = *, :: | 
3.3 10-auth.conf ファイルを編集
| 1 2 3 4 5 6 7 8 | # vi /etc/dovecot/conf.d/10-auth.conf 10行目 : コメント解除し変更 プレーンテキスト認証も許可する場合 disable_plaintext_auth = no 100行目 : 追記 auth_mechanisms = plain login | 
3.4 10-mail.conf ファイルを編集
| 1 2 3 4 | # vi /etc/dovecot/conf.d/10-mail.conf 31行目 : 追記 mail_location = maildir:~/Maildir | 
3.5 10-master.conf ファイルを編集
| 1 2 3 4 5 6 7 8 9 | # vi /etc/dovecot/conf.d/10-master.conf 107-109行目 : コメント解除して追記 # Postfix smtp-auth unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix group = postfix } | 
3.6 10-ssl.conf ファイルを編集
| 1 2 3 4 5 | # vi /etc/dovecot/conf.d/10-ssl.conf 8 行目あたり 「ssl = required」を「ssl = yes」に変更 ssl = yes | 
3.7 dovecot をサービスに登録し、起動する
| 1 2 3 4 5 | # systemctl enable dovecot.service Created symlink /etc/systemd/system/multi-user.target.wants/dovecot.service → /usr/lib/systemd/system/dovecot.service. # systemctl is-enabled dovecot.service Enabled # systemctl start dovecot.service | 
3.8 firewalldでPOP/IMAP サービスの許可ポート開放を行う
POP は [110/TCP], IMAP は [143/TCP]
| 1 2 3 | # firewall-cmd --permanent --add-service=pop3 # firewall-cmd --permanent --add-service=imap # firewall-cmd --reload | 
4.メールユーザー作成と動作確認
4.1 事前準備
①新規ユーザーの為の前処理
新規でユーザーを追加した場合、自動的にメールの送受信ができるよう設定をします。
| 1 2 3 4 | # mkdir -p /etc/skel/Maildir/{new,cur,tmp} # chmod -R 700 /etc/skel/Maildir/ # echo "~/Maildir/"> /etc/skel/.forward # chmod 600 /etc/skel/.forward | 
②既存ユーザーのメール環境前処理
すでに作成してあるユーザーがメールの送受信が出来るよう設定をします。
| 1 2 3 4 | # mkdir -p /home/huong/Maildir/{new,cur,tmp} # chown -R huong:huong /home/huong/Maildir/ # chmod 700 /home/huong/Maildir # chmod 700 /home/huong/Maildir/{new,cur,tmp} | 
4.2 ユーザーアカウント作成
メールクライアントインストール
| 1 2 | # dnf -y install mailx # echo 'export MAIL=$HOME/Maildir' >> /etc/profile.d/mail.sh | 
ユーザー [linux] を追加
| 1 2 3 4 5 6 | # useradd linux # passwd linux ユーザー linux のパスワードを変更。 New password: Retype new password: passwd: all authentication tokens updated successfully. | 
4.3 動作確認 ①
メールユーザーでログインして、テストメールを送る
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # su - linux $ mail linux@localhost Subject: Test Mail test mail .   ← 本文終了するには「.」を入力 EOT メールを確認する $ mail Heirloom Mail version 12.5 7/5/10.  Type ? for help. "/home/linux/Maildir": 1 message 1 new >N  1 linux@korodes.com     Fri Aug 18 11:39  17/511   "Test Mail" & 1 Message  1: From linux@korodes.com Fri Aug 18 11:39:00 2023 Return-Path: <linux@korodes.com> X-Original-To: linux@localhost Delivered-To: linux@localhost Date: Fri, 18 Aug 2023 11:39:00 +0900 To: linux@localhost Subject: Test Mail User-Agent: Heirloom mailx 12.5 7/5/10 Content-Type: text/plain; charset=utf-8 From: linux@korodes.com Status: R test mail & 終了するには「q」を入力 | 
4.4 動作確認 ②
Mozilla Thunderbirdにアカウントを設定し、確認します。今回は一般ユーザー「huong」で設定します
①Thunderbirdを起動し、「Tools」「Account Settings」

②「Account Actions」「Add Mail Account」

③「Your full name」は任意の名称
「Email addtess」は先ほど追加した「huong@korodes.com」
「Password」はユーザーhuongのパスワード
をそれぞれ入力し、「Configure manually」クリック

④「INCOMMING SERVER」「OUTGOING SERVER」を下図のように設定し、「Re-test」クリック

⑤「サーバーがみつかりました(The following settings were found by probinfg the given server)」が表示される

「Done」をクリックすると、下図の「警告」が表示されるが問題は無いので「Confirm」をクリック

⑥「アカウントが作成されました(Account syccessfuly created)」が表示されますので「Finish」クリック

Mail サーバー : Postfix + Clamav + Amavisd+SpamAssassin
1.メールのリアルタイムスキャン
①Amavisd および Clamav Server をインストール
| 1 2 3 4 | # dnf config-manager --set-enabled ol8_codeready_builder # dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm # dnf config-manager --set-enabled epel # dnf -y install amavisd-new clamd perl-Digest-SHA1 perl-IO-stringy | 
➁設定ファイル編集
| 1 2 3 4 5 6 7 8 9 10 | # vi /etc/clamd.d/scan.conf 77行目:変更 PidFile /var/run/clamd.scan/clamd.pid 81行目:コメント解除 TemporaryDirectory /var/tmp 96行目:変更 LocalSocket /var/run/clamd.scan/clamd.sock | 
| 1 2 3 | # touch /var/log/clamd.scan # chown clamscan. /var/log/clamd.scan # systemctl enable clamd@scan | 
➂Amavisd を設定して起動
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # vi /etc/amavisd/amavisd.conf 13 行目あたり行頭の#削除 @bypass_spam_checks_maps = (1); # controls running of anti-spam code  23行目:自ドメイン名に変更 $mydomain = 'ドメイン名'; 28 行目あたりコメントアウト #$QUARANTINEDIR = undef; # -Q 125 行目あたりコメントアウト # $virus_admin = undef; # notifications recip 158行目:コメント解除して自ホスト名に変更 $myhostname = 'mail.ドメイン名'; # systemctl start amavisd # systemctl enable amavisd  Created symlink /etc/systemd/system/multi-user.target.wants/amavisd.service → /usr/lib/systemd/system/amavisd.service. | 
④Postfix の設定
| 1 2 3 | # vi /etc/postfix/main.cf # 最終行に追記 content_filter=smtp-amavis:[127.0.0.1]:10024 | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # vi /etc/postfix/master.cf # 最終行に追記  smtp-amavis unix - - n - 2 smtp -o smtp_data_done_timeout=1200 -o smtp_send_xforward_command=yes -o disable_dns_lookups=yes 127.0.0.1:10025 inet n - n - - smtpd -o content_filter= -o local_recipient_maps= -o relay_recipient_maps= -o smtpd_restriction_classes= -o smtpd_client_restrictions= -o smtpd_helo_restrictions= -o smtpd_sender_restrictions= -o smtpd_recipient_restrictions=permit_mynetworks,reject -o mynetworks=127.0.0.0/8 -o strict_rfc821_envelopes=yes -o smtpd_error_sleep_time=0 -o smtpd_soft_error_limit=1001 -o smtpd_hard_error_limit=1000 | 
| 1 | # systemctl restart postfix | 
⑤Thunderbirdで確認
Thunderbirdで自分宛にテストメールを送付し、受信メールのヘッダー表示部分に下記のように
「X-Virus-Scanned: amavisd-new at korodes.」が表示される

2.メールのスパム対策
①SpamAssassin の設定
| 1 2 3 4 | # vi /etc/mail/spamassassin/v310.pre 24 行目あたり行頭の#を削除 loadplugin Mail::SpamAssassin::Plugin::DCC | 
➁SpamAssassin 設定ファイル最新化スクリプト
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # cd /var/www/system # vi spamassassin-update.sh 下記内容を記述 #!/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再起動 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 | 
spamassassin-update スクリプトに実行権限を付与し、実行
| 1 2 | # chmod 700 /var/www/system/spamassassin-update.sh # /var/www/system/spamassassin-update.sh | 
/etc/mail/spamassassin ディレクトリにSpamAssassin 設定ファイルが当日日付で
作成されていることを確認
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # ls -l /etc/mail/spamassassin total 1632 drwxr-xr-x 2 root root     40 Aug 14 10:19 channel.d -rw-r--r-- 1 root root   1194 May  6  2022 init.pre -rw-r--r-- 1 root root 500636 Aug 14 10:35 local.cf drwx------ 2 root root      6 May  6  2022 sa-update-keys -rw-r--r-- 1 root root     62 May  6  2022 spamassassin-default.rc -rwxr-xr-x 1 root root     39 May  6  2022 spamassassin-helper.sh -rw-r--r-- 1 root root     55 May  6  2022 spamassassin-spamc.rc -rw-r--r-- 1 root root 500588 Apr 30 10:24 user_prefs -rw-r--r-- 1 root root 500588 Aug 14 10:35 user_prefs.org -rw-r--r-- 1 root root   2523 Aug 14 10:34 v310.pre -rw-r--r-- 1 root root   1194 May  6  2022 v312.pre -rw-r--r-- 1 root root   2416 May  6  2022 v320.pre -rw-r--r-- 1 root root   1237 May  6  2022 v330.pre -rw-r--r-- 1 root root   1020 May  6  2022 v340.pre -rw-r--r-- 1 root root   1309 May  6  2022 v341.pre -rw-r--r-- 1 root root   1469 May  6  2022 v342.pre -rw-r--r-- 1 root root   1264 May  6  2022 v343.pre | 
SpamAssassin 設定ファイルを最新化するスクリプトが毎日自動実行されるようcron に設定
| 1 2 | # crontab -e 0 2 * * * /var/www/system/spamassassin-update.sh > /dev/null 2>&1 | 
➂Postfix にSpamAssassin を組み込む
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # vi /etc/postfix/master.cf 12 行目の行頭に「#」を追加し13 行目にSpamAssassin 設定を追加 # smtp inet   n   -   n   -   -   smtpd smtp    inet    n    -    n    -    -    smtpd -o content_filter=spamassassin 最終行へ追加 smtp-amavis unix -    -    n    -    2  smtp     -o smtp_data_done_timeout=1200     -o smtp_send_xforward_command=yes     -o disable_dns_lookups=yes 127.0.0.1:10025 inet n    -    n    -    -  smtpd     -o content_filter=     -o local_recipient_maps=     -o relay_recipient_maps=     -o smtpd_restriction_classes=     -o smtpd_client_restrictions=     -o smtpd_helo_restrictions=     -o smtpd_sender_restrictions=     -o smtpd_recipient_restrictions=permit_mynetworks,reject     -o mynetworks=127.0.0.0/8     -o strict_rfc821_envelopes=yes     -o smtpd_error_sleep_time=0     -o smtpd_soft_error_limit=1001     -o smtpd_hard_error_limit=1000 spamassassin  unix  -   n       n       -       -       pipe   user=nobody argv=/usr/bin/spamc -e /usr/sbin/sendmail.postfix -oi -f ${sender} ${recipient} | 
④postfix を再起動
| 1 | # systemctl restart postfix | 
⑤Thunderbirdで自分宛に本文に下記記入してメールを送ると、ヘッダーに次のように表示される。
「XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X」



