Postfix configuration in ubuntu

Postfix is a free and open-source mail transfer agent. It is the most suitable, easy to configure, and widely-used Sendmail programme for a dedicated web server. I’ve a dedicated server hosted by Digital Ocean and the server is configured with LAMP stack. Recently I needed an email with the blogpipers.com domain to verify the domain with Sendgrid – a transactional email platform. So I’ve googled around and found this mailing software as the most suggested one. Then I searched a bit more for the setup and configuration and it seemed very easy to me.

Postfix Setup & Configuration

Prerequisites:

  • You should have an FQDN (Fully Qualified Domain Name) pointed at your server. Otherwise, the mail configuration won’t work properly.
  • Make sure that the iptables firewall is not blocking any of the standard mail ports (25, 465, 587, 110, 995, 143, and 993).

Installation and Configuration steps:

  1. It is always a good practice to run sudo apt-get update && sudo apt-get upgrade before installing any new package or component.
  2. Then install the postfix by running sudo apt-get install postfix. It will ask you what type of mail configuration you want to have for your server. Choose “Internet Site” for this option. Next, you will be asked for the FQDN for your server i.e. “blogpipers.com” in our case.
  3. Postfix should get started automatically after the installation. You can verify it by running sudo service postfix status and it should show you postfix is running
  4. You can also check if it is running a mail server on port 25 with the netstat command sudo netstat -ltnp | grep 25netstat
  5. Now you need to verify the DNS settings for your domain. It is essential to ensure that the MX records for your domain are pointing to the right server where the mailing software is installed. This can be checked with the dig command. Run dig blogpipers.com mx.MX Record Check

    In the “answer” section you can see that mail.blogpipers.com is reported as the mail server for blogpipers.com. Now, dig the a records for the domain mail.blogpipers.com and it will show the server IP to which it points.

    A Record Check

    Now the “answer” section shows the IP address of mail.blogpipers.com which over here is correct. Do the same for your domain and verify that the MX records are setup correctly.

  6. Run the postconf | grep config_directory command to identify the configuration path. It should be like config_directory = /etc/postfix. So now we know the configuration directory and we can edit the configuration file. Run sudo nano /etc/postfix/main.cf.Find the myhostname parameter and update it from “localhost” to the FQDN, e.g.

    myhostname = blogpipers.com

    Add the following lines at the end of the configuration file for email alias and forwarding.

    virtual_alias_domains = blogpipers.com
    virtual_alias_maps = hash:/etc/postfix/virtual

    Make sure that the mynetworks line looks like below

    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128

  7. Next, edit the virtual file sudo nano /etc/postfix/virtual and add the emails which will go to which user
    [email protected] username1
    [email protected] username2
  8. Save and close the file and we can now implement the mapping by running this command:sudo postmap /etc/postfix/virtual

    You need to reload the service to have the effect of the changes:

    sudo service postfix restart

Leave a Reply

Your email address will not be published. Required fields are marked *