jonathanvix
Goto Top

How to setup a Mail server on Ubuntu

smtp-server-iredmail-1030x1030

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
Find the myhostname parameter and set it to your registered domain name:

myhostname = mail.example.com
Next, find the mydomain parameter and set it to your domain:

mydomain = example.com
Set the myorigin parameter to $mydomain:

myorigin = $mydomain
Under the INTERNET_PROTOCOLS section, make sure ipv4 is enabled:
inet_interfaces = all
inet_protocols = all
This allows Postfix to listen on all available IPv4 network interfaces.

Now find the mydestination parameter and set it to the following:

mydestination = $myhostname, localhost.$mydomain, $mydomain
This specifies the domains that Postfix will deliver mail to locally.

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
Create the user and password file:

$ sudo nano /etc/postfix/sasl_passwd
Add your email and password on separate lines:

mail.example.com username@example.com
mail.example.com password123
Save and close the file.

Now edit the Postfix SASL configuration:

$ sudo nano /etc/postfix/sasl/smtpd.conf
Make sure it has the following:

pwcheck_method: saslauthd
mech_list: plain login
This sets Postfix to use the saslauthd service for authentication.

Restart Postfix to load the new configuration:

$ sudo systemctl restart postfix
Postfix is now configured and ready for sending and receiving emails.

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
Find the protocols section and enable imap and pop3:

protocols = imap pop3
Enable SMTP authentication:

disable_plaintext_auth = yes
Set the mail location:

mail_location = maildir:/var/mail/%d/%n
Now open the SMTP authentication config file:

$ sudo nano /etc/dovecot/conf.d/10-auth.conf
Find the auth_mechanisms parameter and set it to:

auth_mechanisms = plain login
This allows plain text and login authentication similar to Postfix.

Finally, open the permissions file:

$ sudo nano /etc/dovecot/conf.d/10-mail.conf
And set:
mail_access_groups = mail
This allows members of the mail group to access mailboxes.

Save and restart Dovecot:
$ sudo systemctl restart dovecot
Dovecot is now ready to handle IMAP and POP3 mail access.

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 a database called mailserver:
CREATE DATABASE mailserver;
Create a new user and grant permissions on the database:

GRANT SELECT,INSERT,UPDATE,DELETE ON mailserver.* TO 'mailuser'@'127.0.0.1' IDENTIFIED BY 'password123';  
Exit MySQL:

quit
Now we can import the Postfix configuration SQL file to create the necessary tables:
$ sudo mysql mailserver < /etc/postfix/mysql/postfix_db.sql
The MySQL database is now ready to store domain and user information for our mail server.

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
Uncomment the config_directory parameter and set it to our MySQL config:

config_directory = /etc/postfix/mysql
Now let’s create a virtual domain entry in the database. Log into MySQL:

$ sudo mysql mailserver -p
Insert a row for the domain:

INSERT INTO `virtual_domains` (`id` ,`name`) VALUES ('1', 'example.com');  
Exit MySQL.

Next, open the virtual users file:
$ sudo nano /etc/postfix/mysql-virtual_mailboxes.cf
Set the config_directory like before:

config_directory = /etc/postfix/mysql
This allows Postfix to lookup users in MySQL.

Enter MySQL again:

$ sudo mysql mailserver -p 
Create a sample user:

INSERT INTO `virtual_users` (`id`, `domain_id`, `password` , `email`) VALUES ('1', '1', ENCRYPT('password123', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), 'username@example.com');  
This creates a user “username@example.com” with an encrypted password.

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');  
Exit MySQL and restart Postfix for the changes to take effect:

$ sudo systemctl restart postfix
We can create more domains and users in the same way.

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
Open the main config file:

$ sudo nano /etc/opendmarc.conf
Set your domain:

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
Finally, generate the DMARC TXT record for your domain:
$ sudo opendmarc-gen-policy --domain example.com --policy none --report email:postmaster@example.com
Take this TXT record and add it to your domain’s DNS configuration.

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  
Check if the mail was delivered:

$ sudo ls -l /var/mail
You should see a file named after the user you sent it to if delivery was successful.

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
Type EHLO, then MAIL FROM:, RCPT TO: and finally the test message data. This validates that SMTP sending and delivery are working properly.

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!
Kommentar vom Moderator colinardo am 07.05.2024 um 16:30:21 Uhr
The content of a tutorial must not come from another website or another author. This violates our board rules!
=> Trash

Content-ID: 93734328301

Url: https://administrator.de/en/how-to-setup-a-mail-server-on-ubuntu-93734328301.html

Ausgedruckt am: 26.12.2024 um 16:12 Uhr

Lochkartenstanzer
Lochkartenstanzer 07.05.2024 aktualisiert um 16:24:00 Uhr
Goto Top
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