System-generated emails addressed to root are crucial for server monitoring, but accessing them directly through the local mail spool isn't practical. Here's how to properly redirect them to your external inbox.
Before proceeding, you'll need:
- An Ubuntu server (tested on 20.04/22.04 LTS)
- Working internet connection
- Sudo privileges
- An external email account that accepts forwarded mail
1. Install Minimal Mail Transfer Agent
We recommend postfix for its reliability and simplicity:
sudo apt update sudo apt install postfix mailutils
During installation:
- Select "Internet Site"
- Enter your domain name or server hostname
2. Configure .forward File
Create or edit the forward file for root:
sudo mkdir -p /root/.forward echo "your.external@email.com" | sudo tee /root/.forward sudo chown root:root /root/.forward sudo chmod 644 /root/.forward
3. Verify Postfix Configuration
Ensure these settings exist in /etc/postfix/main.cf:
myhostname = your.server.hostname mydestination = $myhostname, localhost.localdomain, localhost relayhost = inet_interfaces = loopback-only
Apply changes:
sudo systemctl restart postfix
4. Test the Setup
Send a test email from root:
echo "Test message" | mail -s "Root Forward Test" root
Check your external inbox (including spam folder) for the test message.
Multiple Recipients
Edit /root/.forward to include multiple addresses:
admin1@domain.com, admin2@domain.com
Mail Filtering with Procmail
For advanced filtering before forwarding:
sudo apt install procmail
Then modify .forward to include:
"|/usr/bin/procmail"
Mail Not Being Delivered
Check mail logs:
sudo tail -f /var/log/mail.log
Postfix Relay Issues
If using external SMTP, configure relayhost in main.cf:
relayhost = [smtp.gmail.com]:587
Add authentication in /etc/postfix/sasl_passwd:
[smtp.gmail.com]:587 username:password
Permission Problems
Ensure correct permissions:
sudo chmod 600 /root/.forward sudo chown root:root /root/.forward
For systems without full mail server:
sudo nano /etc/aliases
Add line:
root: external@email.com
Update aliases database:
sudo newaliases
System-generated emails for the root user (cron jobs, sudo alerts, etc.) typically get stuck in /var/mail/root
on Ubuntu servers. Here's how to properly redirect them to an external mailbox.
# Install Postfix
sudo apt update
sudo apt install postfix mailutils
# During installation select:
# Internet Site → Enter your domain name
# Then configure aliases:
sudo nano /etc/aliases
# Add these lines:
root: your-external@email.com
default: your-external@email.com
# Update aliases and restart Postfix
sudo newaliases
sudo systemctl restart postfix
For minimal setups without a full mail server:
# Install SSMTP
sudo apt install ssmtp
# Configure SSMTP
sudo nano /etc/ssmtp/ssmtp.conf
# Add these parameters:
root=your-external@email.com
mailhub=smtp.yourprovider.com:587
AuthUser=your-smtp-username
AuthPass=your-smtp-password
UseSTARTTLS=YES
# Then configure the revaliases file:
sudo nano /etc/ssmtp/revaliases
# Add:
root:your-external@email.com:smtp.yourprovider.com:587
Verify with a test email:
echo "Test message" | mail -s "Server Alert" root
- Check mail logs:
tail -f /var/log/mail.log
- Test SMTP connectivity:
telnet smtp.yourprovider.com 587
- Ensure port 25/587 isn't blocked (common in cloud environments)
When forwarding sensitive system emails:
- Use TLS encryption in SMTP configuration
- Consider using dedicated SMTP credentials
- Set up SPF/DKIM records for your domain