Amazon Web Services - AWS EC2 - Ubuntu 14.04

Postfix & Dovecot Mail Agent

Installation and configuration of Postfix MTA (Mail Transfer Agent) on AWS EC2 - Ubuntu instance.

Postfix is used as a SMTP server for sending emails.

Other Mail Transport Agent (MTA) Servers: Sendmail, Postfix, Qmail, Exim

 

1. Installation

Check which mail server is installed

#lsof -i tcp:25

COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

master  24281 root   12u  IPv4 488988      0t0  TCP *:smtp (LISTEN)

 

where master is the name for postfix daemon.

If we get sendmail or exim then we have to remove it first:

 

# apt-get install postfix

# postconf mail_version  //verzija postfixa

mail_version = 2.11.0

 

2. Starting

# service postfix start | restart | stop | status

 

3. Configuration

# vim /etc/postfix/main.cnf

 

3.1 myorigin

This is a name other SMTP servers will see and identify my SMTP server.

myorigin = /etc/mailname   ## /etc/mailname is created on Postfix installation and contains domain name, for example: mikosofter.info

myorigin = $mydomain

myorigin = vps.mikosofter.net

 

Now when we send mail from terminal #mail mojmail@gmail.com  received email will be root@vps.mikosofter.net . If mydestination is not defined then received mail will be root@ip-172-31-26-219.localdomain

 

3.2 mydestination

 

 

 

 

 

 

 

 

 

 

4. Creating email account with Maildir and hash mapping

4.1 Create real linux user 'vmail' and its dir /var/vmail/

We want to use Postfix virtual users which will bind with vmail:vmail linux user.

So first we need to create that linux user.

# groupadd vmail -g 2222

# useradd vmail -r -g 2222 -u 2222 -d /var/vmail -m -c "mail user"      -create dir /var/vmail/ with vmail:vmail

 

4.2 Config file /etc/postfix/main.cf with hash mapping

a) Check if hash mapping is installed

# postconf -m

btree
cidr
environ
fail
hash
internal
memcache
nis
proxy
regexp
sdbm
socketmap
sqlite
static
tcp
texthash
unix

 

b) backup config files

# cp /etc/postfix/main.cf{,.orig}

# cp /etc/postfix/master.cf{,.orig}

 

c) edit config file

# vim /etc/postfix/main.cf

Add lines:

##virtual directives
virtual_alias_maps=hash:/etc/postfix/vmail_aliases
virtual_mailbox_domains=hash:/etc/postfix/vmail_domains
virtual_mailbox_maps=hash:/etc/postfix/vmail_mailbox
virtual_mailbox_base = /var/vmail
virtual_minimum_uid = 2222
virtual_transport = virtual
virtual_uid_maps = static:2222
virtual_gid_maps = static:2222

 

 

c) create Postfix virtual mail accounts with hash mapping

  c1) vmail_domains - defines which domains will be active to receive and send emails

# vim /etc/postfix/vmail_domains

localhost                           OK
localhost.localdomain         OK
mikosofter.net                    OK
brvnare.com                       OK
i-oglasnik.com                    OK

 

  c2) vmail_mailbox - associate directory (inside /var/vmail/ dir) to each email address.

info@i-oglasnik.com               i-oglasnik.com/info/
info@brvnare.com                  brvnare.com/info/
root@mikosofter.net               mikosofter.net/root/

 

Must end with /. For example: mikosofter.net/root/ is OK but mikosofter.net/root is not.

 

 

  c3) vmail_aliases - define email forwarding

root                                    root@mikosofter.net
root@localhost                          root@mikosofter.net
root@localhost.localdomain              root@mikosofter.net
postmaster@localhost                    root@mikosofter.net
sysadmin@localhost                      root@mikosofter.net
abuse@localhost                         root@mikosofter.net
@localhost                              root@mikosofter.net
@localhost.localdomain                  root@mikosofter.net
www-data@localhost                      root@mikosofter.net
www-data@mikosoft.net                   root@mikosofter.net

