Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dimitar-grigorov/624c58343a2caca0888dc9136ce36d8e to your computer and use it in GitHub Desktop.
Save dimitar-grigorov/624c58343a2caca0888dc9136ce36d8e to your computer and use it in GitHub Desktop.
Linux forward local user mails to external e-mail address via Postfix

Sources

Linode postfix guide for Gmail

How to rewrite outgoing address in Postfix

In this guide we are using these sample credentials:

  • ourserver.rules.com - for the server we are configuring. FQDN
  • [email protected] - for our e-mail address.
  • mail.awesomedomain.com - for our SMTP server.
  • uLtraSecret#$@PassWord - for SMTP password.

Debian example

Change the priority of asked question during the installation of packages. In the third dialog change priority to low.

sudo dpkg-reconfigure debconf

1. Update the system packages.

sudo apt-get update && sudo apt-get upgrade

2. Install postfix and SASL package

sudo apt-get install libsasl2-modules postfix

  • In the first dialog select Internet Site
  • In the second dialog write our server hostname: ourserver.rules.com
  • In the third dialog don't change anything.
  • In the fourth dialog we can enter our email address ([email protected]), but it is not mandatory.
  • In the next 4-5 dialogs don't change anything.

3. Add alias for root

sudo vim /etc/aliases

# See man 5 aliases for format
postmaster:    root
root:   [email protected]

sudo newaliases

sudo chfn -f 'root at ourserver.rules.com' root

4. Edit the main postfix configuration file /etc/postfix/main.cf

sudo vim /etc/postfix/main.cf

  • Change the row which contains myhostname = to myhostname = ourserver.rules.com
  • Change the row which contains relayhost = to relayhost = [mail.awesomedomain.com]:587
  • Optionally change inet_interfaces to inet_interfaces = 127.0.0.1 to use postfix only localy.
  • Optionally change inet_protocols to inet_protocols = ipv4.
  • At the end of the file append the following:
#Rewrite the sender of every mail
smtp_generic_maps = hash:/etc/postfix/generic

# Enable SASL authentication
smtp_sasl_auth_enable = yes
# Disallow methods that allow anonymous authentication
smtp_sasl_security_options = noanonymous
# Location of sasl_passwd
smtp_sasl_password_maps = hash:/etc/postfix/sasl/sasl_passwd
# Enable STARTTLS encryption
smtp_tls_security_level = encrypt
# Location of CA certificates
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
  • Save and exit

5. Add credentials for the relay host (mail.awesomedomain.com)

sudo vim /etc/postfix/sasl/sasl_passwd

Set the contents in the following format:

[mail.awesomedomain.com]:587 [email protected]:uLtraSecret#$@PassWord

Create the hash db file for Postfix by running the postmap command:

sudo postmap /etc/postfix/sasl/sasl_passwd

Protect the files:

sudo chown root:root /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db
sudo chmod 0600 /etc/postfix/sasl/sasl_passwd /etc/postfix/sasl/sasl_passwd.db

6. Rewrite the sender of every mail send from this domain.

sudo vim /etc/postfix/generic

Set the contents in the following format:

@rules.com            [email protected]

Generate the Postfix DB file. sudo postmap /etc/postfix/generic

7. Restart Postfix

sudo systemctl restart postfix

8. Send test mail.

sendmail root

From: [email protected]
Subject: test
Testing postfix 
.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment