服务器之家:专注于服务器技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - 教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

2021-03-08 23:30Linux就该这么学逄增宝 Linux

在本文中介绍如何在Linux系统上为ssh登录设置电子邮件提醒。以接收有关对root用户的未经授权或非法登录访问的警报。

 教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

本文中介绍如何在Linux系统上为ssh登录设置电子邮件提醒。以接收有关对root用户的未经授权或非法登录访问的警报。

.bashrc文件是做什么的?

.bashrc文件是一个脚本,每当以交互方式启动新的终端会话时,便会执行该脚本。

1)如何为root用户启用电子邮件警报

以root用户身份登录,然后将以下一行脚本添加到root用户“ .bashrc”文件中以实现此目的:

[root@localhost ~]# vim /root/.bashrc  

 

echo 'ALERT - SSH root shell access found on '$HOSTNAME' on:' `date` `who` | mail -s "Alert: SSH root shell access"  root@localhost 

教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

执行以下命令,使命令生效:

[root@localhost ~]# source .bashrc 

完成后,以root用户身份登录。将收到类似以下内容的邮件警报。

教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

将代码中的root@localhost改为自己用来接收邮件的邮箱。

2)如何为特定用户启用电子邮件警报

需要将以下脚本添加到指定用户的.bashrc文件中:

[root@localhost ~]# vim /home/bob/.bashrc  

 

echo 'ALERT - '$USER' shell access found on '$HOSTNAME' on:' `date` `who` | mail -s "Alert: User shell access" root@localhost 

教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

将代码中的root@localhost改为自己用来接收邮件的邮箱。

登录后的提示如下图:

教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

3)如何为所有用户启用电子邮件警报

为所有用户启用电子邮件报警和上面的配置类似。但是需要将以下脚本添加到/etc/bashrc文件最后一行中:

[root@localhost ~]# vim /etc/bashrc  

 

echo 'ALERT - '$USER' shell access found on '$HOSTNAME' on:' `date` `who` | mail -s "Alert: User shell access"  root@localhost 

教你如何在 Linux 上为 SSH 登录设置电子邮件提醒

原文地址:https://mp.weixin.qq.com/s?__biz=MzA4NzQzMzU4Mg==&mid=2652948219&idx=2&sn=ca07e7c0cb9f9f5bed3fce5cf0822337&chksm=8bedeefabc9a67ece998cd7b3b5fc6ab496bebb2f45ccf544c0fcb39e3d5d729235334f17b20&mpshare=1&

延伸 · 阅读

精彩推荐