Setting Up and Configuring a Linux Mail Server
Last Updated :
28 Feb, 2024
Setting up and configuring a Linux mail server is a crucial task for individuals and organizations seeking to manage their email communication efficiently and securely. This article will guide you through the process of establishing a robust mail server on a Linux system, covering essential steps such as choosing the right mail server software, configuring DNS records, securing communication with SSL/TLS, and managing user accounts. Whether you're a system administrator or a Linux enthusiast, this comprehensive guide aims to simplify the complexities of creating a reliable mail server, ensuring smooth and reliable email communication.
What is a Linux Mail Server?
A Linux mail server is a software program installed in the Linux Operating System that is primarily configured for mailing purposes such as Sending, receiving, and storing mail. A Linux mail server is essential for managing email communication within an organization. The Linux Mail server provides instant email delivery over the internet, where the system of information storage is organized.
The main components of a Linux Mail Server are:
- Mail Transfer Agent (MTA): The main purpose of MTA is to deliver mail between servers over the Internet. Some popular Linux MTAs are SendMail, Postfix, Exim, etc.
- Mail Delivery Agent (MDA): The purpose of MDA is to deliver the incoming mail to the relevant user’s mailbox. Some popular Linux MDAs are Dovecot, Cyrus, etc.
- Mail Protocols: Linux mail server supports many protocols for accessing and delivering mail over the internet, such as IMAP (Internet Message Access Protocol) and POP3 (Post Office Protocol version 3), Simple Mail Transfer Protocol (SMTP), etc.
- Administration Tools: Linux mail servers provide administration tools for managing user accounts, monitoring server performance, and configuring various settings related to email delivery and security.
- Security Features: Linux Mail Servers provide security features to protect from spam, malware, and unauthorized access. This includes encryption protocols like TLS (Transport Layer Security) and antivirus scanning tools.
In this section, we will explore the detailed process to set up and configure Linux Mail Server. So, follow the below steps with proper command execution.
Step 1: Update the System
The first step is to update and upgrade our Linux system so that it has the latest information on available packages and their versions. To update the Linux system, open the terminal and enter the following command:
sudo apt update
Step 2: Installing SendMail
To install SendMail we need an active internet connection, after setting up your internet connection open the terminal and enter the following command in the terminal to install SendMail.
sudo apt-get install SendMail

Step 3: Configuring SendMail
After the installation, run the command "sendmailconfig" in the terminal to configure SendMail.
sudo sendmailconfig

Step 4: Adding the localhost name
Now after configuration, we will get the name of the host from file hosts which is in the/etc directory. So change the directory to etc using the "cd /etc" command and then view the hosts file using the following command:
cat hosts

Step 5: Defining localhost name
After opening the file, and getting the names of local hosts create a file "local-host-names" in the "/etc/mail" directory so our mail server will receive requests for email sending from these names. Change the directory to /etc/mail and add the file "local-host-names" to it and define the hostname in the file.
cd /etc/mail
sudo touch local-host-names
ls

Once the localhost name is defined, we can verify it by using the below command.
cat local-host-names

