0
4

More than 3 years have passed since last update.

CentOSのメールサーバ構築からアカウント作成

Last updated at Posted at 2020-06-05

Postfixからメールを送信するが、Office365のOutlook.comへメールが届かず、
エラーになっていたので、
解除申請フォームに送信して
メールが遅れるようにするまでの手順の過程で必要な、
メールサーバ構築からアカウント作成してメールを受信できるようにするまでの方法を書いていきます。

参考にさせていただいたサイト一覧
CentOS7の環境にメールサーバを構築する
メールサーバー構築手順(Postfix / Dovecot)初心者でもできるメモ
メールサーバー構築(Postfix+Dovecot)
メールユーザーアカウント登録#2

バージョンCentOS Linux release 7.3.1611 (Core)

■Postfixインストール

[root@centos ~]# yum -y install postfix ← Postfixインストール

■Postfix設定
(1)Postfix設定

[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.centossrv.com ← 追加(自FQDN名を指定)

# 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 = centossrv.com ← 追加(自ドメイン名を指定)

# 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
#myorigin = $mydomain
myorigin = $mydomain ← 追加(ローカルからのメール送信時の送信元メールアドレス@以降にドメイン名を付加)

# 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 = 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
↓
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain ← 変更(自ドメイン宛メールを受信できるようにする)
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
#       mail.$mydomain, www.$mydomain, ftp.$mydomain

# 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 = Mailbox
#home_mailbox = Maildir/
home_mailbox = Maildir/ ← 追加(メール格納形式をMaildir形式にする)

# 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 $mail_name
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
smtpd_banner = $myhostname ESMTP unknown ← 追加(メールサーバーソフト名の隠蔽化)

以下を最終行へ追加(SMTP認証設定)
smtpd_sasl_auth_enable = yes

smtpd_recipient_restrictions =
    permit_mynetworks
    permit_sasl_authenticated
    reject_unauth_destination

以下を最終行へ追加(受信メールサイズ制限)
message_size_limit = 10485760 ← 追加(受信メールサイズを10MB=10*1024*1024に制限)

[root@centos ~]# vi /etc/postfix/master.cf ← master.cf編集
submission inet n       -       n       -       -       smtpd ← 行頭の#を削除してコメント解除(SUBMISSIONポート有効化)
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes ← 行頭の#を削除してコメント解除(SUBMISSIONポートでSMTP認証有効化)

(2)SMTP認証設定
SMTP認証にシステムのユーザー名、パスワードを使用する場合

[root@centos ~]# yum -y install cyrus-sasl ← cyrus-saslインストール
[root@centos ~]# systemctl start saslauthd ← saslauthd起動
[root@centos ~]# systemctl enable saslauthd ← saslauthd自動起動設定

# vim /etc/sasl2/smtpd.conf
saslauthdからauxpropに変更
pwcheck_method: auxprop
mech_list: plain login

(3)Maildir形式メールボックス作成
Postfixのメール格納形式は共有ディレクトリ形式(「/var/spool/mail/ユーザー名」というファイルに全てのメールが蓄積されていく形式)だが、アクセス性能改善及びセキュリティ強化の観点からMaildir形式へ移行する。

【新規ユーザー対処】
新規ユーザー追加時に自動でホームディレクトリにMaildir形式のメールボックスが作成されるようにする

[root@centos ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp} ← 新規ユーザー追加時に自動でMaildir形式メールボックス作成
[root@centos ~]# chmod -R 700 /etc/skel/Maildir/ ← メールボックスパーミッション設定

4)存在しないユーザー宛メールの破棄
メールサーバーは、存在しないユーザー宛メールを受信すると、宛先不明による不達メールを送信元へ返そうとするが、送信元を偽装した迷惑メールが存在しないユーザー宛にわざと送られてくる場合があり、この場合、送信元が偽装されているため、不達メール自体が不達となってしまい、メールがキューに滞留してしまう(mailqコマンドで確認可能)。最終的に不達メールの送信を一定期間繰り返して破棄するが、このような迷惑メールが大量に送られてくると、キューにメールが大量にたまり、たまった大量の不達メールの再送処理により、サーバー自体が高負荷状態に陥って機能しなくなる恐れがあるため、存在しないユーザー宛のメールは破棄するようにする。
なお、正当なユーザーが宛先を間違えてメールを送った場合に、不達メールが返ってこずに、宛先を間違えたことに気づかない懸念はあるが、現在のセキュリティ事情では、送達実績のない宛先へのメールは送達確認をするようにすることが一般的であることから、本懸念は無視するものとする。

