Masquerading domains with Sendmail

From TykWiki
Jump to navigationJump to search

This article outlines the method I used to get sendmail to rewrite the sender domain, in order to get around the classic problem where a server has a local hostname and has to deliver mail to a real-world recipient domain. The server in this example is called and all mail for root is forwarded to my real-world reachable email address That means that I receive mail from the not-resolvable sender address which can be a problem if the receiving server uses anti-spam measures, like checking if the sender domain is real.

I only use mail from the server for system messages from crontab and the like, but it could easily be a server running some sort of application that sends mail through sendmail.

Configuring Masquerading

Sendmail calls the rewriting of the sender domain to something other than the machines hostname masquerading. You cannot just write a couple of lines in /etc/mail/ which is the main sendmail configuration file. To know why you just have to look inside the file, the syntax is so retarded you have to wonder who thought it up. Anyway, masquerading is something you have to configure in a .mc file, then you compile a new which you copy to /etc/mail/ and overwrite the existing one.

Make a new .mc file

First make a copy of the default file by running the following commands:

$ sudo cp /usr/src/etc/sendmail/ /usr/src/etc/sendmail/

'Note: If you already have a non-standard .mc file that your current file is built from, you need to add the new line to that .mc file and not a copy of the default .mc file, or you will loose your other changes!

It doesn't matter what you call the new .mc file. Open it up in an editor and add the following lines in the bottom (replacing with your own domain, of course):


This will make the following changes to sendmail behaviour:

  1. MASQUERADE_AS will rewrite the sender domain if it is the same as the servers hostname: becomes
  2. masquerade_envelope will rewrite the envelope sender as well as the header. This is required for my purposes at least.
  3. masquerade_entire_domain will rewrite the sender domain to if it is a subdomain of the servers hostname: also becomes This can be left out if you have no subdomains of the hostname of the server.

You are now ready to compile a new configuration file, using the .mc file you just created as a basis.

Compiling the new file

To compile the new configuration file, enter the following command while still in /usr/src/etc/sendmail:

$ pwd
$ sudo make
rm -f
m4 -D_CF_DIR_=/usr/src/etc/sendmail/../../contrib/sendmail/cf/   /usr/src/etc/sendmail/../../contrib/sendmail/cf/m4/cf.m4 /usr/src/etc/sendmail/ >
chmod 444

There should be a new file in the current directory called that you can now copy to /etc/mail/ (backup the exising one before you overwrite it). Then restart sendmail and try sending a mail:

$ sudo cp /etc/mail/ /etc/mail/
$ sudo cp /usr/src/etc/sendmail/ /etc/mail/
$ sudo /etc/rc.d/sendmail restart
Stopping sendmail_submit.
$ echo "testing 1 2 3" | mail -s "testing sender masquerading in sendmail"

Obviously you need to replace my email address with your own. Then go see if you received a mail, and if not, check /var/log/maillog to see what's going on.

--Tykling 15:20, 7 July 2009 (UTC)