Install Postfix on your VPS
Postfix is a mail transfer agent, the program that sends and receives email between servers using SMTP. This guide installs Postfix on Ubuntu 24.04 in a send-only configuration, which is the safest choice for application notifications, and sends a first test message.
Before you begin
- A VPS running Ubuntu 24.04 LTS, with SSH access as a sudo user (this guide uses
alex). - A hostname for the server, such as
mail.example.com, with a DNS A record pointing at the VPS IP203.0.113.10. - Understanding of how email is routed. If not, read How email works: SMTP, IMAP and MX first.
Step 1: Set the server's hostname
Mail servers identify themselves by name during delivery, so the hostname should match the DNS name you created. Set it, then confirm:
sudo hostnamectl set-hostname mail.example.com
hostname -f
mail.example.com
mail.example.com.Step 2: Install Postfix
Install Postfix along with bsd-mailx, which provides the mail command for sending test messages. A setup screen appears:
sudo apt update
sudo apt install -y postfix bsd-mailx
- General type of mail configuration. Choose
Internet Site. This lets Postfix send mail directly to other servers. - System mail name. Enter
example.com, your domain. Addresses will be built from it.
Step 3: Configure Postfix for sending only
Postfix's main settings live in /etc/postfix/main.cf. The postconf -e command edits one setting at a time. Start by making Postfix listen only on the loopback address. Applications on the server can still send through it, but the internet cannot use it to relay spam.
sudo postconf -e "myhostname = mail.example.com"
sudo postconf -e "inet_interfaces = loopback-only"
sudo systemctl restart postfix
Review the settings that now differ from the defaults:
sudo postconf -n
mailbox_size_limit = 0
myhostname = mail.example.com
mydestination = $myhostname, example.com, localhost.localdomain, localhost
inet_interfaces = loopback-only
myorigin = /etc/mailname
...
inet_interfaces = all and open port 25 unless you intend to receive mail for your domain. An open relay is quickly abused by spammers and can get your IP address blocklisted.Step 4: Send a test message
The mail command reads the message body from standard input. The pipe symbol | sends the output of echo into mail, and -s sets the subject line. Replace the recipient with an address you can check:
echo "This is a test message from my VPS." | mail -s "Postfix test" alex@example.org
Watch the delivery log. Postfix logs through the systemd journal on Ubuntu, so read it with journalctl:
sudo journalctl -u postfix -n 30 --no-pager
postfix/smtp[...]: ... to=<alex@example.org>, ... status=sent (250 ...)
A line containing status=sent means the receiving server accepted the message. Check that the queue is empty:
mailq
Mail queue is empty
Step 5: Publish SPF and DMARC records
Without these records, many receiving servers will mark your messages as spam. Add two TXT records in your DNS. If your domain is managed in Lantern DNS, use the DNS editor there.
Name: example.com
Type: TXT
Value: v=spf1 ip4:203.0.113.10 -all
Name: _dmarc.example.com
Type: TXT
Value: v=DMARC1; p=none; rua=mailto:postmaster@example.com
The SPF record says only this server may send mail for the domain. DMARC starts with p=none, which reports problems without rejecting mail, so you can watch results before tightening the policy. DKIM signing is a separate step, which you can add later.
Troubleshooting
- "Relay access denied" or the message never leaves. The sender must be on the server itself. With
loopback-only, only local programs can send through Postfix. - status=deferred with "Connection timed out". Your provider may be blocking outbound port 25. Ask LightServe support by chat or email.
- The test message lands in spam. Check the SPF and DMARC records, the reverse DNS record, and the message headers. A brand-new IP address often has a poor reputation at first.
- Postfix will not start. Run
sudo postfix checkto find configuration errors, then readsudo journalctl -u postfix -n 50 --no-pager.
Next steps
For mail that your server should receive or that users should read, you need an IMAP service such as Dovecot, plus a DKIM signer. Add those when you have a clear need, and keep the server send-only until then.
Something not working? Chat or email our support team and we will help you get unstuck.
Contact support