アンドロイド

postfixとdovecotをインストールして構成する

目次:

Anonim

これは、メールサーバーシリーズのセットアップと構成の2番目の投稿です。 この投稿では、メールシステムの2つの主要コンポーネントであるPostfixとDovecotをインストールして構成する方法を示します。

Postfixは、電子メールの送受信に使用されるサービスであるオープンソースのメール転送エージェント(MTA)です。 DovecotはIMAP / POP3サーバーであり、セットアップではローカル配信とユーザー認証も処理します。

このチュートリアルはUbuntu 16.04向けに作成されていますが、Ubuntuの新しいバージョンでも少し変更を加えた同じ手順が機能するはずです。

前提条件

このチュートリアルを続ける前に、sudo特権を持つユーザーとしてログインしていることを確認してください。

PostfixとDovecotをインストールする

UbuntuのデフォルトリポジトリのDovecotパッケージは古くなっています。 imap_sieve モジュールを利用するために、DovecotコミュニティリポジトリからDovecotをインストールします。

次のwgetコマンドを使用して、リポジトリGPGキーをaptソースキーリングに追加します。

wget -O- https://repo.dovecot.org/DOVECOT-REPO-GPG | sudo apt-key add -

次のコマンドを使用して、Dovecotコミュニティリポジトリを有効にします。

echo "deb https://repo.dovecot.org/ce-2.3-latest/ubuntu/$(lsb_release -cs) $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/dovecot.list

sudo apt update sudo debconf-set-selections <<< "postfix postfix/mailname string $(hostname -f)" sudo debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'" sudo apt install postfix postfix-mysql dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-mysql

後置構成

仮想メールボックスとドメインを使用するようにPostfixをセットアップします。

このシリーズの最初のパートで作成したMySQLデータベースへのアクセス方法をpostfixに指示する sql 構成ファイルを作成することから始めます。

sudo mkdir -p /etc/postfix/sql

テキストエディタを開き、次のファイルを作成します。

/etc/postfix/sql/mysql_virtual_domains_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT domain FROM domain WHERE domain='%s' AND active = '1' /etc/postfix/sql/mysql_virtual_alias_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias WHERE address='%s' AND active = '1' /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias, alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1' /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias, alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1' /etc/postfix/sql/mysql_virtual_mailbox_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1' /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT maildir FROM mailbox, alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'

SQL構成ファイルが作成されたら、メインのpostfix構成ファイルを更新して、MySQLデータベースに保存されている仮想ドメイン、ユーザー、およびエイリアスに関する情報を含めます。

sudo postconf -e "virtual_mailbox_domains = mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf" sudo postconf -e "virtual_alias_maps = mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf" sudo postconf -e "virtual_mailbox_maps = mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf" postconfコマンドは、構成パラメーターの実際の値を表示したり、構成パラメーター値を変更したり、Postfixメールシステムに関するその他の構成情報を表示したりします。

ローカル配信エージェントは、受信メールをユーザーのメールボックスに配信します。 次のコマンドを実行して、DovecotのLMTPサービスをデフォルトのメール配信トランスポートとして設定します。

sudo postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"

以前に生成されたSSL証明書を暗号化して、TLパラメータを設定します

sudo postconf -e 'smtp_tls_security_level = may' sudo postconf -e 'smtpd_tls_cert_file = /etc/letsencrypt/live/mail.linuxize.com/fullchain.pem' sudo postconf -e 'smtpd_tls_key_file = /etc/letsencrypt/live/mail.linuxize.com/privkey.pem'

認証済みのSMTP設定を構成し、Dovecotに認証を渡します。

sudo postconf -e 'smtpd_sasl_type = dovecot' sudo postconf -e 'smtpd_sasl_path = private/auth' sudo postconf -e 'smtpd_sasl_local_domain =' sudo postconf -e 'smtpd_sasl_security_options = noanonymous' sudo postconf -e 'smtpd_sasl_auth_enable = yes' sudo postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination'

