・CentOS8でローカルネットワーク内で使うメールサーバを構築したい。
・具体的な手順を教えてほしい。
こういった疑問に答えます。
本記事の内容
- CentOS8.2にPostfixをインストールしてメール送信サーバを構築する手順
この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。
こういった私が、解説していきます。
私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。
CentOS8.2にPostfixをインストールしてメール送信サーバを構築する手順
Linuxで使えるメール送信プログラムでは、「Postfix」を使うのが主流だそうです。
Postfixのインストール
dnf install postfix
[root@SV-EARTH ~]# dnf install postfix メタデータの期限切れの最終確認: 0:43:54 時間前の 2020年10月24日 16時56分00秒 に実施しました。 依存関係が解決しました。 ======================================================================================================================== パッケージ アーキテクチャー バージョン リポジトリー サイズ ======================================================================================================================== インストール中: postfix x86_64 2:3.3.1-12.el8 BaseOS 1.4 M トランザクションの概要 ======================================================================================================================== インストール 1 パッケージ ダウンロードサイズの合計: 1.4 M インストール済みのサイズ: 4.1 M これでよろしいですか? [y/N]: y
パッケージのダウンロード: postfix-3.3.1-12.el8.x86_64.rpm 3.2 MB/s | 1.4 MB 00:00 ------------------------------------------------------------------------------------------------------------------------ 合計 1.6 MB/s | 1.4 MB 00:00 トランザクションの確認を実行中 トランザクションの確認に成功しました。 トランザクションのテストを実行中 トランザクションのテストに成功しました。 トランザクションを実行中 準備 : 1/1 scriptlet の実行中: postfix-2:3.3.1-12.el8.x86_64 1/1 インストール中 : postfix-2:3.3.1-12.el8.x86_64 1/1 scriptlet の実行中: postfix-2:3.3.1-12.el8.x86_64 1/1 検証 : postfix-2:3.3.1-12.el8.x86_64 1/1 Installed products updated. インストール済み: postfix-2:3.3.1-12.el8.x86_64 完了しました!
Postfixの自動起動の設定
systemctl enable postfix
[root@SV-EARTH ~]# systemctl enable postfix Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service.
設定ファイル「main.cf」の修正
変更前の「main.cf」ファイルのバックアップ
cp -p /etc/postfix/main.cf /etc/postfix/main.cf.original_bk
[root@SV-EARTH ~]# cp -p /etc/postfix/main.cf /etc/postfix/main.cf.original_bk
「main.cf」ファイルの修正
vi /etc/postfix/main.cf
[root@SV-EARTH postfix]# vi /etc/postfix/main.cf
mydomain (ドメイン)
mydomain = ad.planet.jp
# 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 # 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 = ad.planet.jp
myorigin (ドメイン)
myorigin = $mydomain
# 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
inet_interfaces (ローカルホスト以外からも受け付ける)
inet_interfaces = all
# RECEIVING MAIL # 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
inet_protocols (プロトコルはIPv4のみ)
inet_protocols = ipv4
# Enable IPv4, and IPv6 if supported #inet_protocols = all inet_protocols = ipv4
mydestination (宛先ドメイン)
mydestination = $myhostname, localhost.$mydomain, localhost, ad.planet.jp
# 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, ad.planet.jp #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, # mail.$mydomain, www.$mydomain, ftp.$mydomain
mynetworks (ネットワークは最初はローカルホストのみ)
mynetworks = localhost
# You can also specify the absolute pathname of a pattern file instead # of listing the patterns here. Specify type:table for table-based lookups # (the value on the table right-hand side is not used). # #mynetworks = 168.100.189.0/28, 127.0.0.0/8 #mynetworks = $config_directory/mynetworks #mynetworks = hash:/etc/postfix/network_table mynetworks = localhost
home_mailbox (メールボックスの形式)
home_mailbox = 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 = Mailbox home_mailbox = Maildir/
設定ファイル変更後のPostfix再起動
systemctl restart postfix
systemctl status postfix
[root@SV-EARTH postfix]# systemctl restart postfix [root@SV-EARTH postfix]# systemctl status postfix ● postfix.service - Postfix Mail Transport Agent Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled) Active: active (running) since Sat 2020-10-24 17:50:52 JST; 6s ago Process: 82559 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS) Process: 82577 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS) Process: 82575 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS) Process: 82573 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS) Main PID: 82645 (master) Tasks: 3 (limit: 47652) Memory: 5.0M CGroup: /system.slice/postfix.service tq82645 /usr/libexec/postfix/master -w tq82646 pickup -l -t unix -u mq82647 qmgr -l -t unix -u 10月 24 17:50:50 SV-EARTH systemd[1]: Stopped Postfix Mail Transport Agent. 10月 24 17:50:50 SV-EARTH systemd[1]: Starting Postfix Mail Transport Agent... 10月 24 17:50:52 SV-EARTH postfix/master[82645]: daemon started -- version 3.3.1, configuration /etc/postfix 10月 24 17:50:52 SV-EARTH systemd[1]: Started Postfix Mail Transport Agent.
Postfixインストール後のメール送信テスト
テスト準備
telnetインストール
dnf install telnet
[root@SV-EARTH postfix]# dnf install telnet メタデータの期限切れの最終確認: 0:58:23 時間前の 2020年10月24日 16時56分00秒 に実施しました。 依存関係が解決しました。 ======================================================================================================================== パッケージ アーキテクチャー バージョン リポジトリー サイズ ======================================================================================================================== インストール中: telnet x86_64 1:0.17-73.el8_1.1 AppStream 72 k トランザクションの概要 ======================================================================================================================== インストール 1 パッケージ ダウンロードサイズの合計: 72 k インストール済みのサイズ: 153 k これでよろしいですか? [y/N]: y
パッケージのダウンロード: telnet-0.17-73.el8_1.1.x86_64.rpm 700 kB/s | 72 kB 00:00 ------------------------------------------------------------------------------------------------------------------------ 合計 179 kB/s | 72 kB 00:00 トランザクションの確認を実行中 トランザクションの確認に成功しました。 トランザクションのテストを実行中 トランザクションのテストに成功しました。 トランザクションを実行中 準備 : 1/1 インストール中 : telnet-1:0.17-73.el8_1.1.x86_64 1/1 scriptlet の実行中: telnet-1:0.17-73.el8_1.1.x86_64 1/1 検証 : telnet-1:0.17-73.el8_1.1.x86_64 1/1 Installed products updated. インストール済み: telnet-1:0.17-73.el8_1.1.x86_64 完了しました!
テスト用ユーザーの作成
useradd mail01 -s /sbin/nologin
passwd mail01
[root@SV-EARTH postfix]# useradd mail01 -s /sbin/nologin [root@SV-EARTH postfix]# passwd mail01 ユーザー mail01 のパスワードを変更。 新しいパスワード: よくないパスワード: このパスワードは 8 文字未満の文字列です。 新しいパスワードを再入力してください: passwd: すべての認証トークンが正しく更新できました。
ディレクトリの書き込み権限付与
chmod 777 /var/spool/mail
[root@SV-EARTH postfix]# chmod 777 /var/spool/mail
telnetコマンドでSMTP接続のテスト
SMTPの25番ポートに接続します。
telnet localhost 25
[root@SV-EARTH postfix]# telnet localhost 25 Trying ::1... telnet: connect to address ::1: Connection refused Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. 220 SV-EARTH.ad.planet.jp ESMTP Postfix
helo
コマンドで応答します。
helo localhost
helo localhost 250 SV-EARTH.ad.planet.jp mail from: mail01@ad.planet.jp 250 2.1.0 Ok
あて先のメールアドレスをrcpt to:
で入力します。
rcpt to: mail01@ad.planet.jp
rcpt to: mail01@ad.planet.jp 250 2.1.5 Ok
data
と入力すると、本文の入力モードになります。
data
data 354 End data with <CR><LF>.<CR><LF>
本文を書いて、末尾に.
(ピリオド)を入力します。
helo postfix
.
helo postfix . 250 2.0.0 Ok: queued as D0460C2600
終了するときはquit
です。
quit
quit 221 2.0.0 Bye Connection closed by foreign host.
送信後のログの確認
/var/log/maillog
Oct 24 18:00:01 sv-earth postfix/smtpd[83210]: D0460C2600: client=localhost[127.0.0.1] Oct 24 18:00:34 sv-earth postfix/cleanup[83219]: D0460C2600: message-id=<20201024090001.D0460C2600@SV-EARTH.ad.planet.jp> Oct 24 18:00:34 sv-earth postfix/qmgr[83203]: D0460C2600: from=<mail01@ad.planet.jp>, size=329, nrcpt=1 (queue active) Oct 24 18:00:34 sv-earth postfix/local[83224]: D0460C2600: to=<mail01@ad.planet.jp>, relay=local, delay=53, delays=53/0.02/0/0.01, dsn=2.0.0, status=sent (delivered to maildir) Oct 24 18:00:34 sv-earth postfix/qmgr[83203]: D0460C2600: removed
まとめ
今回は、CentOS8.2にPostfixをインストールしてメール送信サーバを構築する手順を紹介しました。
次の記事では、「Dovecot」をインストールして送受信ができるメールサーバを構築する手順を書きます。