Step 6: To send mail using Gmail
Open the SendMail.mc file in the etc/mail directory and add the following lines to configure SendMail for Gmail:
define(`SMART_HOST',`smtp.gmail.com')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`RELAY_MAILER',`esmtp')dnl
define(`RELAY_MAILER_ARGS', `TPC $h 587')dnl
FEATURE(`AuthInfo',`hash file_path')dnl
- The first line define(`SMART_HOST',`smtp.gmail.com')dnl, defines to use of the SMTP server as SMART HOST to send the mail.
- The next line define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define the authentication mechanism used by sendmail.
- define(`RELAY_MAILER',`esmtp')dnl, This line defines the mailer to be used for relaying mail.
- The define(`RELAY_MAILER_ARGS', `TPC $h 587')dnl defines the arguments for the relay mailer, stating the TCP protocol and the Port "587" to be used while connecting to the relay host.
- The FEATURE(`AuthInfo',`hash file_path')dnl line defines the authentication information stored in the file. The AuthInfo feature allows sendmail to use the external authentication credentials followed by the path of the database file with its type i.e. ".db" in this case.

Step 7: Creating Authentication Directory
Create the directory /etc/mail/auth to store the login and password of the e-mail id.
mkdir /etc/mail/auth
ls
cd /etc/mail/auth

Once the directory is created, create a file using the below command.
touch client-info
ls

Step 8: Adding the user-name and password
After creating the file "client-info" add the following to the client-info file to store the username and password for the domain.
AuthInfo: smtp.gmail.com "U: root" "I :[email protected]" "P: password" "M: PLAIN"
AuthInfo: smtp.gmail.com:587 "U: root" "I:[email protected]" "P: password" "M: PLAIN"
Note: Replace [email protected] with your domain/mail-id and password with actual password.
Step 9: Creating the Database
Change the file format into the required format i.e. creating a database, to create a database convert the file "client-info" to "client-info.db" using the following command. create the ".db" file in the same directory i.e. "/etc/mail/auth" otherwise you will get an error while configuring or sending the mail.
makemap -r hash client-info.db < client-info

Step 10: Building the Configuration.
Now to save the configuration we made for SendMail, we will use the "make" command, if the make command gives you some error try adding sudo to it or switching the user as root and then try to save the configuration.
make

Step 11: Restarting the services
Now restart the MTA(SendMail) to apply the changes. To restart the sendmail and "apache2" use the following command:
sudo /etc/init.d/SendMail reload
sudo /etc/init.d/apache2 restart

Step 12: Checking the status
Check the status of the SendMail using the following command:
service SendMail status

Step 13: Sending the Mail
The "mail" command in Linux is a simple and versatile tool for sending emails from the command line interface. To use it, enter the "mail" command followed by the recipient's email address.
mail -s "subject" "receiver_mailaddress"


Conclusion
In conclusion, setting up and configuring a Linux mail server is a complex process that requires attention to detail and a solid understanding of Linux systems and mail server protocols. By following the steps indicated in this article, we have covered the essential steps to establish a functional and secure mail server within a local network. By following the guidelines outlined in this article and actively engaging with the Linux community for support and updates, one can create a reliable and efficient mail server designed to their specific necessity. Whether it's for personal use, small businesses, or educational purposes, the knowledge gained from setting up a Linux mail server locally provides a solid foundation for understanding email protocols and server administration principles.
Similar Reads
How to setup and configure an FTP server in Linux?
FTP (file transfer protocol) is an internet protocol that is used for transferring files between client and server over the internet or a computer network. It is similar to other internet protocols like SMTP which is used for emails and HTTP which is used for websites. FTP server enables the functio
9 min read
How to setup and configure an FTP server in Linux?
FTP (file transfer protocol) is an internet protocol that is used for transferring files between client and server over the internet or a computer network. It is similar to other internet protocols like SMTP, which is used for emails, and HTTP, which is used for websites. FTP server enables the func
13 min read
Setting Up a Media Server on Linux with Plex
Using the well-known Plex media server, you may stream your media collection frameworks to any device that has the Plex app pre-installed within the system software. Organizing and streaming your internal library of films, music, TV series, and images across multiple devices is a terrific use of a m
6 min read
What is a Linux Server and Why use it
A Linux server is a computer running the Linux operating system designed to serve various functions, such as hosting websites, managing databases, and handling network services. In this article, we'll explore what Linux servers are and delve into the reasons why they are widely used in enterprise en
9 min read
Setting up and Securing Ubuntu server with a Basic Firewall
VPS(Virtual Private Servers) are commonly used to host and serve many types of services. There are many providers that provide virtual servers. Many of these provide VPS's with their custom-built Linux OS which is lightweight compared to their desktop versions. These OS have built-in security but we
3 min read
Best Linux mail servers in 2023
A mail server is a software which is used for transferring emails on the internet from one point to another. It is an important component of the email communication system, it is a specialized software application that manages the sending, receiving, and storing of email messages. Mail servers are f
10 min read
How to Setup RabbitMQ in Linux Server?
RabbitMQ is a robust message broker that is open source, used between two different applications to great advantage in handling asynchronous messaging; it is, in turn, very effective at delivering the reliable routing of messages between services. The following guide will show you how to install and
3 min read
How to use SSH to connect to a remote server in Linux | ssh Command
Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It's like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you're sending a letter, and instead of sending it openly, you put it in a magic envelope th
8 min read
How to Find Out Postfix Mail Server Version in Linux?
The most commonly used implementations of SMTP in most Linux distros are Sendmail and Postfix. Postfix is an open-source mail-transfer agent that was originally developed as an alternative to Sendmail and is usually set up as the default mail server. We'll show you how to figure out what version of
1 min read
Installation and configuration of DHCP server on UBUNTU
A network technology called Dynamic Host Configuration technology (DHCP) allows the host of all the systems to receive IP addresses and associated network configurations automatically from an internal server. It makes using services like DNS, NTP, and any UDP or TCP-based communication protocol poss
4 min read