Mailserver from scratch
This guide will cover installing a barebones Dovecot + Postfix mailserver.
First things first, make sure the required ports are open in the firewall:
sudo ufw allow 80,443,587,465,143,993,110,995,25/tcp
Install Dovecot
Dovecot will enable the use of IMAP and POP3.
sudo apt install dovecot-imapd dovecot-pop3d
Make a backup of the config file:
sudo cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.bak
Confirm Dovecot is working:
telnet localhost pop3
Set the maildir in /etc/dovecot/conf.d/10-mail.conf:
mail_location = maildir:/home/md/Maildir/%d/%u
Tell Dovecot to use Postfix auth in /etc/dovecot/conf.d/10-master.conf:
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
Make sure /etc/dovecot/conf.d/10-ssl.conf has SSL enabled with valid key:
ssl = yes
ssl_cert = </etc/dovecot/private/dovecot.pem
ssl_key = </etc/dovecot/private/dovecot.key
Don't forget to sudo systemctl start dovecot and sudo systemctl enable dovecot.
Enable IMAP/POP3
Enable IMAP/POP3 in Dovecot, edit the main config: sudo nano /etc/dovecot/dovecot.conf:
# Enable installed protocols
protocols = imap pop3
!include_try /usr/share/dovecot/protocols.d/*.protocol
Now add dovecot to the mail group so it can read INBOX: sudo adduser dovecot mail.
LMTP
We need Postfix to pass incoming mail to Dovecot via LMTP. First, install Dovecot LMTP:
sudo apt install dovecot-lmtpd
Then add LMTP to protocols in /etc/dovecot/dovecot.conf:
# Enable installed protocols
protocols = imap pop3 lmtp
!include_try /usr/share/dovecot/protocols.d/*.protocol
Then edit /etc/dovecot/conf.d/10-master.conf and add:
service lmtp {
unix_listener /var/spool/postfix/private/dovecot-lmtp {
mode = 0600
user = postfix
group = postfix
}
}
Configure authentication mechanism
Edit the file /etc/dovecot/conf.d/10-auth.conf and uncomment:
disable_plaintext_auth = yes
Then find the line #auth_username_format = %Lu, uncomment and change to:
auth_username_format = %Ln
%L will lowercase the username.
%n will drop the domain if entered.
%Ln@%Ld would lowercase the username, and include + lowercase the domain.
Make sure auth_mechanisms = plain login is enabled for supporting older clients.
Important: auth_username_format will affect how the password_query works in /etc/dovecot/dovecot-sql.conf. For it to work whether you only enter username or username@domain, leave auth_username_format = %Ln and set /etc/dovecot/dovecot-sql.conf to something like:
password_query = SELECT username, domain, password FROM mailbox WHERE username = '%n'
username, domain, password has to correspond to columns in the database table.
mailbox has to match the database table name that contains the user info.
TLS certificates
Edit /etc/dovecot/conf.d/10-ssl.conf and change ssl = yes to ssl = required. Change the certificate paths:
ssl_cert = /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/mail.privacyroot.org/mail.privacyroot.org.crt
ssl_key = /var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/mail.privacyroot.org/mail.privacyroot.org.key
Uncomment #ssl_prefer_server_ciphers = no and change it to ssl_prefer_server_ciphers = yes.
Uncomment ssl_min_protocol = TLSv1.2.
FIPS Providers
There's some issue with Ubuntu 22.04 and FIPS providers with Dovecot. Edit the file /etc/ssl/openssl.cnf and comment out providers = provider_sect.
Configure SASL
Edit the file /etc/dovecot/conf.d/10-master.conf and make sure the service auth { section has this:
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
Auto create Sent and Trash folders
Edit the file sudo nano /etc/dovecot/conf.d/15-mailboxes.conf
Add auto = create to the sections that you want automatically created:
namespace inbox {
mailbox Drafts {
auto = create
special_use = \Drafts
}
mailbox Junk {
auto = create
special_use = \Junk
}
mailbox Trash {
auto = create
special_use = \Trash
}
mailbox Sent {
auto = create
special_use = \Sent
}
}
Database
You can use any database. Here we'll use SQLite. Do:
sudo apt install dovecot-sqlite
Edit /etc/dovecot/dovecot.conf and add:
passdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf
}
userdb {
driver = sql
args = /etc/dovecot/dovecot-sql.conf
}
Edit /etc/dovecot/dovecot-sql.conf and add:
driver = sqlite
connect = /home/md/mailserver.db
password_query = SELECT userid AS username, domain, password \
FROM users WHERE userid = '%n' AND domain = '%d'
user_query = SELECT home, uid, gid FROM users WHERE userid = '%n' AND domain = '%d'
# For using doveadm -A:
iterate_query = SELECT userid AS username, domain FROM users
Edit /etc/dovecot/conf.d/10-auth.conf and uncomment:
!include auth-sql.conf.ext
And comment out:
!include auth-system.conf.ext
Be sure to create the db with sqlite3 mailserver and then create the table:
CREATE TABLE users (
userid TEXT,
username TEXT,
domain TEXT,
pgpkey TEXT,
password TEXT
);
Install Postfix
Postfix is your MTA that will allow you to send and receive mail.
sudo apt install postfix
Select internet site and then enter mail domain as system mail name, ie privacyroot.org and later add both mail.privacyroot.org and privacyroot.org to other destinations. Settings can always be changed with sudo dpkg-reconfigure postfix.
Configure Postfix to use Maildir instead of Mbox format:
sudo postconf -e 'home_mailbox = /home/md/Maildir/'
Check Postfix version with postconf mail_version.
Make sure port 25 is working
Confirm Postfix is listening on port 25 with sudo ss -lnpt | grep master.
Check inbound port 25 with this scanner: https://www.ipvoid.com/port-scan/
Check outbound port 25 with telnet gmail-smtp-in.l.google.com 25. If it's open, you will see something like:
$ telnet gmail-smtp-in.l.google.com 25
Trying 66.102.1.27...
Connected to gmail-smtp-in.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP e13-20020a5d530d000000b003251303356asi4742478wrv.185 - gsmtp
If it times out, VPS provider likely blocking port 25.
Test sending
At this point sending can be tested by running:
sudo echo "test email" | sendmail your-account@whatever.com.
This may go to spam, but don't worry about that for now.
Attachment size limit
Default attachment limit is 10MB. Check it with sudo postconf | grep message_size_limit.
Change limit to 50MB with sudo postconf -e message_size_limit=52428800.
Mailbox size limit
Check mailbox size limit with postconf | grep mailbox_size_limit.
Aliases
Edit /etc/aliases like:
# See man 5 aliases for format
postmaster: root
root: md
After saving the changes run sudo newaliases.
This way emails to postmaster@domain will go to user@domain.
Enable Submission
To be able to send mail from a mail client, enable Submission in Postfix:
sudo nano /etc/postfix/master.cf
Add these blocks to enable smtp and smtpbs submission:
submission inet n - y - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_tls_wrappermode=no
-o smtpd_sasl_auth_enable=yes
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_path=private/auth
smtps inet n - y - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o smtpd_recipient_restrictions=permit_mynetworks,permit_sasl_authenticated,reject
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_path=private/auth
TLS certificates
Postfix needs to know about the TLS certificates for mail.privacyroot.org. So edit the main config file with sudo nano /etc/postfix/main.cf and edit according to where the certs are:
# TLS parameters
#smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_cert_file=/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/mail.privacyroot.org/mail.privacyroot.org.crt
#smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_key_file=/var/lib/caddy/.local/share/caddy/certificates/acme-v02.api.letsencrypt.org-directory/mail.privacyroot.org/mail.privacyroot.org.key
Save and restart Postfix with sudo systemctl restart postfix and then confirm it is listening on the expected ports 465, 587 with sudo ss -lnpt | grep master.
Enable LMTP
Edit /etc/postfix/main.cf and add:
mailbox_transport = lmtp:unix:private/dovecot-lmtp
smtputf8_enable = no
This will tell Postfix to use Dovecot via LMTP.
Database
You can use any database. Here we'll use SQLite. Do:
sudo apt install postfix-sqlite
Configure DNS
Add an MX record like:
name: @
Type: MX
Exchange: mail.privacyroot.org
Create an A record like:
name: mail
Type: A
IPv4 address: 173.212.237.88
Create a PTR that points the domain mail.privacyroot.org to the IP 173.212.237.88. This must be done at the VPS provider.
To check PTR record do host 173.212.237.88 or dig -x 173.212.237.88 +short. A successful output should look like this:
$ host 173.212.237.88
88.237.212.173.in-addr.arpa domain name pointer mail.privacyroot.org.
$ dig -x 173.212.237.88 +short
mail.privacyroot.org.
Troubleshooting
Check open ports on the server with sudo nmap mail.privacyroot.org to confirm 25, 143, 465, 587, 993 are open.
Do sudo journalctl -eu dovecot to check Dovecot logs.
Do sudo journalctl -eu postfix to check Postfix logs.
If /var/log/mail.log goes missing, do:
touch /var/log/mail.log
sudo chmod a+w /var/log/mail*
sudo systemctl restart postfix