You are on page 1of 14

11/17/2017 How to Setup an Email Server on CentOS 7

Want your very own server? Get our 1GB memory, Xeon V4, 20GB SSD VPS for 10.00 / month.

View Plans

HostPresto! > Community > Tutorials > How to Setup an Email Server on CentOS 7

Liptan Biswas
0 3 0

How to Setup an Email Server on CentOS


7
19th July 2016 31,562k

In this tutorial we are going to learn how to set up an Email server using Post x, Dovecot and Squirrelmail on CentOS 7.x. We will be using Post x
for SMTP (Simple Mail Transfer Protocol), Dovecot for POP/IMAP and Squirrelmail as webmail client to send or receive emails. We will also learn to
setup MX records which is important to route the emails.

Requirements
The requirements for setting up email server will simply be a VPS or dedicated server with a fresh CentOS 7.x install and also a static IP address. In
this tutorial we will be using a root account to execute the commands. If you are logged in as non-root user, use sudo command at the start of
all the commands or you can execute su command to login as root user.

Setting up DNS
It is very important to setup DNS records, speci cally MX records in your domain control panel. Login to your domain control panel and change
your DNS settings to add these following MX records entries. A typical MX record will look like this.

Type Host Destination Priority TTL


MX @ mail.yourdomain.com 10 3600
A mail 192.168.0.1 3600

Where MX is the type of record, MX stands for Mail Exchangers. Next is the value for host, you can either enter your domain name or you can also
use @ which represents the value of the zone name which is same as your domain name. Next you will have to choose the destination, you will
need to enter the hostname or FQDN of your mail server.

The next value is the priority. The lowest number is priority. For example 0 will have the highest priority and 20 will have a lower priority. Priority is
used because we can add multiple MX records for a single domain, mail is forwarded to the server having highest priority. If the server having
highest priority is not available then mail will be forwarded to the server having second highest priority. Next is TTL or Time to Live, it should be set
to 3600.

It is very important that you also setup an A record for your hostname of the mail server FQDN. Again select the type as A record, host should be
the hostname you are using in your FQDN, for example in this case we have used the hostname as mail.yourdomain.com, hence our host will

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 1/14
11/17/2017 How to Setup an Email Server on CentOS 7

be mail. Next, at destination, enter the IP address of your server. A records does not have priority option hence, you will only need to provide
TTL.

Once you have con gured your DNS settings, you will need to wait some time so that DNS gets propagated. It usually takes around two hours
these days. Once propagated, you can check your MX records here.

Until your DNS gets propagated, you can continue with the installation.

Installing Post x
Login to your server and run the following command to update the repository and packages available in your system.

yum -y update

Now update the hostname of your system to the FQDN you want to use with your mail server. Run the following command to change your
hostname.

hostname mail.yourdomain.com

You can replace the hostname according to your choice, but it should be same as the FQDN which we have used in our DNS settings.

Now add the hostname entry in the host les of your system. Edit /etc/hosts les using your favorite editor. For example if your are using nano
then you will need to run the following command.

nano /etc/hosts

You will see two lines of entries in there, append your server IP address followed by hostname at the end of the le. It should look like the one
shown below.

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4


::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
104.36.18.239 mail.yourdomain.com

Now we can install Post x, enter the following command to do so.

yum -y install postfix

Before con guring post x we will need to con gure SSL which will be used to encrypt and secure the emails.

mkdir /etc/postfix/ssl
cd /etc/postfix/ssl

Now we will have to create SSL certi cates. If you do not have openssl installed you can install it using the following command.

yum -y install openssl

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 2/14
11/17/2017 How to Setup an Email Server on CentOS 7

Now run the following command to create certi cate and key les.

openssl req -x509 -nodes -newkey rsa:2048 -keyout server.key -out server.crt -nodes -days 365

Now you will be asked some information which is to be added into your CSR (Code Signing Request). You will be asked your country name in two
letters, for example consider IN for India. Then you will be asked about the state or province. Then you will be asked about your city and
organization. Finally, a common name of your server and your email address. If you want to leave some details blank use full stop or period ( . )
sign. You can also enter the default values just by pressing enter. Example output is given below.

Generating a 2048 bit RSA private key


