Learn how to masquerade the domain in Sendmail. Our Sendmail Support team is here to answer your queries and concerns.
How to Masquerade Domain in Sendmail
Most of our customers prefer to hide their internal usernames and homenames when sending emails from a server.
For example, instead of showing the sender as `bobishere@server01.bobcares.com`, we may prefer it to appear as `bob@bobcares.com`. This is especially useful when we have a centralized mail server (mail hub) or need better control over email identity and domain consistency.
This technique is called email masquerading. It rewrites the hostname or full email address in outgoing mail headers.
Want to secure your Sendmail setup with authentication? Check out Sendmail AUTH_OPTIONS explained.
Today, we will examine how to configure masquerading in Sendmail using two approaches
Basic Domain Masquerading in Sendmail
To mask the internal hostname and make emails appear from our preferred domain, follow these steps:
- First, edit the Sendmail configuration file:
vi /etc/mail/sendmail.mc
- Then, add or modify the following lines:
MASQUERADE_AS(`bobcares.com')dnl
FEATURE(`masquerade_envelope')dnl
FEATURE(`masquerade_entire_domain')dnl
MASQUERADE_DOMAIN(`bobcares.com')dnl
Remember to replace bobcares.com with your actual domain name.
- Now, rebuild the configuration and restart Sendmail:
m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
/etc/init.d/sendmail restart
This will rewrite the domain part of the email but keeps the local username intact (e.g., bobishere@server01.bobcares.com).
If you’re setting up relays or allowing specific IPs to send mail, don’t miss our guide on Sendmail allow relay from IP.
Advanced Masquerading with Username Rewriting
If we also want to change the username in outgoing emails (e.g., change `root@server01.bobcares.com` to `no-reply@bobcares.net`), we will need to use the `generic stable` feature.
- First, comment out existing masquerade settings in `sendmail.mc`:
grep 'MASQUERADE' /etc/mail/sendmail.mc
Also, make sure the output lines begin with `dnl`, indicating they’re commented:
dnl # MASQUERADE_AS(`target-domain.net')dnl
dnl MASQUERADE_DOMAIN(localhost)dnl
dnl MASQUERADE_DOMAIN(domain-to-masquerade.net)dnl
- Then, enable the `domaintable` feature in `sendmail.mc`:
FEATURE(`domaintable')
- Next, create `/etc/mail/domaintable`:
domain-to-masquerade.net target-domain.net
- At this point, we have to enable and configure `genericstable` in `sendmail.mc`:
FEATURE(`genericstable', `hash -o /etc/mail/genericstable.db')dnl
GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')dnl
- Then, edit `/etc/mail/generics-domains`:
target-domain.net
- Now, create `/etc/mail/genericstable` with user mappings:
root no-reply@target-domain.net
other-user1 no-reply@target-domain.net
other-user2 no-reply@target-domain.net
- Next, compile the configuration:
cd /etc/mail
make
If you see the following error:
WARNING: ‘sendmail.mc’ is modified. Please install package sendmail-cf to update your configuration.
Install the missing package:
yum install sendmail-cf
cd /etc/mail
make
- Now, restart the Sendmail service:
service sendmail restart
Now we will see the following:
Shutting down sm-client: [ OK ]
Shutting down sendmail: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
Verifying the Configuration
We can run the following test to confirm if masquerading is working:
#hostname
domain-to-masquerade.net
echo "This is a test mail to verify the sendmail sender masquerade." | mailx -s "Test Sender Masquerade" name@domain.com
Ensure that the domain (`target-domain.net`) is a fully qualified domain name (FQDN) and resolvable by authoritative DNS servers. Otherwise, our emails might be rejected or marked as spam.
Want to automate emails with file attachments? Here’s how to use the Sendmail command with attachment in shell script.
For better email deliverability and authenticity, consider setting up DKIM for Sendmail. Here’s our full guide on configuring Sendmail DKIM on Ubuntu.
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
Masquerading in Sendmail is a great way to control how our emails appear to the outside world. Whether we need to hide internal infrastructure or ensure consistent branding across all emails, this feature is essential for professional mail server management.
In brief, our Support Experts demonstrated how to masquerade the domain in Sendmail.
0 Comments