Command line 'mail' does not work for root but does for user

Ubuntu Version:
Ubuntu Server 22.04.5 LTS (GNU/Linux 5.15.0-160-generic x86_64)

Problem Description:
Command line ‘mail’ does not work for root. I have a back up script that runs as sudo under a user we will call ‘user’ for now. I noticed after some updates a few days back it quite sending the email to report on the backup. After a little testing I found I can still send mail fine as ‘user’ but as soon as I switch to root with ‘sudo su’ it no longer works. It had worked fine up to a few days ago.

I have included the output of the mail commands here.

root@servername:/home/username# mail --debug-level=3 -s "Backup finished successfully." myemail@domain.com <<< "test mail"
mail: sendmail binary: /usr/sbin/sendmail
mail: source=system, name=root, passwd=x, uid=0, gid=0, gecos=root, dir=/root, shell=/bin/bash, mailbox=/var/mail/root, quota=0, change_uid=1
mail: source=system, name=root, passwd=x, uid=0, gid=0, gecos=root, dir=/root, shell=/bin/bash, mailbox=/var/mail/root, quota=0, change_uid=1
mail: mu_mailer_send_message(): using From: root@servername
mail: Sending headers...
mail: Sending body...
mail: /usr/sbin/sendmail exited with: 1
mail: progmailer error: Process exited with a non-zero status
mail: cannot send message: Process exited with a non-zero status
mail: source=system, name=root, passwd=x, uid=0, gid=0, gecos=root, dir=/root, shell=/bin/bash, mailbox=/var/mail/root, quota=0, change_uid=1
root@servername:/home/username# exit
exit
username@servername:~$ mail --debug-level=3 -s "Backup finished successfully." myemail@domain.com <<< "test mail"
mail: sendmail binary: /usr/sbin/sendmail
mail: source=system, name=username, passwd=x, uid=1000, gid=1000, gecos=username, dir=/home/username, shell=/bin/bash, mailbox=/var/mail/username, quota=0, change_uid=1
mail: source=system, name=username, passwd=x, uid=1000, gid=1000, gecos=username, dir=/home/username, shell=/bin/bash, mailbox=/var/mail/username, quota=0, change_uid=1
mail: mu_mailer_send_message(): using From: username@servername
mail: Sending headers...
mail: Sending body...
mail: /usr/sbin/sendmail exited with: 0

This has not changed but I originally set up command line email by editing the file /etc/ssmtp/ssmtp.conf.


username@servername:~$ sudo cat /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster

# The place where the mail goes. The actual machine name is required no 
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
#hostname=servername

# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
#FromLineOverride=YES

# The user that gets all the mails (UID < 1000, usually the admin)
root=myemail@domain.com

# The mail server (where the mail is sent to), both port 465 or 587 should be acceptable
# See also http://mail.google.com/support/bin/answer.py?answer=78799
mailhub=smtp.gmail.com:587

# The address where the mail appears to come from for user authentication.
rewriteDomain=gmail.com

# The full hostname
hostname=localhost

# Use SSL/TLS before starting negotiation
UseTLS=Yes
UseSTARTTLS=Yes

# Username/Password
AuthUser=username@gmail.com
AuthPass=something

# Email 'From header's can override the default domain?
FromLineOverride=yes

I saw the same thing as you did and about the same time as you did. That’s how I found your question, pulling my hair out trying figure out how I could send email as sudo and/or from the root account. I’m using SSMTP and it turns out that the recent Linux update is only enforcing an SSMTP rule that has been present all along. This is what worked for me using SSMTP, I added the following line to the revaliases file:

root:{my email addy}@gmail.com:{my mailhub} smtp.gmail.com:{my mailhub port}587

don’t forget the colons that separate the … email addy:mailhub:port

I don’t know if sendmail uses a revaliases file, but if it does, I hope this works for you.

Thank you so much @dewat. I think that did it.

I edited the revaliases file and ran my test from sudo su again and the mail command exited with zero and I got the email!

I found the revaliases file at /etc/ssmtp/revaliases. Google AI said " This file is used by the sSMTP mail client to create reverse aliases, which define a local system username and an external email address and server to send mail through."

I do not follow why that is needed now and was not before but it seems to work. The final test will be when my script runs tonight.

My updated /etc/ssmtp/revaliases file looks like this:

root@servername:/etc/ssmtp# cat revaliases 
# sSMTP aliases
# 
# Format:       local_account:outgoing_address:mailhub
#
# Example: root:your_login@your.domain:mailhub.your.domain[:port]
# where [:port] is an optional port number that defaults to 25.
root:username@gmail.com:smtp.gmail.com:587

To be clear the account username@gmail.com is the google mail account I am using to send the emails not who the email is being sent to. It also matches all the information I already set in the /etc/ssmtp/ssmtp.conf file (I would assume it needs to match).

Also, I am just using username@gmail.com as an example. It is not my real email address. :slight_smile:

I hope this helps someone else. If anyone want to reply with more details of why this works or why it is just now (Mid October 2025) needed please feel free. Thanks

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.