Linux Server setup

Set up a mail server for email forwarding

In this chapter we configure Postfix to receive email for our domain and forward it to existing external email addresses. No mailboxes, IMAP server or POP3 server are needed for this setup.

Now the DNS records from the first chapter comes into play. There we had set the mail subdomain under MX Records. Also a Reverse DNS over the address srv1.linuxservrsetup.com.

Reverse DNS resolves our server IP address to its hostname. The hostname should resolve back to the same IP address and match the name Postfix uses when connecting to other mail servers. This is one of several checks receiving servers use.

The example domain linuxservrsetup.com is deliberately misspelled. Replace every example domain, hostname, IP address and email address with your own values before using the commands.


Install Postfix

The mail server software used is Postfix. This mail transfer agent was developed in the late nineties and is still one of the most used mail servers. Postfix is one of the "easy" mail servers to administer.

Installation

Let's install the Postfix software as usual with apt:


__$ sudo apt install -y postfix dnsutils
 

If a configuration dialog appears, choose the following values using your own hostname. If Postfix is already installed, you can inspect and adjust the settings directly as described below. The screenshots illustrate the dialog:

  • General type of mail configuration: Internet Site
  • System mail name: srv1.linuxservrsetup.com
Graphical installation routine of Postfix
Postfix setup
General type of mail configuration
System mail name

Postfix configuration files

After installation, all Postfix configuration files are located under /etc/postfix:


__$ sudo ls -l /etc/postfix
 

The exact file list depends on the installed package. The important files for the following steps are main.cf and the virtual table we will create.

master.cf defines the Postfix services and stays unchanged here. We configure forwarding in main.cf and the virtual table. First, check the service and the DNS records.

Postfix status

Let's check if the installation was successful and the Postfix service is running properly:


__$ sudo systemctl status postfix
 

Check for reported errors. An active service state alone does not prove that email forwarding works; we will test delivery after configuring it.

DNS MX (Mail Exchange) Resource Record

Use dig to check the MX record, the IPv4 addresses of the MX target and SMTP hostname, and the reverse DNS entry. Here 192.0.2.10 is only an example IP address:


__$ dig +short MX linuxservrsetup.com
__$ dig +short A mail.linuxservrsetup.com
__$ dig +short A srv1.linuxservrsetup.com
__$ dig +short -x 192.0.2.10
 

Output:


10 mail.linuxservrsetup.com.
192.0.2.10
192.0.2.10
srv1.linuxservrsetup.com.

The first result is the MX target, followed by its IP address, the SMTP hostname address and the PTR hostname. For this example, both hostnames point to the same server IP and the PTR record names srv1.linuxservrsetup.com. Set the real DNS records with your DNS provider and the PTR record with your server provider.

The MX target must resolve to this server. Changing Postfix settings does not create or update DNS records.

DNS Reverse Hostname

Postfix uses myhostname as its own hostname and, through smtp_helo_name = $myhostname, for outgoing SMTP connections. This setting does not change the sender shown in forwarded messages. Open the configuration file:


__$ sudo nano /etc/postfix/main.cf
 

The following excerpt shows the relevant initial settings. In this starting configuration, myhostname = localhost and inet_interfaces = loopback-only still prevent the intended public mail service. Keep the other package defaults; the excerpts here are not complete replacement files.

Excerpt from /etc/postfix/main.cf


compatibility_level = 3.9
myorigin = /etc/mailname
inet_protocols = all
mynetworks_style = host
mynetworks = 127.0.0.0/8 [::1]/128
mydestination = $myhostname, localhost.$mydomain, localhost
mailbox_command =
recipient_delimiter = +
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination
myhostname = localhost
inet_interfaces = loopback-only
smtp_helo_name = $myhostname

Change the existing hostname and interface settings as follows. The example uses IPv4; enable IPv6 only after configuring its connectivity and DNS, including reverse DNS. Do not add a second copy of settings that already exist.


myhostname = srv1.linuxservrsetup.com
inet_interfaces = all
inet_protocols = ipv4

Keep compatibility_level = 3.9, mailbox_command =, the relay restrictions and the TLS settings from the initial configuration. Pure forwarding to external addresses does not need Procmail or local mailboxes.

Set the name used for locally generated mail in /etc/mailname as well:


__$ sudo nano /etc/mailname


srv1.linuxservrsetup.com

Configure virtual email aliases

Since we are not creating mailboxes, but only receiving and forwarding email, we need a file that lists these mappings. In the folder /etc/postfix we put a file named virtual:


__$ sudo nano /etc/postfix/virtual
 

Enter the incoming address on the left and the existing external destination address on the right. Replace both sides with your own addresses; mail@example.com is only a placeholder. No local user account is needed for tom or jerry:

/etc/postfix/virtual


# Forwarding Addresses (from -> to)
tom@linuxservrsetup.com mail@example.com
jerry@linuxservrsetup.com mail@example.com
 

