SSH でログインされたらメールするように設定してみます。今回は CentOS7 に設定し、メール送信には Gmail アドレスを使いました。
パッケージのインストール
以下、2 つのパッケージが必要になります。
パッケージ名 | 役割 |
---|---|
mailx | メール送信時に使う mail コマンドを含む |
ssmtp | リレー機能等を除外した、最低限の MTA として動作する |
MTA には Postfix や Sendmail を使っても良いのですが、リレー機能等は今回の要件に不要です。そこで、最小限の MTA 機能を提供する ssmtp をインストールします。ssmtp は標準リポジトリにありません。EPEL リポジトリにあるので、先に EPEL リポジトリを追加しておきます。
yum install epel-release yum --enablerepo=epel -y install mailx ssmtp
デフォルトの設定ファイル
/etc/ssmtp/ssmtp.conf
ssmtp の設定ファイルは /etc/ssmtp/ssmtp.conf にあります。
[root@sig9 ssmtp]# cat /etc/ssmtp/ssmtp.conf # # /etc/ssmtp.conf -- a config file for sSMTP sendmail. # # See the ssmtp.conf(5) man page for a more verbose explanation of the # available options. # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=postmaster # The place where the mail goes. The actual machine name is required # no MX records are consulted. Commonly mailhosts are named mail.domain.com # The example will fit if you are in domain.com and your mailhub is so named. mailhub=mail # Example for SMTP port number 2525 # mailhub=mail.your.domain:2525 # Example for SMTP port number 25 (Standard/RFC) # mailhub=mail.your.domain # Example for SSL encrypted connection # mailhub=mail.your.domain:465 # Where will the mail seem to come from? #RewriteDomain= # The full hostname #Hostname= # Set this to never rewrite the "From:" line (unless not given) and to # use that address in the "from line" of the envelope. #FromLineOverride=YES # Use SSL/TLS to send secure messages to server. #UseTLS=YES #IMPORTANT: The following line is mandatory for TLS authentication TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt # Use SSL/TLS certificate to authenticate against smtp host. #UseTLSCert=YES # Use this RSA certificate. #TLSCert=/etc/pki/tls/private/ssmtp.pem # Get enhanced (*really* enhanced) debugging information in the logs # If you want to have debugging of the config file parsing, move this option # to the top of the config file and uncomment #Debug=YES
/etc/ssmtp/ssmtp.conf(コメント除外版)
コメントを除外すると以下のようになります。
# grep -v -e '^\s*#' -e '^\s*$' /etc/ssmtp/ssmtp.conf root=postmaster mailhub=mail TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
ssmtp の設定
Gmail を使ってメール送信するには ssmtp を以下のように設定します。上述の通り、設定ファイルは /etc/ssmtp/ssmtp.conf にあります。Google の二要素認証を利用している場合は AuthPass にアプリケーション用に割り当てたパスワードを指定します。
Mailhub=smtp.gmail.com:587 AuthUser=[送信元メールアドレス] AuthPass=[パスワード] UserTLS=Yes UseSTARTTLS=Yes
SSH ログイン時にメールするスクリプト
(ユーザを限定せずに)SSH ユーザログインが発生した場合、/etc/ssh/sshrc の内容が実行されます。今回は /etc/ssh/sshrc を以下の内容で新規作成しました。
#!/bin/sh MAIL_TO=[通知先メールアドレス] TIME=`/bin/date "+%Y/%m/%d/ %H:%M:%S"` FROM_ADDR=`echo $SSH_CONNECTION | cut -d' ' -f1` FROM_PORT=`echo $SSH_CONNECTION | cut -d' ' -f2` FROM_ADDR_PORT=${FROM_ADDR}:${FROM_PORT} TO_ADDR=`echo $SSH_CONNECTION | cut -d' ' -f3` TO_PORT=`echo $SSH_CONNECTION | cut -d' ' -f4` TO_ADDR_PORT=${TO_ADDR}:${TO_PORT} echo -e "Time\t${TIME}\nHost\t${HOSTNAME}\nUser\t${USER}\nFrom\t${FROM_ADDR_PORT}\nTo\t${TO_ADDR_PORT}" | /bin/mail -s "[SSH]${HOSTNAME}/${USER} (${TIME})" ${MAIL_TO}
通知メールのサンプル
今回作成したスクリプトでは、サーバに対して SSH ログインが実行される度に以下のようなメールが送られてきます。
差出人: [送信元メールアドレス] 件名: [SSH][HOSTNAME]/[USERNAME] (2016/09/22/ 13:42:30) 日付: 2016年9月22日 13:42:30 JST 宛先: [通知先メールアドレス] Time 2016/09/22/ 13:42:30 Host [HOSTNAME] User [USERNAME] From [アクセス元アドレス]:[ポート] To [アクセス先アドレス]:[ポート]