[root@centos ~]# vi /etc/postfix/main.cf ← Postfix設定ファイル編集
以下を最終行へ追加(存在しないユーザー宛メールをunknown_userへ配送)
local_recipient_maps =
luser_relay = unknown_user@localhost

[root@centos ~]# echo unknown_user: /dev/null >> /etc/aliases ← unknown_user宛メールを破棄
[root@centos ~]# newaliases ← /etc/aliases変更反映

■Postfix起動
(1)Postfix起動

[root@centos ~]# systemctl restart postfix ← Postfix再起動※CentOS7の場合
[root@centos ~]# systemctl enable postfix ← Postfix自動起動設定※CentOS7の場合

■Dovecotインストール

[root@centos ~]# yum -y install dovecot ← Dovecotインストール

■Dovecot設定

[root@centos ~]# vi /etc/dovecot/conf.d/10-mail.conf ← 10-mail.conf編集
# Location for users' mailboxes. The default is empty, which means that Dovecot
# tries to find the mailboxes automatically. This won't work if the user
# doesn't yet have any mail, 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 =
mail_location = maildir:~/Maildir ← 追加(メール格納形式をMaildir形式にする)

# ':' separated list of directories under which chrooting is allowed for mail
# processes (ie. /var/mail will allow chrooting to /var/mail/foo/bar too).
# This setting doesn't affect login_chroot, mail_chroot or auth chroot
# settings. If this setting is empty, "/./" in home dirs are ignored.
# WARNING: Never add directories here which local users can modify, that
# may lead to root exploit. Usually this should be done only if you don't
# allow shell access for users. 
#valid_chroot_dirs =
valid_chroot_dirs = /home ← 追加※OpenSSH+Chrootを導入している場合のみ

[root@centos ~]# vi /etc/dovecot/conf.d/10-auth.conf ← 10-auth.conf編集
# Disable LOGIN command and all other plaintext authentications unless
# SSL/TLS is used (LOGINDISABLED capability). Note that if the remote IP
# matches the local IP (ie. you're connecting from the same computer), the
# connection is considered secure and plaintext authentication is allowed.
#disable_plaintext_auth = yes
disable_plaintext_auth = no ← 追加(プレインテキスト認証を許可)
※メールサーバー間通信内容暗号化導入推奨

[root@centos ~]# vi /etc/dovecot/conf.d/10-ssl.conf ← 10-ssl.conf編集
# SSL/TLS support: yes, no, required. 
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
ssl = no ← SSL接続無効
※メールサーバー間通信内容暗号化導入推奨

■Dovecot起動
(1)Dovecot起動

[root@centos ~]# systemctl start dovecot ← Dovecot起動※CentOS7の場合
[root@centos ~]# systemctl enable dovecot ← Dovecot自動起動設定※CentOS7の場合

(2)TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポート開放
【ルーター】
ルーター側の設定で、TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポートへのアクセスをサーバーに転送するようにする。

【ファイアウォール】※ファイアウォール導入している場合のみ
サーバー側のファイアウォール設定で、TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポートへのアクセスを許可するようにする。

■メールユーザー追加
例としてメールユーザーcentos(メールアドレス:centos@centossrv.com)を追加する。

