Configuring email virtual hosting using sendmail

From Notes_Wiki
Revision as of 14:22, 13 March 2022 by Saurabh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Home > CentOS > CentOS 6.x > Email server configuration > Sendmail server configuration > Configuring email virtual hosting using sendmail

If you are new to email server configurations, then it is recommended to try postfix before attempting to use sendmail.

We can host email services for multiple domains like @domain1.com, @domain2.com, etc. on same email server with single IP address. For this the MX records for all the domains to be hosted on server should point to the IP address of the server. Until DNS MX records are properly set email system will not work.


Choose outgoing email domain

Choose email domain for outgoing emails. Suppose outgoing emails from users should go as '<user_name>@sbarjatiya.in' by default. Then edit file '/etc/hosts' and type domain name 'sbarjatiya.in' as first item in front of localhost addresses, i.e. 127.0.0.1 and ::1. This would make reverse resolution of local IPs to default domain name. Note that local hostname resolution must happen through /etc/hosts even if there is DNS entry. Also this resolution must happen through first non-commented line of /etc/hosts. If other entries are added above this entry, even then sendmail will have issues.

Sample '/etc/hosts' file is

127.0.0.1       sbarjatiya.in commonhosting localhost.localdomain localhost
::1             sbarjatiya.in commonhosting localhost6.localdomain6 localhost6



Configuring sendmail

Basic configuration of sendmail is done in file '/etc/mail/sendmail.mc'. After sendmail configuration files are modified we go to folder '/etc/mail' and run 'make'. Then we use 'service sendmail reload' so that new configuration is used in place of old one.


Alias file

To configure sendmail to use aliases file following line should be present in 'sendmail.mc' file

