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


  • 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. “” 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 mx.MX Record Check

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

    A Record Check

    Now the “answer” section shows the IP address of 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/ the myhostname parameter and update it from “localhost” to the FQDN, e.g.

    myhostname =

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

    virtual_alias_domains =
    virtual_alias_maps = hash:/etc/postfix/virtual

    Make sure that the mynetworks line looks like below

    mynetworks = [::ffff:]/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

By Supratim Roy

I'm a technology enthusiast with over fifteen years of hands-on work experience in the IT industry. My specialization in web development technology, allows me to quickly adopt innovative approaches and leading-edge technology solutions. After passing the HS, I joined B.Com. under the CU. From the second year, I started studying computer courses. After graduating in Computer Applications (BCA) from Indira Gandhi National Open University (IGNOU), I started my career as a Jr. Software Dev. in 2005. After four years, in 2009, I got the opportunity to pursue a master's degree. In 2011, I completed MCA from Sikkim Manipal University (SMU). I carried out the journey of my carrier as Jr. Software Dev., Sr. Software Dev., TL, and PM. Now I'm working as the VP of Technology & Product at CodeClouds. I use my technical knowledge, skill, expertise, and previous experience to analyze the requirements of the clients and understand their business philosophy. I try to deliver quality solutions and products in a timely manner with a high percentage of accuracy. I also prepare and manage project specifications, scope documentation, workflow, data diagram, and development documentation. I lead a team of around 50 highly talented members in a well-formed and structured way. I love listening to music and watching the action, adventure, thriller, horror movies, and Discovery, Animal Planet, and Nat Geo TV channels. Photography is also one of my hobbies, however, I'm really an amateur! I like to read technical articles, journals, and documentaries about Indian Himalayas and wildlife. I'm passionate about adventure sports. I encourage peoples to love adventure sports. I've traveled to many places and trekked in several parts of the Himalayas in India and Nepal. I'm a member of "MTG - Miles To Go" - a nature lover's association in Kolkata. I've done the Rock Climbing course from Parvat Abhiyatri Sangha affiliated with Anandabazar Patrika. I also participate in several community initiatives taken by an NGO.

Leave a comment

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