ホーム > Linux Server

Linuxでsendmailを使う

大容量のストレージが欲しくてTime4VPSのStorage VPSを使っていますが、OpenVZなのでLinuxしか使えません。この環境下でもFreeBSDで使い慣れたsendmailを使えるようにしましょう。

sendmailとmailクライアントのインストール

CentOSやDebianには標準でsendmailとmailコマンドが用意されていないので、パッケージからインストールします。

CentOSの場合、sendmailサーバーに加えてクライアントプログラムのmailxをインストールします。

1
yum install sendmail mailx

Debianの場合はsendmailサーバーに加えてsendmail-cfというツールとクライアントプログラムのmailutilsをインストールします。

1
apt install sendmail sendmail-cf mailutils sasl2-bin

今回Debian 9.11でsendmailをインストールしたところ、sasl2-binを要求されたので追加インストールしています。

sasl-binのメッセージに従って/etc/mail/sendmail.mcと/etc/mail/submit.mcの最後にinclude('/etc/mail/sasl/sasl.m4')dnlを追記してsendmailをリロードしました。

1
/etc/init.d/sendmail reload

CentOSの場合はインストールできたらサービスの自動起動設定をしてからサービスを起動します。Debianはインストールと同時に起動処理が完了しているので以下は不要です。

1
2
systemctl enable sendmail.service
systemctl start sendmail.service

通常のCentOSにはsendmailの代わりにPostfixが入っているらしくMTAの切り替えが必要になりますが、Time4VPSでインストールされるCentOS 7.6にはPostfixは入っていませんでした。

1
2
3
4
5
6
# alternatives –config mta
There is 1 program that provides 'mta'.
Selection Command
———————————————–
*+ 1 /usr/sbin/sendmail.sendmail
Enter to keep the current selection[+], or type selection number:

MTAってのはMail Transfer Agentね。

ポート開放とメール設定

CentOSならfirewall-cmdを使ってsmtpポートとpop3ポートを開放します。

1
2
3
firewall-cmd --add-service=smtp --permanent
firewall-cmd --add-service=pop3 --permanent
firewall-cmd --reload

確認結果は以下のとおり。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: venet0
sources:
services: ssh dhcpv6-client http https dns ntp smtp pop3
ports: 20022/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

Debianの場合はufwでポート開放します。

1
2
ufw allow 110
ufw allow 587

確認結果は以下のとおり。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# ufw status
Status: active

To Action From
-- ------ ----
20022 LIMIT Anywhere
80 ALLOW Anywhere
443 ALLOW Anywhere
25 ALLOW Anywhere
110 ALLOW Anywhere
587 ALLOW Anywhere
20022 (v6) LIMIT Anywhere (v6)
80 (v6) ALLOW Anywhere (v6)
443 (v6) ALLOW Anywhere (v6)
25 (v6) ALLOW Anywhere (v6)
110 (v6) ALLOW Anywhere (v6)
587 (v6) ALLOW Anywhere (v6)

sendmailの初期設定はFreeBSDのsendmail設定と同じです。/etc/mailディレクトリ内のaccess, local-host-names, virtusertableファイルをそれぞれ設定します。

これに加えて一点だけLinux独自の設定が必要です。Sendmail で外部からのメールを受け入れ可能にする (CentOS 5.5)によるとCentOSのsendmailはデフォルトで外部メールを受信しない設定とのこと。/etc/mail/sendmail.cfの設定を書き換える必要があります。CentOS7もDebian9もSMTP daemon optionsのところにあります。

CentOS 7.6

1
2
3
4
5
# diff sendmail.cf.org sendmail.cf
269c269
< O DaemonPortOptions=Port=smtp,Addr=127.0.0.1, Name=MTA

> O DaemonPortOptions=Port=smtp, Name=MTA

Debian 9.11

1
2
3
4
5
# diff sendmail.cf.org sendmail.cf
269c269
< O DaemonPortOptions=Family=inet, Name=MTA-v4, Port=smtp, Addr=127.0.0.1
---
> O DaemonPortOptions=Family=inet, Name=MTA-v4, Port=smtp

これでmakeすればmailコマンドが使えるようになるよ。