I just sent a local mail using the following syntax:

echo "Testing" | mail -s "This is just a test" root@localhost

Then I log into root using:

sudo -i

And check my mail. The message is there. If I repeat the mail command and log into root using:

su root

The mail is not in my mailbox. In both cases I am logging into the root account, so what is happening here? It's a fairly new, basic installation of CentOS 7.

I even checked again by logging in using su and when it wasn't there I logged out and back in using sudo -i and sure enough, the mail was there.

  • What happens with su - root? (Note the - argument) – Andrew Henle Apr 25 '17 at 22:26

The cause of this behavior is the MAIL environment variable. When you switch to root with

sudo -i

you simulate an intial login (-i) and the MAIL environment variable is set to /var/spool/mail/root. When you switch to root with

su root

The MAILenvironment variable stay to its old value i.e. /var/spool/mail/jo and the command mail check the mailbox of the user and not the mailbox of root. This why you see it empty.

  • Brilliant answer. You got it spot on. I tried logging into root with sudo -i, then echo $MAIL gives /var/spool/mail/root. Then I try the other way with su root and I get /var/spool/mail/user. Perfect answer. Thanks. – bitofagoob Apr 25 '17 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.