そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

AlmaLinux8とPostfixを使ってメール送信できるサーバーを構築

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・AlmaLinux8 にPostfixをインストールしてメール送信する方法を知りたい。
・具体的な手順を分かりやすく教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. AlmaLinux8とPostfixを使ってメール送信できるサーバーを構築

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

AlmaLinux8とPostfixを使ってメール送信できるサーバーを構築

手順① Postfixのインストール

dnf -y install postfix
[root@SV-VEGA ~]# dnf -y install postfix
メタデータの期限切れの最終確認: 0:02:57 前の 20230905073708秒 に実施しました。
依存関係が解決しました。
====================================================================================================
 パッケージ            アーキテクチャー     バージョン                   リポジトリー         サイズ
====================================================================================================
インストール:
 postfix               x86_64               2:3.5.8-4.el8                baseos               1.5 M

トランザクションの概要
====================================================================================================
インストール  1 パッケージ

ダウンロードサイズの合計: 1.5 M
インストール後のサイズ: 4.3 M
パッケージのダウンロード:
postfix-3.5.8-4.el8.x86_64.rpm                                      5.9 MB/s | 1.5 MB     00:00
----------------------------------------------------------------------------------------------------
合計                                                                1.8 MB/s | 1.5 MB     00:00
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                            1/1
  scriptletの実行中: postfix-2:3.5.8-4.el8.x86_64                                               1/1
  インストール中   : postfix-2:3.5.8-4.el8.x86_64                                               1/1
  scriptletの実行中: postfix-2:3.5.8-4.el8.x86_64                                               1/1
  検証             : postfix-2:3.5.8-4.el8.x86_64                                               1/1

インストール済み:
  postfix-2:3.5.8-4.el8.x86_64

完了しました!
dnf list installed | grep postfix
[root@SV-VEGA ~]# dnf list installed | grep postfix
postfix.x86_64                       2:3.5.8-4.el8                          @baseos

手順② デフォルト設定の確認

postconf -n
[root@SV-VEGA ~]# postconf -n
alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
command_directory = /usr/sbin
compatibility_level = 2
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
debug_peer_level = 2
debugger_command = PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin ddd $daemon_directory/$process_name $process_id & sleep 5
html_directory = no
inet_interfaces = localhost
inet_protocols = all
mail_owner = postfix
mailq_path = /usr/bin/mailq.postfix
manpage_directory = /usr/share/man
meta_directory = /etc/postfix
mydestination = $myhostname, localhost.$mydomain, localhost
newaliases_path = /usr/bin/newaliases.postfix
queue_directory = /var/spool/postfix
readme_directory = /usr/share/doc/postfix/README_FILES
sample_directory = /usr/share/doc/postfix/samples
sendmail_path = /usr/sbin/sendmail.postfix
setgid_group = postdrop
shlib_directory = /usr/lib64/postfix
smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_security_level = may
smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_security_level = may
unknown_local_recipient_reject_code = 550
cd /etc/postfix
[root@SV-VEGA ~]# cd /etc/postfix
[root@SV-VEGA postfix]# ls -l
合計 220
-rw-r--r-- 1 root root 21111  98  2019 access
-rw-r--r-- 1 root root 13194  64  2018 canonical
-rw-r--r-- 1 root root    60  413  2022 dynamicmaps.cf
drwxr-xr-x 2 root root     6  413  2022 dynamicmaps.cf.d
-rw-r--r-- 1 root root 10221  917  2016 generic
-rw-r--r-- 1 root root 23802 109  2016 header_checks
-rw-r--r-- 1 root root 29369  413  2022 main.cf
-rw-r--r-- 1 root root 29130  413  2022 main.cf.proto
-rw-r--r-- 1 root root  6372  413  2022 master.cf
-rw-r--r-- 1 root root  6372  413  2022 master.cf.proto
-rw-r--r-- 1 root root 20163  413  2022 postfix-files
drwxr-xr-x 2 root root     6  413  2022 postfix-files.d
-rw-r--r-- 1 root root  6929  214  2016 relocated
-rw-r--r-- 1 root root 13436  111  2020 transport
-rw-r--r-- 1 root root 13963  64  2018 virtual

手順③ main.cfの編集

[root@SV-VEGA postfix]# cp -p /etc/postfix/main.cf /etc/postfix/main.cf.original_bk
vi /etc/postfix/main.cf

myhostname

     87 # INTERNET HOST AND DOMAIN NAMES
     88 #
     89 # The myhostname parameter specifies the internet hostname of this
     90 # mail system. The default is to use the fully-qualified domain name
     91 # from gethostname(). $myhostname is used as a default value for many
     92 # other configuration parameters.
     93 #
     94 #myhostname = host.domain.tld
     95 #myhostname = virtual.domain.tld
     96 myhostname = sv-vega.planet.space.com  ←★★変更

mydomain

     98 # The mydomain parameter specifies the local internet domain name.
     99 # The default is to use $myhostname minus the first component.
    100 # $mydomain is used as a default value for many other configuration
    101 # parameters.
    102 #
    103 #mydomain = domain.tld
    104 mydomain = space.com  ←★★変更