また、Postfixマスター構成ファイル master.cf を編集し、 master.cf ポート( 587 )とsmtpsポート( 465 )を有効にする必要があります。

テキストエディターでファイルを開き、次の行のコメントを解除/編集します。

/etc/postfix/master.cf

submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no -o smtpd_client_restrictions=permit_sasl_authenticated, reject # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated, reject -o milter_macro_daemon_name=ORIGINATING smtps inet n - y - - smtpd -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no -o smtpd_client_restrictions=permit_sasl_authenticated, reject # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated, reject -o milter_macro_daemon_name=ORIGINATING

変更を有効にするには、postfixサービスを再起動します。

sudo systemctl restart postfix

この時点で、Postfixサービスを正常に構成できました。

Dovecotを構成する

このセクションでは、セットアップに合わせてDovecotを構成します。 黄色で強調表示されている行を必ず編集してください。

dovecot-sql.conf.ext 、Dovecotにデータベースへのアクセス方法と電子メールアカウントに関する情報の検索方法を指示する dovecot-sql.conf.ext ファイルを構成します。

/etc/dovecot/dovecot-sql.conf.ext

driver = mysql connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=P4ssvv0rD default_pass_scheme = MD5-CRYPT iterate_query = SELECT username AS user FROM mailbox user_query = SELECT CONCAT('/var/mail/vmail/', maildir) AS home, \ CONCAT('maildir:/var/mail/vmail/', maildir) AS mail, \ 5000 AS uid, 5000 AS gid, CONCAT('*:bytes=', quota) AS quota_rule \ FROM mailbox WHERE username = '%u' AND active = 1 password_query = SELECT username AS user, password FROM mailbox \ WHERE username = '%u' AND active='1'

正しいMySQL資格情報(dbname、ユーザー、およびパスワード)を使用することを忘れないでください。

次に、 conf.d/10-mail.conf ファイルを編集し、次の変数を編集します。

/etc/dovecot/conf.d/10-mail.conf

… mail_location = maildir:/var/mail/vmail/%d/%n… mail_uid = vmail mail_gid = vmail… first_valid_uid = 5000 last_valid_uid = 5000… mail_privileged_group = vmail… mail_plugins = quota…

認証を機能させるには、 conf.d/10-auth.conf 、次の行を編集して auth-sql.conf.ext ファイルを auth-sql.conf.ext ます。

/etc/dovecot/conf.d/10-auth.conf

… disable_plaintext_auth = yes… auth_mechanisms = plain login… #!include auth-system.conf.ext !include auth-sql.conf.ext…

conf.d/10-master.conf ファイルを開き、次のように変更します。

/etc/dovecot/conf.d/10-master.conf

… service lmtp { unix_listener /var/spool/postfix/private/dovecot-lmtp { mode = 0600 user = postfix group = postfix }… }… service auth {… unix_listener auth-userdb { mode = 0600 user = vmail group = vmail }… unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix group = postfix }… }… service auth-worker { user = vmail }… service dict { unix_listener dict { mode = 0660 user = vmail group = vmail } }…

conf.d/10-ssl.conf を開き、SSL / TLSを有効にします。

/etc/dovecot/conf.d/10-ssl.conf

… ssl = yes… ssl_cert = Make sure you use the correct path to the SSL certificate files.


If you have followed this series from the beginning, you should already have the fullchain.pem , privkey.pem , dhparam.pem files created on your server. For more information about how to create a free Let's encrypt SSL certificate and Diffie–Hellman key check this tutorial.


Thanks to Nevyn for noticing the problem and providing a solution.
… ssl = yes… ssl_cert = Make sure you use the correct path to the SSL certificate files.


If you have followed this series from the beginning, you should already have the fullchain.pem , privkey.pem , dhparam.pem files created on your server. For more information about how to create a free Let's encrypt SSL certificate and Diffie–Hellman key check this tutorial.


Thanks to Nevyn for noticing the problem and providing a solution.

