Install Postfix (email server) in Linux

by | Jan 14, 2015 | RHEL / CentOS

This article shows you how to install and configure a simple mail server called Postfix. It can be used to send emails or act as a email relay server for internal network wherein only one Server is allowed access to Internet and then all clients hosts will redirect email to this Postfix email relay server.

We would need to install two packages via YUM.

# yum -y install postfix
# yum -y install mailx
# yum -y install cyrus-sasl-plain (If using GMAIL as external relay host)

 Once the install is done we need to edit the config file of Postfix

# vi /etc/postfix/

 Lines to change 74,98,113,116,264,426
NOTE: Line numbers can change with the version of Postfix
* You can press “:set nu” while editing the file to get the line numbers

Change myhostname

myhostname = hostname.domain

Change origin

myorigin = $myhostname (or) $mydomain

 * This is the field which shows you FROM field in the email.

Change protocol on which it will listen. You can use all for ALL protocols i.e. IPv4 & 6

inet_interfaces = all 

If you want to restrict by Interface IP (eth0/eth1) change proxy interface

proxy_interfaces =

Now change the networks it will listen to.

mynetworks = 172.xx.xx.0/24

Enable this field as well

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

Enable the spool directory

mail_spool_directory = /var/spool/mail

Now restart postfix after the above changes are made

# service postfix restart

Try sending test email locally to users

Mail root@hostname
Subject: This is test email Her is where the message goes . EOT

Now simply type “mail” and it will show you the mail received locally.

Now if you want the Alias FROM: in Postfix, carry out the below changes
Edit /postfix/generic

# vi /etc/postfix/generic

Edit postfix/

# vi /etc/postfix/
smtp_generic_maps = hash:/etc/postfix/generic

Save and quit the file

# postmap /etc/postfix/generic
# postfix reload

Now change the name in passwd file from root to your preferred name

# vi /etc/passwd
root:x:0:0:Own Name:/root:/bin/bash

Save and quit the file

NOTE: If you receive an error “Postfix: /usr/sbin/sendmail: No such file or directory”. There might be a symlink which is missing between sendmail and postfix. Create one.

# ln -s /usr/sbin/sendmail.postfix /usr/sbin/sendmail

If sending emails in POSTFIX through external SMTP, follow the steps below.
You will need to make changes in your configuration file (in standard postfix configuration it would be /etc/postfix/ You need to add at the end of configuration file following lines:

# vi /etc/postfix/
relayhost =
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes
smtp_cname_overrides_servername = no
smtp_sasl_security_options = noanonymous

Make sure that you will edit “relayhost” (in first line of this listing), with your account SMTP address and port number.

If using Gmail as the SMTP provider
Add this in /etc/postfix/

# vi /etc/postfix/
relayhost =
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes

Postfix have to know your account login and password as most SMTP servers would not allow to rely emails from unauthorized connections. You will need to make a password file “/etc/postfix/sasl_passwd”.

For “relayhost” in /etc/postfix/ username:password
# postmap hash:/etc/postfix/sasl_passwd (to embed the entries into Postfix database)

You may test it by typing:

# postmap -q /etc/postfix/sasl_passwd

If you will get an output like “username:password” it means that password file has been generated correctly.
You can remove “/etc/postfix/sasl_passwd“. POSTFIX will not need it anymore.
Its all ready now and you can Restart your POSTFIX and test it.

You can also restrict the Recipients Clients for email if you want to by following the steps below.
Create a file called “rbl_override” with the allowed IP’s of hosts in it.

# vi /etc/postfix/rbl_override       OK       OK       OK
 # postmap /etc/postfix/rbl_override (to embed the entries into Postfix database)

Now Need to edit in postfix

# vi /etc/postfix/

 And add below lines

smtpd_recipient_restrictions = check_client_access hash:/etc/postfix/rbl_override,

 Restart Postfix now and now you are ready to start sending emails.

# service postfix restart

If you encounter any errors during the process, let us know via Contact Us and will get it resolved promptly.

Related Articles….