Setting Up and Configuring a Linux Mail Server (2023)

Setup Linux Email Server

In order to configure a Linux mail server, you’ll first need to check if Postfix is already installed. It’s the default mail server on the lion’s share of Linux distributions these days, which is good because server admins like it a lot.

Here’s how to check if it’s already on the system:

$ rpm -qa | grep postfix

If not, this is how you install it on Red Hat distributions:

$ dnf -y install postfix

Next, run it and activate it on system start-up:

$ systemctl start postfix

$ systemctl activate postfix

For distributions based on Debian, like Ubuntu, you’d install them like this:

$ apt-get -y install postfix

As you configure Linux mail server you will receive a prompt to choose how you want to configure your Postfix mail server.

You’ll be presented with these choices:

  • No configuration
  • Internet site
  • Internet with smarthost
  • Satellite system and Local only

Let’s go with the No configuration option for our Linux email server.

Configure Linux Mail Server

After installing the Postfix mail server, you will need to set it up, and most of the files you’ll need for this can be found inside the /etc/postfix/ directory.

You can find the main configuration for Postfix Linux mail server in the /etc/postfix/main.cf file.

This file contains numerous options like:

myhostname

Use this one to specify the hostname of the mail server, which is where postfix will obtain its emails.

The hostnames will look something like mail.mydomain.com, smtp.mydomain.com.

You incorporate the hostname this way:

myhostname = mail.mydomain.com

exampledomain.com

This option is the mail domain that you will be servicing, like mydomain.com

The syntax looks like this:

(Video) Linux Mail Server Configuration in 10 minutes

mydomaindomain.com = mydomain.com

myorigin

All emails sent from this mail server will look as though they came from the one that you specify in this option. You can set this to $exampledomain.com.

myorigin = $exampledomain.com

Use any value that you want for this option but put a dollar sign in front of it like this: $exampledomain.com.

mydestination

This option shows you which domains the Postfix server uses for incoming emails to your Linux email server.

You can assign values like this:

mydestination = $myhostname, localhost.$exampledomain.com, $exampledomain.com, mail.$exampledomain.com, www.$exampledomain.com

mail_spool_directory

A Postfix Linux mail server can use two modes of delivery:

  • straight to someone’s mailbox.
  • to a central spool directory, which means the mail will sit in /var/spool/mail with a file for every user.

mail_spool_directory = /var/spool/mail

mynetworks

This will let you arrange which servers can relay through your Postfix server.

It should only take local addresses like local mail scripts on your server.

If this isn’t the case, then spammers can piggyback on your Linux mail server. That means your lovely shiny server will be doing the heavy lifting for some bad guys and it will also end up getting banned.

Here’s the syntax for this option:

mynetworks = 127.0.0.0/8, 192.168.1.0/24

smtpd_banner

This one determines what message is sent after the client connects successfully.

Consider changing the banner so it doesn’t give away any potentially compromising information about your server.

inet_protocols

This option designates which IP protocol version is used for server connections.

inet_protocols = ipv4

When you change any of files used to configure Linux mail server for Postfix, you must reload the service, with this directive:

$ systemctl reload postfix

Of course, we all get distracted and typing things in can often result in mistakes, but you can track down any misspellings that might compromise your Linux mail server using this command:

$ postfix check

Fight Spam with SpamAssassin

Nobody likes spam, and SpamAssassin is probably the best free, open source spam fighting ninja that you could hope to have in your corner.

Installing it is as simple as doing this:

$ dnf -y install spamassassin

Then you just start the service and activate it at start-up:

$ systemctl start spamassassin

$ systemctl activate spamassassin

Once you’ve done that, you can see how it’s configured in the /etc/mail/spamassassin/local.cf file.

SpamAssassin runs a number of scripts to test how spammy an email is. The higher the score that the scripts deliver, the more chances there are that it’s spam.

In the configuration file, if the parameter required_hits is 6, this tells you that SpamAssassin will consider an email to be spam if it scores 6 or more.

The report_safe command will have values of 0, 1, or 2. A 0 tells you that email marked as spam is sent without modification, and only the headers will label it as spam.

