Sendmail Aliases for Fun and Profit
October 2, 2010 - 12:59pm EDT
Have you ever set up a Linux server to run some simple tasks (ie: serving static content) and come back months later to find several massive files in /var/mail/ using up all the disk space? It’s happened to me quite often.
Many Linux utilities -- like our old friend cron -- periodically send email messages to local users on the system if something goes wrong. However, with the magic of sendmail aliases, we can forward these messages from local users to remote email accounts.
First, the fun
Assuming you’ve already installed and set up sendmail, we will want to edit the raw aliases file found at /etc/aliases. In my case, I have my normal user forwarded to one address and the root user forwarded to a separate address:
ekaufman: me@mydomain.com
root: www-admin@mydomain.com
Note that you can be more creative than just an email address! You can write directly to a local file, pipe to a command, or even include aliases from a separate file elsewhere on the system. See this man page for the nitty gritty details.
Once we’ve made our changes to the raw aliases file, we need to rebuild the binary database file that sendmail actually uses:
sudo /usr/sbin/newaliases
Now the profit
With the aliases set up, you can enjoy an empty /var/mail directory and a full inbox!
$ ls -al /var/mail/
total 8
drwxrwsrwt 2 root mail 4096 2010-10-02 19:36 .
drwxr-xr-x 15 root root 4096 2010-10-02 10:31 ..
-rw-rw---- 1 ekaufman mail 0 2010-10-02 09:58 ekaufman
-rw------- 1 mail mail 0 2010-10-02 19:36 mail
-rw------- 1 root mail 0 2010-10-02 19:21 root