Running your own mail server gives you full control over email delivery, privacy and cost. This guide sets up a complete mail stack with Postfix for sending and Dovecot for receiving.
Prerequisites
- Ubuntu 18.04 or 20.04 server
- A domain name with DNS control
- Port 25, 465, 587, 993 open in firewall
- Reverse DNS (PTR record) set for your server IP
Step 1 — Set Hostname
sudo hostnamectl set-hostname mail.yourdomain.com
echo "YOUR_IP mail.yourdomain.com mail" | sudo tee -a /etc/hosts
Step 2 — Install Postfix
sudo apt update
sudo apt install postfix -y
During installation select Internet Site and enter your domain name.
Step 3 — Configure Postfix
sudo nano /etc/postfix/main.cf
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost =
mynetworks = 127.0.0.0/8
home_mailbox = Maildir/
# TLS
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_use_tls = yes
smtpd_tls_security_level = may
smtp_tls_security_level = may
Step 4 — Install Dovecot
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d -y
Step 5 — Configure Dovecot
sudo nano /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp
mail_location = maildir:~/Maildir
disable_plaintext_auth = yes
ssl = required
ssl_cert =
Step 6 — Create Mail User
sudo adduser mailuser
sudo mkdir -p /home/mailuser/Maildir/{cur,new,tmp}
sudo chown -R mailuser:mailuser /home/mailuser/Maildir
Step 7 — Configure DNS Records
# MX record
yourdomain.com MX 10 mail.yourdomain.com
# A record for mail server
mail.yourdomain.com A YOUR_SERVER_IP
# SPF record
yourdomain.com TXT "v=spf1 mx ~all"
Step 8 — Start Services
sudo systemctl enable postfix dovecot --now
sudo systemctl status postfix dovecot
Step 9 — Test Mail Server
# Send test email
echo "Test email body" | mail -s "Test Subject" [email protected]
# Check mail logs
sudo tail -f /var/log/mail.log
Conclusion
Your mail server is now sending and receiving email. For professional managed mail server support including anti-spam, DKIM and monitoring, contact our team.
Comments