When logged into my server via SSH the $ mail command doesn't work properly. Instead of returning the list of mail it returns No mail for <user>.

$ mail works correctly when on a terminal at the machine, so it's definitely an issue with using SSH, which is how I will need to access it.

  • 1
    What is the ssh command that you're using? – schaiba Jan 9 '17 at 14:28
  • @schaiba, I'm just accessing the server like ssh user@server and then running the mail command at the prompt. $ mail. – Alxs Jan 9 '17 at 14:37
  • 1
    What is the value of $MAIL in each case? – JigglyNaga Jan 9 '17 at 14:42
  • @JigglyNaga, this seems to be the problem. when logged in via SSH (even as regular user) the path is set to /var/spool/mail/root. Via the actual console it is /var/spool/mail/alxs. – Alxs Jan 9 '17 at 14:47
  • 1
    something is incorrectly setting $MAIL, then – Jeff Schaller Jan 9 '17 at 14:48

I'm able to correctly read user mails by giving in input the file path:

mail -f /var/spool/mail/$USER

I also encountered some issue reading (root)user email, I ssh logged in with user phil, then I changed user to root typing sudo su and then issuing mail command I got:

No mail for phil

So it seems that mail command try to read the email of the user with whom you opened the ssh connection. And as it came out the cause is a wrong value of $MAIL variable:

[root@preprod ~]# echo $MAIL
  • Cheers @lese, That's what's happening. It's just a VM so I'm logging in as root and then su to a regular user. So the environment remains set from the original login. Thanks mate! – Alxs Jan 9 '17 at 14:53
  • 1
    You need to use 'su - <user>'. That sets all the user's environment variables correctly – warren Jan 9 '17 at 15:00
  • Yeah sudo - <user> correctly sets the environment variables. I'll get into the habit of doing it this way. – Alxs Jan 9 '17 at 15:04

try using sudo su - the '-' would allow the profile and environment variable to be set for user you are switching to which would then allow the mail command to run as expected.

Note: It will be for all environment variables and not just $MAIL.

  • Thanks Kerbi. I'll get into the habit of doing it this way! Up-voted. :) – Alxs Jan 9 '17 at 15:06

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.