CronがEメールを送信せずにコマンドラインEメールが機能します。

CronがEメールを送信せずにコマンドラインEメールが機能します。

Raspbian 10 Busterを実行しているRaspberry Piにssmtpをインストールしました。たとえば、コマンドラインから電子メールを送信すると、mailうまく機能します。しかし、MAILTOファイルに変数を追加して電子メールを送信するようにユーザーのcronタスクも設定しました。

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).

MAILTO="[email protected],[email protected]"

# we need to set the user path to add the system scripts directory /usr/local/sbin
# I tried with PATH=$PATH:/usr/local/sbin but it used this verbatim in the path
PATH=/usr/sbin:/usr/bin:/bin:/usr/local/sbin:

# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

26 3 * * * cronic python3 ~/redacted_directory_name/redacted_script_name.py
24 22 * * * echo "Testing cron email"

他のオンライン検索で私が理解したところ(間違っている場合は訂正してください)、cronはデフォルトでsendmailを使用して電子メールを送信します。

Sendmailはシステムで使用できますが、実際にはssmtpです。

$ which sendmail
/usr/sbin/sendmail
$ ls -l /usr/sbin/sendmail 
lrwxrwxrwx 1 root root 5 Jul 20  2014 /usr/sbin/sendmail -> ssmtp

このコンピュータでは、外部SMTPサーバーを使用してメールを送信するようにssmtpを構成しました。

cronはrootやsudo以外のユーザーに電子メールを送信しません。

メールログには以下が表示されます。

$ tail /var/log/mail.log
Jan 17 16:43:02 ed-mh-pi01 sSMTP[25679]: Cannot open mailhub:25
Jan 18 03:22:24 ed-mh-pi01 sSMTP[9846]: Creating SSL connection to host
Jan 18 03:22:25 ed-mh-pi01 sSMTP[9846]: SSL connection using ECDHE_RSA_AES_256_GCM_SHA384
Jan 18 03:22:26 ed-mh-pi01 sSMTP[9846]: Sent mail for root@[email protected] (221 2.0.0 Bye) uid=0 username=root outbytes=638
Jan 18 22:20:02 ed-mh-pi01 sSMTP[6924]: /etc/ssmtp/ssmtp.conf not found
Jan 18 22:20:02 ed-mh-pi01 sSMTP[6924]: Unable to locate mailhub
Jan 18 22:20:02 ed-mh-pi01 sSMTP[6924]: Cannot open mailhub:25
Jan 18 22:24:01 ed-mh-pi01 sSMTP[6988]: /etc/ssmtp/ssmtp.conf not found
Jan 18 22:24:01 ed-mh-pi01 sSMTP[6988]: Unable to locate mailhub
Jan 18 22:24:01 ed-mh-pi01 sSMTP[6988]: Cannot open mailhub:25

1月18日22:20:02、1月18日22:24:02のエントリは、クローンジョブEメールに関連しているようです。その当時、いくつかのテストをしました。

電子メールを送信するプロセスにはアクセス権がないと指定されています/etc/ssmtp/ssmtp.confArch Linux Wiki状態

/usr/bin/ssmtp バイナリは配布グループとして実行され、このファイルを読み取ることができます。自分や他のユーザーを配布グループに追加する理由はありません。

これがRaspbianの場合かどうかはわかりませんが、おそらくそれが問題かもしれないと思います。/etc/ssmtp/グループの所有権とその内容を次のようにメールに変更してみました。

$ ls -l /etc/ssmtp/
total 8
-rw-r--r-- 1 root mail 200 Jul 20  2014 revaliases
-rw-r----- 1 root mail 764 Jan 17 12:03 ssmtp.conf

しかし、問題はまだ存在します

[root] ~ $ tail /var/log/mail.log
<snip>
Jan 18 22:39:01 ed-mh-pi01 sSMTP[7559]: /etc/ssmtp/ssmtp.conf not found
Jan 18 22:39:01 ed-mh-pi01 sSMTP[7559]: Unable to locate mailhub
Jan 18 22:39:01 ed-mh-pi01 sSMTP[7559]: Cannot open mailhub:25

それでは、cronが電子メールを送信しないのはなぜですか?

編集する

すべてのユーザーが閲覧できるように権限を(一時的に)変更すると、/etc/ssmtp/ssmtp.conf電子メールが送信されます。ファイルに電子メールアカウントのプレーンテキストパスワードが含まれていて、通常のユーザーがコマンドラインから送信できるがcronでは送信できない理由を説明していないため、これは解決策ではありません。

編集2

$ ls -l /usr/sbin/ssmtp
-rwxr-xr-x 1 root root 30588 Jul 20  2014 /usr/sbin/ssmtp

ベストアンサー1

すでに書かれていますが、

/usr/bin/ssmtp バイナリは配布グループとして実行され、このファイルを読み取ることができます。

しかし、バイナリの場合はそうではないことを証明しました。

-rwxr-xr-x 1 ルート ルート 30588 2014 年 7 月 20 日/usr/sbin/ssmtp

ウィキペディアは言います。

電子メールパスワードは/etc/ssmtp/ssmtp.confにプレーンテキストで保存されるため、このファイルを安全に保つことが重要です。デフォルトでは、/etc/ssmtp ディレクトリ全体はルートグループと配布グループのみにアクセスできます。 /usr/bin/ssmtp バイナリは配布グループとして実行され、このファイルを読み取ることができます。自分や他のユーザーを配布グループに追加する理由はありません。

したがって、破損した権限を変更する必要があります。

chown -R root:mail /etc/ssmtp /usr/sbin/ssmtp
chmod -R g=u,g-w,o= /etc/ssmtp
chmod a=rx,g+s /usr/sbin/ssmtp

しかし、私はまたArch wikiが言う最初の内容が次のようであることを指摘したいと思います。

sSMTPは保持されません。 msmtpやOpenSMTPDなどの代替用途を検討してください。

おすすめ記事