@vps.mikosoft.net                       root@mikosofter.net

 

 

   c4) Hash the config files. Creates binary database files.

# postmap /etc/postfix/vmail_domains

-create /etc/postfix/vmail_domains.db

# postmap /etc/postfix/vmail_mailbox

-create /etc/postfix/vmail_mailbox.db

# postmap /etc/postfix/vmail_aliases

-create /etc/postfix/vmail_aliases.db

 

   c5) Edit master.cf

# vim +/submission /etc/postfix/master.cf

then uncomment line

submission inet n       -       n       -       -       smtpd


This will enable port 587 in the case that our ISP is blocking port 25. To check if the port 25 is blocked do: $telnet ISP_IP_ADDRESS 25

 

 

5. Test Postfix

5.1.Configuration parameters

#postconf | more   //lists all Postfix configuration parameters

#postconf -n     //show main.cf file without commants

#postconf mydestination   - daje aktualni mydestination

#postconf -d mydestination   - daje default mydestination bez obzira na konfiguraciju u main.cf fajlu

 

5.2 Check if sendmail can send emails

# mail suport@mikosofert.info

 

5.3 TELNET

Send email by telnet.

From server do: $telnet mail.mikosofter.net 25

Trying 52.28.170.2...
Connected to mail.mikosoft.net.
Escape character is '^]'.
220 ip-172-31-26-219.eu-central-1.compute.internal ESMTP Postfix (Ubuntu)

ehlo mail.mikosofter.net

250-ip-172-31-26-219.eu-central-1.compute.internal
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN

mail from: root@localhost

250 2.1.0 Ok

rcpt to: smiks@gmail.comm

250 2.1.5 Ok

Subject: Naslov

Poruka neka

.

quit

 

 

5.4 log files

/var/log/mail.log

/var/log/mail.err

 

5.5 final config files

#main.cf

######## BASIC CONFIG  ###########
mydomain = mikosoft.net
myhostname = vps.mikosoft.net
myorigin = /etc/mailname
mydestination = $myhostname, localhost.$mydomain, localhost
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)

######### NETWORK  ##########
mynetworks_style = host
#mynetworks = 127.0.0.0/8 52.28.170.2 [::1]/128 [fe80::]/64
inet_interfaces = all
inet_protocols = all
relayhost =
relay_domains = $mydomain



##### MISC #####
mailbox_size_limit = 0
recipient_delimiter = +

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
unknown_local_recipient_reject_code = 550
#alias_maps = hash:/etc/postfix/aliases
#alias_database = $alias_maps


debug_peer_level = 2
debugger_command =
         PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
         ddd $daemon_directory/$process_name $process_id & sleep 5

sendmail_path = /usr/sbin/sendmail
newaliases_path = /usr/bin/newaliases
mailq_path = /usr/bin/mailq
#setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.6.6/samples
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES



### DOVECOT ####
virtual_alias_maps=hash:/etc/postfix/vmail_aliases
virtual_mailbox_domains=hash:/etc/postfix/vmail_domains
virtual_mailbox_maps=hash:/etc/postfix/vmail_mailbox

virtual_mailbox_base = /var/vmail
virtual_minimum_uid = 2222
virtual_transport = virtual
virtual_uid_maps = static:2222
virtual_gid_maps = static:2222

smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_sasl_tls_security_options = $smtpd_sasl_security_options
smtpd_sasl_local_domain = $mydomain
broken_sasl_auth_clients = yes

#smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination




### DKIM ###
smtpd_milters           = inet:127.0.0.1:8891
non_smtpd_milters       = $smtpd_milters
milter_default_action   = accept

 

 

 

#master.cf

