Step-by-Step Guide to Setting Up a Mail Server on Linux
Setting up a mail server on Linux allows you to send and receive emails securely without relying on third-party providers. In this guide, we’ll configure Postfix, Dovecot, and Roundcube to create a self-hosted email system.
1. Prerequisites: What You Need Before Setup
- ✅ A Linux server (Ubuntu, Debian, or CentOS).
- ✅ A fully qualified domain name (FQDN) (e.g.,
mail.yourdomain.com
). - ✅ Open firewall ports: 25 (SMTP), 143/993 (IMAP), and 110/995 (POP3).
⚡ Update Your Server
sudo apt update && sudo apt upgrade -y
2. Set Up a Hostname & DNS Records
✅ Step 1: Set a Hostname (FQDN)
sudo hostnamectl set-hostname mail.yourdomain.com
✅ Step 2: Configure DNS Records
Configure the following DNS records:
Record Type | Host | ✅ Value |
---|---|---|
MX | @ | mail.yourdomain.com (Priority: 10) |
A | mail.yourdomain.com | Server IP Address |
SPF | @ | v=spf1 mx -all |
3. Install & Configure Postfix (SMTP Server)
✅ Step 1: Install Postfix
sudo apt install postfix -y
✅ Step 2: Configure Postfix
sudo nano /etc/postfix/main.cf
Update the file with:
myhostname = mail.yourdomain.com mydomain = yourdomain.com myorigin = $mydomain inet_interfaces = all home_mailbox = Maildir/
✅ Step 3: Restart Postfix
sudo systemctl restart postfix
4. Install & Configure Dovecot (IMAP/POP3)
✅ Step 1: Install Dovecot
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d -y
✅ Step 2: Configure Dovecot
sudo nano /etc/dovecot/dovecot.conf
Ensure the following settings are enabled:
protocols = imap pop3 lmtp mail_location = maildir:~/Maildir
✅ Step 3: Restart Dovecot
sudo systemctl restart dovecot
5. Secure the Mail Server with SSL/TLS
✅ Step 1: Install Let’s Encrypt SSL
sudo apt install certbot -y
✅ Step 2: Obtain a Free SSL Certificate
sudo certbot certonly --standalone -d mail.yourdomain.com
6. Install Roundcube Webmail
✅ Step 1: Install Apache & PHP
sudo apt install apache2 php php-mysql php-xml php-mbstring -y
✅ Step 2: Install Roundcube
sudo apt install roundcube -y
Access Roundcube at:
http://mail.yourdomain.com/roundcube
Final Thoughts: Your Linux Mail Server is Ready!
- ✅ Configured Postfix for sending emails (SMTP).
- ✅ Set up Dovecot for receiving emails (IMAP/POP3).
- ✅ Secured email transmission with SSL/TLS.
- ✅ Installed Roundcube for webmail access.
Want a fully functional, secure mail server? Implement SPF, DKIM, and DMARC to prevent spam and spoofing!