You are not logged in.
Hey guys, am continuing to learn, and googling my way through the process of how to do things. Thought I'd share how I got email notifications working whenever a user or root logs in as well as when the system is rebooted.
My key questions are:
- Is there a smarter way to do this?
- Can a hacker work around these scripts without my knowledge?
Use cases:
- Email me using gmail SMTP relay when the system reboots
- Email me using gmail SMTP relay when ANY user logs into the system (including root)
Here are the steps I took:
- Checked/synced packages prior to install of postfix
pacman -Syu- Installed postfix
pacman -S postfix- Edited postfix to use gmail as smtp relay
nano /etc/postfix/main.cf- Add this code at the end of the file
# sets gmail as relay
relayhost = [smtp.gmail.com]:587
# use tls
smtp_use_tls=yes
# use sasl when authenticating to foreign SMTP servers
smtp_sasl_auth_enable = yes
# path to password map file
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
# list of CAs to trust when verifying server certificate
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
# eliminates default security options which are incompatible with gmail
smtp_sasl_security_options =- Add your gmail ID and password for SMTP relay
nano /etc/postfix/sasl_passwd- In the file you should add following
[smtp.gmail.com]:587 jane.doe@gmail.com:doeadeerNote I was able to get this to work without specifying the @gmail.com for the user id.
- Encrypt the password
postmap /etc/postfix/sasl_passwd- Start up postfix
rc.d start postfix- Send a quick email using gmail SMTP relay
echo 'hello world!' | mail -s 'first email' recipient@whateveremailaddress.comYou should get an email to the user address that you specified - If so, you've got SMTP relay working with gmail for sending emails out of your archlinux box! Hey, I was excited!
Now, for the email notifications of reboot and when a user logs in:
- Add postfix into your daemons - scroll down and find your startup daemons at the end, add postfix
nano /etc/rc.conf- Reboot email script
nano /etc/rc.local- Add following to the end of the file
sleep 15
echo 'ALERT - System rebooted on' `date` `who` | mail -s "Alert: Reboot from `who | cut -d"(" -f2 | cut -d")" -f1`" recipient@whateveremailaddress.comNote: I added 15 seconds before the email goes out as my system pulls latest time and date upon each reboot, so I wanted that pulled first before email was sent.
- User login email script
nano /etc/profile- Add following at end of the file
echo 'ALERT - User accessed webserver' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" recipient@whateveremailaddress.comDo a reboot of the system, and see if you get a reboot email. Then, log in and see if you get an email notification as well. This was fairly fun to work on. Hope it helps, feel free to provide advice if there's a better way to do this as I'm just learning.
Last edited by huntrm (2012-10-05 21:42:16)
Offline
You could realise the reboot mail also via a "@reboot cronjob". The login partn (depending on where the server is and how users log in) you maybe also be done via /etc/ssh/sshrc. sshrc is a rather unknown/unused feature of openssh which gets executed everytime a user logs in via ssh.
This could be overridded by the user by creating a personal configuration, but as its rather unknown....
The last thing is your mailserver setup:
when you only want to send mails, a full blown postfix installation is way to much. As an alternative you could use for instance ssmtp.
Offline
Thanks for the note. I looked into ssmtp, but because it was being discontinued (https://wiki.archlinux.org/index.php/SSMTP), and I'm planning on installing Dovecot at some point for a full-blown mail server, I went with postfix. Am sure others could try using the replacement for ssmtp, msmtp.
BTW, am running this on a Pogoplug V2.
Offline