Postfix configuration introduction
Postfix is an alternative to sendmail. It is a free and opensource MTA. Postfix works as an SMTP server and provide defense against malware and spambots. However, postfix has a very limited features and requires third-party software. Postfix uses SMTP and Milter(Mail Filter) for full control over message and content.
Postfix works as a pipeline of processes, it means if one process fails in delivering the message, postfix passes the task to other process and the other process draws back and retries the process later. This makes postfix very flexible.
In this article we will learn about a part of mail server that is how to carry out postfix configuration to work as an SMTP server for sending mail via Gmail in Linux
Install required packages
The package mailx and cyrus-sasl-plain must be installed with postfix.
# yum install postfix mailx cyrus-sasl-plain
You must start the postfix service when it is installed. Please note that for CentOS7 and Redhat7 use the 'systemctl' command and for the lower version OS you can use the command 'service'.
# systemctl start postfix
# service postfix start
Sendmail is installed by default. Stop and remove the sendmail service.
# service sendmail stop # yum remove sendmail
Enable boot start
Enable the postfix service to start at boot time.
# systemctl enable postfix
# chkconfig postfix on
Configure your postfix main.cf file as given below. The relayhost we are using here is smtp for gmail, because we will be carrying out postfix configuration to send mail via gmail smtp server. Change the hostname to your server hostname. Copy and paste the rest of the line at the bottom your main.cf file.
# vi /etc/postfix/main.cf
myhostname = mail.mailmania.com
relayhost = [smtp.gmail.com]:587 smtp_sasl_password_maps = hash:/etc/postfix/pass_sasl smtp_tls_CAfile = /etc/ssl/certs/ca-bundle.crt smtp_sasl_security_options = noanonymous smtp_sasl_tls_security_options = noanonymous smtp_sasl_auth_enable = yes smtp_use_tls = yes
Create sasl file for postfix
Create a file named 'pass_sasl' under postfix directory as given. Edit it and give a valid gmail id and password.
# vi /etc/postfix/pass_sasl
Generate postfix lookup table
The postmap command will create a posfix database file. Posfix uses this database file as a lookup table. These lookup tables are used to store information for access control.
# postmap /etc/postfix/pass_sasl
Securing sasl_passwd file
Change the user and group ownership for the given file. You certainly don't want the hackers to gain access of your secure pass_sasl file.
Change the user and group ownership as given below.
# chown root:postfix /etc/postfix/pass_sasl*
Change the file permission.
# chmod 640 /etc/postfix/pass_sasl*
You need to reload posfix after postfix configuration of main.cf file.
# systemctl reload postfix
# service postfix reload
Setup postfix as default
By now I assume you are done with postfix configuration and reloaded postfix service.
So if you have more than one mta installed on the smtp server, you have to set postfix as default mta. Verify it by below command and type selection number to choose postfix.
# alternatives --config mta
There are 2 programs which provide 'mta'. Selection Command ----------------------------------------------- * 1 /usr/sbin/sendmail.postfix + 2 /usr/sbin/sendmail.ssmtp Enter to keep the current selection[+], or type selection number:1
Test your mail by echo command to check if postfix working.
# echo "Testing mail." | mail -s "TEST" firstname.lastname@example.org
That's it with the article Postfix configuration as a SMTP server on Linux