How to Configure Postfix to Forward Root’s Email to External Address on Ubuntu Server


10 views

When working with Ubuntu servers, many administrators encounter an issue where cron job outputs and system mail intended for root@localhost mysteriously get routed through the domain's primary mail server. The core issue lies in Postfix's configuration interpreting root@mydomain.com as an external address rather than applying local alias rules.

From the mail.log snippet, we can observe the exact routing behavior:

Mar  7 09:39:18 linux1 postfix/smtp[31558]: F3B9C98025E: to=<root@mydomain.com>, 
orig_to=<root>, relay=my.isps.relayhost.com[<IP address omitted>]:25, 
status=sent (250 Ok: queued as A97F5D8126)

The key indicators are:

  • Postfix is rewriting root@localhost to root@mydomain.com
  • The message gets relayed through ISP's SMTP server
  • Local aliases (/etc/aliases) are being ignored

Three configuration files significantly impact this behavior:

1. /etc/postfix/main.cf - The core issue stems from these settings:

myorigin = /etc/mailname
mydestination = linux1.mydomain.com, localhost.linux1.mydomain.com, localhost
relayhost = my.isps.relayhost.com

2. /etc/mailname - Contains just the domain:

mydomain.com

3. /etc/hosts - Has unexpected VM hostname entries:

127.0.0.1 localhost
127.0.1.1 linux1.mylinux.mydomain.com linux1

Step 1: Correct Local Delivery Domain

Modify /etc/postfix/main.cf to ensure proper local delivery:

# Change myorigin to use system hostname
myorigin = $myhostname

# Ensure localhost is properly defined in mydestination
mydestination = $myhostname, localhost.$mydomain, localhost

# Optional: Explicitly define domain for local mail
mydomain = mydomain.com
local_recipient_maps = proxy:unix:passwd.byname $alias_maps

Step 2: Verify and Update Aliases

Ensure /etc/aliases contains:

root: anothermail@anotherdomain.com
postmaster: root

Then rebuild aliases database:

sudo newaliases
sudo postalias /etc/aliases

Step 3: Test Email Routing

Use these commands to verify the configuration:

# Test local delivery
echo "Test Message" | mail -s "Local Delivery Test" root

# Check mail queue
postqueue -p

# Verify logs
tail -f /var/log/mail.log

For more complex forwarding scenarios, create /etc/postfix/virtual:

root@localhost anothermail@anotherdomain.com
root@linux1 anothermail@anotherdomain.com
@localhost anothermail@anotherdomain.com

Then add to main.cf:

virtual_alias_maps = hash:/etc/postfix/virtual

Compile the map:

sudo postmap /etc/postfix/virtual
sudo systemctl reload postfix
  • Always check /var/log/mail.log after configuration changes
  • Use postconf -n to verify active Postfix settings
  • Test with sendmail -v root < /dev/null for detailed delivery info
  • Consider SELinux/apparmor permissions if issues persist

Many sysadmins face this exact issue where cron jobs send root's mail to an unexpected external address instead of handling it locally or forwarding to a designated administrator email. Let's examine why this happens and how to properly configure Postfix.

From your mail.log, we can see the mail flow:

Mar  7 09:39:17 linux1 postfix/pickup[31381]: F3B9C98025E: uid=1000 from=
Mar  7 09:39:18 linux1 postfix/smtp[31558]: F3B9C98025E: to=

The key observation here is that Postfix is rewriting root to root@mydomain.com and sending it externally. This occurs because of your myorigin setting combined with the relayhost configuration.

First, let's modify your /etc/postfix/main.cf:

# Add local domain to mydestination
mydestination = linux1.mydomain.com, localhost.linux1.mydomain.com, localhost, $myhostname

# Ensure proper local delivery
local_recipient_maps = proxy:unix:passwd.byname $alias_maps

Your /etc/aliases is actually correct:

root: anothermail@anotherdomain.com
postmaster:    root

But you need to ensure Postfix processes these aliases by:

sudo newaliases
sudo systemctl restart postfix

Verify with these commands:

# Test local delivery
echo "Test local mail" | mail -s "Local Test" root

# Check mail queue
postqueue -p

# Verify aliases
postmap -q root hash:/etc/aliases

If aliases don't work, create /root/.forward:

anothermail@anotherdomain.com

Then set permissions:

chmod 644 /root/.forward
chown root:root /root/.forward

1. Verify mydestination includes local domains
2. Confirm alias_maps points to /etc/aliases
3. Check local_recipient_maps includes aliases
4. Ensure proper file permissions