Linux online course

Redirect Local Email with Linux Mail Aliases

Learn how to configure Linux mail aliases to redirect root mail to an administrator, rebuild alias databases, save local mail to a file, and verify delivery.

An email alias is a local mail name that redirects or expands delivery to another recipient or destination. For example, an alias can make mail addressed to the local name root arrive in the mailbox of a local administrative user.

Aliases are primarily intended for mail handled by the local system. They are not a general-purpose replacement for sending arbitrary mail to any remote address. The exact behavior depends on the installed mail transfer agent (MTA), such as Sendmail or Postfix.

Why redirect root mail?

System services, scheduled jobs, monitoring tools, and administrative commands commonly send notices to the root account. These messages may contain failed-job reports, disk warnings, service errors, or other information that requires attention.

Reading root-directed mail through an ordinary administrative account is usually more practical and safer than routinely logging in as root or opening a mailbox as root. In this lesson, the local administrative user is named bob.

Important alias terms

  • Local name: A recipient name handled by the local system's mail configuration.
  • Root mail: Mail addressed to the root account, often containing system and service notifications.
  • Aliases file: The configuration file containing local alias mappings.
  • Aliases database: A generated lookup file used by some MTAs to apply aliases efficiently.
  • Local mailbox file: A file on the system where delivered mail can be stored.

Find the aliases file

The aliases file is commonly located at /etc/aliases or /etc/mail/aliases. The correct path depends on the distribution and the installed mail system.

Aliases file/etc/aliases or /etc/mail/aliases. Use the path configured for the active MTA.

Aliases database rebuild commandnewaliases. Run it after editing when the installed mail system requires a generated database.

Mail transfer agent examples — Sendmail and Postfix. Their configuration and database details can differ.

Before editing a system configuration file, identify which MTA is installed and which aliases path it uses. Edit the file with appropriate privileges, for example through a root shell or a privilege-elevation tool.

Understand alias syntax

The basic structure is a local alias name, a colon, and one or more recipients:

local_alias_name: recipient

The name on the left side is local. The destination on the right can be another local account or, when supported by the mail system, an additional local delivery destination. Some configurations also permit multiple recipients on one line; consult the active MTA's syntax documentation before relying on advanced forms.

Redirect root mail to a local administrator

Add this entry to the active aliases file:

root: bob

The direction of the mapping is important: mail addressed locally to root is delivered to the local user bob. The destination account must exist, be suitable for administrative notifications, and be monitored regularly.

Alias entryroot: bob

Mail addressed toroot

Delivery destination — Local user bob

Purpose — Redirect system notices from root to an administrative mailbox

Rebuild the aliases database

Some MTAs, including Sendmail and many Postfix configurations, do not use the text aliases file directly for every delivery. They use a generated aliases database instead. After changing the aliases file, rebuild that database with:

newaliases

Run the command after each aliases-file change when the installed mail system requires it. If the command is unavailable or the mail service uses a different activation method, check the MTA's local configuration and documentation.

Save a copy in a local mailbox file

An alias can also direct mail to a local file. Use an absolute path so the mail system knows exactly where to place the message. For example:

bob: /home/bob/bobs_mail_backup

This entry stores mail addressed to the local name bob in the file /home/bob/bobs_mail_backup. Depending on the MTA and delivery configuration, this can be used as an additional destination or as the destination for that alias. Verify the syntax supported by the installed mail system.

Alias entrybob: /home/bob/bobs_mail_backup

Mail addressed tobob

Delivery destination — Local mailbox file /home/bob/bobs_mail_backup

Purpose — Preserve a local copy of mail in a file

The target directory must already exist, and the mail delivery process must be able to write to the destination. Check the following before testing:

  • /home/bob exists and is the intended directory.
  • The path is absolute and correctly spelled.
  • Ownership and permissions allow the delivery service to create or append to the file.
  • The file-delivery format is supported by the active MTA.

Example configuration

A simple configuration might contain both mappings:

root: bob
bob: /home/bob/bobs_mail_backup

In this arrangement, locally addressed mail for root is first redirected to bob. Mail delivered to bob can then be stored according to the second alias rule, including in the specified local file when that delivery form is supported. Because alias expansion rules vary, verify the resulting behavior rather than assuming that every MTA chains aliases identically.

Test and verify delivery

Test with a message that is locally addressed to the aliased name. A system with a command-line mail utility might use:

printf '%s\n' 'Alias test' | mail -s 'Root alias test' root

The exact test command depends on which local mail client is installed. The important part is that the recipient is the local name root, not an unrelated remote address.

  1. Send a test message to the local root name.
  2. Check bob's local mailbox for the message.
  3. If file delivery is configured, check /home/bob/bobs_mail_backup.
  4. Inspect the MTA's local delivery logs if the message does not appear.
  5. After correcting an alias, run newaliases again when required and repeat the test.

Troubleshoot alias problems

Alias changes have no effect

  • Rebuild the aliases database with newaliases if the MTA requires it.
  • Confirm that you edited the aliases file used by the active mail service.
  • Check the MTA configuration and local delivery logs for syntax or database errors.

Mail sent to root does not reach bob

  • Verify the entry uses the local-name-colon-recipient form: root: bob.
  • Confirm that bob is the intended local account and can receive mail.
  • Make sure the test message was locally addressed to root.
  • Check for spelling, formatting, or unsupported alias syntax.

The backup file is not created or updated

  • Use the correct absolute path.
  • Create the destination directory if it does not exist.
  • Check ownership and permissions appropriate to the mail delivery process.
  • Confirm that the active MTA supports file destinations in aliases and that the file path is not rejected by its security policy.

Maintain aliases over time

Review aliases whenever administrative responsibilities or user accounts change. If bob leaves the administrator role, update the root mapping promptly so system notifications do not accumulate in an unattended mailbox.

Also review file destinations periodically. A backup mailbox file can grow without limit, consume disk space, or contain sensitive system information. Apply suitable access controls and establish a retention or rotation process.

Exam-relevant summary

  • An email alias is a local mail name that redirects delivery to another recipient or destination.
  • The aliases file is commonly /etc/aliases or /etc/mail/aliases.
  • The basic syntax is local_name: recipient.
  • root: bob sends locally addressed root mail to the local user bob.
  • newaliases rebuilds the aliases database after edits when required by the MTA.
  • A file destination uses an absolute path, such as /home/bob/bobs_mail_backup.
  • Always test local delivery and verify both the recipient mailbox and any configured backup file.

For related Linux administration skills, see Manage File Ownership, File Structure in Linux, and Show the Full Path of Shell Commands.