Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
What is a Linux Server and Why use it
Next article icon

Setting Up and Configuring a Linux Mail Server

Last Updated : 28 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Table of Content

  • What is a Linux Mail Server?
  • How to Set Up and Configure Linux Mail Server?
  • Frequently Asked Questions on Setting Up and Configuring a Linux Mail Server

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.

How to Set Up and Configure Linux Mail Server?

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

Installing SendMail

Step 3: Configuring SendMail

After the installation, run the command "sendmailconfig" in the terminal to configure SendMail.

sudo sendmailconfig
Configuring SendMail

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

Adding the localhost name

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
Defining localhost name

Once the localhost name is defined, we can verify it by using the below command.

cat local-host-names

Verfying Localhost name

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.
Adding Config lines in SendMail.mc File

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

Creating Authentication Directory

Once the directory is created, create a file using the below command.

touch client-info
ls

Creating auth file

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

Creating a Database

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

Building the Configuration

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
Restarting the Services

Step 12: Checking the status

Check the status of the SendMail using the following command:

service SendMail status

Checking the 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"

Sending Mail using mail command

Mail Received

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.


Next Article
What is a Linux Server and Why use it

N

nrsraosha3dv8
Improve
Article Tags :
  • Linux-Unix

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
  • Steps to Configure Initial Router Settings
    Pre-requisite: Cisco Router basic commands. Routing is the method of selecting a network packet path over the Internet or between or through multiple networks. Routing is conducted on many kinds of networks, including circuit-switched networks, such as public switched telephone networks (PSTN), and
    2 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
  • Setup Web Server Over Docker Container in Linux
    Before setting up a server we must know the real meaning or definition of a server. So, the Server is a program that provides the client with any kind of services. For example, a web server provides our websites, a database server provides us data. This means every server has work to do and for ever
    2 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences