Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

為 Linux 的 SSH 登入加上 Email 通知

為 Linux 的 SSH 登入加上 Email 通知

VPS 一旦開了 SSH 暴露在網路上,就等於把門半開著,若還允許 root 直接登入,風險更高。一個簡單有效的做法是:每次 SSH 登入成功就自動發一封通知信,內容包含登入者、時間與來源 IP。這樣一來,萬一有人用暴力破解闖進來,你會第一時間知道。

以下做法適用於 CentOS 6、CentOS 7、RHEL 6 與 RHEL 7。

1. 以 root 登入伺服器。

2. 若要對 root 與所有普通使用者都生效,把規則寫進全域的 /etc/bashrc:

[root@vps ~]# vi /etc/bashrc

在檔案末尾加入:

echo 'ALERT - Root Shell Access (vps.ehowstuff.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com

3. 若只想對 root 生效,改寫 /root/.bashrc,在尾端加上同一行:

[root@vps ~]# vi .bashrc

echo 'ALERT - Root Shell Access (vps.ehowstuff.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com

# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

4. 若只想針對特定普通使用者(例如 skytech)生效,寫入該使用者的家目錄:

[root@vps ~]# vi /home/skytech/.bashrc

echo 'ALERT - Root Shell Access (vps.ehowstuff.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" recipient@gmail.com

提醒兩點:把 recipient@gmail.com 換成你自己的信箱,並確認郵件寄送工具(mailx/postfix 等)已就緒,否則通知不會出得去。

譯文:https://linux.cn/article-5334-1.html

Copyright © Linux教程網 All Rights Reserved