[root@centos ~]# useradd centos ← ユーザー追加※SSHによるリモート接続もできるようにする場合
[root@centos ~]# useradd -s /sbin/nologin centos ← ユーザー追加※SSHによるリモート接続はできないようにする場合
[root@centos ~]# passwd centos ← パスワード設定
Changing password for user centos.
New UNIX password:  ← パスワード応答
Retype new UNIX password:  ← パスワード応答(確認)
passwd: all authentication tokens updated successfully.
以下はSMTP認証に個別のユーザー名、パスワードを使用する場合のみ
[root@centos ~]# echo "パスワード" | saslpasswd2 -p -u centos.centossrv.com -c centos ← SMTP認証用ユーザー/パスワード登録
[root@centos ~]# sasldblistusers2 ← SMTP認証用ユーザー/パスワード確認
centos@centos.centossrv.com: userPassword
[root@centos ~]# chgrp postfix /etc/sasldb2 ← /etc/sasldb2所有グループをpostfixに変更※最初の1回のみ
※SMTP認証用ユーザー/パスワードを削除する場合
[root@centos ~]# saslpasswd2 -d centos -u centos.centossrv.com ← SMTP認証用ユーザー/パスワード削除

■メールサーバー動作確認(CUIでやる場合)

送信側
```
$ telnet localhost 25  #SMTPでアクセス
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.bobo.com ESMTP unknown

HELO bobo.com              #アクセス成功
502 5.5.2 Error: command not recognized

MAIL FROM: bobo@bobo.com         #送り元メールアドレス
250 2.1.0 Ok

RCPT TO: bobo@bobo.com          #RCPT TO検証
250 2.1.5 Ok

DATA                   #ここからメールの内容を記載
354 End data with .
From: bobo@bobo.com            #宛先メールアドレス
Subject: test               #件名
Hello wrold.               #ここからメールの内容↓
test
test
test
test
.                     #終了(ピリオド)

250 2.0.0 Ok: queued as 576F6245A
QUIT                   #メール終了(送信)
221 2.0.0 Bye
Connection closed by foreign host.
$
```

ログで正常にメールを送信できたことを確認。

$ less /var/log/maillog
Jan 10 17:51:19 t-bobo011 postfix/smtpd[24686]: connect from localhost[::1]
Jan 10 17:51:55 t-bobo011 postfix/smtpd[24686]: 576F6245A: client=localhost[::1]
Jan 10 17:52:50 t-bobo011 postfix/cleanup[24691]: 576F6245A: message-id=<20140110085155.576F6245A@mail.bobo.com>
Jan 10 17:52:50 t-bobo011 postfix/qmgr[12693]: 576F6245A: from=<bobo@bobo.com>, size=369, nrcpt=1 (queue active)
Jan 10 17:52:50 t-bobo011 postfix/local[24694]: 576F6245A: to=<bobo@bobo.com>, relay=local, delay=69, delays=69/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Jan 10 17:52:50 t-bobo011 postfix/qmgr[12693]: 576F6245A: removed

受信側

$ telnet localhost 110    #POP3でアクセス
Trying ::1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
USER bobo       #ユーザー入力
+OK
PASS cocobat5555    #ユーザーパスワード入力
+OK Logged in.
LIST          #メッセージリスト確認
+OK 5 messages:
1 422
2 455
3 429
4 322
5 458
.
RETR 5         #5番目のメッセージを確認
+OK 458 octets
Return-Path: <bobo@bobo.com>
X-Original-To: bobo@bobo.com
Delivered-To: bobo@bobo.com
Received: from localhost (localhost [IPv6:::1])
        by mail.bobo.com (Postfix) with SMTP id 576F6245A
        for <bobo@bobo.com>; Fri, 10 Jan 2014 17:51:41 +0900 (JST)
From: bobo@bobo.com
Subject: test
Message-Id: <20140110085155.576F6245A@mail.bobo.com>
Date: Fri, 10 Jan 2014 17:51:41 +0900 (JST)
To: undisclosed-recipients:;

Hello wrold.
test
test
test
test

.
QUIT         #終了
+OK Logging out.
Connection closed by foreign host.
$

ログで正常にメールを受信できたことを確認。