A 1 or a 2 means that a new report message will be created by SpamAssassin and delivered to the recipient.

A value of 1 indicates that the spam message is coded as content message/rfc822, and if it’s a 2, that means the message has been coded as text or plain content.

Text or plain is less dangerous because some mail clients execute message/rfc822, which is not good if they contain any kind of malware.

The next thing to do is integrate it into Postfix, and the easiest way to do that is with procmail.

We’ll make a file called/etc/procmailrc, and add this to it:

:0 hbfw | /usr/bin/spamc

Then we’ll edit the Postfix configuration file /etc/postfix/main.cf and alter the mailbox_command, thus:

mailbox_command = /usr/bin/procmail

Last but not least, restart Postfix and SpamAssassin services:

$ systemctl restart postfix

$ systemctl restart spamassassin

Unfortunately, SpamAssassin can’t catch everything, and spam messages can still sneak through to fill up the mailboxes on your Linux email server.

But never fear because you can filter messages before they even get to the Postfix server with Realtime Blackhole Lists (RBLs).

Open the Postfix server configuration at /etc/postfix/main.cf and change smtpd_recipient_restrictions option by adding the following options like this:

strict_rfc821_envelopes = yes

relay_domains_reject_code = 554

unknown_address_reject_code = 554

unknown_client_reject_code = 554

(Video) Setup Mail Server on Linux with Multiple domain names

unknown_hostname_reject_code = 554

unknown_local_recipient_reject_code = 554

unknown_relay_recipient_reject_code = 554

unverified_recipient_reject_code = 554

smtpd_recipient_restrictions =

reject_invalid_hostname,

reject_unknown_recipient_domain,

reject_unauth_pipelining,

permit_mynetworks,

permit_sasl_authenticated,

reject_unauth_destination,

reject_rbl_client dsn.rfc-ignorant.org,

reject_rbl_client dul.dnsbl.sorbs.net,

reject_rbl_client list.dsbl.org,

reject_rbl_client sbl-xbl.spamhaus.org,

reject_rbl_client bl.spamcop.net,

reject_rbl_client dnsbl.sorbs.net,

permit

Now, restart your postfix Linux mail server:

$ systemctl restart postfix

The above RBLs are the most common ones found, but there are plenty more on the web for you to track down and try.

(Video) Linux Mail Server Configuration Step by Step

POP3 and IMAP Protocol Basics

We now know how a SMTP Linux mail server sends and receives emails, but what about other user needs, like when they want local copies of emails to view off-line?

mbox file format isn’t supported; it’s used by many mail user agents such as mailx and mutt.

Due to security concerns, some mail servers restrict access to the shared mail spool directories.

(Video) Install a mail server on Linux in 10 minutes - docker, docker-compose, mailcow

Another class of protocols—called mail access protocols—was introduced to deal with such situations.

The commonest ones are POP and IMAP – Post Office Protocol and Internet Message Access Protocol.

POP’s underlying methodology is very simple: a central Linux mail server is online 24/7 for reception and storage of all user emails.

When an email is sent, the email client relays it through the central Linux mail server using SMTP.

Be aware that the SMTP server and POP server can easily be on the same system, and that this is a common thing to do.

IMAP was developed because previously you couldn’t keep a master copy of a user’s email on the server.

With IMAP, your Linux email server supports three kinds of access:

  • online mode is like having direct access to the Linux email server file system.
  • offline mode feels like POP, where the client only connects to the network to get their mail, and the server won’t keep a copy.
  • disconnected mode lets users keep cached copies of their emails and the server keeps one too.

There are a few different implementations for IMAP and POP, with the most prevalent being dovecot server, which offers both.

POP3, POP3S, IMAP, and IMAPS listen on ports 110, 995, 143, and 993 respectively.

Secure Dovecot

Dovecot features generic SSL certificates and key files used with /etc/dovecot/conf.d/10-ssl.conf

ssl_cert = </etc/pki/dovecot/certs/dovecot.pem

ssl_key = </etc/pki/dovecot/private/dovecot.pem

If you try to connect to a dovecot server and certificates haven’t been signed, then you’ll get a warning, but if you go to a certificate authority you can buy one, so no worries there.

