Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> linux下如何設置SSH登錄後有郵件Email提醒通知

linux下如何設置SSH登錄後有郵件Email提醒通知

日期:2017/2/7 14:30:59      編輯:Linux教程
 

服務器上啟用 SSH 服務使得該服務器暴露到互聯網中,為黑客攻擊提供了機會,尤其是當 linux服務器 還允許root 直接訪問時。 linux服務器應該為每次 SSH 登錄成功嘗試配置一個自動的 email 警告。

linux服務器的所有者會得到各種 SSH 服務器訪問日志的通知,例如登錄者、登錄時間以及來源 IP 地址等信息。這是一個對於服務器擁有者來說,保護服務器避免未知登錄嘗試的重要安全關注點。這是因為如果黑客使用暴力破解方式通過 SSH 來登錄到你的 linux服務器的話,後果很嚴重。在本文中,我會解釋如何在 CentOS 6、 CentOS 7、 RHEL 6 和 RHEL 7上為所有的 SSH 用戶登錄設置一個 email 警告。

1、針對 linux服務器 上所有能登陸SSH的用戶(root和普通用戶)

編輯 vim /etc/bashrc 在結尾處添加

echo 'ALERT - Root Shell Access (root.aliyun.cnhzz.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" [email protected]
2、只針對 root 用戶生效

編輯 vim .bashrc 在結尾處添加

echo 'ALERT - Root Shell Access (root.aliyun.cnhzz.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" [email protected]
3、你也可以選擇性地讓警告只對特定的普通用戶生效(例如bin2u):

編輯 vim /home/bin2u/.bashrc 在結尾處添加

echo 'ALERT - Root Shell Access (root.aliyun.cnhzz.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" [email protected]
以上設置當場有效,退出ssh後重新登錄即可收到郵件提醒,如果沒有收到請發郵件進行測試;

echo 'ALERT - Root Shell Access (root.aliyun.cnhzz.com) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" [email protected]
如果發現有類似報錯信息

send-mail: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol send-mail: warning: inet_protocols: configuring for IPv4 support only postdrop: warning: inet_protocols: IPv6 support is disabled: Address family not supported by protocol postdrop: warning: inet_protocols: configuring for IPv4 support only
查看當前inet_protocols

# /usr/sbin/postconf | grep inet_protocolsinet_protocols = all
修改成ipv4

# vi /etc/postfix/main.cfinet_protocols = all改為inet_protocols = ipv4
直接通過外部SMTP發郵件 (直接拋棄sendmail和postfix)

有的朋友肯定會在發郵件這裡糾結一段時間,會出現各種雜七雜八的錯誤一時半會也解決不了。因此使用外部SMTP發郵件是個不錯的方法。

#vi /etc/mail.rc set [email protected] smtp=smtp.139.comset smtp-auth-user=123456set smtp-auth-password=sbsbsbsbset smtp-auth=login
登陸後,你的接受的郵箱會收到類似的郵件。
ssh-mail

轉載請注明:菜鳥HOW站長 » linux下如何設置SSH登錄後有郵件Email提醒通知
 

Copyright © Windows教程網 All Rights Reserved