歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 如何設置Linux上ssh登錄的Email提醒

如何設置Linux上ssh登錄的Email提醒

日期:2017/2/27 15:53:06   编辑:Linux教程

虛擬私有服務器 (VPS)上啟用 SSH 服務使得該服務器暴露到互聯網中,為黑客攻擊提供了機會,尤其是當 VPS 還允許root 直接訪問時。VPS 應該為每次 SSH 登錄成功嘗試配置一個自動的 email 警告。 VPS 服務器的所有者會得到各種 SSH 服務器訪問日志的通知,例如登錄者、登錄時間以及來源 IP 地址等信息。這是一個對於服務器擁有者來說,保護服務器避免未知登錄嘗試的重要安全關注點。這是因為如果黑客使用暴力破解方式通過 SSH 來登錄到你的 VPS 的話,後果很嚴重。在本文中,我會解釋如何在 CentOS 6、 CentOS 7、 RHEL 6 和 RHEL 7上為所有的 SSH 用戶登錄設置一個 email 警告。

1. 使用root用戶登錄到你的服務器;

2. 在全局源定義處配置警告(/etc/bashrc),這樣就會對 root 用戶以及普通用戶都生效:
[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`" [email protected]
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`" [email protected]
# .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
echo 'ALERT - Root Shell Access (vps.ehowstuff.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" [email protected]
4. 你也可以選擇性地讓警告只對特定的普通用戶生效(例如 skytech):

將下面的內容加入到/home/skytech/.bashrc文件尾部:

[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`" [email protected]
譯文:http://linux.cn/article-5334-1.html
Copyright © Linux教程網 All Rights Reserved