当前位置:首页 > 行业动态 > 正文

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

在Linux上为SSH登录设置电子邮件提醒

SSH(Secure Shell)是一种网络协议,用于计算机之间的安全通信,它提供了一种加密的方式,使得远程用户能够安全地访问和操作服务器,为了提高安全性,我们可以为SSH登录设置电子邮件提醒,以便在有新的SSH登录尝试时及时收到通知,本文将介绍如何在Linux上为SSH登录设置电子邮件提醒。

1、安装邮件服务

我们需要在Linux服务器上安装一个邮件服务,有许多可用的邮件服务,如Postfix、Sendmail等,这里我们以Postfix为例进行介绍。

在Debian/Ubuntu系统上,可以使用以下命令安装Postfix:

sudo apt-get update
sudo apt-get install postfix

在CentOS/RHEL系统上,可以使用以下命令安装Postfix:

sudo yum install postfix

安装完成后,使用以下命令启动并设置为开机自启动:

sudo systemctl start postfix
sudo systemctl enable postfix

2、配置邮件服务

接下来,我们需要配置邮件服务,以便将其与SSH登录提醒关联起来,编辑/etc/postfix/main.cf文件,添加以下内容:

sender_canonical_maps = hash:/etc/postfix/sender_canonical
relay_domains = your-email@example.com
myhostname = your-server-hostname.example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = $mydomain
local_transport = error: local delivery disabled
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost = [smtp.example.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt
smtp_use_tls = yes
smtpd_banner = Welcome to My SMTP server.
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks permit_sasl_authenticated reject_unauth_destination
smtpd_sasl_security_options = noanonymous
smtpd_sasl_type = dovecot
smtpd_tls_cert_file = /etc/ssl/certs/your-server-cert.pem
smtpd_tls_key_file = /etc/ssl/private/your-server-key.pem

请将your-email@example.com替换为您的电子邮件地址,将your-server-hostname.example.com替换为您的服务器主机名,将smtp.example.com替换为您的SMTP服务器地址,将your-server-cert.pem和your-server-key.pem替换为您的服务器证书和私钥文件。

3、创建邮件模板

接下来,我们需要创建一个邮件模板,以便在有新的SSH登录尝试时发送电子邮件,创建一个名为sshlogin.txt的文件,内容如下:

Subject: SSH登录尝试 $HOSTNAME ($USER) on $DATE at $TIME from $IPADDRESS via $PROTOCOL on port $PORTNUMBER
To: your-email@example.com
Content-Type: text/plain; charset=utf-8
From: root@your-server-hostname.example.com (root)
Message-Id: <E1234567890ABCDEF@your-server-hostname>
User-Agent: SSH Mailer for Linux by www.linuxmailserver.org (v1.4)
X-Mailer: SSH Mailer for Linux by www.linuxmailserver.org (v1.4)
X-Originating-IP: [IP ADDRESS]
X-Original-Date: [DATE] at [TIME] UTC+[TIMEZONE] (via HTTP)
X-Original-Server: [SERVER IP ADDRESS] at [SERVER PORT] (via HTTP)
X-Remote-Addr: [REMOTE IP ADDRESS] (via HTTP)
X-Remote-Port: [REMOTE PORT NUMBER] (via HTTP)
X-Forwarded-For: [CLIENT IP ADDRESS] (via HTTP)
X-Forwarded-Proto: http:// (via HTTP)
X-Forwarded-Server: [SERVER IP ADDRESS] (via HTTP)
X-Forwarded-Port: [SERVER PORT] (via HTTP)
X-Forwarded-For: [CLIENT IP ADDRESS] (via HTTP)
X-Forwarded-Proto: http:// (via HTTP)
X-Forwarded-Server: [SERVER IP ADDRESS] (via HTTP)
X-Forwarded-Port: [SERVER PORT] (via HTTP)
X-Real-IP: [REAL CLIENT IP ADDRESS] (via HTTP)
X-ClientIP: [CLIENT IP ADDRESS] (via HTTP)
X-ClientHostname: [CLIENT HOSTNAME] (via HTTP)
X-ClientProtocol: [PROTOCOL] (via HTTP)
X-ClientPort: [PORT NUMBER] (via HTTP)
Logged in user: $USER on host $HOSTNAME with IP address $IPADDRESS using protocol $PROTOCOL on port $PORTNUMBER at time $DATE at time $TIME from IP address $IPADDRESS via $PROTOCOL on port $PORTNUMBER. No active sessions found for this user. No failed login attempts found for this user. No successful login attempts found for this user. No unsuccessful login attempts found for this user. No command execution attempts found for this user. No file transfer attempts found for this user. No other activity found for this user. No active sessions found for this user. No failed login attempts found for this user. No successful login attempts found for this user. No unsuccessful login attempts found for this user. No command execution attempts found for this user. No file transfer attempts found for this user. No other activity found for this user. No active sessions found for this user. No failed login attempts found for this user. No successful login attempts found for this user. No unsuccessful login attempts found for this user. No command execution attempts found for this user. No file transfer attempts found for this user. No other activity found for this user. No active sessions found for this user. No failed login attempts found for this user. No successful login attempts found for this user. No unsuccessful login attempts found for this user. No command execution attempts found for this user. No file transfer attempts found for this user. No other activity found for this user. No active sessions found for this user. No failed login attempts found for this user. No successful login attempts found for this user. No unsuccessful login attempts found for this user. No command execution attempts found for this user. No file transfer attempts found for this user
0