A self-hosted mail server provides full control over your data, independence from third-party providers, and the flexibility to customize the setup for business needs. This solution is ideal for companies that value confidentiality, want to customize their email infrastructure, or aim to reduce operational costs.
For security, it is recommended not to work as root. Create a separate user with administrator privileges:
sudo adduser info
sudo usermod -aG sudo info
Example configuration for /etc/ssh/sshd_config:
Port 7080
PermitRootLogin no
AllowUsers info myname
After making changes, restart the service:
sudo systemctl restart sshd
sudo apt update
sudo apt -y install postfix sasl2-bin mailutils
During Postfix setup, select "No Configuration" — we will configure it manually.
/etc/postfix/main.cfmyhostname = test.ru
mydomain = test.ru
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/
message_size_limit = 10485760
mailbox_size_limit = 1073741824
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_recipient_restrictions = permit_mynetworks, permit_auth_destination, permit_sasl_authenticated, reject
Apply the changes:
sudo newaliases
sudo systemctl restart postfix
sudo apt -y install dovecot-core dovecot-pop3d dovecot-imapd
/etc/dovecot/dovecot.conf
listen = *, ::
/etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
/etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:/home/info/Maildir
/etc/dovecot/conf.d/10-master.conf — Integration with Postfix:
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
Restart the service:
sudo systemctl restart dovecot
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d test.ru
In main.cf, add:
smtp_use_tls = yes
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/letsencrypt/live/test.ru/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/test.ru/privkey.pem
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
In /etc/dovecot/conf.d/10-ssl.conf:
ssl = yes
ssl_cert = /etc/letsencrypt/live/test.ru/fullchain.pem
ssl_key = /etc/letsencrypt/live/test.ru/privkey.pem
Apply the changes:
sudo systemctl restart postfix dovecot
In main.cf:
virtual_mailbox_domains = test.ru
virtual_mailbox_base = /home/info
virtual_mailbox_maps = hash:/etc/postfix/virtual-mailbox
virtual_uid_maps = static:20000
virtual_gid_maps = static:20000
/etc/postfix/virtual-mailboxinfo@test.ru test.ru/info/Maildir/
Apply the mapping:
sudo postmap /etc/postfix/virtual-mailbox
sudo systemctl restart postfix
Create the file /etc/dovecot/users with a hashed password:
sudo doveadm pw -s CRAM-MD5
Add a line in the following format:
info@test.ru:{CRAM-MD5}password_hash
In auth-passwdfile.conf.ext, specify the path to the users file and the authentication method.
Allows email sending only from your server:
test.ru. IN TXT "v=spf1 ip4:100.90.88.77 a mx ~all"
Specifies how to handle emails that fail authentication checks:
_dmarc.test.ru. IN TXT "v=DMARC1; p=none; aspf=r; sp=none"
It is recommended to start with the
p=nonepolicy for monitoring, then transition top=quarantineorp=reject.
Signing outgoing emails with a cryptographic key.
Setup requires installing opendkim and publishing the public key in DNS.
echo "Test message" | mail -s "Test" info@test.ru
sudo tail -f /var/log/mail.log
Install pflogsumm to generate reports:
sudo apt install pflogsumm
sudo pflogsumm -d today /var/log/mail.log
telnet test.ru 25
telnet test.ru 587
telnet test.ru 143
Add a daily report to crontab:
00 01 * * * sudo pflogsumm -e -d yesterday /var/log/mail.log | mail -s 'Postfix Daily Report' admin@test.ru
Regularly archive: