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/hostnameand/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
- Update the package index. Run
sudo apt updateso the installer sees the current package versions in the Stretch repository. - Install the Sendmail packages. Run
sudo apt install sendmail sendmail-bin. Thesendmailpackage pulls in the binaries and thesendmailconfighelper, whilesendmail-binprovides the daemon itself. Accept any dependency prompts. - 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.
- Set the system mail name. The installer also asks for the "System mail name". Enter the domain portion of your FQDN, for example
example.comrather than the full hostname. This value is written to/etc/mailnameand is used to qualify local addresses. - Verify the hostname resolves. Run
hostname -fand confirm it prints the FQDN. If it prints only a short name, fix/etc/hostnameand/etc/hostsbefore continuing; Sendmail will otherwise use a malformed name in the SMTP banner and in outgoing headers. - Edit the Sendmail macro file. Open
/etc/mail/sendmail.mcin an editor. Find theDAEMON_OPTIONSline that restricts the listener to127.0.0.1. If you want to receive mail from other hosts, change the address to0.0.0.0or remove that line; if you only need local delivery, leave it as is. Also check theMASQUERADE_ASsetting if you want outgoing mail to appear from your domain. - Regenerate the configuration. Run
sudo sendmailconfigand answer the prompts, or rebuild the file directly withsudo m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf. The generatedsendmail.cfis what the daemon actually reads, so editing the.mcfile alone has no effect until you regenerate. - Restart the service. Run
sudo systemctl restart sendmailand thensudo systemctl enable sendmailso it starts at boot. Check the status withsudo systemctl status sendmail. - Confirm the listener. Run
sudo ss -tlnp | grep :25orsudo netstat -tlnp | grep :25. You should see the Sendmail process bound to port 25 on the address you configured. - Send a test message. Use the
mailcommand, which is provided by thebsd-mailxormailutilspackage if it is not already present:echo "Test from Sendmail" | mail -s "Sendmail test" you@example.com. Then inspect/var/log/mail.logfor the delivery status. - Check the queue. Run
sudo mailqto 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.