Virtual alias domains works OK if you want all your email going to local user mailboxes. That means
user1@x.domain.com and
user1@y.domain.com are each going to have different Linux user accounts since they both can't have user1. This can be confusing to users and is harder to manage.
If you want each
user1@x.domain.com and
user1@y.domain.com account to have it's own mailbox but not a Linux user account, you need virtual mailboxes. This is how I do it on my Ubuntu Postfix box at home.
**Make sure to back up your config files before making any changes!**
First make sure Postfix has the virtual module available to use. Look for the virtual line in /etc/postfix/master.cf. If it's there (which it should) continue.
Create an account that will have access to the virtual mailboxes directory and one for each domain to write to them.
Code:
useradd vuser -u 2000 (or any uid you want above 1000)
groupadd vuser -g 2000 (same as uid)
useradd xdomain -u 2001
groupadd xdomain -g 2001
useradd ydomain -u 2002
groupadd ydomain -g 2002
Then create the base directory for your virtual mailboxes.
Code:
mkdir /var/spool/virtual_mailboxes
chown vuser:vuser /var/spool/virtual_mailboxes
chmod 755 /var/spool/virtual_mailboxes
Next, create the subdirectories.
Code:
mkdir /var/spool/virtual_mailboxes/x.domain.com
mkdir /var/spool/virtual_mailboxes/y.domain.com
chown xdomain /var/spool/virtual_mailboxes/x.domain.com/
chown ydomain /var/spool/virtual_mailboxes/y.domain.com/
chgrp xdomain /var/spool/virtual_mailboxes/x.domain.com/
chgrp ydomain /var/spool/virtual_mailboxes/y.domain.com/
chmod 700 /var/spool/virtual_mailboxes/x.domain.com/
chmod 700 /var/spool/virtual_mailboxes/y.domain.com/
Next you need to build a file that you can enter all your email addresses into. Call it /etc/postfix/virtual_build_map_source.
Code:
user1@x.domain.com x.domain.com/user1/ 2001 2001
user2@x.domain.com x.domain.com/user2/ 2001 2001
user1@y.domain.com y.domain.com/user1/ 2002 2002
user2@y.domain.com y.domain.com/user2/ 2002 2002
Then create a script to take that virtual_build_map_source file and create all the other needed files that Postfix will need. Call it /etc/postfix/build_virtual_maps.sh.
Code:
# !/bin/bash
#
# Build all virtual mailbox maps from one source
# section: paths
SOURCE=/etc/postfix/virtual_build_map_source
VMAP=/etc/postfix/virtual_mailbox_recipients
VUID=/etc/postfix/virtual_uid_map
VGID=/etc/postfix/virtual_gid_map
AWK=/usr/bin/awk
POSTMAP=/usr/sbin/postmap
# section: build
# build $virtual_mailbox_maps
$AWK '{printf("%s %s\n",$1,$2)}' $SOURCE > $VMAP
$POSTMAP hash:$VMAP
# build $virtual_uid_maps
$AWK '{printf("%s %s\n",$1,$3)}' $SOURCE > $VUID
$POSTMAP hash:$VUID
# build $virtual_gid_maps
$AWK '{printf("%s %s\n",$1,$4)}' $SOURCE > $VGID
$POSTMAP hash:$VGID
Make the file executable.
Code:
chmod 755 /etc/postfix/build_virtual_maps.sh
Then run the script and it will create the virtual_uid_map, virtual_gid_map, virtual_mailbox_recipients files and postmap them. You should also create a virtual_aliases file for any aliases you want set up. The aliases format is simple. Just write the incoming address, a space, and what address you want it to go to. Then postmap it.
Code:
user1@x.domain.com user2@y.domain.com
Go into main.cf and add the following lines:
Code:
## Virtual Mailboxes
virtual_mailbox_domains = x.domain.com, y.domain.com
virtual_uid_maps = hash:/etc/postfix/virtual_uid_map
virtual_gid_maps = hash:/etc/postfix/virtual_gid_map
virtual_mailbox_base = /var/spool/virtual_mailboxes
virtual_mailbox_maps = hash:/etc/postfix/virtual_mailbox_recipients
virtual_alias_maps = hash:/etc/postfix/virtual_aliases
DO NOT PUT YOUR VIRTUAL DOMAINS IN "mydestination=" OR POSTFIX WILL TRY TO DELIVER THOSE DOMAINS LOCALLY!
Postfix will now start delivering all incoming mail to those virtual mailboxes. The next step would be to set up your POP3 or IMAP server to allow users to get those emails. I use dovecot-pop3 and dovecot-imap.
/etc/dovecot/dovecot.conf
Code:
base_dir = /var/run/dovecot/
protocols = imap pop3
log_path = /var/log/dovecot
info_log_path = /var/log/dovecot.info
login_dir = /var/run/dovecot/login
login_chroot = yes
login_user = dovecot
protocol imap {
login_executable = /usr/lib/dovecot/imap-login
mail_executable = /usr/lib/dovecot/imap
}
protocol pop3 {
login_executable = /usr/lib/dovecot/pop3-login
mail_executable = /usr/lib/dovecot/pop3
pop3_uidl_format = %08Xu%08Xv
}
valid_chroot_dirs = /var/spool/virtual_mailboxes
default_mail_env = maildir:/var/spool/virtual_mailboxes/%d/%n
disable_plaintext_auth = no
auth default {
mechanisms = plain digest-md5
userdb passwd-file {
args = /etc/dovecot/users
}
passdb passwd-file {
args = /etc/dovecot/passwd
}
}
auth_executable = /usr/lib/dovecot/dovecot-auth
auth_user = root
auth_verbose = yes
Create /etc/dovecot/users and include all your users in there in the following format:
Code:
user1@x.domain.com::2001:2001::/var/spool/virtual_mailboxes/x.domain.com/:/bin/false::
user2@x.domain.com::2001:2001::/var/spool/virtual_mailboxes/x.domain.com/:/bin/false::
user1@y.domain.com::2002:2002::/var/spool/virtual_mailboxes/y.domain.com/:/bin/false::
user2@y.domain.com::2002:2002::/var/spool/virtual_mailboxes/y.domain.com/:/bin/false::
Make the file only usable by root and readable by root group.
Code:
chmod 740 /etc/dovecot/users
Create the following script and put it into /usr/sbin/mkdovecotpasswd.
Code:
#!/bin/bash
mkpasswd --hash=md5 $2 > /tmp/hash
echo "$1:`cat /tmp/hash`" >> /etc/dovecot/passwd
Make it executable.
Code:
chmod 755 /usr/sbin/mkdovecotpasswd
Make your Dovecot password file and give it only root rights.
Code:
touch /etc/dovecot/passwd
chmod 740 /etc/dovecot/passwd
Now you need to assign passwords for your users. Run your new script in this format:
Code:
mkdovecotpasswd user1@x.domain.com password
Do this for every user you have. If you ever need to change a password, just delete the corresponding line in /etc/dovecot/passwd and run the command again with the new password.
To add a new user, add them to the /etc/virtual_build_map_source file, run /etc/postfix/build_virtual_maps.sh, add them to /etc/dovecot/users, and run the mkdovecotpasswd command to assign them a password. All your users can now retrieve mail via POP3 and IMAP (assuming your box is set to accept ports 110, 143, and 25 and/or have them forwarded from your router to your server) and users will be using their full email address as their username.
Restart Postfix and dovecot and you should be good.