- Postfixの設定。
- Dovecotのインストール。
- SMTP認証設定。
- Maildir形式メールボックスの作成。
- TCP25番、TCP587番ポート開放。
- ポート開放テスト。
- メールサーバーのOP25B対策。
- Gmailのメール転送設定。
- TCP110番またはTCP143番ポート開放。
- ポート開放テスト。
- メールユーザーの追加。
- メールソフトの登録。
- サーバー証明書の取得。
- SSLの設定。
- TCP465番ポート開放。
- ポート開放テスト。
- TCP587番ポート閉鎖。
- ポート開放テスト。
- Dovecot設定。
- TCP995番またはTCP993番ポート開放。
- ポート開放テスト。
- メールソフトの設定変更。
- アンチウィルスソフトの連携(Clamav)。
Postfixの設定。
外部からWebアクセスできるようにします。
root@www:~# vi /etc/apache2/sites-available/virtualhost-00-ubuntuserver.jp.conf <VirtualHost *:80> ServerName ubuntuserver.jp DocumentRoot /var/www/html ServerAlias mail.ubuntuserver.jp RewriteEngine On RewriteCond %{HTTP_HOST} ^ubuntuserver\.jp RewriteRule ^(.*)$ http://ubuntuserver.jp/$1 [R=301,L] </VirtualHost>
root@www:~# apt -y install postfix sasl2-bin
ドメイン:linuxserver.jp
root@www:~# cp /usr/share/postfix/main.cf.dist /etc/postfix/main.cf root@www:~# vi /etc/postfix/main.cf
mail_owner = postfix
myhostname = mail.ubuntuserver.jp
mydomain = ubuntuserver.jp
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
local_recipient_maps = unix:passwd.byname $alias_maps
mynetworks_style = subnet
mynetworks = 127.0.0.0/8, 192.168.1.1/24
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
home_mailbox = Maildir/
#smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_banner = $myhostname ESMTP unknown
sendmail_path = /usr/sbin/postfix
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
setgid_group = postdrop
#html_directory =
#manpage_directory =
#sample_directory =
#readme_directory =
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
message_size_limit = 10485760
root@www:~# vi /etc/postfix/master.cf
submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
root@www:~# newaliases
root@www:~# systemctl restart postfix
Dovecotのインストール。
Dovecotをインストールします。
root@www:~# apt -y install dovecot-core dovecot-pop3d dovecot-imapd
root@www:~# vi /etc/dovecot/dovecot.conf
listen = *
root@www:~# vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir
root@www:~# vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
root@www:~# vi /etc/dovecot/conf.d/10-master.conf
# Postfix smtp-auth unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix group = postfix }
root@www:~# vi /etc/dovecot/conf.d/10-ssl.conf
ssl = no
root@www:~# systemctl restart dovecot
SMTP認証設定。
cyrus-saslをインストールします。
root@www:~# apt -y install sasl2-bin cyrus-sasl-plain
root@www:~# systemctl start saslauthd
Maildir形式メールボックスの作成。
新規ユーザー追加時に自動でMaildir形式メールボックス作成する設定をします。
root@www:~# mkdir -p /etc/skel/Maildir/{new,cur,tmp}
root@www:~# chmod -R 700 /etc/skel/Maildir/
root@www:~# systemctl restart postfix
[root@www ~]# wget https://rcg.jp/perfect_maildir/perfect_maildir.pl -O /usr/local/bin/perfect_maildir.pl [root@www ~]# chmod +x /usr/local/bin/perfect_maildir.pl
[root@www ~]# apt -y install perl-TimeDate
[root@www ~]# systemctl stop postfix
[root@www ~]# vi migrate-maildir.sh #!/bin/bash # #Maildir一括変換スクリプト # #メールボックス=>Maildir形式変換スクリプト #http://perfectmaildir.home-dn.net/ FOLDERCONVERT=/usr/local/bin/perfect_maildir.pl #一般ユーザリスト USERLIST=`ls /home/` #ログ MIGRATELOG=/tmp/migrate-maildir.log rm -f $MIGRATELOG #引数(変換元メールボックス形式)チェック if [ "$1" != "mbox" ] && [ "$1" != "Mailbox" ]; then echo "Usage: migrate-maildir.sh {mbox|Mailbox}" exit fi #一般ユーザメールボックス移行 for user in $USERLIST; do if [ "$1" = "mbox" ]; then inbox="/var/spool/mail/${user}" else inbox="/home/${user}/Mailbox" fi if [ -f "${inbox}" ]; then newdir="/home/${user}/Maildir/" mkdir -p "$newdir" mkdir -p "$newdir"/cur mkdir -p "$newdir"/new mkdir -p "$newdir"/tmp chmod -R 700 "${newdir}" $FOLDERCONVERT "$newdir" < "${inbox}" >> $MIGRATELOG 2>&1 chown -R ${user}. "$newdir" find "$newdir" -type f -exec chmod 600 {} \; fi done #rootユーザメールボックス移行 user="root" if [ "$1" = "mbox" ]; then inbox="/var/spool/mail/${user}" else inbox="/${user}/Mailbox" fi if [ -f "${inbox}" ]; then newdir="/${user}/Maildir/" mkdir -p "$newdir" mkdir -p "$newdir"/cur mkdir -p "$newdir"/new mkdir -p "$newdir"/tmp chmod -R 700 "${newdir}" $FOLDERCONVERT "$newdir" < "${inbox}" >> $MIGRATELOG 2>&1 chown -R ${user}. "$newdir" find "$newdir" -type f -exec chmod 600 {} \; fi [ -a $MIGRATELOG ] && cat $MIGRATELOG;rm -f $MIGRATELOG
[root@www ~]# sh migrate-maildir.sh mbox
[root@www ~]# rm -f migrate-maildir.sh
[root@www ~]# rm -f /usr/local/bin/perfect_maildir.pl
[root@www ~]# systemctl restart postfix dovecot
[root@www ~]# vi /etc/aliases
#webmaster: root
[root@www ~]# newaliases
TCP25番、TCP587番ポート開放。
ルーターの設定で、TCP25番、TCP587番ポートを開放します。(設定方法はご自身の環境に合わせて調べてください。)
ポート開放テスト。
開放確認サイトで「ホスト名(ubuntuserver.jp)」、「ポート番号(25、587)」の開放確認をします。
メールサーバーのOP25B対策。
メールサーバーのOP25B対策として送信メールをGmailを経由して送信するように設定。します。
Postfixの設定します。
root@www:~# vi /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
root@www:~# echo [smtp.gmail.com]:587 Gmailアドレス:Gmailパスワード > /etc/postfix/sasl_passwd
root@www:~# chmod 640 /etc/postfix/sasl_passwd
root@www:~# postmap /etc/postfix/sasl_passwd
root@www:~# systemctl reload postfix
Gmailのメール転送設定。
先にThunderbirdへGmail(送信用メール)の設定をします。
TCP110番またはTCP143番ポート開放。
ルーターの設定で、TCP110番またはTCP143番ポートを開放します。(設定方法はご自身の環境に合わせて調べてください。)
ポート開放テスト。
開放確認サイトで「ホスト名(ubuntuserver.jp)」、「ポート番号(110または143)」の開放確認をします。
メールユーザーの追加。
ユーザーを追加します。(ユーザー例:ubuntu)
root@www:~# useradd ubuntu
root@www:~# passwd ubuntu Changing password for user ubuntu. New UNIX password: Retype new UNIX password:
「/etc/ssh/sshd_config」を編集して、秘密鍵での認証接続を無効にする。
ファイルを編集します。
root@www:~# vi /etc/ssh/sshd_config
PasswordAuthentication yes
root@www:~# systemctl restart sshd
SSHサーバー公開鍵認証方式接続の設定をします。
メールソフトの登録。
Thunderbirdを起動し、メールをクリックします。
サーバーのホスト名 | ポート番号 | SSL | 認証方式 | ||
受信サーバー | POP3 | mail.ubuntuserver.jp | 110 | 接続の保護なし | 通常のパスワード認証 |
送信サーバー | SMTP | mail.ubuntuserver.jp | 587 | 接続の保護なし | 通常のパスワード認証 |
サーバー証明書の取得。
Certbotをインストールします。
root@www:~# apt -y install certbot
ドキュメントルート:/var/www/html/
メールアドレス:webmaster@ubuntuerver.jp
メールサーバー名:mail.ubuntuserver.jp
root@www:~# certbot certonly --webroot -w /var/www/html/ -m webmaster@ubuntuserver.jp -d mail.ubuntuserver.jp --agree-tos
root@www:~# vi /etc/cron.d/letsencrypt
00 16 * * 2 root /usrobin/certbot renew --post-hook "service httpd restart"
SSLの設定。
main.cfを設定します。
root@www:~# vi /etc/postfix/main.cf
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.ubuntuserver.jp/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.ubuntuserver.jp/privkey.pem
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
tls_high_cipherlist = kEECDH:+kEECDH+SHA:kEDH:+kEDH+SHA:+kEDH+CAMELLIA:kECDH:+kECDH+SHA:kRSA:+kRSA+SHA:+kRSA+CAMELLIA:!aNULL:!eNULL:!SSLv2:!RC4:!MD5:!DES:!EXP:!SEED:!IDEA:!3DES
smtp_tls_ciphers = high
smtpd_tls_ciphers = high
smtpd_tls_mandatory_ciphers = high
smtpd_tls_mandatory_protocols=!SSLv2,!SSLv3
smtp_tls_mandatory_protocols=!SSLv2,!SSLv3
smtpd_tls_protocols=!SSLv2,!SSLv3
smtp_tls_protocols=!SSLv2,!SSLv3
root@www:~# vi /etc/postfix/master.cf
#submission inet n - n - - smtpd
# -o smtpd_sasl_auth_enable=yes
smtps inet n - n - - smtpd
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
tlsmgr unix - - n 300 1 tlsmgr
root@www:~# systemctl restart postfix
TCP465番ポート開放。
ルーターの設定で、TCP465番ポートを開放します。(設定方法はご自身の環境に合わせて調べてください。)
ポート開放テスト。
開放確認サイトで「ホスト名(mail.ubuntuserver.jp)」、「ポート番号(465)」の開放確認をします。
TCP587番ポート閉鎖。
ルーターの設定で、TCP587番ポートを閉鎖します。(設定方法はご自身の環境に合わせて調べてください。)
ポート開放テスト。
開放確認サイトで「ホスト名(mail.ubuntuserver.jp)」、「ポート番号(587)」の開放されていないことを確認をします。
Dovecot設定。
ファイルを設定します。
root@www:~# vi /etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/letsencrypt/live/mail.ubuntuserver.jp/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.ubuntuserver.jp/privkey.pem
root@www:~# systemctl restart dovecot
TCP995番またはTCP993番ポート開放。
ルーターの設定で、TCP995番またはTCP993番ポートを開放します。(設定方法はご自身の環境に合わせて調べてください。)
ポート開放テスト。
開放確認サイトで「ホスト名(mail.ubuntuserver.jp)」、「ポート番号(995または993)」の開放確認をします。
メールソフトの設定変更。
Thunderbirdを起動し、メールアドレスを選択し、「このアカウントの設定を表示する」をクリックします。
アンチウィルスソフトの連携(Clamav)。
Clamav をインストールします。
root@www:~# apt -y install clamav-daemon clamsmtp
root@www:~# vi /etc/clamsmtpd.conf
Header: X-AV-Checked: ClamAV using ClamSMTP
User: clamav
root@www:~# chown -R clamav. /var/spool/clamsmtp root@www:~# chown -R clamav. /var/run/clamsmtp
root@www:~# vi /etc/postfix/main.cf
content_filter = scan:127.0.0.1:10026
root@www:~# vi /etc/postfix/master.cf
scan unix - - n - 16 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 - 16 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_style=host
-o smtpd_authorized_xforward_hosts=127.0.0.0/8
root@www:~# systemctl restart clamav-daemon clamsmtp postfix
コメント