技術者になりたい何か

技術者になりたい何かの覚書的な何かです

postfixでroot宛メールをgmailに転送

サーバに溜まっていくroot宛のメールをgmailに転送するための設定を入れてみることにします。

環境はDebian 9.3。メールサーバはpostfixを使用。

■必要パッケージインストール

$ sudo apt install postfix bsd-mailx libsasl2-modules

postfix自動起動有効に。

$ sudo systemctl enable postfix

Synchronizing state of postfix.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable postfix

 

postfixのmain.cfで設定

$ emacs /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_use_tls = yes
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_tls_security_options = noanonymous
smtp_tls_CApath = /etc/pki/tls/certs/ca-bundle.crt


■パスワードファイルとハッシュファイルの作成

パスワードファイルはsmtp_sasl_password_mapsで指定したもの。
別に名前はこれじゃなくても良い。

$ emacs /etc/postfix/sasl_passwd
[smtp.gmail.com]:587 <username>@gmail.com:<password>

sasl_passwdは平文でgmailのアカウントとパスワードを保存しているので、
root以外読み取れないようにアクセス権変更

$ sudo chmod 600 /etc/postfix/sasl_passwd

ハッシュファイル作成

$ sudo postmap /etc/postfix/sasl_passwd

設定ファイルの状態

$ ls -la /etc/postfix/
合計 148
drwxr-xr-x 5 root root 4096 2月 9 20:31 .
drwxr-xr-x 104 root root 4096 2月 9 21:00 ..
-rw-r--r-- 1 root root 43 1月 18 19:56 dynamicmaps.cf
drwxr-xr-x 2 root root 4096 9月 27 13:56 dynamicmaps.cf.d
-rw-r--r-- 1 root root 231 2月 9 20:25 main.cf
-rw-r--r-- 1 root root 27130 1月 18 19:56 main.cf.proto
-rw-r--r-- 1 root root 4416 9月 27 13:56 makedefs.out
-rw-r--r-- 1 root root 6068 1月 18 19:56 master.cf
-rw-r--r-- 1 root root 6068 1月 18 19:56 master.cf.proto
-rwxr-xr-x 1 root root 29446 9月 27 13:56 post-install
-rw-r--r-- 1 root root 10170 9月 27 13:56 postfix-files
drwxr-xr-x 2 root root 4096 1月 18 19:56 postfix-files.d
-rwxr-xr-x 1 root root 9502 9月 27 13:56 postfix-script
drwxr-xr-x 2 root root 4096 9月 27 13:56 sasl
-rw------- 1 root root 48 2月 9 20:42 sasl_passwd ★
-rw------- 1 root root 12288 2月 9 20:42 sasl_passwd.db ★

postfix 再起動(or 再読込)

$sudo systemctl restart postfix

■動作確認①

テストメール送信

$ echo "Test message" | mail <username>@gmail.com

→自分のgmailに届けばおk。

ログは/var/log/mail.logに出るので、うまく行かなかったときは確認すると良い

下はうまく行かなかったとき。

アカウント or パスワード間違えてて「535-5.7.8 Username and Password not accepted.」

$ sudo tail -50 /var/log/mail.log
~~抜粋~~
Feb 9 20:37:53 tminserver postfix/pickup[8922]: 9E8B93E0068: uid=1000 from=<tmin>
Feb 9 20:37:53 tminserver postfix/cleanup[8940]: 9E8B93E0068: message-id=<20180209113753.9E8B93E0068@tminserver.localdomain>
Feb 9 20:37:53 tminserver postfix/qmgr[8923]: 9E8B93E0068: from=<tmin@tminserver.localdomain>, size=406, nrcpt=1 (queue active)
★Feb 9 20:37:53 tminserver postfix/smtp[8942]: connect to smtp.gmail.com[2404:6800:4008:c04::6c]:587: Network is unreachable
★Feb 9 20:37:55 tminserver postfix/smtp[8942]: 9E8B93E0068: SASL authentication failed; server smtp.gmail.com[108.177.97.108] said: 535-5.7.8 Username and Password not accepted. Learn more at?535 5.7.8 https://support.google.com/mail/?p=BadCredentials x15sm6410111pff.55 - gsmtp
Feb 9 20:37:57 tminserver postfix/smtp[8942]: 9E8B93E0068: to=<mail@example.com>, relay=smtp.gmail.com[108.177.97.109]:587, delay=3.6, delays=0.11/0.06/3.4/0, dsn=4.7.8, status=deferred (SASL authentication failed; server smtp.gmail.com[108.177.97.109] said: 535-5.7.8 Username and Password not accepted. Learn more at?535 5.7.8 https://support.google.com/mail/?p=BadCredentials n1sm5159052pge.19 - gsmtp)
Feb 9 20:43:10 tminserver postfix[10439]: Postfix is running with backwards-compatible default settings

今度はうまくいったとき。

dsn=2.0.0, status=sent (250 2.0.0 OK 1518176614 i3sm4710337pgs.63 - gsmtp)

Feb 9 20:43:31 tminserver postfix/pickup[10621]: B9D563E006B: uid=1000 from=<tmin>
Feb 9 20:43:31 tminserver postfix/cleanup[10738]: B9D563E006B: message-id=<20180209114331.B9D563E006B@tminserver.localdomain>
Feb 9 20:43:31 tminserver postfix/qmgr[10622]: B9D563E006B: from=<tmin@tminserver.localdomain>, size=406, nrcpt=1 (queue active)
Feb 9 20:43:31 tminserver postfix/smtp[10626]: connect to smtp.gmail.com[2404:6800:4008:c04::6c]:587: Network is unreachable
★ Feb 9 20:43:34 tminserver postfix/smtp[10626]: B9D563E006B: to=<mail@example.com>, relay=smtp.gmail.com[74.125.204.108]:587, delay=3.1, delays=0.08/0/2.1/0.92, dsn=2.0.0, status=sent (250 2.0.0 OK 1518176614 i3sm4710337pgs.63 - gsmtp)
Feb 9 20:43:34 tminserver postfix/qmgr[10622]: B9D563E006B: removed

■root宛のメールを転送する設定。

aliaseの設定

$ cat /etc/aliases
# /etc/aliases
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: root
www: root
ftp: root
abuse: root
noc: root
security: root
root: tmin

rootのところにアカウントを入れる。

$ emacs /etc/aliases
root:<username>@gmail.com

設定反映

$ sudo newaliases

■動作確認②

$ sudo echo test | mail root

gmailにtestでメールが届けばおk。

うまく行っていれば、サーバからのroot宛のmailも転送されてくるはず。

 

Sponsored Link