
Introduction
Sendmail is one of the most widely used mail transfer agents (MTAs) on Linux. It facilitates the sending and forwarding of emails across networks efficiently. Setting up mail forwarding in Sendmail on Linux is essential for ensuring emails reach the right recipients, even when they change their email addresses or need multiple destinations. In this guide, we will cover multiple methods to configure mail forwarding in Sendmail, including the use of .forward files, the /etc/aliases file, and mail relay configurations.
Prerequisites
Before proceeding, ensure that:
- You have root or sudo privileges.
- Sendmail is installed and running.
- You have access to configuration files.
- You understand basic command-line operations.
To check if Sendmail is installed, run:
sendmail -VIf Sendmail is not installed, you can install on Debian based system like Ubuntu, use the following command
sudo apt update && sudo apt install sendmailyou can install on RHEL based system like CentOS, Oracle Linux, Alma Linux, use the following command
sudo yum install sendmail sendmail-cf Method 1: Using the .forward File
The simplest way to forward emails for a specific user is by using a .forward file in their home directory.
Steps to Configure the .forward File
- Access the user’s home directory:
cd /home/username2. Create or edit the .forward file:
nano .forward3. Enter the forwarding email address: You can add multiple addresses separated by commas.
user@example.com, user1@example.com, usman@a2zeducate.com4. Save and exit: Press CTRL + X, then Y, and Enter.
5. Verify the forwarding configuration: Send a test email and check if it is received at the destination.
Method 2: Using the /etc/aliases File
For system-wide forwarding, you can use the /etc/aliases file
Steps to Configure the /etc/aliases File
- Edit the aliases file:
sudo nano /etc/aliases2. Add a forwarding rule: Replace localuser with the Linux username.
localuser: user@example.com3. Save and exit.
4. Update the alias database:
sudo newaliases5. Restart Sendmail for changes to take effect:
sudo systemctl restart sendmail6. Test email forwarding.
Method 3: Configuring Sendmail for Mail Relay
If you need to forward all outgoing emails through another SMTP server, configure mail relay settings.
Steps to Configure Mail Relay in Sendmail
- Edit Sendmail’s configuration file:
sudo nano /etc/mail/sendmail.mc2. Find the following line:
dnl define(`SMART_HOST', `smtp.yourprovider.com')3. Uncomment and modify it to your relay server:
define(`SMART_HOST', `smtp.relayserver.com')4. Regenerate Sendmail’s configuration:
sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf5. Restart Sendmail:
sudo systemctl restart sendmailTesting Mail Forwarding in Sendmail
After configuring forwarding, test it by sending an email from another system:
echo "Test email" | mail -s "Forwarding Test" username@yourdomain.comAlternative Mail Forwarding Solutions
you can use the following alternative solutions to forward mails.
- Postfix
- Exim
- Procmail
Setting up mail forwarding in Sendmail on Linux is straightforward using .forward files for user-specific forwarding, /etc/aliases for system-wide forwarding, or mail relay for sending emails through an external SMTP server. By following this guide, you can efficiently manage email forwarding and ensure smooth mail delivery.
1. Can I forward emails to multiple addresses using Sendmail?
Yes, you can list multiple addresses separated by commas in the .forward file.
2. How can I verify if my emails are being forwarded?
Send a test email and check if it arrives at the target email address.
3. What permissions should the .forward file have?
The .forward file should have 644 permissions.
Can I forward emails without keeping a copy in my inbox?
Yes, simply omit your own email address from the .forward file.