Sendmail masquerade outgoing email address

If you just need an outgoing email from web server use sendmail. But what is masquerading?

With the help of masquerading your outgoing email appers from instead of This will also hide your internal user name or host name from rest of the world.

So this feature rewrites the hostname in the address of outgoing mail. This is also used when you have centralized mail server i.e. mail hub.

Sendmail masquerading configuration

Open your sendmail config file /etc/mail/
# vi /etc/mail/
Append/add/modify the lines as follows:

Save and close the file. Replace domain name with your actual domain name. Update and restat sendmail server:
# m4 /etc/mail/ > /etc/mail/
# /etc/init.d/sendmail restart

Further reading

=> Masquerading and relaying

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 21 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
21 comments… add one
  • didier Nov 16, 2006 @ 23:14

    Nice. So, this explains how to ‘hide’ the domain name. But what about the user name ? With the changes you describe, it still shows our real unix username. Am I missing something here ?

    thanks for your help.

  • 🐧 nixCraft Nov 17, 2006 @ 13:01

    Do you have virtual UNIX user name mapping like: drealuser


  • didier Nov 20, 2006 @ 20:17

    Hi nixcraft,

    No I haven’t set this up. To be fully honest, I am not familiar with virtual unix user name mapping. Can you explain a little bit more.

    I did try to use the virtusertable file but it didn’t to work for outgoing email. The problem I am trying to solve is EXACTLY the on you mention in your post: I have a machine (prod1) and a user (prod) and I want to send emails as (I am just interested in outgoing email… and no, I am not a spammer 🙂

    The masquerade works for the domain name, but for the user, I ended up having to create a unix user ‘invitation’ on my box and sudo to invitation before sending the emails… It works fine but I am sure there is a better/simpler way…

    Thanks for your reply btw.

  • ifti Feb 16, 2009 @ 5:58

    i m facing the same problem as mentioned by didier , actually i have set a linux monitoring server which monitors network devices,if any device is down it sends emails on our local domain using sendmail, the outgoing mails are sent from this server like root@localhos.localdomain i want to masquerade it to say, nix and didier any help will be highly appreciable….

  • Mrmartin Feb 17, 2010 @ 18:36

    Great, simple article, how would the masquerading work if I’m using Apache virtual hosts ie more than 1 domain on the server. If a php app from one virtual host sends mail using the PHP mail() function how do I set which from address is used?


  • Shai May 11, 2010 @ 7:19

    Make sure that you have placed the above 4 lines above the MAILER lines at the end or you’ll get an error when issuing the m4 cmd from cli

  • josh from america May 11, 2010 @ 19:01

    sendmail totally ignores all changes related to masquerade, also know as:

    “does not work”, or “tutorial non- helpfull”

  • J James May 18, 2010 @ 20:24

    One more thing needs to done, if sending mail as root
    #C{E}root – comment out

  • Anon Jul 7, 2010 @ 10:11

    To allow root to masquerade (eg: for logwatch) in change:



    dnl EXPOSED_USER(`root’)dnl

    This command bypasses masquerading for the root user, which makes it very confusing when testing this from the root account.

    • grateful Jan 12, 2017 @ 19:36

      Thank you for mentioning this. Was having issues with emails getting sent out as the root user but masquerading wasn’t working.

  • vmunix Aug 12, 2010 @ 17:21

    The article does not mention how to masquerade the username.

  • ipkwena Sep 14, 2010 @ 19:11

    Thanks for the article. Now I have managed to have my Linux box send out emails. However, I have not resolved masquerading the user name. There must be a tutorial somewhere.


  • Salam Nov 7, 2010 @ 19:45

    I followed all instructions, added the 4 lines, added an alias to my root in usersgeeneric table

    Note : I already have the following 4 lines in my
    dnl MASQUERADE_DOMAIN(localhost)dnl
    dnl MASQUERADE_DOMAIN(localhost.localdomain)dnl
    dnl MASQUERADE_DOMAIN(mydomain.lan)dnl

    which I didnt touch.
    Whenever I issue the following command
    echo “hello worls” | mail -s “Firstmail”

    I get right away an email for root indicating :

    The following addresses had permanent fatal errors —–

    (reason: 550 5.1.8 your MAIL FROM is refused for this recipient : 553 sorry, your envelope sender domain must exist (#5.7.1))

  • Josh from America Apr 6, 2011 @ 20:08

    This absolutely does not work, in any capacity whatsoever.

  • jeffrey smith Jun 21, 2011 @ 15:59

    I managed to get this working by doing the following.

    change you /etc/hosts file so the first entry with a . for 127.0.01 is the domain your trying to masquerade i.e server1 server1.domain.local

    Then chnage the entries so they are.


    As stated above this only works for non root users.

    • Habib Mumin Jan 2, 2012 @ 18:21

      To allow user root to send email and use masquerade domain, in the line beginning


      remove word ‘root’ and you have


      restart sendmail as per above instructions.

      There is no need to edit hosts file.

  • Sandeep Feb 3, 2012 @ 6:35

    Hi Vivek,
    Small query about out going mails.
    I would like to add some disclaimer message for every outgoing mail form our mail server. Can you please suggest me the best way to do this?


  • RCRON Jul 27, 2012 @ 0:06


    Thank you all for your comments and info… my problem was the EXPOSED_USER for ROOT that was causing me all the grief since i was testing that way…

    Thank you again

  • karan Dec 26, 2012 @ 13:26


    I have configured postfix mail server whose hostname is “” but registered domain is “” [supposed] so while i send mail it returns error

    Diagnostic-Code: smtp; 553 5.1.8 … Domain of
    sender address does not exist

    so how can i change sender domain address, it will be appreciated if i can change it without touching postfix major configuration files.


  • Vijay May 28, 2013 @ 19:59


    My expectation:

    FROM: , but what it returns is,

    How to suppress the hostname, and get the actual output that what am passing?

  • Prayag Apr 21, 2017 @ 10:18

    1)Can anyone tell me how to configure for incoming mail.
    2) Is it possible to configure sendmail for incoming mail ?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum