BSD How to Forward Root’s Mail to a Another User in FreeBSD

0 Comments

After setting up a FreeBSD system I can sometimes go long stretches without becoming the superuser again. When I finally do issue the su command I’m usually presented with the line “You have mail.” I sigh and enter the command mail, where I’m presented with a multitude of system-generated mail updating me on everything from the status of cron jobs that have run (or not) to security and daily run reports. It’s at this point that I think to myself “I really need to forward that mail to my inbox so I read them more often.” This post will describe how to setup FreeBSD so that the root user’s mail is forwarded to another user account.

The versions for the software used in this post are as follows:

  • FreeBSD 11.0-RELEASE
  • To forward the root user’s mail to another users’ inbox open the file /etc/aliases as root and look for the following line:

    Uncomment this line and replace “me@my.domain” with the user name you’d like the mail forwarded to. For example, to forward root’s mail to the user iceflatline on the same host, the line would look like this:

    Now we need to make the sendmail (sendmail is the default Mail Transfer Agent in FreeBSD) aware of the changes we’ve made to /etc/aliases before they’ll take effect: We’ll do that by using the newaliases command as root:

    Now any mail the system (or anyone else) sends to root will also be forwarded to the user iceflatline’s mail inbox. To test whether mail is indeed being forwarded use the mail command to send root a message. Start by specifying a subject, then hit enter and type the message. When complete, hit enter enter again to place the cursor at the beginning of a blank line, then enter CTRL+D to send the message:

    Now enter the mail command at the user prompt and you should see the message you just sent to the root user:

    Now, this approach works just fine, so long as I remember to enter the mail command occasionally. But what would be really cool is if I could be notified if mail is available to read when I login to this host via ssh . Fortunately, there is a way we can do this. Open the file /etc/ssh/sshd_config as root and uncomment the UseLogin option and change its value to yes:

    Now restart sshd as root so that the change takes effect:

    Now the next time I login via ssh I’m presented with the message “You have new mail” if root receives mail from the system (or anyone else) in the intervening time.

    Conclusion

    Well, that’s it. A couple of tweaks on FreeBSD and you’ll have no excuse for missing those system reports.

    References

    http://www.openssh.org/manual.html

    Tags: ,

    Leave a Reply