Alternatively, you can point to them using Let’s Encrypt certificates:

ssl_cert = </etc/letsencrypt/live/yourdomain.com/fullchain.pem

ssl_key = </etc/letsencrypt/live/yourdomain.com/privkey.pem

You’ll need to open dovecot server ports in your iptables firewall by adding iptables rules for ports 110, 995, 143, 993, 25.

Do that and save the rules.

Or if you have a firewall then do this:

$ firewall-cmd --permanent --add-port=110/tcp --add-port=995/tcp

$ firewall-cmd --permanent --add-port=143/tcp --add-port=993/tcp

$ firewall-cmd --reload

Finally, for troubleshooting, check through the log files /var/log/messages, /var/log/maillog, and /var/log/mail.log files.

Linux mail server (and particularly Postfix) is one of the simplest systems you can work with.

FAQs

How install SMTP server Linux step by step? ›

Configuring SMTP in a single server environment

Configure the E-mail Options tab of the Site Administration page: In the Sending E-mail Status list, select Active or Inactive, as appropriate. In the Mail Transport Type list, select SMTP. In the SMTP Host field, enter the name of your SMTP server.

How do I configure email server? ›

Basic Authentication
  1. Click Admin tab on the web console.
  2. Under Server Settings, click Mail Server Settings.
  3. Specify the following information: Server Name : smtp.gmail.com. Port : 465 (SSL) / 587 (TLS) Sender Email Address: Your email address. Test Email Address: email address to receive test mails. Email Type: SMTP / SMTPS.

What is a Linux mail server? ›

A Linux Mail Server is software running on Linux used for transferring emails between another server and client software. There is numerous Linux mail server software with different features and advantages.

Why we use mail server in Linux? ›

A mail server -- also known as a mail transfer agent, or MTA; mail transport agent; mail router; or internet mailer -- is an application that receives incoming email from local users and remote senders and forwards outgoing messages for delivery.

How do I install mail on Linux? ›

How to Install mail command in RHEL / CentOS 7/8 Using 5 Best...
  1. Step 1: Prerequisites.
  2. Step 2: Update Your System.
  3. Step 3: Install mail command in Linux.
  4. Step 4: Check mail command version.
  5. Step 5: Send a Test email using mail command in Linux.
7 Aug 2020

Where is SMTP config file in Linux? ›

Configuring Postfix – The SMTP Service. The main configuration file for Postfix is /etc/postfix/main.cf.

How connect SMTP server Linux? ›

  1. Step 1: Checking SMTP Connection Using Telnet. Telnet to your host, you want to check connection. ...
  2. Step 2: Checking The SMTP Connection From Command Line Using Ncat Or Nc. ...
  3. Step 3: Checking SMTP Connection Over TLS Using Openssl.
15 Dec 2021

How use SMTP Linux? ›

How to Send Email via SMTP Server from Linux Command Line (with SSMTP)
  1. Step 1 – Install SSMTP Server. SSMTP service packages are available under EPEL repository, So make you have EPEL yum repository configured in your system. ...
  2. Step 2 – Configure SSMTP. ...
  3. Step 3 – Send Test Email. ...
  4. Step 4 - Setup SSMTP as Default.
5 Apr 2014

What does email configuration mean? ›

When you configure an email account, you must define your email address, password, and the mail servers used to send and receive messages. Fortunately, most webmail services configure your account automatically, so you only need to enter your email address and password.

What is SMTP server configuration? ›

The SMTP. server defines who can send outgoing messages via your Kerio Connect and what actions they can perform. If an unprotected SMTP server is accessible from the Internet, anyone can connect and send email messages through Kerio Connect.

Which mail server is best? ›

Gmail is the overall best email service. Outlook is good for multiple app integrations. Yahoo has good spam blocking capabilities. Zoho Mail is the best option for home businesses. AOL provides unlimited storage.

How do I start email on Linux? ›

To Configure the Mail Service on a Linux Management Server
  1. Log in as root to the management server.
  2. Configure the pop3 mail service. ...
  3. Ensure that the ipop3 service has been set to run at levels 3, 4, and 5 by typing the command chkconfig --level 345 ipop3 on .
  4. Type the following commands to restart the mail service.

