How to setup a Mail server on Ubuntu
A mail server allows you to send and receive email. Setting up your own mail server on Ubuntu gives you more control over your email and increases privacy and security. However, managing your own mail server requires a bit more technical knowledge.
This comprehensive guide will walk you through all the steps to create a fully-functioning mail server on Ubuntu 20.04/22.04 from start to finish.
Prerequisites
Before getting started, you’ll need the following:
A Ubuntu 20.04/22.04 server with a static public IP address. Using a VPS is recommended.
A registered domain name. This will be used to send and receive emails from your mail server.
Administrative access to your Ubuntu server.
Basic knowledge of the Linux command line.
We’ll be using Postfix for the SMTP server, Dovecot for IMAP/POP3, and OpenDMARC for email authentication. A MySQL database will also be configured to store information like virtual domains and users.
Let’s start by updating the package repository and installing some dependencies on our Ubuntu server:
$ sudo apt update
$ sudo apt install postfix postfix-mysql dovecot-imapd dovecot-pop3d mariadb-server openssl openssl-blacklist
Next, we’ll go through the steps to configure each component.
Configuring Postfix
Postfix handles the SMTP service for sending and receiving emails. We need to update some settings in the main Postfix configuration file.
Open the file with:
$ sudo nano /etc/postfix/main.cf
myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
inet_protocols = all
Now find the mydestination parameter and set it to the following:
mydestination = $myhostname, localhost.$mydomain, $mydomain
Save and close the file when you are done editing.
Next, we need to set up SMTP authentication. Generate a password file for Postfix with the postmap command:
$ sudo postmap /etc/postfix/sasl_passwd
$ sudo nano /etc/postfix/sasl_passwd
mail.example.com username@example.com
mail.example.com password123
Now edit the Postfix SASL configuration:
$ sudo nano /etc/postfix/sasl/smtpd.conf
pwcheck_method: saslauthd
mech_list: plain login
Restart Postfix to load the new configuration:
$ sudo systemctl restart postfix
Configuring Dovecot
Dovecot will be used to handle IMAP and POP3 protocols for accessing emails from mail clients like Outlook or Thunderbird.
Open the Dovecot configuration file:
$ sudo nano /etc/dovecot/dovecot.conf
protocols = imap pop3
disable_plaintext_auth = yes
mail_location = maildir:/var/mail/%d/%n
$ sudo nano /etc/dovecot/conf.d/10-auth.conf
auth_mechanisms = plain login
Finally, open the permissions file:
$ sudo nano /etc/dovecot/conf.d/10-mail.conf
mail_access_groups = mail
Save and restart Dovecot:
$ sudo systemctl restart dovecot
MySQL Database Setup
Next, we’ll set up a MySQL database to store virtual domains and users for our mail server.
Log into the MySQL shell:
$ sudo mysql
CREATE DATABASE mailserver;
GRANT SELECT,INSERT,UPDATE,DELETE ON mailserver.* TO 'mailuser'@'127.0.0.1' IDENTIFIED BY 'password123';
Exit MySQL:
quit
$ sudo mysql mailserver < /etc/postfix/mysql/postfix_db.sql
Virtual Domains and Users
With the database configured, we can create virtual domains and users.
A virtual domain allows you to host multiple domains from a single mail server.
First, open the Postfix virtual domain configuration file:
$ sudo nano /etc/postfix/mysql-virtual_domains.cf
config_directory = /etc/postfix/mysql
$ sudo mysql mailserver -p
INSERT INTO `virtual_domains` (`id` ,`name`) VALUES ('1', 'example.com');
Next, open the virtual users file:
$ sudo nano /etc/postfix/mysql-virtual_mailboxes.cf
config_directory = /etc/postfix/mysql
Enter MySQL again:
$ sudo mysql mailserver -p
INSERT INTO `virtual_users` (`id`, `domain_id`, `password` , `email`) VALUES ('1', '1', ENCRYPT('password123', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'username@example.com');
Now we need to allow the user to access mailboxes. Insert a row into virtual_aliases:
INSERT INTO `virtual_aliases` (`id`, `domain_id`, `source`, `destination`) VALUES ('1', '1', 'username@example.com', 'username@example.com');
$ sudo systemctl restart postfix
OpenDMARC
OpenDMARC implements the DMARC email authentication standard. This helps improve security and prevent spam and phishing.
First, install OpenDMARC:
$ sudo apt install opendmarc opendmarc-tools
$ sudo nano /etc/opendmarc.conf
AuthservID mail.example.com
Enable logging and reporting:
Socket inet:8893@localhost
LogLevel debug
Syslog true
RejectFailures false
ReportFailures true
HistoryFile /var/lib/opendmarc/opendmarc.dat
StatsSocket /var/run/opendmarc/opendmarc.sock
MinServers 3
ServerInterval 60
This logs activity to syslog and enables daily report emails.
Add your domain as the From address:
/etc/opendmarc/ignore.hosts
mail.example.com
Now enable OpenDMARC:
$ sudo systemctl enable opendmarc
$ sudo systemctl start opendmarc
$ sudo opendmarc-gen-policy --domain example.com --policy none --report email:postmaster@example.com
OpenDMARC is now active and will validate incoming emails.
Testing the Mail Server
Our Ubuntu mail server should now be properly configured. Let’s do some testing to validate that it works.
First, send a test email from the server itself with:
$ echo "This is a test" | mail -s Testing username@example.com
$ sudo ls -l /var/mail
Next, configure an email client like Thunderbird to connect to the mail server. Add a new account using the IMAP and SMTP credentials you configured.
Send a test message to the email address on your domain. It should be delivered to the user’s inbox folder on the Ubuntu server.
You can also use Telnet to manually connect to Postfix SMTP and send a message:
$ telnet mail.example.com 25
Check /var/log/mail.log and /var/log/syslog for any errors with Postfix, Dovecot, MySQL or OpenDMARC during testing. Debug and resolve any issues that come up.
When everything is working as expected, your Ubuntu mail server is ready for use!
Please also mark the comments that contributed to the solution of the article
Kommentar vom Moderator colinardo am May 07, 2024 um 14:30:21 Uhr
The content of a tutorial must not come from another website or another author. This violates our board rules!
=> Trash
=> Trash
Content-ID: 93734328301
Url: https://administrator.de/contentid/93734328301
Printed on: December 12, 2024 at 17:12 o'clock
1 Comment
Hi
Why do you just post the contents of
https://www.webhi.com/how-to/install-mail-server-ubuntu-debian-linux/
Looks like copy&paste
lks
Why do you just post the contents of
https://www.webhi.com/how-to/install-mail-server-ubuntu-debian-linux/
Looks like copy&paste
lks