Linux: Setup Mail server

Setting up a mail server on a Linux system is useful for hosting email for a domain name, or even just to have local email capabilities. Local email in itself is a convenient feature on a machine used by many users, and even if there is no such traffic, many services dump their reports and messages into e-mails, which is delivered to the root user locally, so the sysadmin will be notified on any events of interest when they log in and switches to root user.

A simple example would be scheduled cron jobs: any output not redirected from the output of a script running from cron will be wrapped to an e-mail and will be delivered to root’s mailbox, so the administrator does not even have to make any effort to have the reports of the nightly backup jobs at hand. While this standalone mode is certainly a nice service as it is, we can also setup a full-fledged mail server, capable of receiving, forwarding, relaying, and filtering of e-mails.

In this tutorial, we will go over the step by step instructions to set up a mail server on a Linux system. This will be facilitated by Postfix, a popular mail transport agent for Linux which is often installed by default. We will also use a local mail user agent called mailx to test the functionality of our mail server.

In this tutorial you will learn:

  • How to install Postfix and mailx on major Linux distros
  • How to send an email from command line
  • How to retrieve email
Linux: Setup Mail server
Linux: Setup Mail server
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software postfix, mailx
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Linux: Setup Mail Server step by step instructions

Postfix mail server and mailx are sometimes installed by default, but if not, you can grab them from your distro’s software repository. Run through the step by step instructions below to get the packages installed and setup your mail server:

  1. You can use the appropriate command below to install Postfix and mailx with your system’s package manager.

    To install Postfix and mailx on Ubuntu, Debian, and Linux Mint:

    $ sudo apt install postfix mailutils

    To install Postfix and mailx on Fedora, CentOS, AlmaLinux, and Red Hat:

    $ sudo dnf install postfix mailx

    To install Postfix and mailx on Arch Linux and Manjaro:

    $ sudo pacman -S postfix mailx
  2. During installation, you can select what type of email server you want to configure. The two most common options are ‘Internet Site’ and ‘Local only.’ If you have a fully qualified domain name configured on your server, you can select Internet Site. Otherwise, stick with the local configuration.

    Select type of Postfix configuration
    Select type of Postfix configuration

  3. Set your fully qualified domain name or local domain name.
    Set the domain name
    Set the domain name
  4. Next, start the Postfix mail server and enable it to start up automatically on future boots:
    $ sudo systemctl start postfix
    $ sudo systemctl enable postfix
  5. Add your user, and any other users that need to use the mail service, to the mail group.
    $ sudo usermod -aG mail $(whoami)
  6. Now we will try sending an email to the root user account.
    $ mail root
    Subject: Testing email
    This is an email from

    Press Ctrl + D to send the email and return to your terminal prompt.

    It is also possible to send an email using telnet, in case the mail command is not available or you wanted an even nerdier way to send mail from command line.
  7. We can check to the root inbox to see that we have received the test email:
    $ sudo mail
    "/var/mail/root": 1 message 1 new
    >N   1        Sun Mar 12 00:54  15/489   Testing email

    Where N is “new”, 1 is the ID of the message, is the sender (username@hostname), delivery time and the subject can also be seen. By typing the message ID, in this case 1 and hitting enter you can read your local mail, verifying the mail delivery system is working as intended.

Closing Thoughts

In this tutorial, we saw how to set up a mail server on a Linux system. Using your own mail server gives you control and privacy over the emails on your system, and allows you to easily receive system alerts from various services.

Comments and Discussions
Linux Forum