How do you Sendmail in Linux? ›

Use the clause of mail to end the mail, the type “-s” to specify the subject, type the recipient email address, press the ENTER key, it will ask for the CC (carbon copy) give it or skip it by pressing the ENTER key, type the message, you want to communicate and finally press CTRL+D to send the message.

How does a mail server work? ›

At its simplest, a mail server collects and distributes emails to their intended destination. You can think of it as a computer that acts as an electronic post office for email, which allows you to control the transfer of emails within a network through different protocols.

How do I check mail on Linux server? ›

How To Check Mail Logs - Linux server?
  1. Login into shell access of the server.
  2. Go to below mentioned path: /var/logs/
  3. Open the desired Mail logs file and search the contents with grep command.
21 Oct 2008

What are the different types of email servers? ›

Types of Mail Servers

Incoming mail servers come in two main varieties. POP3, or Post Office Protocol, version 3, servers are best known for storing sent and received messages on PCs' local hard drives. IMAP, or Internet Message Access Protocol, servers always store copies of messages on servers.

How do I know if my email is working Linux? ›

Type "ps -e | grep sendmail" (without quotes) at the command line. Press the "Enter" key. This command prints a listing that includes all running programs whose name contains the text "sendmail." If sendmail is not running, there will be no results.

What is send command in Linux? ›

DESCRIPTION. The system calls send(), sendto(), and sendmsg() are used to transmit a message to another socket. The send() call may be used only when the socket is in a connected state (so that the intended recipient is known). The only difference between send() and write() is the presence of flags.

How do you send a message in Linux terminal? ›

Now to send messages to all users, use the wall command, it comes pre-installed in all Linux Distributions which will allow us to send messages to another user in the terminal using tty2. You can use any symbol, character, or white space in the message.

What is write command in Linux? ›

The write command enables message sending over the system in real time. It provides conversation-like communication with another logged-in user. Each user alternately sends and receives short messages from the other workstation.

How do I find my SMTP server IP address in Linux? ›

Type "ping," a space and then the name of your SMTP Server. For example, type "ping smtp.server.com" and press "Enter." The window will then try to contact the SMTP server by the IP address. It will say, "Pinging x.x.x.x with 32 bytes of data." The "x.x.x.x" will be the SMTP server's IP address.

How do you check if SMTP is configured on a Linux server? ›

We can type a sample email while the connection is open and see if the SMTP server will receive and relay it: [user@localhost ~]# telnet localhost 2525 Trying ::1...

How do you check if SMTP is configured on a server? ›

How to manually test if an SMTP server can receive email
  1. From the Windows Start Menu select Start->Run and enter CMD as the application to open. Select OK.
  2. At the command prompt, enter the following: telnet mail.mailenable.com 25. ...
  3. Type the word QUIT and then press enter.
12 Nov 2019

What are SMTP commands? ›

SMTP commands
SubcommandSupported by SMTP ServerDescription
EHLONOIdentifies the domain name of the sending host to SMTP.
EXPNYESVerifies whether a mailbox exists on the local host.
HELOYESIdentifies the domain name of the sending host to SMTP.
HELPYESProvides help with SMTP commands.
11 more rows

How do you configure SMTP postfix mail in Linux? ›

Postfix mail server configuration in Linux step by step
  1. Update system. Open terminal and run the following command to update Ubuntu system. ...
  2. Set hostname and add host. ...
  3. Install Postfix. ...
  4. Start & Enable Postfix service. ...
  5. Install mailx email client. ...
  6. Configure Postfix mail server. ...
  7. Test Postfix Mail Server. ...
  8. Secure Mail Server.
24 Jul 2021

How do I connect to SMTP server? ›

To connect to an SMTP server by using Telnet on port 25, you need to use the fully-qualified domain name (FQDN) (for example, mail.contoso.com) or the IP address of the SMTP server. If you don't know the FQDN or IP address, you can use the Nslookup command-line tool to find the MX record for the destination domain.

What is the port number of SMTP? ›

Port 25 is the original standard email SMTP port and the oldest, since it first debuted in 1982. Today, the Internet Assigned Numbers Authority (IANA), the group responsible for maintaining the internet addressing scheme, still recognizes port 25 as the standard, default SMTP port.

