Author: Madhan Gopalakrishnan | Published on : 19-02-2025
A Mail Server is an essential component for managing email communication within an organization or for public email services. This guide provides an easy-to-follow, in-depth understanding of setting up and managing a Linux mail server, including monitoring with Grafana.
📧 Types of Mail Servers
| Mail Server Component | Description |
|---|---|
| Postfix 📬 | SMTP server used for sending emails |
| Dovecot 📥 | IMAP/POP3 server for receiving emails |
| SpamAssassin 🚫 | Filters spam emails |
| ClamAV 🛡️ | Antivirus for scanning email attachments |
| Grafana 📊 | Monitoring mail server performance |
🔑 Accessing the Mail Server (Login Page)
To access the mail server remotely and manage emails, you can set up a Webmail Interface such as Roundcube or Rainloop.
1️⃣ Install Roundcube Webmail
sudo apt install roundcube -y
2️⃣ Configure Roundcube
- Open the configuration file:
sudo nano /etc/roundcube/config.inc.php
- Ensure the following settings are correct:
$config['default_host'] = 'ssl://mail.example.com';
$config['smtp_server'] = 'ssl://mail.example.com';
$config['smtp_user'] = '%u';
$config['smtp_pass'] = '%p';
3️⃣ Access Webmail Interface
- Open a web browser and go to:
http://mail.example.com/roundcube
- Login using the email address and password of a created user.
🛠️ Setting Up a Mail Server with Postfix and Dovecot
1️⃣ Install Postfix (SMTP Server)
sudo apt update
sudo apt install postfix -y
2️⃣ Configure Postfix
- Open the main configuration file:
sudo nano /etc/postfix/main.cf
- Modify the following settings:
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
home_mailbox = Maildir/
message_size_limit = 10485760 # Restrict mail size to 10MB
- Restart Postfix:
sudo systemctl restart postfix
3️⃣ Install Dovecot (IMAP/POP3 Server)
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d -y
4️⃣ Configure Dovecot
- Edit the configuration file:
sudo nano /etc/dovecot/dovecot.conf
- Ensure the following settings:
protocols = imap pop3
mail_location = maildir:~/Maildir
mail_max_userip_connections = 10 # Restrict number of concurrent connections per user
- Restart Dovecot:
sudo systemctl restart dovecot
5️⃣ Creating Users for Mail
- Create a new user for the mail server:
sudo adduser mailuser
- Set the password:
sudo passwd mailuser
- Ensure the user’s mail directory exists:
mkdir -p /home/mailuser/Maildir
chown -R mailuser:mailuser /home/mailuser/Maildir
chmod -R 700 /home/mailuser/Maildir
6️⃣ Configure SSL/TLS for Secure Email Communication
- Install Let’s Encrypt:
sudo apt install certbot python3-certbot-apache -y
- Obtain SSL certificate:
sudo certbot certonly --standalone -d mail.example.com
- Update Postfix and Dovecot to use SSL:
sudo nano /etc/postfix/main.cf
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.example.com/privkey.pem
sudo nano /etc/dovecot/conf.d/10-ssl.conf
ssl_cert = </etc/letsencrypt/live/mail.example.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.example.com/privkey.pem
- Restart Services:
sudo systemctl restart postfix dovecot
📂 Mail Server Backup & Recovery
1️⃣ Backup Mail Directory
tar -czvf mail_backup.tar.gz /var/mail/
2️⃣ Restore Mail Backup
tar -xzvf mail_backup.tar.gz -C /
🔄 Day 2 Operations (Mail Server Maintenance)**
1️⃣ Managing Mailboxes
- Create a new mailbox for a user:
sudo mkdir -p /home/mailuser/Maildir
sudo chown -R mailuser:mailuser /home/mailuser/Maildir
sudo chmod -R 700 /home/mailuser/Maildir
- Check the mailbox size:
du -sh /home/mailuser/Maildir
2️⃣ Restricting Mail Size Per User
- Edit the Dovecot configuration file:
sudo nano /etc/dovecot/conf.d/20-imap.conf
- Add or modify:
mail_max_userip_connections = 10 # Limit user connections
mail_attachment_min_size = 102400 # Minimum attachment size (100KB)
- Restart Dovecot:
sudo systemctl restart dovecot
3️⃣ Managing Users
- Delete a user and their mailbox:
sudo deluser --remove-home mailuser
4️⃣ Monitoring Mail Logs
- Check Postfix logs:
tail -f /var/log/mail.log
- Check Dovecot logs:
tail -f /var/log/dovecot.log
⚡ Monitoring Mail Server with Grafana
1️⃣ Install Grafana
sudo apt install grafana -y
2️⃣ Start and Enable Grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server
3️⃣ Install and Configure Prometheus Exporters
Install Postfix Exporter:
sudo apt install prometheus-postfix-exporter
sudo systemctl start prometheus-postfix-exporter
Install Dovecot Exporter:
sudo apt install prometheus-dovecot-exporter
sudo systemctl start prometheus-dovecot-exporter
✅ Conclusion
By following this guide, you now have a fully operational Linux Mail Server with Postfix and Dovecot, secured with SSL/TLS, and monitored with Grafana. Additionally, you have learned how to create and manage mail users, mailboxes, and maintain the server efficiently. This setup ensures efficient email handling, security, and real-time monitoring. 🚀
Would you like additional details on spam filtering or clustering? 🤔