inet_interfaces

    122 # RECEIVING MAIL
    123
    124 # The inet_interfaces parameter specifies the network interface
    125 # addresses that this mail system receives mail on.  By default,
    126 # the software claims all active interfaces on the machine. The
    127 # parameter also controls delivery of mail to user@[ip.address].
    128 #
    129 # See also the proxy_interfaces parameter, for network addresses that
    130 # are forwarded to us via a proxy or network address translator.
    131 #
    132 # Note: you need to stop/start Postfix when this parameter changes.
    133 #
    134 #inet_interfaces = all
    135 #inet_interfaces = $myhostname
    136 #inet_interfaces = $myhostname, localhost
    137 inet_interfaces = localhost  ←★★変更

inet_protocols

    139 # Enable IPv4, and IPv6 if supported
    140 inet_protocols = ipv4  ←★★変更

masquerade_domains

    742 masquerade_domains = space.com  ←★★変更

確認

[root@SV-VEGA postfix]# postconf -n
[root@SV-VEGA postfix]# postfix check

postfixを再起動して設定を反映

systemctl restart postfix
systemctl status postfix
[root@SV-VEGA postfix]# systemctl restart postfix
[root@SV-VEGA postfix]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-09-05 07:56:48 JST; 10s ago
  Process: 6459 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 6456 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 6451 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
  Process: 6449 ExecStartPre=/usr/sbin/restorecon -R /var/spool/postfix/pid/master.pid (code=exited>
 Main PID: 6527 (master)
    Tasks: 3 (limit: 24822)
   Memory: 5.0M
   CGroup: /system.slice/postfix.service
           tq6527 /usr/libexec/postfix/master -w
           tq6528 pickup -l -t unix -u
           mq6529 qmgr -l -t unix -u

 905 07:56:48 SV-VEGA systemd[1]: Starting Postfix Mail Transport Agent...
 905 07:56:48 SV-VEGA postfix/master[6527]: daemon started -- version 3.5.8, configuration /etc/>
 905 07:56:48 SV-VEGA systemd[1]: Started Postfix Mail Transport Agent.

手順④ 自動起動の設定

systemctl enable postfix
[root@SV-VEGA postfix]# systemctl enable postfix
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service.

手順⑤ 送信の確認

telnetインストール

dnf -y install telnet
[root@SV-VEGA postfix]# dnf -y install telnet
メタデータの期限切れの最終確認: 0:20:45 前の 20230905073708秒 に実施しました。
依存関係が解決しました。
====================================================================================================
 パッケージ          アーキテクチャー    バージョン                    リポジトリー           サイズ
====================================================================================================
インストール:
 telnet              x86_64              1:0.17-76.el8                 appstream               71 k

トランザクションの概要
====================================================================================================
インストール  1 パッケージ

ダウンロードサイズの合計: 71 k
インストール後のサイズ: 119 k
パッケージのダウンロード:
telnet-0.17-76.el8.x86_64.rpm                                       754 kB/s |  71 kB     00:00
----------------------------------------------------------------------------------------------------
合計                                                                135 kB/s |  71 kB     00:00
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                            1/1
  インストール中   : telnet-1:0.17-76.el8.x86_64                                                1/1
  scriptletの実行中: telnet-1:0.17-76.el8.x86_64                                                1/1
  検証             : telnet-1:0.17-76.el8.x86_64                                                1/1

インストール済み:
  telnet-1:0.17-76.el8.x86_64

完了しました!

telnetで25番ポートに接続

[root@SV-VEGA 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-vega.planet.space.com ESMTP Postfix

heloで応答

helo localhost
250 sv-vega.planet.space.com

mail from 送信者アドレス

mail from:test1@space.com
250 2.1.0 Ok

rcpt to 受信者アドレス

rcpt to:test2@space.com
250 2.1.5 Ok

data 本文 (ピリオドで終了)

data
354 End data with <CR><LF>.<CR><LF>
test
.
250 2.0.0 Ok: queued as 3A79A497932

quit 切断

quit
221 2.0.0 Bye
Connection closed by foreign host.

送信ログの確認

/var/log/maillog
Sep  5 08:03:32 SV-VEGA postfix/smtpd[6881]: connect from localhost[127.0.0.1]
Sep  5 08:04:18 SV-VEGA postfix/smtpd[6881]: A7521497932: client=localhost[127.0.0.1]
Sep  5 08:04:32 SV-VEGA postfix/cleanup[6884]: A7521497932: message-id=<20230904230418.A7521497932@sv-vega.planet.space.com>
Sep  5 08:04:32 SV-VEGA postfix/qmgr[6529]: A7521497932: from=<test1@space.com>, size=341, nrcpt=1 (queue active)
Sep  5 08:04:32 SV-VEGA postfix/smtp[6885]: A7521497932: to=<test2@space.com>, relay=XXXXXXXXXXXX[XXX.XXX.XXX.XXX]:25, delay=28, delays=28/0.02/0.05/0.05, dsn=2.0.0, status=sent (250 2.0.0 dIcSqMqeI2DtddIcSqGbmL mail accepted for delivery)

受信メールの確認

手順⑥ SPFの確認

dig @8.8.8.8 ドメイン名 txt
[root@SV-VEGA postfix]# dig @8.8.8.8 planet.space.com txt

; <<>> DiG 9.11.36-RedHat-9.11.36-8.el8_8.1 <<>> @8.8.8.8 planet.space.com txt
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27312
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;planet.space.com.            IN      TXT

;; ANSWER SECTION:
planet.space.com.     300     IN      TXT     "v=spf1 +ip4:XXX.XXX.XXX.XXX a:ZZZZZZZZZZZZ a mx -all"

;; Query time: 22 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 火  9月 05 08:17:48 JST 2023
;; MSG SIZE  rcvd: 114