#
# Postfix master process configuration file.  For details on the format
# of the file, see the master(5) manual page (command: "man 5 master").
#
# Do not forget to execute "postfix reload" after editing this file.
#
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
smtp      inet  n       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
#  -o smtpd_tls_security_level=encrypt
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#smtps     inet  n       -       n       -       -       smtpd
#  -o smtpd_tls_wrappermode=yes
#  -o smtpd_sasl_auth_enable=yes
#  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#  -o milter_macro_daemon_name=ORIGINATING
#628      inet  n       -       n       -       -       qmqpd
pickup    fifo  n       -       n       60      1       pickup
cleanup   unix  n       -       n       -       0       cleanup
qmgr      fifo  n       -       n       300     1       qmgr
#qmgr     fifo  n       -       n       300     1       oqmgr
tlsmgr    unix  -       -       n       1000?   1       tlsmgr
rewrite   unix  -       -       n       -       -       trivial-rewrite
bounce    unix  -       -       n       -       0       bounce
defer     unix  -       -       n       -       0       bounce
trace     unix  -       -       n       -       0       bounce
verify    unix  -       -       n       -       1       verify
flush     unix  n       -       n       1000?   0       flush
proxymap  unix  -       -       n       -       -       proxymap
proxywrite unix -       -       n       -       1       proxymap
smtp      unix  -       -       n       -       -       smtp
# When relaying mail as backup MX, disable fallback_relay to avoid MX loops
relay     unix  -       -       n       -       -       smtp
    -o smtp_fallback_relay=
#       -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq     unix  n       -       n       -       -       showq
error     unix  -       -       n       -       -       error
retry     unix  -       -       n       -       -       error
discard   unix  -       -       n       -       -       discard
local     unix  -       n       n       -       -       local
virtual   unix  -       n       n       -       -       virtual
lmtp      unix  -       -       n       -       -       lmtp
anvil     unix  -       -       n       -       1       anvil
scache    unix  -       -       n       -       1       scache
#
# ====================================================================
# Interfaces to non-Postfix software. Be sure to examine the manual
# pages of the non-Postfix software to find out what options it wants.
#
# Many of the following services use the Postfix pipe(8) delivery
# agent.  See the pipe(8) man page for information about ${recipient}
# and other message envelope options.
# ====================================================================
#
# maildrop. See the Postfix MAILDROP_README file for details.
# Also specify in main.cf: maildrop_destination_recipient_limit=1
#
#maildrop  unix  -       n       n       -       -       pipe
#  flags=DRhu user=vmail argv=/usr/local/bin/maildrop -d ${recipient}
#
# ====================================================================
#
# The Cyrus deliver program has changed incompatibly, multiple times.
#
#old-cyrus unix  -       n       n       -       -       pipe
#  flags=R user=cyrus argv=/usr/lib/cyrus-imapd/deliver -e -m ${extension} ${user}
#
# ====================================================================
#
# Cyrus 2.1.5 (Amos Gouaux)
# Also specify in main.cf: cyrus_destination_recipient_limit=1
#
#cyrus     unix  -       n       n       -       -       pipe
#  user=cyrus argv=/usr/lib/cyrus-imapd/deliver -e -r ${sender} -m ${extension} ${user}
#
# ====================================================================
#
# See the Postfix UUCP_README file for configuration details.
#
#uucp      unix  -       n       n       -       -       pipe
#  flags=Fqhu user=uucp argv=uux -r -n -z -a$sender - $nexthop!rmail ($recipient)
#
# ====================================================================
#
# Other external delivery methods.
#
#ifmail    unix  -       n       n       -       -       pipe
#  flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r $nexthop ($recipient)
#
#bsmtp     unix  -       n       n       -       -       pipe
#  flags=Fq. user=bsmtp argv=/usr/local/sbin/bsmtp -f $sender $nexthop $recipient
#
#scalemail-backend unix -       n       n       -       2       pipe
#  flags=R user=scalemail argv=/usr/lib/scalemail/bin/scalemail-store
#  ${nexthop} ${user} ${extension}
#
#mailman   unix  -       n       n       -       -       pipe
#  flags=FR user=list argv=/usr/lib/mailman/bin/postfix-to-mailman.py
#  ${nexthop} ${user}