How do I find my SMTP port? ›

How to Find an SMTP Port Number
  1. Log in to the Exchange System Manager in your mail server. ...
  2. Click "Servername."
  3. Click "Protocols."
  4. Click "SMTP."
  5. Find the icon labeled "Default SMTP Virtual Server" and right-click on the icon.
  6. Select "Properties."
  7. Select the "General" tab and click the button marked "Advanced."

How do I set email alerts in Linux? ›

Setup
  1. root=username@gmail.com. Change it from postmaster to the machines admin's Email.
  2. mailhub=smtp.gmail.com:587. ...
  3. hostname=username@gmail.com. ...
  4. UseSTARTTLS=YES. ...
  5. AuthUser=username. ...
  6. AuthPass=password. ...
  7. FromLineOverride=yes.
5 May 2011

Is Gmail a mail server? ›

The Google's Gmail service provides email customer access for sending and receiving emails without having to log into the account online. For several security reasons, this email server uses a POP3 protocol to make sure that your email customer supports an encrypted SSL connection before running the setup.

What is a server address for email? ›

An SMTP email server will have an address (or addresses) that can be set by the mail client or application that you are using and is generally formatted as smtp.serveraddress.com. For example, the SMTP server Gmail uses is smtp.gmail.com, and Twilio SendGrid's is smtp.sendgrid.com.

How do I create an email step by step? ›

Here's what you'll need to do:
  1. From your Internet browser, navigate to the sign-up page for your email provider. ...
  2. Enter your details to open an email account. ...
  3. Follow the rest of the steps as required. ...
  4. Accept the “Terms of Service.” ...
  5. Start using your new email account!
6 Oct 2020

How do I add an IP address to my SMTP server? ›

How to Assign an IP AddressTo assign an IP address to your SMTP virtual server, follow these steps:
  1. Click Start, point to Programs, point to Administrative Tools, and then click Internet Services Manager. ...
  2. Right-click Default SMTP Virtual Server, and then click Properties.
  3. Click the General tab. ...
  4. Click OK.

How many types of email are there? ›

How many email types can you Google – 5, 7, 10, or more?
5 Types7 Types
read moreread more
1. Newsletter 2. Lead nurturing 3.Promotional 4. Milestone 5. Survey1. Newsletter 2. Standalone 3. Lead nurturing 4. Transactional 5. Milestone 6. Plain-text 7. Mobile optimized
15 Nov 2019

How do I setup a free email server? ›

How to Build Free SMTP Server with Open Source SMTP [Postal] in 10 ...

Which email is most secure? ›

6 Most Secure Email Providers
  1. ProtonMail. ProtonMail was founded in 2014 at the CERN research facility by Andy Yen, Jason Stockman, and Wei Sun. ...
  2. Hushmail. ...
  3. Tutanota. ...
  4. CounterMail. ...
  5. Mailfence. ...
  6. Librem Mail.
16 Sept 2022

How do I find my mail server Linux? ›

To check if SMTP is working from the command line (Linux), is one critical aspect to be considered while setting up an email server. The most common way of checking SMTP from Command Line is using telnet, openssl or ncat (nc) command. It is also the most prominent way to test SMTP Relay.

How do I find my SMTP server in Linux? ›

Type nslookup and hit enter. Type set type=MX and hit enter. Type the domain name and hit enter, for example: google.com. The results will be a list of host names that are set up for SMTP.

How do I enable mail in Linux? ›

To Configure the Mail Service on a Linux Management Server
  1. Log in as root to the management server.
  2. Configure the pop3 mail service. ...
  3. Ensure that the ipop3 service has been set to run at levels 3, 4, and 5 by typing the command chkconfig --level 345 ipop3 on .
  4. Type the following commands to restart the mail service.

How do you send mail in Linux? ›

Use the clause of mail to end the mail, the type “-s” to specify the subject, type the recipient email address, press the ENTER key, it will ask for the CC (carbon copy) give it or skip it by pressing the ENTER key, type the message, you want to communicate and finally press CTRL+D to send the message.

