bijavix blog

Forwarding System Mail to a Central Mail Server

bijavix <[email protected]>

Redirect system-generated email from multiple GNU/Linux hosts to a single central SMTP server. Set-up lightweight relay-only or full-featured MTAs such as nullmailer, msmtp, Postfix, Exim 4, Sendmail or ssmtp.

1. 1. Configure system aliases to redirect root mail to the central mail server

Edit /etc/aliases and set root to your central e-mail address:

#/etc/aliases
mailer-daemon: postmaster
postmaster: root
nobody: root
hostmaster: root
usenet: root
news: root
webmaster: root
www: root
ftp: root
abuse: root
noc: root
security: root
root: [email protected]

Then run:

newaliases

2. 2. Check whether you already have an MTA installed

readlink -f /usr/sbin/sendmail

If this points at a real binary (Postfix, Exim 4, Sendmail, ssmtp, nullmailer, msmtp), you already have an MTA or send-only client.


3. 3. Install a mail client + relay (if none present)

The mail client used in this guide is bsd-mailx for simplicity, but you can use any you prefer.

For the Mail Transfer Agent there are different options, depending on your needs. If unsure, choose one of the first entries and continue:

  • nullmailer - minimal relay-only MTA with disk queue, recommended

  • msmtp - light on-demand send-only SMTP client with no queue

  • postfix - full-feature MTA with queue, filtering and policy

  • exim4 - monolithic MTA with advanced ACL and routing

  • sendmail - heavyweight but classic, not recommended

  • ssmtp - similar to msmtp, deprecated


4. 4. Configuration

4.1. A. nullmailer

apt update
apt install bsd-mailx nullmailer

When installing nullmailer in an interactive frontend, debconf automatically prompts you to set the mailname and smarthost (relay), allowing you to skip the next three steps.

  1. Set your local domain

    echo "mysystem.local" | tee /etc/mailname
  2. Set the relay

    echo "hermes.local smtp" | tee /etc/nullmailer/remotes
  3. Restart

    systemctl restart nullmailer

4.2. B. msmtp

apt update
apt install msmtp-mta bsd-mailx
  1. Create system-wide config at /etc/msmtprc (or /etc/msmtp/msmtprc):

    # Default SMTP relay
    account default
    host hermes.local
    port 25
    # no TLS/auth inside trusted LAN
    tls off
    auth off
    
    # Set envelope-from domain
    from [email protected]
  2. Set permissions so only root can read it:

    chmod 600 /etc/msmtprc
  3. Restart isn’t needed, msmtp runs on demand as /usr/sbin/sendmail.


4.3. C. postfix

apt update
apt install postfix bsd-mailx
  1. Set relayhost

    postconf -e 'relayhost = [hermes.local]:25'
  2. Ensure LAN is trusted in /etc/postfix/main.cf:

    mynetworks = 127.0.0.0/8, 192.168.0.0/16    # adjust to your LAN
  3. Reload

    newaliases
    systemctl reload postfix

4.4. D. Exim 4

apt update
apt install exim4-daemon-light bsd-mailx
  1. Reconfigure

    dpkg-reconfigure exim4-config

    Choose mail sent by smarthost; no local mail and set the smarthost to hermes.local::25.

  2. Restart

    systemctl restart exim4

4.5. E. Sendmail

apt update
apt install sendmail bsd-mailx
  1. Edit /etc/mail/sendmail.mc and add:

    define(`SMART_HOST', `hermes.local')dnl
  2. Regenerate

    m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
  3. Restart

    systemctl restart sendmail

4.6. F. ssmtp (Deprecated - for reference only)

apt update
apt install ssmtp bsd-mailx

Edit /etc/ssmtp/ssmtp.conf:

mailhub=hermes.local:25
AuthMethod=

No daemon to restart; ssmtp hands off on demand.


5. 5. Test end-to-end

Send an e-mail locally, it should automatically be relayed to the central mail server.

echo "Hello from $(hostname)" | mail -s "Test from $(hostname)" root@localhost

On the mail server, watch the log:

tail -f /var/log/mail.log

You should see each client connect and deliver mail successfully.