Phit

How to Install Sendmail on Debian 9 Stretch

– Reading time: 6 min – Stretching and Mobility

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To install Sendmail on Debian 9 Stretch, run apt update and then apt install sendmail sendmail-bin as root or with sudo. The package manager will prompt you to choose a mail configuration type; selecting "Internet Site" is the usual choice for a server that sends and receives mail directly. After the install finishes, edit /etc/mail/sendmail.mc to set your hostname and listening address, regenerate sendmail.cf with sendmailconfig or m4, and restart the service. Finally, confirm the daemon is listening on port 25 and send a test message with the mail command.

Sendmail is a classic Mail Transfer Agent (MTA): it accepts mail from local programs, queues it, and delivers it to remote servers over SMTP. On Debian 9 (Stretch) it ships in the main repository, so no third-party source is required. The steps below assume a fresh install, a working network connection, and a fully qualified domain name (FQDN) for the machine. If you only need to send outgoing mail (for cron jobs, web applications, or system alerts), you can keep Sendmail listening on the loopback interface and skip the inbound-mail configuration.

What You'll Need

  • A Debian 9 Stretch system with root or sudo access.
  • A working internet connection and the ability to reach the Debian package mirrors.
  • A fully qualified domain name (FQDN) for the host, such as mail.example.com, configured in /etc/hostname and /etc/hosts.
  • DNS records for that domain, ideally including an MX record if you plan to receive mail.
  • Port 25 outbound open on your network or provider; many hosting providers block it by default, so check before you start.
  • A terminal or SSH session.

Step-by-Step

  1. Update the package index. Run sudo apt update so the installer sees the current package versions in the Stretch repository.
  2. Install the Sendmail packages. Run sudo apt install sendmail sendmail-bin. The sendmail package pulls in the binaries and the sendmailconfig helper, while sendmail-bin provides the daemon itself. Accept any dependency prompts.
  3. Choose a configuration type. During installation a debconf screen asks how Sendmail should be configured. For a server that sends mail directly to the internet, pick "Internet Site". For a machine that only relays through another mail server, pick "Satellite system" and supply the smarthost address. If you are unsure, "Internet Site" is the common default.
  4. Set the system mail name. The installer also asks for the "System mail name". Enter the domain portion of your FQDN, for example example.com rather than the full hostname. This value is written to /etc/mailname and is used to qualify local addresses.
  5. Verify the hostname resolves. Run hostname -f and confirm it prints the FQDN. If it prints only a short name, fix /etc/hostname and /etc/hosts before continuing; Sendmail will otherwise use a malformed name in the SMTP banner and in outgoing headers.
  6. Edit the Sendmail macro file. Open /etc/mail/sendmail.mc in an editor. Find the DAEMON_OPTIONS line that restricts the listener to 127.0.0.1. If you want to receive mail from other hosts, change the address to 0.0.0.0 or remove that line; if you only need local delivery, leave it as is. Also check the MASQUERADE_AS setting if you want outgoing mail to appear from your domain.
  7. Regenerate the configuration. Run sudo sendmailconfig and answer the prompts, or rebuild the file directly with sudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf. The generated sendmail.cf is what the daemon actually reads, so editing the .mc file alone has no effect until you regenerate.
  8. Restart the service. Run sudo systemctl restart sendmail and then sudo systemctl enable sendmail so it starts at boot. Check the status with sudo systemctl status sendmail.
  9. Confirm the listener. Run sudo ss -tlnp | grep :25 or sudo netstat -tlnp | grep :25. You should see the Sendmail process bound to port 25 on the address you configured.
  10. Send a test message. Use the mail command, which is provided by the bsd-mailx or mailutils package if it is not already present: echo "Test from Sendmail" | mail -s "Sendmail test" you@example.com. Then inspect /var/log/mail.log for the delivery status.
  11. Check the queue. Run sudo mailq to list queued messages. An empty queue after a successful test means delivery completed. If a message remains, read the reason in the queue output and in the log.

Common Mistakes

Editing /etc/mail/sendmail.cf by hand is the most frequent error. That file is generated; make changes in sendmail.mc and regenerate, or your edits will be overwritten the next time sendmailconfig runs. Another common problem is a hostname that does not resolve to the machine's address, which causes Sendmail to advertise an invalid name in the SMTP greeting and leads to rejections by remote servers. Leaving the default loopback-only listener while expecting to receive external mail is another trap: the daemon simply will not accept connections from other hosts. Finally, many people forget that outbound port 25 is often blocked by cloud providers; the install succeeds, but every delivery attempt times out, and the queue grows silently.

Tips

Keep a copy of a working sendmail.mc before making changes so you can roll back quickly. Use sendmail -bt to test address rewriting rules without sending mail, and sendmail -v to run a delivery in verbose mode when diagnosing a single message. If the machine only sends mail, consider restricting the listener to loopback and using a smarthost for relay; this reduces exposure and often works around blocked port 25 by using the provider's submission port. Review /var/log/mail.log regularly, and set up log rotation so the file does not grow without limit. For applications that expect a sendmail binary at a fixed path, confirm that /usr/sbin/sendmail exists and is executable.

Troubleshooting

If systemctl status sendmail shows a failed unit, read the journal with sudo journalctl -u sendmail -n 50. A syntax error in sendmail.cf usually points to a bad regeneration step; rerun sendmailconfig and watch for error output. If messages stay in the queue, run sudo mailq and then sudo sendmail -q -v to force a verbose queue run; the output names the remote host and the failure reason. Connection timeouts to port 25 on remote servers usually mean your provider blocks outbound SMTP, in which case configure a smarthost or submission relay. "Relaying denied" errors mean the server you are sending through does not trust your host; authenticate or add the address to its relay list. If local mail never arrives, check that the recipient exists and that /var/mail is writable. When DNS is the suspect, test resolution with dig MX example.com and confirm the MX record points to a host that accepts mail for the domain.

Frequently Asked Questions

Do I need to open port 25 to send mail?

Outbound connections to port 25 on remote servers are required for direct delivery. Many providers block this port; in that case use a smarthost or submission relay on another port.

Can I install Sendmail without receiving external mail?

Yes. Leave the listener bound to 127.0.0.1 and use the server only to send mail from local applications and cron jobs.

Why does hostname -f show a short name?

The FQDN is derived from /etc/hostname and /etc/hosts. Add an entry mapping the machine's address to the full name, then retest.

What is the difference between sendmail.mc and sendmail.cf?

sendmail.mc is the human-edited macro source; sendmail.cf is the generated configuration the daemon reads. Always edit the .mc file and regenerate.

How do I check whether Sendmail is running?

Use systemctl status sendmail and confirm the process is listening on port 25 with ss -tlnp.

Where are delivery errors logged?

On Debian 9, mail activity is written to /var/log/mail.log. Use journalctl -u sendmail for service-level errors.