$ less /var/log/maillog
Jan 10 17:58:29 t-bobo011 dovecot: pop3-login: Login: user=, method=PLAIN, rip=::1, lip=::1, mpid=24714, secured
Jan 10 17:59:05 t-bobo011 dovecot: pop3(bobo): Disconnected: Logged out top=0/0, retr=1/474, del=0/5, size=2086

■メールクライアント設定(受信メールサーバーをPOPにする場合)
自宅や会社等の複数拠点でメールを使用する場合で、メールボックスを同期しなくてもいい場合(例:会社で受信したメールは自宅で受信できなくてもいい場合)、受信メールサーバーにPOPを使用する。
ここでは、メールソフトとしてThunderbirdを使用する。

Thunderbirdを起動し、メニューの「ファイル」⇒「新規作成」⇒「既存のメールアカウント」でメールアカウント設定を行う
「あなたのお名前」⇒表示名(例:webmaster@centossrv.com)
「メールアドレス」⇒メールアドレス(例:webmaster@centossrv.com)
「パスワード」⇒パスワード
「続ける」
「受信サーバー」⇒「POP3」を選択
「受信サーバー」-「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「受信サーバー」-「ポート番号」⇒「110」を選択
「受信サーバー」-「SSL」⇒「接続の保護なし」を選択
「受信サーバー」-「認証方式」⇒「通常のパスワード認証」を選択
「送信サーバー」-「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「送信サーバー」-「ポート番号」⇒「587」を選択
「送信サーバー」-「SSL」⇒「接続の保護なし」を選択
「送信サーバー」-「認証方式」⇒「通常のパスワード認証」を選択
「再テスト」
「次のアカウント設定が、指定されたサーバーを調べることにより見つかりました。」と表示されることを確認して「完了」
「接続する上での危険性を理解しました」をチェックして「完了」

■メールクライアント設定(受信メールサーバーをIMAPにする場合)
自宅や会社等の複数拠点でメールを使用する場合で、メールボックスを同期したい場合(例:会社で受信したメールを自宅でも受信したい場合)、受信メールサーバーにIMAPを使用する。
ここでは、メールソフトとしてThunderbirdを使用する。

Thunderbirdを起動し、メニューの「ファイル」⇒「新規作成」⇒「既存のメールアカウント」でメールアカウント設定を行う

「あなたのお名前」⇒表示名(例:webmaster@centossrv.com)
「メールアドレス」⇒メールアドレス(例:webmaster@centossrv.com)
「パスワード」⇒パスワード
「続ける」
「受信サーバー」⇒「IMAP」を選択
「受信サーバー」-「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「受信サーバー」-「ポート番号」⇒「143」を選択
「受信サーバー」-「SSL」⇒「接続の保護なし」を選択
「受信サーバー」-「認証方式」⇒「通常のパスワード認証」を選択
「送信サーバー」-「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「送信サーバー」-「ポート番号」⇒「587」を選択
「送信サーバー」-「SSL」⇒「接続の保護なし」を選択
「送信サーバー」-「認証方式」⇒「通常のパスワード認証」を選択
「再テスト」
「次のアカウント設定が、指定されたサーバーを調べることにより見つかりました。」と表示されることを確認して「完了」
「接続する上での危険性を理解しました」をチェックして「完了」

■メールサーバー確認
□内部で同一ユーザー同士でメールの送受信
□内部で他ユーザー間でメールの送受信
□内部で外部(プロバイダのメールアドレス等)との送受信
□内部で携帯との送受信※
□外部(会社等)で同一ユーザー同士でメールの送受信
□外部(会社等)で他ユーザー間でメールの送受信
□外部(会社等)で外部(プロバイダのメールアドレス等)との送受信
□外部(会社等)で携帯との送受信※

※携帯はドメイン指定受信等でメールサーバーからのメールが拒否されないようにしておくこと

■メール不正中継拒否テスト
Open Relay Testで「Mail Server」に自宅サーバーのホスト名(例:centossrv.com)を入力して「Check for Open Relay」ボタンを押下する。
18種類のテストが行われ、すべてのTestについてRelay NOT Acceptedと表示されればOK。

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4