コンピュータ/ソフトウェア関連Tips

postfixセットアップメモ(FreeBSD 自前サーバ版)

作成日: Oct 17, 2020
更新日: Oct 17, 2020
カテゴリー: FreeBSD タグ: FreeBSD

自前のSMTPサーバを利用して、root宛メールを指定したメールアドレスに送信するようにしたときのメモです。

$ sudo pkg install postfix-sasl

sendmail無効化とpostfix有効化。

$ sudo vi /etc/rc.conf
sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"
postfix_enable="YES"
$ sudo mkdir -p /usr/local/etc/mail
$ sudo install -m 0644 /usr/local/share/postfix/mailer.conf.postfix /usr/local/etc/mail/mailer.conf
$ cd /usr/local/etc/mail
$ cd /usr/local/etc/postfix
$ sudo cp main.cf main.cf.org
$ sudo vi main.cf

以下を修正

#myhostname = host.domain.tld
#myhostname = virtual.domain.tld
↓
myhostname = freebsd

#myorigin = $myhostname
#myorigin = $mydomain
↓
myorigin = $myhostname

#relayhost = $mydomain
#relayhost = [gateway.my.domain]
#relayhost = [mailserver.isp.tld]
#relayhost = uucphost
#relayhost = [an.ip.add.ress]
↓
relayhost = [xxx.xxx.xxx.xxx]:12587

以下を main.cf 末尾に追加

smtp_use_tls = yes
smtp_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtp_tls_CAfile = /etc/ssl/cert.pem
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/usr/local/etc/postfix/sasl_passwd
sender_canonical_maps = hash:/usr/local/etc/postfix/sender_canonical
$ sudo vi /usr/local/etc/postfix/sender_canonical
---------->8---------->8---------->8---------->8----------
@microserver    @email.example.com
---------->8---------->8---------->8---------->8----------
$ sudo vi /usr/local/etc/postfix/sasl_passwd
[xxx.xxx.xxx.xxx]:12587  USERNAME:PASSWORD

$ sudo chmod 0600 /usr/local/etc/postfix/sasl_passwd

$ sudo postmap -v hash:/usr/local/etc/postfix/sasl_passwd
postmap: name_mask: ipv4
postmap: name_mask: host
postmap: inet_addr_local: configured 2 IPv4 addresses
postmap: been_here: 192.168.0.196/32: 0
postmap: been_here: 127.0.0.1/32: 0
postmap: mynetworks_core: 192.168.0.196/32 127.0.0.1/32
postmap: open hash /usr/local/etc/postfix/sasl_passwd
postmap: Compiled against Berkeley DB version 1
$ sudo postmap -v hash:/usr/local/etc/postfix/sender_canonical
postmap: name_mask: ipv4
postmap: name_mask: host
postmap: inet_addr_local: configured 2 IPv4 addresses
postmap: been_here: 192.168.0.194/32: 0
postmap: been_here: 127.0.0.1/32: 0
postmap: mynetworks_core: 192.168.0.194/32 127.0.0.1/32
postmap: open hash /usr/local/etc/postfix/sender_canonical
postmap: Compiled against Berkeley DB version 1

postfixサービスの起動

$ sudo service postfix start
postfix/postfix-script: starting the Postfix mail system
$ sudo service postfix status
postfix is running as pid 8848.

テストメール送信

$ echo test mail|mail -v -s test status@email.example.com

root宛メールを特定ユーザに転送する

/etc/aliases を変更。

$ sudo vi /etc/aliases

# Pretty much everything else in this file points to "root", so
# you would do well in either reading root's mailbox or forwarding
# root's email from here.

# root: me@my.domain

最後の行のコメントを外し、メールアドレスを変更する。その後、newalieasesを実行。

$ sudo newaliases