How to add e-mail accounts to Postfix on Ubuntu Server 20.04

Louis SanchezJuly 29th 2021, 8:09

Follow these steps to add e-mail account:

Step 1 : Create a new user account:

sudo useradd -s /usr/sbin/nologin -m usermail

Step 2 : Get the UID and GID for this account

sudo grep usermail /etc/passwd

Step 3 : Create a base directory layout for domains and users

sudo mkdir -p /home/usermail/example.org/user1
sudo mkdir -p /home/usermail/example.net/user2

Step 4 : Allow only the user usermail to access these files:

sudo chown -R usermail:usermail /home/usermail
chmod -R 700 /home/usermail

Step 5 : Next, configure Postfix. Edit /etc/postfix/main.cf and add the following lines:

virtual_mailbox_base = /home/usermail
virtual_mailbox_domains = /etc/postfix/virtual_domains
virtual_mailbox_maps = hash:/etc/postfix/virtual_maps
virtual_alias_maps = hash:/etc/postfix/virtual_alias
virtual_uid_maps = static:1000  # user ID for user usermail
virtual_gid_maps = static:1000  # group ID for user usermail

Step 6 : Create the file virtual_domains under /etc/postfix

sudo nano /etc/postfix/virtual_domains

example.org
example.net

Step 7 : Create the virtual_maps file

sudo nano /etc/postfix/virtual_maps
user1@example.org  example.org/user1/
user2@example.org  example.org/user2/
@example.org  example.org/catchall/   # catch all address

Step 8 : Create the virtual_alias file and optionally set a redirect

sudo nano /etc/postfix/virtual_alias
# redirect emails for tim to bob
user3@example.org  user1@example.org

Step 9 : Now generate database of virtual maps and aliases by hashing respective files:

sudo postmap /etc/postfix/virtual_maps
sudo postmap /etc/postfix/virtual_alias

Step 10 : Reload Postfix

sudo postfix reload

Step 11 : send an e-mail to the newly created address:

mail user1@example.org