# Ubuntu 22, installing the email server

It is a fast and complete guide to installing an email server using Ubuntu 22 but it should work too with Ubuntu 16 and higher.

## Installing postfix

Postfix is our tool that does the heavy-lifting. It will also install other services. What postfix does in the background is overly complex but we will only touch the surface of it.

```bash
apt install postfix
```

It will show the next screens:

1. Internet sites:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677871385708/68a9d660-0593-4301-a81c-0aa6577ca29f.png align="center")

1. select your domain. If you want a domain called john@domain.dom, then pick:
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677871434059/46f38485-69cf-48a0-9875-397b90f4000b.png align="center")

1. The postmaster (you can pick whatever you want to)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677871485495/e52ed064-1e72-4d89-9401-a2720933fc91.png align="center")

1. And you can pick other domains
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677871540612/8257dec6-ca34-47ff-84bc-7051948411d8.png align="center")

1. Force sync? No
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677871560110/3f67fc66-e1c0-435b-bcdc-0891c0a4153f.png align="center")

1. Relay IP. Does not change it.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677871600238/5ffccf07-6756-4fbd-9583-f6fee5145590.png align="center")

* Mailbox size, you can set the file that you want to or zero for unlimited.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677931647042/913ff7cf-096c-4065-bebc-fab1b5d16034.png align="center")

* Delimiter. Set this values as default
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677931699689/965d2d7e-ad31-47a3-81f0-ab16b767e659.png align="center")

* Internet protocol. Usually "all" works but if you have troubles, then change it for "IPV4"
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677931742255/ab996a20-1641-4fd1-a105-6709625476e4.png align="center")

And reload the service

```bash
systemctl reload postfix
```

You can re-configure the system using the next command (or you could edit the configuration file manually)

```bash
sudo dpkg-reconfigure postfix
```

## Editing Postfix

Indicates the mail folder:

```bash
postconf -e 'home_mailbox= Maildir/'
```

And restart postfix.

```bash
sudo systemctl restart postfix
```

In the folder /etc/postfix, you could find many files. There are two important files, one is **main.cnf** and **master.cnf**.

## Firewall

You must allow the services to pass the firewall.

```bash
sudo ufw allow Postfix
```

If you want to receive emails, then you must open the port TCP/IP 25 from your firewall.

> **Note:** If you are using AWS, then you must ask permission to Amazon (and open the firewall), otherwise it will not work. And as far as I know, Azure put the same restrictions. Many cloud providers could put their own restrictions, some simply deny the use of the port 25.
> 
> Most home connections block the port 25.

How to test if the port is open?

In a different machine, runs the next command:

```bash
telnet <external-ip> 25
```

If you are using Windows, then you can use Putty (a free program) and try to do telnet using port 25.

## Create the mail folders

Log in with the user and run the next command

```bash
mkdir ~/Maildir
mkdir -p ~/Maildir/{cur,new,tmp}
chown -R USERNAME:USERNAME ~Maildir
```

Where username is your username

## Installing a client

To install a client compatible with postfix, you can use Mutt.

```bash
yum install mutt
```

There are many clients but it is easy and minimalist.

The other popular client is s-nail but it is less intuitive.

![Mutt (email client) - Wikipedia](https://upload.wikimedia.org/wikipedia/commons/a/a1/Mutt.png align="left")

### Sends an email using mutt

> **Note:** You must configure the DNS before you want to send email. It could work locally but it is better to test it with the DNS up and running.

Open mutt

```bash
mutt
```

And select "m" (mail)

Fill in the information in the email. And finally, send it "y"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1677931252004/a48a4888-d615-4ef3-817f-0f3629c72b97.png align="center")

## Sends an email using the cli

You can send an email using the next command.

```bash
echo "body" | mail -s "title" <your email>
Configuring the DNS
```

How do you know that it is working?

You can check the log at:

```bash
cat /var/log/mail.log
```

## DNS

Finally, you will need to configure your DNS.

Go to the DNS configuration and enter the next information:

<table><tbody><tr><td colspan="1" rowspan="1"><p>type</p></td><td colspan="1" rowspan="1" colwidth="200"><p>name</p></td><td colspan="1" rowspan="1"><p>content</p></td><td colspan="1" rowspan="1"><p>priority</p></td></tr><tr><td colspan="1" rowspan="1"><p>MX</p></td><td colspan="1" rowspan="1" colwidth="200"><p>&lt;your domain&gt;</p></td><td colspan="1" rowspan="1"><p>&lt;your domain&gt;</p></td><td colspan="1" rowspan="1"><p>10</p></td></tr><tr><td colspan="1" rowspan="1"><p>TXT</p></td><td colspan="1" rowspan="1" colwidth="200"><p>_dmarc</p></td><td colspan="2" rowspan="1"><p>v=DMARC1; p=none; rua=<a target="_blank" rel="noopener noreferrer nofollow" href="mailto:sender@<domain" style="pointer-events: none">mailto:sender@&lt;domain</a>&gt;</p></td></tr><tr><td colspan="1" rowspan="1"><p>TXT</p></td><td colspan="1" rowspan="1" colwidth="200"><p>&lt;your domain&gt;</p></td><td colspan="1" rowspan="1"><p>v=spf1 ip4:&lt;your IP&gt; ~all</p></td><td colspan="1" rowspan="1"><p>&nbsp;</p></td></tr></tbody></table>

Change &lt;domain&gt; by your domain and &lt;your IP&gt; for the private IP of the server.

> **Note**: Gmail did some changes to the SPF entries. It does not accept includes or domain names, only IPs.
