How To Set Up SSH Mail Alert On Ubuntu System

Posted By : Iftikhar Hussain | 01-Dec-2022

linux

Loading...

How To Set Up Disk Alert Mail Along With SSH Login Alert

Space Quota Email To Be Triggered

For this, we first need to install postfix to send mail occasionally.

Step 1. You need to Install all the required packages by installing mailutils.

Command: sudo apt-get update

Command: sudo apt-get install mailutils

As sometimes the 2nd command doesn’t work there at the server. Hence, we ran the below command and performed the following:-

Command: sudo apt-get install postfix -y

Before getting to Step 2 we need to check the hostname at cd /etc/hostname and set the hostname for the server as the server’s hostname. And reboot the server with the command

Command: sudo reboot.

Step 2. Select “Internet Site” in the postfix configuration prompt.

And in 2nd prompt, you can give any name like your server name or domain name for this we need to check the hostname.

Also, Read An Introduction To Linux and Ubuntu

Step 3. Edit the main.cf of postfix for Gmail smtp configuration with vi or nano editor as per your preference. I am using nano,

Command: sudo nano /etc/postfix/main.cf

*Look for relayhost and add this= [smtp.gmail.com]:587

And at the bottom of the file add these lines below.

smtp_sasl_auth_enable = yes

smtp_sasl_security_options = noanonymous

smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd

smtp_tls_security_level = encrypt

smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt

smtp_sasl_mechanism_filter = login

  • After that save the file and run the reload command.

Command: sudo postfix reload

*Note: Reload postfix whenever you make any changes in main.cf file.

Step 4. Create a sasl_passwd file and add our smtp details

We will create a sasl_passwd file with the same path we mentioned in the previous step.

Command: sudo nano /etc/postfix/sasl/sasl_passwd

And paste this

[smtp.gmail.com]:587 <your-email-id>@gmail.com:<email-password>

Replace the <email-id> and <email-password> with your credentials, Then map credentials with this command.

Command: sudo postmap /etc/postfix/sasl/sasl_passwd

Step 5. Ensure you have allowed less secure apps in your given gmail account in the previous step. Follow these steps as below:

Step 6. Test the setup by sending a mail

Command: echo “test alert demo email.” | mail -s ‘demo subject’ <any-email>

You will receive an email on your given email id used in th in this command.

To debug this, we can check the logs by

Also, Read OpenVPN MFA Setup Via Docker Compose and OpenVPN In General

Command: sudo tail -f /var/log/mail.log

*Now the foremost step is to add a bash script to trigger the mail. The one I have added is as follows:

Step 1:- We will first create a .sh file and add the following below:

Command: sudo nano /home/ubuntu/disk-usage-alert-2.sh

*Then paste the following:

#!/bin/bash

[email protected]

[email protected],[email protected]

used=$(df -Ph | grep '/dev/mapper/ubuntu--vg-ubuntu--lv' | awk {'print $5'})

max=80%

if [ ${used%?} -ge ${max%?} ]; then

echo "The Mount Point "/DB" on $(hostname) has used $used at $(date)" | mail -s "Disk space alert on $(hostname): $used used" ${TO_EMAIL} ${CC_EMAIL}

fi

Step 2:- Run the below command for permissions:

Command: sudo chmod +x disk-usage-alert-2.sh

Step 3:- Edit your cronjob

Command: sudo nano crontab -e

Step 4: And add this in the Cronjob

0 */2 * * * sh /home/ubuntu/disk-usage-alert-2.sh

SSH-Login email alert

Login to your server and go to the below path:

Command: sudo nano /etc/profile

At the bottom of that file, add the following lines:

#MAIL ALERT AT SSH LOGIN

if [ -n "$SSH_CLIENT" ]; then

TEXT="$(date): ssh login to ${USER}@$(hostname -f)"

TO_EMAIL=[email protected]

CC_EMAIL=[email protected]

TEXT="$TEXT from $(echo $SSH_CLIENT|awk '{print $1}')"

echo $TEXT|mail -s "ssh login" ${TO_EMAIL} ${CC_EMAIL}

fi

2. Save and close the file.

Log out and log back into the system with SSH and you should eventually see an email alert to the destination email address of the login. The alert will not only tell you which username logged in, but it will also give you the IP address they logged in from.