define(`ALIAS_FILE', `/etc/aliases')dnl

By default on most distributions aliases file is enabled. In some distributions the file can be at location '/etc/mail/aliases.

Aliases file can be used to forward email sent to <user_name>@<any_local_domain> to other users on same system or email addresses hosted on other email servers. This file can also be used to forward copy of received email to more then one destinations and hence is very useful when same email needs to be delivered to more then one user.

The limitation of aliases file is that all decisions are based on only username. We can specify aliases such that mails sent to 'user1@domain1.com' go to place X and mails sent to 'user1@domain2.com' go to place Y. Since aliases file ignores domains it would look like we want mails sent to user1 to go to place X. Since alias has already matched, second rule that mails should go to Y will not be matched.

Unlike usual configuration files when we make changes to alias file we have to use command 'newaliases' and then use 'service sendmail reload' to make new aliases effective.

Note that it is not necessary that users mentioned in aliases file exist as users in system. We can specify alias for user 'postmaster' even if there is no user named postmaster present in /etc/passwd.

Sample '/etc/aliases' file is

# Basic system aliases -- these MUST be present.
mailer-daemon:  postmaster
postmaster:     root

# General redirections for pseudo accounts.
bin:            root
daemon:         root
adm:            root
lp:             root
....
....
dumper:         root
abuse:          root

newsadm:        news
newsadmin:      news
usenet:         news
ftpadm:         ftp
ftpadmin:       ftp
ftp-adm:        ftp
ftp-admin:      ftp
www:            webmaster
webmaster:      root
noc:            root
security:       root
hostmaster:     root
info:           postmaster
marketing:      postmaster
sales:          postmaster
support:        postmaster


# trap decode to catch security attacks
decode:         root

# Person who should get root's mail
root:           barjatiya.saurabh@gmail.com


Virtual user table

In order to user virtual user table feature of sendmail, 'sendmail.mc' file should have following configuration:

FEATURE(`virtusertable', `hash -o /etc/mail/virtusertable.db')dnl

With the help of virtual user table we can specify rules like mails sent to 'user1@domain1.com' should go to following address and mails sent to 'user1@domain2.com' should go to another address.

Hence by using virtual user table we can ensure that mails sent to same username but for different domains can be sent to two different destinations. Here destinations can be some other complete email address or name of some local user. In case name is of local user, aliases file will be consulted before delivering mails to see if it is an alias and the mails should go to some other address.

In virtual user table we can specify only one destination per email address. Hence if we want to send emails to user1@domain1.com to two different locations then we can configure virtual user table to forward emails for user1@domain1.com to unused alias like 'forwardforuser1'. Then in aliases file we can configure mails sent to 'forwardforuser1' to be forwarded to actual two destinations where we wanted mails to go.

With virtual user table we can also use email domain forwarding, ie, mails sent to <any_user>@domain can be forwarded to specific destination. This allows receiving email with any username for entire domain. To do this we just specify '@domain.com' in first column without any username'

Sample '/etc/mails/virtusertable' file is

saurabh@leet.co.in      barjatiya.saurabh@gmail.com
saurabh@sbarjatiya.in   barjatiya.saurabh@gmail.com
contact@leet.co.in      barjatiya.saurabh@gmail.com
@leet.co.in             barjatiya.saurabh@gmail.com
@pbarjatiya.in          pbarjatiya@gmail.com
@sbarjatiya.in          barjatiya.saurabh@gmail.com
@abarjatiya.in          abarjatiya@yahoo.com
@guruprasadj.in         guru.j.prasad@gmail.com


We can also have virtual user table entires like

spam@sbarjatiya.in     error:nouser No such user here
@sbarjatiya.in         %1@leet.co.in

These entries mean that if mail is sent to 'spam@sbarjatiya.in' then mail server should return the indicated error rather then accepting the email. In case mail is sent to some other user for domain 'sbarjatiya.in', say 'user1@sbarjatiya.in', then mail should get forwarded to same user at 'leet.co.in'. Hence mails sent to 'user1@sbarjatiya.in' will get forwarded to 'user1@leet.co.in' and mails sent to 'user2@sbarjatiya.in' will get forwarded to 'user2@leet.co.in' and so on.


Using generics table and generics-domains

To use generics table or generics-domain the 'sendmail.mc' file should contain

FEATURE(`genericstable')dnl
GENERICS_DOMAIN_FILE(`/etc/mail/generics-domains')dnl

Generics table and generics-domains allow changing of email address on outgoing emails. By default all emails sent by some user say 'user1' would go as 'user1@defaultdomain.com'. But in order to setup fully featured virtual hosting you would also want that mails sent from some user say 'specialuser' go as 'specialuser@domainx.com' and not as 'specialuser@defaultdomain.com'. This requirement can be full filled by generics table.

We can only send emails for domains that we have listed in 'generics-domains' file. Hence, if we want to send emails with domains 'domain1.com', 'domain2.com' and 'defaultdomain.com' then 'generics-domains' file should contain all this three domain names.

Sample 'generics-domains' file is

sbarjatiya.in
pbarjatiya.in
abarjatiya.in
leet.co.in
guruprasadj.in

Sample 'genericstable' file is

webadmin_abarjatiya_in  ankur@abarjaitya.in
webadmin_pbarjatiya_in  parag@pbarjatiya.in
webadmin_sbarjatiya_in  saurabh@sbarjatiya.in
webadmin_leet_co_in     saurabh@leet.co.in
webadmin_guruprasadj_in guruprasad@guruprasadj.in

Note that names on the left are names of local users who are trying to send emails. We don't have email addresses in first column of genericstable file as selection is done based on username and we cant have two different default outgoing addresses for particular user.


Configuring local-host names

Since we would receive mails for multiple domains we have to specify to email server which domains are hosted locally and mails sent to those domains should get delivered to local users after consulting local aliases file. For this there is generally configuration to use local-host-names file in 'sendmail.mc'.

The configuration line that make sendmail look for local-host-names file is:

FEATURE(use_cw_file)dnl

Sample 'local-host-names' file is:

sbarjatiya.in
pbarjatiya.in
abarjatiya.in
leet.co.in
guruprasadj.in

Hence whenever we want to host additional domain on same server we need to make sure that:

  1. MX records for the new domain point to the correct IP address.
  2. The new domain name is added to file local-host-names
  3. Since we would also want mails to go from that new domain, the name should be added to generics-domains file too.

We can specify local domain names in 'sendmail.mc' file directly using

LOCAL_DOMAIN(`leet.co.in')dnl

In case we do not want to add some domain to local-host-names file.


Tips

Hence, by using combination of aliases file, local-host-names, virtual user table, generics table and generics domain we can host multiple email domains on same system with considerable flexibility.

In order to accommodate same username for multiple domains it is best to create local users with domain name as suffix like instead of creating user webadmin for domains sbarjatiya.in, create user webadmin_sbarjatiya_in. Then configure virtual user table so that mails for webadmin@sbarjatiya.in get delivered to webadmin_sbarjatiya_in and configure generics table so that mails sent from webadmin_sbarjatiya_in go with address webadmin@sbarjatiya.in. This way if we want to create user 'webadmin' for other domain like 'leet.co.in' then we can do that as well without causing any confusion or problem.

If mails for entire domain are going to be forwarded to some local user then it is advisable to have some quota limit for that user, so that if some one sends lot of spam emails to different usernames at same domain, then after a while user account is full and mails do not get delivered. This will protect against hard-disk getting filled and all other domains / users from getting affected.



Home > CentOS > CentOS 6.x > Email server configuration > Sendmail server configuration > Configuring email virtual hosting using sendmail