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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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:
- You can use the appropriate command below to install Postfix and mailx with your system’s package manager.
$ sudo apt install postfix mailutils
$ sudo dnf install postfix mailx
$ sudo pacman -S postfix mailx
- 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.
- Set your fully qualified domain name or local domain name.
- Next, start the Postfix mail server and enable it to start up automatically on future boots:
$ sudo systemctl start postfix $ sudo systemctl enable postfix
- Add your user, and any other users that need to use the mail service, to the
$ sudo usermod -aG mail $(whoami)
- Now we will try sending an email to the root user account.
$ mail root Cc: Subject: Testing email This is an email from linuxconfig.org
Ctrl + Dto send the email and return to your terminal prompt.
DID YOU KNOW?
It is also possible to send an email using telnet, in case the
- 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 linuxconfiglinuxconfig.org Sun Mar 12 00:54 15/489 Testing email ?
1is the ID of the message,
email@example.com 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.
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.