..........................+++
...........................+++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:IN
State or Province Name (full name) []:RAJ
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:My Unit
Common Name (eg, your name or your server's hostname) []:mail.rackvoucher.com
Email Address []:me@liptanbiswas.com

This will generate the key le and certi cates and will save then in /etc/postfix/ssl directory.

Now edit post x con guration le which can be found at /etc/postfix/main.cf, with your favorite editor.

nano /etc/postfix/main.cf

and append these lines at the end of the le.

myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
home_mailbox = mail/
mynetworks = 127.0.0.0/8
inet_interfaces = all
inet_protocols = all
inet_interfaces = localhost
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 3/14
11/17/2017 How to Setup an Email Server on CentOS 7

smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_key_file = /etc/postfix/ssl/server.key
smtpd_tls_cert_file = /etc/postfix/ssl/server.crt
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

Now open another con guration le /etc/postfix/master.cf using your favorite editor.

nano /etc/postfix/master.cf

and nd the following lines in the con guration le.

# service type private unpriv chroot wakeup maxproc command + args


# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtp inet n - n - - smtpd

Now add the following lines at just below these lines.

submission inet n - n - - smtpd


-o syslog_name=postfix/submission
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
smtps inet n - n - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING

Installing Dovecot
Now install Dovecot using the following command:

yum -y install dovecot

Once Dovecot is installed, edit the following le using your favorite editor.

nano /etc/dovecot/conf.d/10-master.conf

and nd the following lines:

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 4/14
11/17/2017 How to Setup an Email Server on CentOS 7

# Postfix smtp-auth

Now Append the following lines, just below these lines:

# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}

Now open another con guration using the following command.

nano /etc/dovecot/conf.d/10-auth.conf

and nd the following lines.

# Space separated list of wanted authentication mechanisms:


# plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp skey
# gss-spnego
# NOTE: See also disable_plaintext_auth setting.
auth_mechanisms = plain

Append login at the end of the line auth_mechanisms = plain to make it look like

auth_mechanisms = plain login

Again edit /etc/dovecot/conf.d/10-mail.conf le using your favorite editor.

nano /etc/dovecot/conf.d/10-mail.conf

Find the following lines

#
#
#mail_location =

Now add the following line just below these lines:

mail_location = maildir:~/mail

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 5/14
11/17/2017 How to Setup an Email Server on CentOS 7

Now edit /etc/dovecot/conf.d/20-pop3.conf using your favorite editor.

nano /etc/dovecot/conf.d/20-pop3.conf

and nd the following lines.

#pop3_uidl_format = %08Xu%08Xv

Uncomment the above line to make it look like as shown below.

# Note that Outlook 2003 seems to have problems with %v.%u format which was
# Dovecot's default, so if you're building a new server it would be a good
# idea to change this. %08Xu%08Xv should be pretty fail-safe.
#
pop3_uidl_format = %08Xu%08Xv

Now restart post x, and dovecot using the following command.

systemctl restart postfix


systemctl enable postfix
systemctl restart dovecot
systemctl enable dovecot

Now if you have a rewall running you will need to allow port number 25, 587, 465, 110, 143, 993, 995 and 80. All the ports except 80 are used to
send and receive emails and port 80 is used to make HTTP connections. HTTP connections will be used to access Squirrelmail using web interface.

To unblock all these ports from rewall, run the following commands.

firewall-cmd --permanent --add-service=smtp


firewall-cmd --permanent --add-port=587/tcp
firewall-cmd --permanent --add-port=465/tcp
firewall-cmd --permanent --add-port=110/tcp
firewall-cmd --permanent --add-service=pop3s
firewall-cmd --permanent --add-port=143/tcp
firewall-cmd --permanent --add-service=imaps
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

Testing Post x and Dovecot


You can check if Post x is working by executing the following command in your terminal.

telnet mail.yourdomain.com smtp

If you do not have telnet installed, then you can run the following command to install telnet.

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 6/14
11/17/2017 How to Setup an Email Server on CentOS 7

yum -y install telnet

Once you are connected using telnet you will see following output.

Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.rackvoucher.com ESMTP Postfix

Now you can also send email using telnet. Use the following command to enter the sender username.

mail from:

To enter the email of recipient, you can use the use the following command.

rcpt to:

To enter the body of email, enter the following command.

data

Once you have entered your message, enter dot (.) to nish the message. Finally enter quit to exit telnet.

To test Dovecot, enter the following command.

telnet mail.yourdomain.com pop3

You will see following output,

Trying 104.36.18.239...
Connected to mail.rackvoucher.com.
Escape character is '^]'.
+OK Dovecot ready.

It tells that Dovecot is working ne, you can login to your mail account by providing login command, then use pass command to enter your
password. To view the mails in your account, use retr command.

user mailuser
+OK
pass Password
+OK Logged in.
retr
-ERR There's no message 1.

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 7/14
11/17/2017 How to Setup an Email Server on CentOS 7
quit
+OK Logging out.
Connection closed by foreign host.

Installing Squirrelmail
As we have both Post x and Dovecot working, we can now install Squirrelmail to your server. Squirrelmail does not comes with the default
CentOS repository, hence you will need to add EPEL repository into your system using the following command.

yum -y install epel-release

Now you can install Squirrelmail using the following command.

yum -y install squirrelmail

After installing Squirrelmail you can con gure it by running the con guration script.

cd /usr/share/squirrelmail/config/
./conf.pl

You will see following output.

SquirrelMail Configuration : Read: config.php (1.4.0)


---------------------------------------------------------
Main Menu --
1. Organization Preferences
2. Server Settings
3. Folder Defaults
4. General Options
5. Themes
6. Address Books
7. Message of the Day (MOTD)
8. Plugins
9. Database
10. LanguagesD. Set pre-defined settings for specific IMAP serversC Turn color off
S Save data
Q Quit
Command >>

In Option 1 you can change your organisation preferences. It is recommended to change it according to your organisation in production
environment. If you choose option 1, you will see following output.

SquirrelMail Configuration : Read: config.php (1.4.0)


---------------------------------------------------------
Organization Preferences
1. Organization Name : SquirrelMail
2. Organization Logo : ../images/sm_logo.png
3. Org. Logo Width/Height : (308/111)
4. Organization Title : SquirrelMail $version

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 8/14
11/17/2017 How to Setup an Email Server on CentOS 7
5. Signout Page :
6. Top Frame : _top
7. Provider link : http://squirrelmail.org/
8. Provider name : SquirrelMailR Return to Main Menu
C Turn color off
S Save data
Q Quit
Command >>

Change the organisation name, logo and title according to your need. Once done, return to main menu using R command. In main menu choose
option 2 for Server settings.

SquirrelMail Configuration : Read: config.php (1.4.0)


---------------------------------------------------------
Server SettingsGeneral
-------
1. Domain : localhost
2. Invert Time : false
3. Sendmail or SMTP : SendmailA. Update IMAP Settings : localhost:143 (uw)
B. Change Sendmail Config : /usr/sbin/sendmailR Return to Main Menu
C Turn color off
S Save data
Q Quit
Command >> 1

Change your domain name by selecting option 1.

The domain name is the suffix at the end of all email addresses. If for example, your email address is
[localhost]: yourdomain.com

Now change your MTA by selecting the 3rd option.

SquirrelMail Configuration : Read: config.php (1.4.0)


---------------------------------------------------------

Server SettingsGeneral
-------
1. Domain : rackvoucher.com
2. Invert Time : false
3. Sendmail or SMTP : SendmailA. Update IMAP Settings : localhost:143 (uw)
B. Change Sendmail Config : /usr/sbin/sendmailR Return to Main Menu
C Turn color off
S Save data
Q QuitCommand >> 3You now need to choose the method that you will use for sending
messages in SquirrelMail. You can either connect to an SMTP server
or use sendmail directly.
1. Sendmail 2. SMTP Your choice [1/2] [1]: 2

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 9/14
11/17/2017 How to Setup an Email Server on CentOS 7

Now save your setting by giving S command and nally quit using Q command.

Now you will need to install the Apache web server, so that we can access Squirrelmail using web interface. Run the following command to install
Apache web server.

yum -y install httpd

Once Apache is installed, edit the con guration le to add a new virtual host.

nano /etc/httpd/conf/httpd.conf

Now add the following lines at the end of the le.

Alias /webmail /usr/share/squirrelmailOptions Indexes FollowSymLinks


RewriteEngine On
AllowOverride All
DirectoryIndex index.php
Order allow,deny
Allow from all

Save the le and start and enable Apache web server using the following commands.

systemctl start httpd


systemctl enable httpd

Now create email users, run the following command to add a user.

useradd -m liptan -s /sbin/nologin


passwd liptan

Theabove command will add a new user liptan and the attribute -s /sbin/nologin will deny login using SSH. Last command will create a
password for the new user.

Now you can browse Squirrelmail by going to following link into the browser.

http:///webmail

You will see following screen.

Once you login you will see the following webmail interface.

You can now read your emails and send emails through this interface.

Conclusion

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 10/14
11/17/2017 How to Setup an Email Server on CentOS 7

In this tutorial we have installed an email server, using Post x, Dovecot and the Squirrelmail webmail client. You can now successfully deploy the
email server and start sending via the mail server.

Liptan Biswas
0 3 0

Want your very own server? Get our 1GB memory, Xeon V4, 20GB SSD VPS for 10.00 / month.

View Plans

Related Posts

How to Install and Secure ProFTPD Server on CentOS 7

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 11/14
11/17/2017 How to Setup an Email Server on CentOS 7

How to Install and Setup GitLab on CentOS 7

How to Secure Your SSH Using Fail2Ban on CentOS 7

Monitor Your CentOS 7 Server Using Monitorix

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 12/14
11/17/2017 How to Setup an Email Server on CentOS 7

Comments

12 Comments hostpresto.com
1 Login

Sort by Best
Recommend Share

Join the discussion

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Michael Hall 9 days ago


Worked first time, brilliant! Thank you so much
Reply Share

Nur Sakibul Huda 3 months ago


Hi I'm getting this error after modifying main.cf file:
Job for postfix.service failed. See 'systemctl status postfix.service' and 'journalctl -xn' for details.

The journalctl -xn says the following:

Aug 07 17:39:08 mail.xyz.org postfix/postfix-script[10396]: fatal: unable to create missing queue directories
Aug 07 17:39:08 mail.xyz.org postfix/postfix-script[10397]: fatal: Postfix integrity check failed!
Aug 07 17:39:08 mail.xyz.org systemd[1]: postfix.service: control process exited, code=exited status=1
Aug 07 17:39:08 mail.xyz.org systemd[1]: Failed to start Postfix Mail Transport Agent.

Please help me resolve the issue.


Reply Share

tanvirul islam 4 months ago


i am having issue with the smtp. when i use "telnet mydomain.com pop3", it is working exactly shown above but when i try to use "telnet
mydomain.com smtp", it says "telnet: connect to address 123.456.789.12: Connection refused" i have disabled the firewall but still facing the same
issue. any solution??
Reply Share

The_Don_Himself > tanvirul islam 4 months ago


I had this problem too, but first, this is one awesome tutorial, thanks Liptan!

Now to solve this issues, please note a small error but the writer in

sudo vi /etc/postfix/main.cf

inet_interfaces = all
inet_protocols = all
inet_interfaces = localhost

inet_interfaces = all is overwritten by localhost binding it to localhost. Delete or uncomment that line, then restart postfix.

sudo systemctl restart postfix

Cheers!
Reply Share

ArafatX > The_Don_Himself 3 months ago


Doesn't work.
Reply Share

Liptan > tanvirul islam 4 months ago


Try disabling SELinux
Reply Share

love guru 9 months ago


Hello, dear special thanks to give this tutorial .God bless.
Reply Share

M Bogdan a year ago


Hi, you are missing the <directory>...</directory> directives in /etc/httpd/conf/httpd.conf

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 13/14
11/17/2017 How to Setup an Email Server on CentOS 7

Reply Share

Noel Becks a year ago


Your love for Cento OS7 is really admirable and enviable. Your knowledge of it is likewise respected. Liptan I may be new to this OS but I've catched
the cento fever right now and I want to try put everything I've learned on here myself. Thanks for making me love what you love. Its really admirable.
Reply Share

Warrick Philip Shannon a year ago


Hey Liptan, your knowledge on this topic seems incredibly deep! Thanks for the code and pointers. This tutorial makes it easy for anyone to install
an email server, using Postfix, Dovecot and the Squirrelmail webmail client!

Thanks for posting


Reply Share

Emmanuel Babatunde a year ago


Hello Liptan Biswas, I really appreciate this Fantastic article; right level of depth and explanation. I am new to
setting up mail servers on centos 7, and this article was incredibly
helpful. Now have a working mail server for JIRA to use; Thanks for the Great tutorial!
Reply Share

Ishem DIB a year ago


hi, thank you for the detailed explanation, what easier way to deploy
mail server on linux, I had in mind for some time to install an email
server on my machine but I abandoned that idea thinking it was some complicated
thing, your tutorial proved me wrong and now I'll do it knowing that some
command line are sufficient
Reply Share

ALSO ON HOSTPRESTO.COM

How to Install and Setup Mantis Bug Tracker on Ubuntu 16.04 5 Free Tools for Testing Website Speed
1 comment 5 months ago 1 comment 2 months ago
Douglas C. R. Paes Hello.Thank you for the blogpost.One minor Load Focus https://loadfocus.com/websi... Website Speed Test Tool to
mistake, the php library package is libapache2-mod-php7.0, and not analyse website speed results and find speed bottlenecks.

Django Rest Framework: A Guide How to Install Gibbon on Ubuntu 14.04


7 comments 5 months ago 4 comments 5 months ago
Jahid Great article!Do you know where I can find resources to learn Emon Excellent tutorial! I have get some new information from this
how to enable AngularJS to upload files to a Django REST framework which i did not know before. Thank you so much.
backend?

Subscribe d Add Disqus to your siteAdd DisqusAdd Privacy

HostPresto! Were known as Dream Hosting (dream-hosting.co.uk) until June 2014

https://hostpresto.com/community/tutorials/how-to-setup-an-email-server-on-centos7/ 14/14