The mail server should receive emails for tom@linuxservrsetup.com and jerry@linuxservrsetup.com and forward them to mail@example.com. The first line starts with a hash (#) and is just a comment.

The notation for multiple recipients would be the following:

/etc/postfix/virtual


# Forwarding Addresses (from -> to)
tom@linuxservrsetup.com mail-1@example.com, mail-2@example.com
jerry@linuxservrsetup.com mail@example.com
 

An email to tom@linuxservrsetup.com would thus be forwarded to mail-1@example.com and mail-2@example.com.

We still need to tell Postfix the domain for the virtual aliases and the above file with the mappings. For this we open the corresponding configuration file /etc/postfix/main.cf:


__$ sudo nano /etc/postfix/main.cf
 

Add these two settings, or update them if they already exist. Do not also put the forwarding domain in mydestination or relay_domains. Without a catch-all entry, unknown addresses in this domain are rejected instead of being accepted for later delivery:

Excerpt from /etc/postfix/main.cf


# virtual alias
virtual_alias_domains = linuxservrsetup.com
virtual_alias_maps = hash:/etc/postfix/virtual
 

The relevant settings now look like this. The omitted settings, including the incoming TLS certificate paths and session cache, stay as supplied by the package:

Excerpt from /etc/postfix/main.cf


compatibility_level = 3.9
myorigin = /etc/mailname
inet_protocols = ipv4
mynetworks_style = host
mynetworks = 127.0.0.0/8 [::1]/128
mydestination = $myhostname, localhost.$mydomain, localhost
mailbox_command =
recipient_delimiter = +
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination
myhostname = srv1.linuxservrsetup.com
inet_interfaces = all
smtp_helo_name = $myhostname

# virtual alias
virtual_alias_domains = linuxservrsetup.com
virtual_alias_maps = hash:/etc/postfix/virtual

Final settings

Build the lookup database and check the configuration before activating it:

For hash:/etc/postfix/virtual, build the indexed lookup table with postmap. Repeat the first command whenever you edit /etc/postfix/virtual. The following lookup checks one configured address:


__$ sudo postmap hash:/etc/postfix/virtual
__$ sudo postmap -q tom@linuxservrsetup.com hash:/etc/postfix/virtual
__$ sudo postfix check
 

The lookup should return your configured destination address. postfix check should report no configuration or permission problems.

If the warning not owned by root: /var/spool/postfix/etc/resolv.conf appears, correct the owner of this file and run the check again:


__$ sudo chown root:root /var/spool/postfix/etc/resolv.conf
__$ sudo postfix check

Then restart Postfix: changes to inet_interfaces and inet_protocols need a restart, not just a reload.


__$ sudo systemctl restart postfix
 

The mail server should start automatically when the server is restarted:


__$ sudo systemctl enable postfix
 

Allow incoming SMTP on TCP port 25 if UFW is active:


__$ sudo ufw allow Postfix
 

This rule allows incoming connections. Outgoing TCP port 25 must also be reachable for direct forwarding; check outbound firewall rules and any provider or cloud firewall restrictions. Check the service and whether it listens on port 25:


__$ sudo systemctl status postfix
 

__$ sudo ss -ltnp 'sport = :25'

Send a message from an external email account to your real forwarding address and check that it arrives at the external destination. Also test an address that is not listed: it should be rejected during SMTP delivery. Finally, from outside the trusted local network, try delivery between two unrelated domains without authentication; this must be refused. Never test with the unchanged example addresses.

Delivery limits of simple forwarding

Forwarding can cause SPF checks to fail. Sender Rewriting Scheme (SRS) can help with the envelope sender; preserving valid DKIM signatures is also important. SRS alone does not guarantee DMARC success or delivery. For production use, plan sender authentication and spam filtering as well. See Gmail forwarding guidance and the DMARC forwarding considerations.


Reconfigure Postfix

If you want to set up Postfix again or reconfigure it by restoring a snapshot, you can call the installation routine again:


__$ sudo dpkg-reconfigure postfix
 

Mailutils

Mailutils is optional for this forwarding setup. Its mail command lets us send test messages and notifications through the locally installed Postfix. It is not needed to receive and forward messages using the virtual alias table.

Install with apt:


__$ sudo apt install -y mailutils
 

Send a test email to your own external address. This checks local submission and outgoing delivery; it does not replace the external forwarding test described above:


__$ echo "Postfix test email" | mail -s "Test subject" mail@example.com
 

Email notification on login

The SSH login notification can continue to use /usr/sbin/sendmail: Postfix provides this compatibility interface itself. Installing Mailutils does not switch the mail server; its mail command uses the installed delivery system.

If the separate Sendmail server was installed earlier, review the package changes when installing Postfix. No additional mail server is needed alongside Postfix, and its sendmail interface should remain available to scripts.


Postfix logs

Inspect Postfix messages in the system journal. The following command shows recent entries, filtered by the process name:


__$ sudo journalctl --since today --no-pager | grep postfix
 

Depending on the logging configuration, messages may instead or additionally be written to /var/log/mail.log and errors to /var/log/mail.err. If those files exist, follow the delivery log during a test:


__$ sudo tail -f /var/log/mail.log
 

Postfix Queue

Read out Postfix queue:


__$ sudo mailq
 

Process Postfix queue again:


__$ sudo postfix flush
 

Delete all queued messages permanently. This discards undelivered email and is not a normal setup or troubleshooting step:


__$ sudo postsuper -d ALL