conf.d/20-imap.conf ファイルを開き、 imap_quota プラグインをアクティブにします。

/etc/dovecot/conf.d/20-imap.conf

… protocol imap {… mail_plugins = $mail_plugins imap_quota… }…

conf.d/20-lmtp.conf ファイルを開き、次のように編集します。

/etc/dovecot/conf.d/20-lmtp.conf

… protocol lmtp { postmaster_address = [email protected] mail_plugins = $mail_plugins }…

conf.d/20-lmtp.conf ファイルでデフォルトのメールボックスを定義します。

/etc/dovecot/conf.d/15-mailboxes.conf

… mailbox Drafts { special_use = \Drafts } mailbox Spam { special_use = \Junk auto = subscribe } mailbox Junk { special_use = \Junk }…

クォータサイズには2種類あり、1つはドメイン全体に設定され、もう1つはユーザーメールボックスごとに設定されます。 このシリーズの前のパートでは、PostfixAdminでクォータサポートを既に有効にしました。つまり、クォータ情報はPostfixAdminデータベースに保存されます。

次に、データベースに接続し、クォータ制限を処理し、ユーザーのクォータが指定された制限を超えたときにユーザーにメールを送信するスクリプトを実行するようにDovecotを構成する必要があります。 これを行うには、 conf.d/90-quota.conf ファイルを開き、次のように変更します。

/etc/dovecot/conf.d/90-quota.conf

plugin { quota = dict:User quota::proxy::sqlquota quota_rule = *:storage=5GB quota_rule2 = Trash:storage=+100M quota_grace = 10%% quota_exceeded_message = Quota exceeded, please contact your system administrator. quota_warning = storage=100%% quota-warning 100 %u quota_warning2 = storage=95%% quota-warning 95 %u quota_warning3 = storage=90%% quota-warning 90 %u quota_warning4 = storage=85%% quota-warning 85 %u } service quota-warning { executable = script /usr/local/bin/quota-warning.sh user = vmail unix_listener quota-warning { group = vmail mode = 0660 user = vmail } } dict { sqlquota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext }

また、クォータSQLディクショナリにアクセスする方法をdovecotに指示する必要があります。 dovecot-dict-sql.conf.ext ファイルを開き、次の行を編集します。

/etc/dovecot/dovecot-dict-sql.conf.ext

… connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=P4ssvv0rD… map { pattern = priv/quota/storage table = quota2 username_field = username value_field = bytes } map { pattern = priv/quota/messages table = quota2 username_field = username value_field = messages }… # map { # pattern = shared/expire/$user/$mailbox # table = expires # value_field = expire_stamp # # fields { # username = $user # mailbox = $mailbox # } # }… 正しいMySQL資格情報(dbname、ユーザー、およびパスワード)を使用していることを確認してください。

クォータが指定された制限を超えた場合にユーザーに電子メールを送信する次のシェルスクリプトを作成します。

/usr/local/bin/quota-warning.sh

#!/bin/sh PERCENT=$1 USER=$2 cat << EOF | /usr/lib/dovecot/dovecot-lda -d $USER -o "plugin/quota=dict:User quota::noenforcing:proxy::sqlquota" From: [email protected] Subject: Quota warning Your mailbox is now $PERCENT% full. EOF

次の chmod コマンドを実行して、スクリプトを実行可能にします。

sudo chmod +x /usr/local/bin/quota-warning.sh

最後に、dovecotサービスを再起動して、変更を有効にします。

sudo systemctl restart dovecot

結論

これで、完全に機能するメールシステムができました。 このシリーズの次のパートでは、Rspamdをインストールして統合する方法を示します。

メールサーバーポストフィックスdovecot

この投稿は、メールサーバーのセットアップと構成シリーズの一部です。

このシリーズの他の投稿:

•PostfixAdminでメールサーバーをセットアップ•PostfixおよびDovecotをインストールおよび構成•Rspamdをインストールおよび統合•Roundcube Webmailをインストールおよび構成