メールサーバの構築
メールサーバを構築する場合は、メール送信サーバとメール受信サーバの双方が必要となります。
ここでは送信サーバに"Postfix"、受信サーバには"Dovecot"をそれぞれ利用します。
Postfixのインストール
下記コマンドでPostfixをインストールします。
[root@centos ~] yum -y install postfix
Postfixの基本的な設定
Postfixの設定ファイルは /etc/postfix/main.cf です。
以下、基本的なディレクティブの設定を行います。
[root@centos ~]# vi /etc/postfix/main.cf ※Postfixの設定ファイルをテキストエディタで開く - ホスト名 - # INTERNET HOST AND DOMAIN NAMES # # The myhostname parameter specifies the internet hostname of this # mail system. The default is to use the fully-qualified domain name # from gethostname(). $myhostname is used as a default value for many # other configuration parameters. # #myhostname = host.domain.tld #myhostname = virtual.domain.tld myhostname = mail.linuxserver.jp - ドメインの指定 - # The mydomain parameter specifies the local internet domain name. # The default is to use $myhostname minus the first component. # $mydomain is used as a default value for many other configuration # parameters. # #mydomain = domain.tld mydomain = linuxserver.jp - ローカルからのメール送信時に@以降に付加するドメインを指定 - # SENDING MAIL # # The myorigin parameter specifies the domain that locally-posted # mail appears to come from. The default is to append $myhostname, # which is fine for small sites. If you run a domain with multiple # machines, you should (1) change this to $mydomain and (2) set up # a domain-wide alias database that aliases each user to # user@that.users.mailhost. # # For the sake of consistency between sender and recipient addresses, # myorigin also specifies the default domain name that is appended # to recipient addresses that have no @domain part. # #myorigin = $myhostname myorign = $mydomain - SMTP接続を許可するインターフェイスを指定 - # The inet_interfaces parameter specifies the network interface # addresses that this mail system receives mail on. By default, # the software claims all active interfaces on the machine. The # parameter also controls delivery of mail to user@[ip.address]. # # See also the proxy_interfaces parameter, for network addresses that # are forwarded to us via a proxy or network address translator. # # Note: you need to stop/start Postfix when this parameter changes. # #inet_interfaces = all #inet_interfaces = $myhostname #inet_interfaces = $myhostname, localhost inet_interfaces = all - メールをローカルで受信するドメイン名を指定 - # The mydestination parameter specifies the list of domains that this # machine considers itself the final destination for. # # These domains are routed to the delivery agent specified with the # local_transport parameter setting. By default, that is the UNIX # compatible delivery agent that lookups all recipients in /etc/passwd # and /etc/aliases or their equivalent. # # The default is $myhostname + localhost.$mydomain. On a mail domain # gateway, you should also include $mydomain. # # Do not specify the names of virtual domains - those domains are # specified elsewhere (see VIRTUAL_README). # # Do not specify the names of domains that this machine is backup MX # host for. Specify those names via the relay_domains settings for # the SMTP server, or use permit_mx_backup if you are lazy (see # STANDARD_CONFIGURATION_README). # # The local machine is always the final destination for mail addressed # to user@[the.net.work.address] of an interface that the mail system # receives mail on (see the inet_interfaces parameter). # # Specify a list of host or domain names, /file/name or type:table # patterns, separated by commas and/or whitespace. A /file/name # pattern is replaced by its contents; a type:table is matched when # a name matches a lookup key (the right-hand side is ignored).# Continue long lines by starting the next line with whitespace. # # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS". # mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain - メールの格納方式に"Maildir形式"を利用する - # DELIVERY TO MAILBOX # # The home_mailbox parameter specifies the optional pathname of a # mailbox file relative to a user's home directory. The default # mailbox file is /var/spool/mail/user or /var/mail/user. Specify # "Maildir/" for qmail-style delivery (the / is required). # home_mailbox = Maildir/ - SMTP名やバージョン番号を非公開に設定 - # SHOW SOFTWARE VERSION OR NOT # # The smtpd_banner parameter specifies the text that follows the 220 # code in the SMTP server's greeting banner. Some people like to see # the mail version advertised. By default, Postfix shows no version. # # You MUST specify $myhostname at the start of the text. That is an # RFC requirement. Postfix itself does not care. # smtpd_banner = $myhostname ESMTP ~ 以下ファイルの末尾に追記 ~ - 必要に応じてサイズ制限 - message_size_limit = 10485760 ※メール一通当たりの最大サイズ制限を指定 mailbox_size_limit = 52428800 ※メールボックスの最大サイズ制限 - SASL 認証 - broken_sasl_auth_clients = yes smtpd_sasl_auth_enable = yes smtpd_sasl_local_domain = $myhostname smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, permit_auth_destination, reject smtpd_sasl_security_options = noanonymous,noplaintext
saslauthdの設定
saslauthdの設定ファイルは/usr/lib/sasl2/smtpd.conf です。
認証にPAMによる認証(※システムのユーザ名・パスワードによる認証)を用いる場合は設定ファイルに下記のように記載します。
[root@centos ~]# vi /usr/lib/sasl2/smtpd.conf ※saslauthdの設定ファイルをテキストエディタで開く pwcheck_method: saslauthd
SMTP-Auth用のユーザ名・パスワードをシステムのユーザ名・パスワードと別にする場合は設定ファイルに下記のように記載します。
[root@centos ~]# vi /usr/lib/sasl2/smtpd.conf ※saslauthdの設定ファイルをテキストエディタで開く pwcheck_method: auxprop
続いてsaslauthdを起動します。
[root@centos ~]# /etc/rc.d/init.d/saslauthd start ※saslauthdを起動 [root@centos ~]# chkconfig saslauthd on ※OS起動時にsaslauthdが起動するように設定
Maildir形式メールボックス作成
Maildir形式で必要なディレクトリを各ユーザのホームディレクトリ直下に作成します。
既存のユーザ用のディレクトリは以下のようにして作成します。
[user1@centos ~]$ cd /home/user1 ※対象のユーザのホームディレクトリに移動 [user1@centos ~]$ mkdir Maildir [user1@centos ~]$ mkdir Maildir/new [user1@centos ~]$ mkdir Maildir/cur [user1@centos ~]$ mkdir Maildir/tmp [user1@centos ~]$ chmod -R 700 Maildir
また、今後新規ユーザを追加した際に、自動でMairdir形式のディレクトリがホームディレクトリ直下に作成されるように設定しておきます。
[root@centos ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp} [root@centos ~]# chmod -R 700 /etc/skel/Maildir/
Postfixの起動とメールサーバの切り替え
デフォルトで起動しているSMTPサーバーであるsendmailを停止します 。
[root@centos ~]# /etc/rc.d/init.d/sendmail stop ※sendmailを停止 [root@centos ~]# chkconfig sendmail off ※OS起動時にsendmailが起動しないように設定
続いてシステムで使用するメールサーバー機能をsendmailからPostfixに切替えます 。
[root@centos ~]# alternatives --config mta Selection Command ----------------------------------------------- *+ 1 /usr/sbin/sendmail.sendmail 2 /usr/sbin/sendmail.postfix Enter to keep the current selection[+], or type selection number: 2 ※2を入力してEnter
Postfixを起動します。
[root@centos ~]# /etc/rc.d/init.d/postfix start ※Postfixを起動 [root@centos ~]# chkconfig postfix on ※OS起動時にApacheが起動するように設定
Dovecotのインストール
下記コマンドでDovecotをインストールします。
[root@centos ~] yum -y install dovecot
Dovecotの基本的な設定
Dovecotの設定ファイルは /etc/dovecot.conf です。
以下、基本的なディレクティブの設定を行います。
[root@centos ~]# vi /etc/dovecot.conf ※Dovecotの設定ファイルをテキストエディタで開く - プロトコル設定 - # Protocols we want to be serving: imap imaps pop3 pop3s # If you only want to use dovecot-auth, you can set this to "none". protocols = imap imaps pop3 pop3s - プロトコル設定 - # Location for users' mailboxes. This is the same as the old default_mail_env # setting. The default is empty, which means that Dovecot tries to find the # mailboxes automatically. This won't work if the user doesn't have any mail # yet, so you should explicitly tell Dovecot the full location. # # If you're using mbox, giving a path to the INBOX file (eg. /var/mail/%u) # isn't enough. You'll also need to tell Dovecot where the other mailboxes are # kept. This is called the "root mail directory", and it must be the first # path given in the mail_location setting. # # There are a few special variables you can use, eg.: # # %u - username # %n - user part in user@domain, same as %u if there's no domain # %d - domain part in user@domain, empty if there's no domain # %h - home directory # # See doc/wiki/Variables.txt for full list. Some examples: # # mail_location = maildir:~/Maildir # mail_location = mbox:~/mail:INBOX=/var/mail/%u # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n # ## mail_location = maildir:~/Maildir
Dovecotの起動
Dovecotを起動します。
[root@centos ~]# /etc/rc.d/init.d/dovecot start ※Dovecotを起動 [root@centos ~]# chkconfig dovecot on ※OS起動時にDovecotが起動するように設定
メールユーザの追加
システムのユーザ名・パスワードをそのまま利用する場合
[root@centos ~]# useradd user1 [root@centos ~]# passwd user1
なお、SSHによるリモート接続を行えないようにする場合は上記ではなく、以下のようなオプションを付加したコマンドで設定します。
[root@centos ~]# useradd -s /sbin/nologin user1 [root@centos ~]# passwd user1
SMTP-Auth用ユーザ名・パスワードとシステムのユーザ名・パスワードを別々にする場合
[root@centos ~]# echo "パスワード" | saslpasswd2 -p -u linuxserver.jp -c user1 ※SMTP-Auth用アカウントを新規追加 [root@centos ~]# sasldblistusers2 ※現在の設定を確認 user1@linuxserver.jp: userPassword [root@centos ~]# chgrp postfix /etc/sasldb2 ※/etc/sasldb2の所有グループを変更(※1回実行すればOK)
また、上記コマンドで設定したアカウントを削除する場合は下記コマンドを実行します。
[root@centos ~]# saslpasswd2 -d user1 -u linuxserver.jp
ユーザの設定が完了したらポートが開放されていることを確認(※POP3(110番)、POP3S(995番)、IMAP(143番)、IMAPS(993番))し、Thunderbirdなどのメールクライアントソフトで送受信ができるか確認を行いましょう。
また、スパムの踏み台として悪用されないように不正中継拒否テストを行いましょう。
不正中継拒否テストは、下記サイトで行えます。
http://www.rbl.jp/svcheck.php
上記の設定でうまく送受信ができない場合
メールクライアントソフトの設定確認
メールクライアントソフトで送信サーバの設定を行う際は、ユーザ名を"user1@linuxserver.jp"のようにユーザ名@ホスト名の形式にしてください。
"user1"のように単にユーザ名だけ記載する形にすると、正しいパスワードを入力しても送信ができない場合があります。
ログの確認
以下のコマンドでメールログからエラーの原因を探ります。
[root@centos ~]# less /var/log/maillog | grep 'fatal' Mar 23 18:04:11 linuxserver.jp postfix/smtpd[9448]: fatal: no SASL authentication mechanisms
上記のように"fatal: no SASL authentication mechanisms"と表示される場合は必要なライブラリが不足しているものと思われます。
当方の場合は下記のコマンドで関係があると思われるライブラリを片っ端からインストールしたら正確に送受信ができるようになりました。
[root@centos ~]# yum -y install cyrus*

この記事へのコメント
まだコメントは投稿されていません。