KaliLinux 2018.2: Install Postfix for SMTP server

This article will describe installing Postfix as SMTP server and send mail to localhost and your domain. And then send mail to internet via Gmail.

1 Send mail to localhost and your domain

  • If you send mail to only localhost, set myhostname = localhost and mydomain = localdomain.
  • If you send mail to your domain, set your hostname to myhostname and your domain to mydomain. This hostname and domain must be known with multiple machine (common /etc/hosts or DNS server). Both mail sender machine and receiver machine needs this Postfix setting.

The following script will set myhostname = localhost and mydomain = localdomain.

#!/bin/sh -e

  cat <<EOF | sudo debconf-set-selections
postfix postfix/main_mailer_type select No configuration
  sudo apt install -y postfix
  sudo systemctl enable postfix

  # shellcheck disable=SC2016
  cat <<EOF | sudo tee /etc/postfix/main.cf
myhostname = ${1}
mydomain = ${2}
myorigin = \$myhostname.\$mydomain
mydestination = localhost, localhost.\$mydomain, \$myhostname, \$mydomain, \$myorigin
compatibility_level = 2
command_directory = /usr/sbin
daemon_directory = /usr/lib/postfix/sbin
data_directory = /var/lib/postfix
mail_owner = postfix
inet_interfaces = all
local_recipient_maps = unix:passwd.byname \$alias_maps
unknown_local_recipient_reject_code = 550
mynetworks_style = subnet
mynetworks =
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
smtpd_banner = \$myhostname ESMTP \$mail_name (Ubuntu)
debugger_command =
         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
inet_protocols = ipv4

  sudo newaliases
  sudo systemctl restart postfix

  postfix_install localhost localdomain             # localhost only.
  # postfix_install ${YOUR_HOSTNAME} ${YOUR_DOMAIN} # your network.

sudo apt install -y mutt

2 Send mail to internet via Gmail

You need the previous Postfix setting. The myhostname and mydomain can be localhost setting or your domain setting.

Append the following to /etc/postfix.main.cf.

$ cat <<EOF | sudo tee -a /etc/postfix/main.cf
relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
smtp_use_tls = yes

Create /etc/postfix/sasl_passwd. Please set your Gmail address to GMAIL_ADDR and your Gmail password to GMAIL_PASSWD.

$ echo "[smtp.gmail.com]:587 ${GMAIL_ADDR}:${GMAIL_PASSWD}" | \
sudo tee /etc/postfix/sasl_passwd
$ sudo chmod 600 /etc/postfix/sasl_passwd
$ sudo postmap hash:/etc/postfix/sasl_passwd

Restart postfix.

$ sudo systemctl restart postfix

Gmail will return authentication error.

postfix/smtp ... SASL authentication failed; server smtp.gmail.com...

The 2-step verification and application password is better. But this article will use "Allow less secure apps: ON".


If Gmail returns authenticaion error again, you need "Allow access to your Google account".


And then you can send mail to internet via Gmail. Mail's from address is your Gmail address.

postfix/smtp ... to=<yourgmail@gmail.com>,relay=smtp.gmail.com ...