Saturday, November 25, 2017

Postfix, how to email with Linux




When I think about email in Linux, the first word that comes into my mind is definitely Postfix. Postfix basically is a free and open source MTA (Mail Transfer Agent), which simply routes email messages. So by using this open-source software you can implement a robust and highly reliable SMTP server.

 So now I’ll try to install and configure Postfix on my Centos 7 server. 
To start let’s get and install the packages. (In the latest releases of Centos Postfix is pre-installed).

#yum -y install postfix

After the rpm deployments we can spot our configuration directory at /etc/postfix , master.cf and
main.cf are our main configuration files.
At the moment, for a basic configuration, we’re only concerned about main.cf, use your favorite editor to open this file (mine is vi editor and I get a lot of trolling from my fellow linux admins about this).

So to start we need to specify a few parameters, locate :

myorigin directive, at the SENDING MAIL section of the conf, here we just need to give the machine’s hostname.

mydomain at INTERNET HOST AND DOMAIN NAMES, where we specify the local domain suffix of the machine eg:mydomain.local.

myhostname where we specify our internet hostname address, this address must refer to a register domain, eg:myhostname.mydomain.com  

mynetworks under TRUST AND RELAY CONTROL where we specify the ip addresses we want to allow email relay from. Those must be written in a pattern of ip/network mask eg: 10.0.100/24.  
Now we may have some nasty ip addresses in our allowed networks spamming our server, so to prevent this we must have a blacklist to put them inside. For this we need to add the following:

smtpd_recipient_restrictions =
    check_client_access hash:/etc/postfix/blacklist
    permit_mynetworks,

Now we have a blacklist file where we can block senders.
Well, actually we have the reference and not the file, to create this file give :

# touch /etc/postfix/blacklist

And to make the file active give:
# postmap /etc/postfix/blacklist

Now we can block a sender by editing the file and simply adding 

“sender’s Ipadress” REJECT  

It seems we’re all set, so to start our server we need to :

#systemctl start postfix 

Althought

#service postfix start

can still do the job

and don’t forget to make the service initializing on system startup
#systemctl enable postfix
So now we have an all working SMTP alive and we can check the status
# systemctl status postfix
Server is able to receive connections (port 25). Now we can watch the incoming email messages flowing in and out and monitor the relaying process by having a look at the logs:
# tail –f /var/log/maillog
Sometimes there are emails which can’t be send for various reasons, and are stuck in the server’s queue, we can list them by giving:
#mailq

and we can examine those emails by opening them, in text format of course
#postcat –qv “queuefilename”

Or get rid of all of deferred emails in the queue
#postsuper –d ALL deferred