Learn Linux Series (#4) - e-mail server (Postfix)

in #utopian-io6 years ago

g_-_-x-_-_-_x20150823134631_0.jpg
What Will I Learn?
What is postfix
How to install postfix on Linux
How to configure postfix
Map of email addresses for Linux accounts
Configuring the environment according to the email location
How to firewall setting
Installation and configuration of the e-mail client
Maildir initialization and client testing
Requirements
Linux system installed
Basic knowledge of terminal operation
Average knowledge of linux commands
Motivation when something goes wrong
Difficulty
Intermediate

Learn Linux Series (#4) - e-mail server (Postfix)
What a Postfix is

Postfix is a popular open source Mail Transfer Agent (MTA) that you can use to route and deliver email messages on Linux. it is fast, easy to administer (in comparison with other available applications of this type) and a secure set of software allowing the creation of a mail server. It is built of many smaller programs cooperating with each other. The basic four queues of the system
Postfix are:

maildrop - queue storing mail sent locally, which will then go from this queue to
incoming;

incoming - an incoming and unprocessed mail queue
part of Postfix - Queue Manager;

active - contains mail processed by Queue Manager and ready to be delivered;

deferred - a queue that stores mail that could not be delivered.

Postfix also has a mechanism that guarantees smooth processing of emails. First of all, Queue
The manager works in such a way that for processing alternates one message from the queue
incoming and with deferred. Secondly, Postfix tries not to make more than two calls when
delivers mail to a specific location until subsequent shipments are delivered
seamlessly. In addition, e-mails that failed to send are marked with a special tag
time, which along with successive unsuccessful attempts is doubled, which blocks it
processing by Queue Manager. Another Postfix security lies in memorizing
the hosts to which the parcel failed to deliver, to avoid sending attempts to the future
them.

Postfix installation

Postfix is in the default Linux repositories, so the installation is very simple.

To go to the postfix installation we must execute the commands:
sudo apt-get install postfix
Make settings according to your needs.

If you do not know which settings to use, I give the example settings below:

General type of mail configuration?: Utopian.io Website
System mail name: com (not mail.example.com)
Root and postmaster mail recipient: vitusc
Other destinations to accept mail for: $myhostname, example.com, mail.example.com, localhost.example.com, localhost
Force synchronous updates on mail queue?: No
Local networks: 0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
Mailbox size limit: 0
Local address extension character: +
Internet protocols to use: all
If you want to ever come back to re-adjust these settings, you can do so by entering:

sudo dpkg-reconfigure postfix

Postfix configuration

For starters, we can set up a mailbox. We will use the Maildir format, which separates messages in separate files, which are then transferred between directories based on user actions. Another option is the mbox format (which we will not discuss here), which stores all messages in one file.

We will install the variable home_mailbox in maildir /, which will create the directory structure under this name in the user's home directory. Command
postconf
can be used to obtain or change configuration settings. We will configure home_mailbox by entering:

sudo postconf -e 'home_mailbox= Maildir/'
Then we can set the location of the virtual_alias_maps table. This table displays any email accounts for the account system in Linux. We will create this table in the / etc / postfix / virtual file. Again, we can use the postconf command:

sudo postconf -e 'virtual_alias_maps= hash:/etc/postfix/virtual'

Map of email addresses for Linux accounts

We can configure a virtual map file. Open the file in a text editor:
sudo nano /etc/postfix/virtual
The virtual map alias table uses a very simple format. On the left, you can list all addresses from which you want to receive email messages. Then separated by spaces, enter the Linux username to which the mail must be delivered.

For example, if you want to receive email at [email protected] and [email protected], and you would like these emails to be delivered to the Linux user's sammy, you can configure the file like this:
/etc/postfix/virtual

[email protected] vitusc
[email protected] vitusc
After you have matched all addresses to the appropriate server accounts, save and close the file.
We can apply the mapping by typing:

sudo postmap /etc/postfix/virtual
We need to restart Postfix to make sure that all of our changes have been applied. For this purpose, we execute the command:

sudo systemctl restart postfix

Configuring the environment according to the email location

Before installing the client, you must ensure that our MAIL environment variable has been correctly installed. The client will check this variable to find where to look for mail users.

In order for the variable to be installed regardless of how you access the account (via ssh, su, su -, sudo, etc.), we must set the variable in several different places. We will add them to the file /etc/bash.bashrc and to the file /etc/profile.d to ensure that each user will have it configured.

To add a variable to these files, type:
echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh
To read the variable to the current session, you can obtain the file /etc/profile.d/mail.sh:

source /etc/profile.d/mail.sh

How to firewall setting

If you are using the UFW firewall, according to the settings in the initial server configuration instructions, you must make an exception for Postfix.

You can allow connection to the service by entering:
sudo ufw allow Postfix
The Postfix server component is installed and ready. Then we will configure the client who can handle the mail that will process Postfix.

Installation and configuration of the e-mail client

In order to interact with the delivered mail, we will install the s-nail package. This is a variant of the BSD xmail client, which is multi-functional, can handle the Maildir format correctly and is mostly backward compatible. The GNU mail version has some limitations, such as reading mbox mail, regardless of the source format.

To install the s-nail package, enter:
sudo apt-get install s-nail
We have to set several parameters. Open the file /etc/s-nail.rc in the editor:

sudo nano /etc/s-nail.rc
The following parameters should be added at the bottom of the file:

/etc/s-nail.rc

. . .
set emptystart
set folder=Maildir
set record=+sent
This will allow customers to open even an empty mailbox. The Maildir directory will also be installed inside the variable folder, and then we will use it to create the sent mbox file for storing sent messages.

Save and close the file when finished.

Maildir initialization and client testing

Initialization of the directory structure

The easiest way to create a Maildir structure within the range of our home directory - send an email to yourself. We can do it using the mail command. Since the sent file will only be available after creating Maildir, we should disable saving to our original email. We can do this by passing the -Snorecord option.

Send an email by moving the line to the command
mail
Configure the command to mark the Linux user as a recipient:

echo 'init' | mail -s 'init' -Snorecord vitusc
You should receive the following answer:

Can't canonicalize "/home/vitusc/Maildir"
This is normal and will only be displayed in this first message. We can check to make sure that the directory was created by searching for ~ / Maildir:

ls -R ~/Maildir
You should see that the directory structure has been created and the new message file is in the ~/ Maildir / new directory:

/home/vitusc/Maildir/:
cur new tmp

/home/vitusc/Maildir/cur:

/home/vitusc/Maildir/new:
1463177269.Vfd01I40e4dM691221.mail.example.com

/home/vitusc/Maildir/tmp:
Our message has been delivered.

Zarządzanie pocztą z klientem

Use the client to check your email:

mail
You should see your new pending message:

s-nail version v14.8.6. Type ? for help.
"/home/vitusc/Maildir": 1 message 1 new

N 1 [email protected] Wed Dec 31 19:00 14/369 init
After pressing the ENTER key the message should be displayed:

[-- Message 1 -- 14 lines, 369 bytes --]:
From [email protected] Wed Dec 31 19:00:00 1969
Date: Fri, 13 May 2016 18:07:49 -0400
To: [email protected]
Subject: init
Message-Id: [email protected]
From: [email protected]

init
Sending messages with the client

You can test the message sending by entering a message in a text editor:

nano ~/test_message
Inside, enter the text you want to send:

Hello utopian,

This is a test message from @vitusc!
With the cat command, we can send a message to the mail process. This will send the message by default as a Linux user. You can adjust the "From" field with the -r flag if you want to change this value to another one:

cat ~/test_message | mail -s 'Test email subject line' -r from_field_account [email protected]
The above parameters are as follows:

-s: Row of email subject.
-r: Optional change in the "From:" line. The login name of the Linux user who is logged in will be used by default to fill this field. The -r option allows you to change this.
[email protected]: The account we send the message to. Change this to a valid account to which you have access.
Check the list of sent messages in your mail client. Start the interactive client by typing again:

mail
To view a list of sent messages enter:

file +sent
You can manage sent messages using the same commands you use for incoming mail.

Curriculum
Part 1 - TCP/IP Computer Adaptation
Part 2 - Proftpd management and configuration
Part 3 - Introduction to programming

Post:diaxpl

Coin Marketplace

STEEM 0.19
TRX 0.18
JST 0.032
BTC 86998.67
ETH 3233.80
USDT 1.00
SBD 2.92