在Linux服务器上设置SSH登录后发送email提醒,可以通过以下步骤实现:
1、安装邮件发送工具
首先需要在服务器上安装一个邮件发送工具,这里以sendmail为例,在Debian/Ubuntu系统上,可以使用以下命令安装:
sudo apt-get update sudo apt-get install sendmail
在CentOS/RHEL系统上,可以使用以下命令安装:
sudo yum install sendmail
2、配置sendmail
安装完成后,需要对sendmail进行配置,配置文件位于/etc/mail/sendmail.mc
,使用文本编辑器打开配置文件,
sudo nano /etc/mail/sendmail.mc
找到以下行并进行修改:
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl
修改为:
DAEMON_OPTIONS(`Port=smtp, Name=MTA')dnl
将以下行:
dnl Uncomment the next line to generate "delayed mail" warnings
define(confCON_EXPENSIVE',
True')dnl
取消注释:
define(confCON_EXPENSIVE',
True')dnl
保存并退出,接下来,使用m4编译器生成新的配置文件:
sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
3、创建邮件模板
创建一个名为auth.log
的文件,用于存放登录日志,文件路径可以自定义,/var/log/auth.log
,使用以下命令创建文件:
sudo touch /var/log/auth.log
创建一个名为login_alert.mc
的邮件模板文件,文件路径可以自定义,/etc/mail/login_alert.mc
,使用以下命令创建文件:
sudo touch /etc/mail/login_alert.mc
编辑login_alert.mc
文件,添加以下内容:
define(confLOGIN_MSG',
$j Sending login alert for $g')dnl define(SMTP_MAILER_ARGS',
-t')dnl FEATURE(delay_warnings)dnl LOCAL_RULES(`$llocal_recipients')dnl include(`smtp-generic')dnl
保存并退出,接下来,使用m4编译器生成新的邮件模板文件:
sudo m4 /etc/mail/login_alert.mc > /etc/mail/login_alert.cf
4、配置PAM(可选)
如果希望在每次用户登录时自动发送邮件提醒,可以配置PAM(可插入认证模块),安装pam_ssmtp模块:
Debian/Ubuntu系统:
sudo apt-get install libpam-ssmtp python-smtplib sasl2-bin spamassassin spamcurl unzip vim gettext-base autoconf automake libtool bison gcc make openssl libsasl2-dev libsasl2-modules-dbg libsasl2-modules:amd64 libsasl2-modules:i386 zlib1g:amd64 zlib1g:i386 libdb-dev libsqlite3-dev libmysqlclient-dev libgssglue1:amd64 libgssglue-dev:amd64 libldap2-dev libldap-common:amd64 libpcre3-dev libpcre3:amd64 libcurl4-openssl-dev libcurl4:amd64 curl:amd64 curl:i386 mailutils:amd64 mailutils:i386 mailcap:any file:any build-essential wget git rsync vim patch perl tar unzip bzip2 make automake autoconf gcc g++ binutils bison flex byacc texinfo gzip bc kmod subversion coreutils screen emacs25 ncurses-base ncurses-bin pax gperf psmisc sysvinit-utils debianutils util-linux procps cron ntpdate xz-utils zlib1g zlib1g-dev openssl openssl-blacklist ca-certificates openssl-policies tcpdump socat netcat dnsutils iproute2 iputils-ping traceroute ltrace strace sysstat iftop iotop htop screenfetch sysfsutils sysctl netcfg bind-utils avahi-daemon libnss3:amd64 libnspr4:amd64 libpam0g:amd64 libpam-modules:amd64 libpam-modules:i386 libpam-runtime:amd64 libpam0g:i386 libpam-dev:amd64 libpam-dev:i386 libsasl2-modules:amd64 libsasl2-modules:i386 libsasl2-modules-dbg:amd64 libsasl2-modules-dbg:i386 libsasl2:amd64 libsasl2:i386' && wget https://github.com/amavis/amavisd/releases/download/v${AMAVISD_VERSION}/amavisd-${AMAVISD_VERSION}.tar.gz && tar -xzf amavisd* && cd amavisd* && ./configure --with-clamav --with-sasl --with-dbdir=${DBDIR} --with-localstatedir=${STATEDIR} && make && make install && rm -rf amavis* && apt-get clean && apt-get autoremove && apt-get autoclean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && echo "deb http://www.debian.org/debian $(lsb_release -sc) main" | tee /etc/apt/sources.list && echo "deb http://www.debian.org/debian $(lsb_release -sc)-updates main" | tee -a /etc/apt/sources.list && echo "deb http://security.debian.org $(lsb_release -sc)-security main" | tee -a /etc/apt/sources.list && echo "deb http://www.debian.org/debian $(lsb_release -sc) universe" | tee -a /etc/apt/sources.list && echo "deb http://www.debian.org/debian $(lsb_release -sc) multiverse" | tee -a /etc/apt/sources.list && echo "deb http://www.debian.org/debian $(lsb_release -sc)-backports main" | tee -a /etc/apt/sources.list && apt update && apt upgrade" && apt install pam_ssmtp spamassassin spamcurl unzip vim gettext-base autoconf automake libtool bison gcc make openssl libsasl2-dev libsasl2-modules-dbg libsasl2-modules:amd64 libsasl2-modules:i386 zlib1g:amd64 zlib1g:i386 libdb-dev libsqlite3-dev libmysqlclient-dev libgssglue1:amd64 libgssglue-dev:amd64 libldap2-dev libldap-common:amd64 libpcre3-dev libpcre3:amd64 libcurl4-openssl-dev libcurl4:amd64 curl:amd64 curl:i386 mailutils:amd64 mailutils:i386 mailcap:any file:any build-essential wget git rsync vim patch perl tar unzip bzip2 make automake autoconf gcc g++ binutils bison flex byacc texinfo gzip bc kmod subversion coreutils screen emacs25 ncurses-base ncurses-bin pax gperf psmisc sysvinit-utils debianutils util-linux procp
本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/472634.html
如有侵犯您的合法权益请发邮件951076433@qq.com联系删除