Loading
29 Oct 2016

821

1028

58

Configuring Postfix and Virtualmin on Ubuntu with name@domain.tld usernames

Servers by Digital Apps

If you have existing users in your Virtualmin account, by default, usernames used to access email via pop3/imap are created with a username.domain convention. If you want to change the username convention to name@domainname.tld there are a couple of issues that you may encounter.

Unknown user problem

<username.domainname@hostname> (expanded from <name@domainname.tld>): unknown user: “username.domainname

User unknown in virtual alias table

Delivery to the following recipient failed permanently:

user@domainname.tld

Technical details of permanent failure:
Google tried to deliver your message, but it was rejected by the server for the recipient domain domainname.tld [your-ip].

The error that the other server returned was:
550 5.1.1 <user@domainname.tld>: Recipient address rejected: User unknown in virtual alias table

Solution

DNS Records Configuration

Setting up a mail server consist of many moving parts. It like a chain of components all linked together into one big system. In order for us to identify where the break occurs, we have to start our investigation from the bottom.

First thing we need to check our DNS records. I use Amazon Route 53 to manage DNS and these 3 records a very important to get your mail working.

  1. Create an A record for mail.your-domain.tld
  2. Create an A record for your server hostname, this name can be anything that you desire, in my case its server.domainname.tld
  3. Create an MX record, with a name mail.your-domain.tld and a value 5 mail.domainname.tld

Once you have your DNS setup, run this command in your terminal:

dig domain.com MX

If everything has been setup correctly you should see this in the response:

;; AUTHORITY SECTION:
digitalapps.co. 899 IN SOA ns-900.awsdns-48.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400

If you are not seeing above in the response then you will be served with the following message, you may also need to revisit your DNS records.

;; ANSWER SECTION:
digitalapps.co. 299 IN MX 5 mail.digitalapps.co.

Linux Box Configuration

When you are done fiddling with your DNS, it is time to SSH into your Linux box to run a couple more checks.

Once logged in, run

hostname -f

and make sure that the hostname served matched your DNS records mentioned in the point #2. If you need to update your hostname, run this command to edit the host file

sudo nano /etc/hostname

Another crucial step is to get our hosts listed under /etc/hosts. To check if we have correct settings, let’s run the following command:

sudo nano /etc/hosts

Now make your hosts file starts with the localhost on the first line with subdomains from your DNS records to follow, like so:

127.0.0.1 localhost
127.0.0.1 server.digitalapps.co
127.0.0.1 mail.digitalapps.co

Once you saved both files, reboot your box by running:

sudo reboot

Webmin/Virtualmin Configuration

The first thing to check once you are logged in to Webmin is our Postfix Mail Server configuration file. This can be also achieved via SSH.

Via Webmin: Webmin>Servers>Postfix Mail Server>Edit Config Files and Edit config file: /etc/postfix/main.cf.

Via SSH: sudo nano /etc/postfix/main.cf

Look for mydestination line in your config file and make sure it matches this:

mydestination = mail.digitalapps.co, server.digitalapps.co, localhost.digitalapps.co, localhost

Lastly, let’s change the way our users login to the mail server. Login to Webmin and go to System>Users and Groups.

From within Users and Groups window, you will be able to update the username from username.domain to name@domain.tld.

Once the username has been updated, click on Virtualmin tab, select the appropriate virtual server and click on Edit Users. Click on the user that you have just updated with a new username. Next to IMAP / POP3 / FTP login you should be seeing a new user name name@domain.tld. This is a very important step to avoid User unknown in virtual alias table problem, under Email settings make sure that Primary email address enabled? is set to Yes.

You should be all set after this.

Bonus Commands

To access virtual alias table using Webmin, go to Webmin>Servers>Postfix Mail Server>Edit Config Files and Edit config file: /etc/postfix/virtual.

To access virtual alias table using SSH, run following command:

sudo nano /etc/postfix/virtual

To see contents of the virtual alias table, run following command:

postmap -s /etc/postfix/virtual | grep digitalapps.co

If you get an error about/etc/postfix/virtual.db not found in your directory, run this command first:

postmap /etc/postfix/virtual

Once new aliases have been created, run below command to refresh the databases hash:

postalias hash:/etc/postfix/aliases or newaliases to update your aliases.db file.

Once the configuration files have been updated, postfix must be reloaded, run this command:

postfix reload