Menu
Mail Transfer Agent (MTA) basics
“What an MTA does, the big three (Postfix, Sendmail, Exim), and how to set up aliases, forwarding and check the mail queue.”
Even a server that never hosts anyone’s inbox sends mail. Cron emails the output of jobs to their owner, smartd warns about a failing disk, sudo reports someone who isn’t allowed to use it, and logwatch sends a daily summary. All of that goes through a Mail Transfer Agent on the machine. If it’s missing or misconfigured, those warnings quietly disappear.
For LPIC-1 you don’t configure a full mail server. You need to know which MTAs exist, how aliases and forwarding work, and the commands every MTA shares.
Who does what in email#
Email passes through three kinds of programs:
| Role | Job | Examples |
|---|---|---|
| MUA, Mail User Agent | The program a person reads and writes mail with | mail, mutt, Thunderbird |
| MTA, Mail Transfer Agent | Accepts mail and passes it on to the next server using SMTP | Postfix, Sendmail, Exim |
| MDA, Mail Delivery Agent | Puts a message into the right mailbox | Postfix’s local, procmail, Dovecot LDA |
MTAs talk to each other with SMTP (Simple Mail Transfer Protocol) on TCP port 25. Mail clients usually hand mail to their provider on port 587 (submission) or 465 (SMTP over TLS).
On a single Linux box, local mail for user amin ends up in a plain file: /var/mail/amin on Debian and Ubuntu, /var/spool/mail/amin on RHEL (where /var/mail is a symlink to it).
The common MTAs#
You don’t have to configure these for the exam, just recognise them:
| MTA | Known for | Main config |
|---|---|---|
| Sendmail | The original, from 1983. Powerful, but its config language is famously hard. Still in RHEL, marked deprecated. | /etc/mail/sendmail.mc, compiled with m4 into /etc/mail/sendmail.cf |
| Postfix | Built as a safer, simpler Sendmail replacement, split into small processes with few privileges. The most common choice today and the standard MTA on RHEL and Fedora. | /etc/postfix/main.cf, /etc/postfix/master.cf |
| Exim | Flexible, single-process design. Debian’s traditional default MTA (package exim4). | /etc/exim4/ on Debian |
Only one MTA can own port 25 and /usr/sbin/sendmail, so distributions let you install just one. Debian’s packages conflict with each other. RHEL uses the alternatives system:
alternatives --display mta # RHEL/Fedora: which MTA provides sendmail, mailq...The Sendmail emulation layer#
Sendmail was everywhere for so long that a lot of software calls /usr/sbin/sendmail directly to send mail. PHP’s mail() function and cron both do. So Postfix and Exim each ship a Sendmail emulation layer: their own sendmail, mailq and newaliases commands that accept the same options.
| Command | Same as | Does |
|---|---|---|
sendmail | Accept a message on standard input and queue it | |
mailq | sendmail -bp | List the mail queue |
newaliases | sendmail -bi | Rebuild the aliases database |
sendmail -q | Try to deliver everything in the queue now |
Each MTA also has native tools you may meet:
| Task | Postfix | Exim |
|---|---|---|
| Show the queue | postqueue -p | exim -bp |
| Flush the queue | postqueue -f or postfix flush | exim -qff |
| Delete a message | postsuper -d QUEUEID | exim -Mrm MSGID |
Try it: a local-only Postfix#
You can test everything below in a Debian or Ubuntu container. Tell Postfix to deliver locally only, then start it:
docker run -it --rm --hostname web1 debian
echo "postfix postfix/main_mailer_type select Local only" | debconf-set-selections
apt update && DEBIAN_FRONTEND=noninteractive apt install -y postfix mailutils
postfix start
useradd -m aminOn a real Debian or Ubuntu machine, run sudo apt install postfix mailutils and pick Local only in the menu. On RHEL or Fedora: sudo dnf install postfix s-nail and sudo systemctl enable --now postfix.
Sending and reading mail with mail#
The mail command is a simple MUA. It comes from mailutils or bsd-mailx on Debian and Ubuntu, and from s-nail on RHEL and Fedora. To send:
echo "The backup finished at 02:00." | mail -s "Backup report" amin
mail -s "Disk report" amin < /tmp/df.txt-s sets the subject. Without a pipe or redirect, mail reads the body from the keyboard until you press Ctrl+D.
Run mail with no arguments to read your own mailbox:
su - amin -c mail"/var/mail/amin": 2 messages 2 new
>N 1 root Tue Dec 1 14:40 13/451 Backup report
N 2 root Tue Dec 1 14:41 13/449 Disk report
?
At the ? prompt, type a number to read that message, d to delete it, r to reply, h to list headers again and q to quit.
You can also talk to the Sendmail layer directly. With -t, it takes the recipients from the To: header:
printf 'To: amin\nSubject: Hello\n\nSent with sendmail -t\n' | sendmail -tEmail aliases: /etc/aliases#
An alias is an extra local name that delivers somewhere else. Aliases live in /etc/aliases, one per line, name: target:
# required by the mail RFCs
postmaster: root
# send root's mail to a real person
root: amin
# one name, several people
support: amin, sara
# an outside address
alerts: oncall@example.com
# pipe the message into a program
tickets: "|/usr/local/bin/new-ticket"
# append to a file
archive: /var/mail-archive/archive.mbox
# read the recipient list from a file
team: :include:/etc/mail/team.listThe MTA doesn’t read this text file directly. It reads a compiled database built from it (such as /etc/aliases.db), which is much faster to search. So after every change, rebuild it:
sudo newaliasesForgetting newaliases is the classic mistake: the file looks right, but nothing changes.
The root: amin line matters on real servers. Cron output, security warnings and failed jobs all go to root, and nobody reads root’s mailbox. One alias sends them to someone who will.
Forwarding a user’s mail: ~/.forward#
Aliases are system-wide and need root. A user can forward their own mail with a .forward file in their home directory. Each line is an address, a file, or a pipe:
amin@example.comThat sends all of amin’s mail to an outside address and keeps nothing locally. To keep a local copy too, add the local name with a backslash:
\amin, amin@example.comThe backslash means “deliver to the local mailbox without looking at aliases or .forward again”, so the forward doesn’t loop.
A few rules:
- No
newaliasesneeded: the MTA reads~/.forwardevery time it delivers. - Keep the file owned by the user and not writable by the group or others. Sendmail, for one, ignores an unsafe
.forwardon purpose. - Forwarding to outside addresses only works if the MTA is allowed to send mail to the internet. A “Local only” setup will queue those messages.
Checking the mail queue with mailq#
When a message can’t be delivered right away (the remote server is down, DNS fails, the address is wrong), the MTA keeps it in a queue and retries later. mailq shows that queue:
mailq-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
4A7C21E0412 456 Tue Dec 1 14:52:03 root@web1
(connect to example.com[93.184.215.14]:25: Connection timed out)
oncall@example.com
-- 0 Kbytes in 1 Request.
Each entry shows the queue ID, size, time, sender, the reason it’s stuck, and the recipient. An empty queue says Mail queue is empty. Once you’ve fixed the cause, retry right away:
sudo sendmail -q # works with any MTA
sudo postqueue -f # the Postfix wayWhen something looks wrong, check the mail log: /var/log/mail.log on Debian and Ubuntu, /var/log/maillog on RHEL, or journalctl -u postfix.
Exam checklist#
- MUA = the reader (
mail, mutt), MTA = the transporter (Postfix, Sendmail, Exim), MDA = the deliverer. SMTP uses port 25. - Postfix: modern, common, default on RHEL. Sendmail: the oldest,
sendmail.cf. Exim: Debian’s traditional default. - The Sendmail emulation layer gives every MTA the same
sendmail,mailq(sendmail -bp) andnewaliases(sendmail -bi) commands. /etc/aliasesholdsname: targetlines. Targets can be users, addresses,"|command",/fileor:include:/file.- Always run
newaliasesafter editing/etc/aliases. ~/.forwardlists where a user’s mail goes.\userkeeps a local copy. Nonewaliasesneeded.mail -s "subject" usersends mail.mailalone reads/var/mail/$USER.mailqlists stuck messages.sendmail -qretries them.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-108): Mail Transfer Agent (MTA) basics"
[main d38c9ae] learn(lpic-108): Mail Transfer Agent (MTA) basics
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: