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/main.cf

 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 = 1.2.3.4

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
root  mydomain.ltd

Edit postfix/main.cf

# vi /etc/postfix/main.cf
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/main.cf). You need to add at the end of configuration file following lines:

# vi /etc/postfix/main.cf
relayhost = smtp.provider.com:25
 
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/main.cf

# vi /etc/postfix/main.cf
relayhost = smtp.gmail.com:587
smtp_use_tls=yes
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/main.cf

smtp.provider.com:25 username:password
# postmap hash:/etc/postfix/sasl_passwd (to embed the entries into Postfix database)

You may test it by typing:

# postmap -q smtp.provider.com:25 /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
172.16.66.200       OK
172.16.66.202       OK 
172.16.66.210       OK
 # postmap /etc/postfix/rbl_override (to embed the entries into Postfix database)

Now Need to edit main.cf in postfix

# vi /etc/postfix/main.cf

 And add below lines

smtpd_recipient_restrictions = check_client_access hash:/etc/postfix/rbl_override,
                                permit_sasl_authenticated,
                                reject_unauth_destination,
                                permit

 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….