メールシステムとして、postfix・Dovecot・Postfixadminのインストールをします。
システムユーザーアカウントを作成します。
# useradd -r -u 150 -g mail -d /var/vmail -s /sbin/nologin -c "Virtual Mail User" vmail # mkdir -p /var/vmail # chmod -R 770 /var/vmail # chown -R vmail:mail /var/vmail
PostfixAdmin用のユーザーとデータベースを作成します。
# mysql -u root -p
CREATE USER 'postfixadmin'@'localhost' IDENTIFIED BY 'StrongPassword';
CREATE DATABASE postfixadmin;
GRANT ALL PRIVILEGES ON postfixadmin.* TO 'postfixadmin'@'localhost';
FLUSH PRIVILEGES;
Postfixをインストールします
# apt install postfix postfix-mysql sasl2-bin
Postfixのインストール中に、メール構成のタイプを尋ねられます。
インターネットサイト」を選択し、「OK」をクリックして続行します。
次に、「システムメール名」フィールドにサーバーのホスト名を入力します。
Postfixのインストール後、いくつかの設定ファイルを作ります。
ディレクトリの作成
# mkdir -p /etc/postfix/sql/
# nano /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
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'
# nano /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
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'
# nano /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
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'
# nano /etc/postfix/sql/mysql_virtual_alias_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
dbname = postfixadmin
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
# nano /etc/postfix/sql/mysql_virtual_domains_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
dbname = postfixadmin
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
# nano /etc/postfix/sql/mysql_virtual_mailbox_limit_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
dbname = postfixadmin
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
# nano /etc/postfix/sql/mysql_virtual_mailbox_maps.cf
user = postfixadmin
password = StrongPassword
hosts = localhost
dbname = postfixadmin
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
Postfix設定ファイルを編集します。
cp /usr/share/postfix/main.cf.dist /etc/postfix/main.cf
次のコマンドを実行して、各設定を編集します。
postconf -e "myhostname = $(hostname -f)" postconf -e "virtual_mailbox_domains = proxy:mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf" postconf -e "virtual_alias_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf" postconf -e "virtual_mailbox_maps = proxy:mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, proxy:mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf" postconf -e "smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem" postconf -e "smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key" postconf -e "smtpd_use_tls = yes" postconf -e "smtpd_tls_auth_only = yes" postconf -e "smtpd_sasl_type = dovecot" postconf -e "smtpd_sasl_path = private/auth" postconf -e "smtpd_sasl_auth_enable = yes" postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination" postconf -e "mydestination = localhost" postconf -e "mynetworks = 127.0.0.0/8" postconf -e "inet_protocols = ipv4" postconf -e "inet_interfaces = all" postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"
/etc/postfix/main.cfを開いて設定を続けます。
#nano /etc/postfix/main.cf
# 78行目:コメント解除 mail_owner = postfix # 576行目:コメントにしてその下に追記 #smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) smtpd_banner = $myhostname ESMTP # 650行目:追記 sendmail_path = /usr/sbin/postfix # 655行目:追記 newaliases_path = /usr/bin/newaliases # 660行目:追記 mailq_path = /usr/bin/mailq # 666行目:追記 setgid_group = postdrop # 670行目:コメント化 #html_directory = # 674行目:コメント化 #manpage_directory = # 679行目:コメント化 #sample_directory = # 683行目:コメント化 #readme_directory =
契約しているプロバイダのメールサーバにリレーさせるのでrelayhostを追加します。
プロバイダのメールサーバにSASL認証を行う設定を追加します。
relayhost = [プロバイダのSMTPサーバ]:587 #add a postscript smtp_use_tls = yes smtp_sasl_auth_enable = yes smtp_sasl_password_maps = hash:/etc/postfix/isppasswd smtp_sasl_security_options = noanonymous smtp_sasl_mechanism_filter = plain, login smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
/etc/postfix/isppasswdの作成
# nano /etc/postfix/isppasswd
中身はこのようになる。
[プロバイダのSMTPサーバ]:587 [アカウント]:[パスワード]
root のみが読めるようにしておく。
# chmod 400 /etc/postfix/isppasswd
そして、postfixが扱うDBファイルを生成します。
# postmap /etc/postfix/isppasswd
/etc/postfix/master.cfの修正
内容を以下のように修正します。
# # Postfix master process configuration file. For details on the format # of the file, see the master(5) manual page (command: "man 5 master" or # on-line: http://www.postfix.org/master.5.html). # # Do not forget to execute "postfix reload" after editing this file. # # ========================================================================== # service type private unpriv chroot wakeup maxproc command + args # (yes) (yes) (no) (never) (100) # ========================================================================== smtp inet n - y - - smtpd #smtp inet n - y - 1 postscreen #smtpd pass - - y - - smtpd #dnsblog unix - - y - 0 dnsblog #tlsproxy unix - - y - 0 tlsproxy 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 # -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -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=$mua_client_restrictions # -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 サービスを再起動します。
# systemctl restart postfix