Configure Postfix to use an Alternate Port for SMTP

Hi Friends,

Hope you are doing well.

As we know Email is the basic part/need of our life. We are surrounded by and depended on it. Our regular and important task also depends on it. We send and receive mail every day.

If we talk about working on emails then we can say that it works on mainly 2 protocols. SMTP and POP/IMAP.

SMTP which is called as Simple Mail Transfer Protocol is used for sending emails and it works on standard port 25, port 465 (Non-secure) and port 587 (secure).

POP/IMAP (Post Office Protocol/ Internet Access Message Protocol) used for receiving emails and it works on standard port 110 and port 143 respectively (Non-secure) & port 995 and port 993 respectively (secure).

We might have noticed that our ISP (Internet Service Provider) blocks our default/standard port for security purpose. Generally, they block non-secure SMTP port i.e.25.

If you come across such situation, do not panic. I will discuss here how you can use another port for your SMTP service. I will discuss Postfix in this post as I have seen it is mostly used for emails. So let’s start.

First, execute the below command and check which port is running for SMTP service in your server:

netstat -ntlp

The above command will show on which port SMTP is running.

Netstat

As we see that port 25 is used for SMTP service. Let’s check whether the server: 139.59.13.87 is listening to this port or not (from outside).

Ip address

Execute the below command to check whether port 25 is listening or not.

telnet 139.59.13.87 25

Telnet

As we see that server: 139.59.13.87 is listening to port 25.

As many ISP blocks this port for security reason, we will run the SMTP service on a different port so that we can send our emails without any issue. Let’s say we want to run the SMTP service on the port 2525. The steps are very simple. Please follow the below steps:

  • Go to the directory /etc/postfix and you will see a file name called master.cf.
  •  First of all, take a copy of this file (for the safety purpose). Refer the below snapshot for your reference.

  • Now open the file master.cf and find the line labelled as: ‘smtp    inet    n    …..’
  • Just below the line, insert the new line containing the following:
  • 2525     inet     n     –     n     –     –     smtpd

Alternate port

Note: 2525 can be changed to match the port that you wish to use. Also make sure you comment the line: ‘smtp    inet    n     …..’ so that SMTP will run only on the new port. If you do not comment this line, then SMTP will run on 2 ports, one is port 25 (default) and another is the port that you are going to set (port 2525 in this case)

  • Save the file and restart postfix.

service postfix restart

Now let’s check on which port, our SMTP service is running. Execute the same netstat command.

 

Alternate port

As we can see that our new SMTP port is now running.

  • Now next step to add this port to the firewall so that it can accept incoming connections. As IPTABLES is installed on this server, we will add allow the port 2525 in this. Execute the below command to add the port and save the rule.

iptables -A INPUT -p tcp --dport 2525 -j ACCEPT

iptabels-save

Verify the new port in iptables. Execute iptables -L

Iptables

It is now set. Further, you can also verify whether the new port is listening from outside or not using telnet.

telnet

Yes, it is listening. Now try to send an email and check whether you are able to send it or not.

mail -s "<Subject>" <Email Address> <<< "<Message>"

mail -s "test mail" testmail@beanexpert.co.in <<< "This is the body of the mail. SMTP is running on port 2525"

Mail Received

Sending of email is working fine.

I hope you like this post. Feel free to contact us, if you face any issue. Thank you. 🙂

Powered by Facebook Comments

Be the first to comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.