How can I tell if an email is configured in Linux? ›

Type "ps -e | grep sendmail" (without quotes) at the command line. Press the "Enter" key. This command prints a listing that includes all running programs whose name contains the text "sendmail." If sendmail is not running, there will be no results.

How use SMTP Linux? ›

How to Send Email via SMTP Server from Linux Command Line (with SSMTP)
  1. Step 1 – Install SSMTP Server. SSMTP service packages are available under EPEL repository, So make you have EPEL yum repository configured in your system. ...
  2. Step 2 – Configure SSMTP. ...
  3. Step 3 – Send Test Email. ...
  4. Step 4 - Setup SSMTP as Default.
5 Apr 2014

What are SMTP commands? ›

SMTP commands
SubcommandSupported by SMTP ServerDescription
EHLONOIdentifies the domain name of the sending host to SMTP.
EXPNYESVerifies whether a mailbox exists on the local host.
HELOYESIdentifies the domain name of the sending host to SMTP.
HELPYESProvides help with SMTP commands.
11 more rows

What is the port number of SMTP? ›

Port 25 is the original standard email SMTP port and the oldest, since it first debuted in 1982. Today, the Internet Assigned Numbers Authority (IANA), the group responsible for maintaining the internet addressing scheme, still recognizes port 25 as the standard, default SMTP port.

What is SMTP server address? ›

An SMTP email server will have an address (or addresses) that can be set by the mail client or application that you are using and is generally formatted as smtp.serveraddress.com. For example, the SMTP server Gmail uses is smtp.gmail.com, and Twilio SendGrid's is smtp.sendgrid.com.

How do you check if SMTP is configured on a server? ›

How to manually test if an SMTP server can receive email
  1. From the Windows Start Menu select Start->Run and enter CMD as the application to open. Select OK.
  2. At the command prompt, enter the following: telnet mail.mailenable.com 25. ...
  3. Type the word QUIT and then press enter.
12 Nov 2019

Which mail server is best? ›

Gmail is the overall best email service. Outlook is good for multiple app integrations. Yahoo has good spam blocking capabilities. Zoho Mail is the best option for home businesses. AOL provides unlimited storage.

Where is sendmail configuration in Linux? ›

The main configuration file for Sendmail is /etc/mail/sendmail.cf , which is not intended to be manually edited. Instead, make any configuration changes in the /etc/mail/sendmail.mc file.

How do I email a file from Linux server? ›

Below are the various, well known methods of sending email with attachment from the terminal.
  1. Using mail Command. mail is part of the mailutils (On Debian) and mailx (On RedHat) package and it is used to process messages on the command line. ...
  2. Using mutt Command. ...
  3. Using mailx Command. ...
  4. Using mpack Command.
17 Dec 2016

How do I email a text file in Linux? ›

Syntax to send the content of a text file using mail command
  1. -s 'Subject' : Specify subject on command line.
  2. you@cyberciti.biz : To email user.
  3. /tmp/output. txt : Send the content of /tmp/output. ...
  4. -r 'from@your-com-tld' : Use from@your-com-tld address as the return address when sending mail.
28 Aug 2022

How do I change the email address in Linux? ›

mail to-addr ... -sendmail-options ...
...
Here are some options:
  1. If you have privelige enough, configure sendmail to do rewrites with the generics table.
  2. Write the entire header yourself (or mail it to yourself, save the entire message with all headers, and re-edit, and send it with rmail from the command line.

Videos

1. Install and Configure Mail Server with PostfixAdmin on Ubuntu | Part 1
(Happy Ghost)
2. Configure Sendmail in Redhat Enterprise Linux (RHEL) | Setup Mail Server in Linux | Nehra Classes
(Nehra Classes)
3. Full Mail Server Setup Tutorial For Beginners
(The Linux Fever)
4. Linux Postfix Mail Server | Dovecot Email Server Install & Configure Easy Steps
(NB Tech Support)
5. Postfix Mail Server Install, Configure & Forward to Gmail
(The Grok Shop)
6. How to install dovecot and sendmail | Linux mail server (CentOS/RHEL)
(NetITGeeks)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated: 01/12/2023

Views: 6159

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.