Like many other people, I use GMail with alarming frequency. So imagine my joy when I got my shiny new Sony Ericsson W715 phone last year and discovered that it had a built in GMail client. Now, like many Linux users, my system’s root account regularly gets updated with e-mails from cronjobs, system status updates and the like. It’s also incredibly easy to write scripts and programs to mail users various alerts. So I wanted to find out how I could route the e-mails sent to root (or any other local Linux system account) elsewhere, like my GMail account. That way, anything my Linux system had to report could get sent directly to my mobile phone without spending any money on fancy equipment.
Sendmail might seem an unnecessary service to leave running on a home server, especially with most home broadband customers having an SMTP server account with their ISP. Besides, sendmail is not set up to receive e-mails outside of it’s local network and that’s not really something I know how to do. However, sendmail is quite an important service to leave running on any Linux system that is being used in a server environment as it’s used as a messaging service to alert the root user of various things that might be happening to the system. This remains true even if you’re running it as a standalone server at home.
You’ll need the following packages installed: -
Install using your favourite package manager.
So, how to send these system alerts to my GMail address? Well, for this to work, you will need several things – a valid hostname for your system and an account on your ISP’s SMTP server (you know, the server you used to use for e-mail in Outlook before WebMail became all the rage :-)). I talked about how to set up DynDNS for a static hostname for your home server here. So assuming you have these two things, there is no reason why you can’t route these system mails to your WebMail account. Let’s assume your Linux server is called my.server.org and your ISP mail server is called my.smtp.server.net.
First you’ll need to edit the /etc/aliases file.
You’ll see something similar to the below: -
# General redirections for pseudo accounts.
The aliases file is essentially like a mailing list file. The first column has the mailing list name (sometimes called a virtual mailbox), and the second column has the members of the mailing list separated by commas. To start, sendmail searches the first column of the file for a match. If there is no match, then sendmail assumes the recipient is a regular user on the local server and deposits the mail in their mailbox. If it finds a match in the first column, sendmail notes the nickname entry in the second column. It then searches for the nickname again in the first column to see if the recipient isn’t on yet another mailing list. If sendmail doesn’t find a duplicate, it assumes the recipient is a regular user on the local server and deposits the mail in their mailbox. If the recipient is a mailing list, then sendmail goes through the process all over again to determine if any of the members is on yet another list, and when it is all finished, they all get a copy of the e-mail message.
We need to add the web e-mail address as a mailing list to the root user so that any e-mails sent to root@localhost actually get sent to a e-mail address somewhere else on the Internet.
You will also notice an associated /etc/aliases.db that is not readable with vi (or emacs if you prefer). This is the file that is actually used by sendmail, but it is encrypted (no, I don’t know why – security?). To compile the /etc/aliases file into the /etc/aliases.db file, issue the command: -
Once this is done, we need to configure sendmail to use our ISP’s SMTP mail server. This is because our server isn’t actually a mail server itself, but will pass the e-mails along to our Service Provider’s mail server for transportation to the wider Internet, hence the need to have an account on this SMTP server. Basically, we’re going to use sendmail here almost like Outlook or Thunderbird, simply passing the e-mails along to another SMTP server for actual transportation :-)
First we need to provide login and server details for the SMTP server at our ISP. Create a new file under /etc/mail called “authinfo”.
and add the following text, substituting values for your actual details where needed: -
AuthInfo:my.smtp.server.net "U:myusername" "I:myusername" "P:mypassword" "M:LOGIN PLAIN".
Like the /etc/aliases file, this needs to be compiled before sendmail will be able to read it. Do so with: -
makemap hash /etc/mail/authinfo < /etc/mail/authinfo
So now we have the e-mail address where root mails will be sent and the login details for the SMTP server which wil be used to send the mails. All that remains is to configure sendmail to pull everything together. Edit /etc/mail/sendmail.mc and add the following lines where indicated: -
dnl # Uncomment and edit the following line if your outgoing mail needs to
dnl # be sent out through an external mail server:
# --- MY MAIL SETTINGS --------------------
We’re using hostname masquerading here, so that sendmail knows that we’re not just pulling a hostname out of our ass here (although we kinda are :-)). If you want to check if your hostname has a valid MX record, issue the command: -
dig mx my.server.org
If you get valid DNS records back, you should be fine.
Remember to replace my.server.org with the hostname of your machine and my.smtp.server.net with the SMTP server name from your ISP. Like the aliases and authinfo file, this file needs to be compiled before sendmail will read it.
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
service sendmail restart
…and you’re done. You can test sending a mail using sendmail using something like: -
echo "Testing" | mail -s "Test" email@example.com
You can check the sendmail system log to see if it reports any errors.
tail -200 /var/log/maillog
Keep in mind that the /var/log/maillog file isn’t actually written to by sendmail itself, it’s written by the rsyslogd daemon. If you’re not getting anything in your mail log, it’s probably because rsyslogd isn’t running. Start it with: -
service rsyslog start
If you do get any errors, try playing around with your authinfo file settings, but the above works for me. It seems by default that sendmail will always try and resolve the SMTP host via it’s MX record. If you just have an internal IP address for your SMTP server (common enough in business networks), you’ll need to add the following to your /etc/hosts file.
…replacing the “xxx.xxx.xxx.xxx” with the IP address of the SMTP mail server and the name to whatever you want. This must be the same hostname you use in the SMART_HOST directive in your “/etc/mail/sendmail.mc” file. If your maillog still isn’t resolving your server, create the file “service.switch” under /etc/mail and add the following lines: -
Restart network and sendmail services with: -
service network restart
service sendmail restart
…and you should be golden.
You might also add separate e-mail addresses for all the users on your Linux system in the /etc/aliases file. It would be nice if each user could assign their own e-mail address without having to get the root user to do it and if I figure out how to do that, I’ll post about it. Have fun!