Menu

Essential System Services

Step 3 of 4 LPIC-1 108.3weight 3

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.”

View .md

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:

RoleJobExamples
MUA, Mail User AgentThe program a person reads and writes mail withmail, mutt, Thunderbird
MTA, Mail Transfer AgentAccepts mail and passes it on to the next server using SMTPPostfix, Sendmail, Exim
MDA, Mail Delivery AgentPuts a message into the right mailboxPostfix’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:

MTAKnown forMain config
SendmailThe 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
PostfixBuilt 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
EximFlexible, 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:

bash
alternatives --display mta          # RHEL/Fedora: which MTA provides sendmail, mailq...
Qmail was a fourth popular MTA in the late 1990s, written with security as its top goal. Its author offered a cash reward for the first security hole, and for years nobody could claim it.

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.

CommandSame asDoes
sendmailAccept a message on standard input and queue it
mailqsendmail -bpList the mail queue
newaliasessendmail -biRebuild the aliases database
sendmail -qTry to deliver everything in the queue now

Each MTA also has native tools you may meet:

TaskPostfixExim
Show the queuepostqueue -pexim -bp
Flush the queuepostqueue -f or postfix flushexim -qff
Delete a messagepostsuper -d QUEUEIDexim -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:

bash
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 amin

On 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:

bash
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:

bash
su - amin -c mail
console
"/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:

bash
printf 'To: amin\nSubject: Hello\n\nSent with sendmail -t\n' | sendmail -t

Email aliases: /etc/aliases#

An alias is an extra local name that delivers somewhere else. Aliases live in /etc/aliases, one per line, name: target:

/etc/aliasestext
# 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.list

The 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:

bash
sudo newaliases

Forgetting 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:

/home/amin/.forwardtext
amin@example.com

That 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:

/home/amin/.forwardtext
\amin, amin@example.com

The backslash means “deliver to the local mailbox without looking at aliases or .forward again”, so the forward doesn’t loop.

A few rules:

  • No newaliases needed: the MTA reads ~/.forward every time it delivers.
  • Keep the file owned by the user and not writable by the group or others. Sendmail, for one, ignores an unsafe .forward on 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:

bash
mailq
console
-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:

bash
sudo sendmail -q          # works with any MTA
sudo postqueue -f         # the Postfix way

When 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) and newaliases (sendmail -bi) commands.
  • /etc/aliases holds name: target lines. Targets can be users, addresses, "|command", /file or :include:/file.
  • Always run newaliases after editing /etc/aliases.
  • ~/.forward lists where a user’s mail goes. \user keeps a local copy. No newaliases needed.
  • mail -s "subject" user sends mail. mail alone reads /var/mail/$USER.
  • mailq lists stuck messages. sendmail -q retries them.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. You added support: amin, sara to /etc/aliases. What else must you do before it works?
  2. User amin wants his mail sent to amin@example.com and kept in his local mailbox. What goes in ~/.forward?
  3. Why does mailq work the same whether Postfix, Sendmail or Exim is installed?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy