Useful Commands for Postfix Mail Server on Ubuntu 22.04/24.04

Introduction

Postfix is one of the most popular and reliable mail servers in Linux.
As a Linux administrator, you need to know the right commands to check mail flow, fix problems, and detect spamming.
In this guide, we will cover the most useful Postfix commands for Ubuntu 22.04.

Check Postfix Service Status

systemctl status postfix
systemctl restart postfix
systemctl enable postfix

Check Mail Queue

Mail queue shows pending emails waiting to be delivered.

mailq             # Show mail queue
postqueue -p      # Same as mailq

To flush (force send) the queue:

postqueue -f

To delete all queued mails (useful if spam mails stuck):

postsuper -d ALL

To delete mails from a specific user:

postqueue -p | grep "spammer@bitscentric.local" | awk '{print $1}' | tr -d '*!' | postsuper -d -

Check Mail Logs

Logs are very important for troubleshooting mail issues.

tail -f /var/log/mail.log
tail -f /var/log/mail.err

Check Configuration

Show current Postfix configuration:

postconf -n      # Show only non-default settings
postconf -d      # Show all default settings

To check a single parameter:

postconf myhostname

Test Sending Email

Send a test email from command line:

echo "Test mail body" | mail -s "Test Subject" user@bitscentric.local

Check Open Relay (Security Test)

Postfix should not allow open relay. Check with:

postconf smtpd_recipient_restrictions

It should contain reject_unauth_destination.

Check Spamming Activity

To check which user is sending bulk emails:

grep "sasl_username" /var/log/mail.log | awk '{print $9}' | sort | uniq -c | sort -nr

To check client IPs sending most mails:

grep "sasl_username" /var/log/mail.log | awk '{print $7}' | sort | uniq -c | sort -nr

Flush DNS Cache for Postfix

Sometimes DNS caching causes delivery issues. Reload Postfix:

systemctl reload postfix

Useful Postfix Queue Management

  • List mails in queue: postqueue -p
  • Requeue mail: postsuper -r QUEUE_ID
  • Delete mail: postsuper -d QUEUE_ID
  • Hold mail in queue: postsuper -h QUEUE_ID
  • Release held mail: postsuper -H QUEUE_ID

Best Practices for Administrators

  • Monitor /var/log/mail.log regularly.
  • Check queue daily using mailq.
  • Block spammers quickly by checking sasl_username logs.
  • Use SPF, DKIM, and DMARC DNS records for good delivery.
  • Always keep Postfix updated: sudo apt upgrade postfix.
  • Set up monitoring tools (Nagios, Zabbix, or Grafana) for mail queue and service health.

Conclusion

Postfix is powerful but needs regular monitoring.
By using commands like mailq, postqueue, postconf, and analyzing logs,
a Linux administrator can keep the mail server secure, fast, and spam-free.
With these troubleshooting and best practice commands, you will be able to manage Postfix effectively on Ubuntu 22.04.

Leave a Reply

Your email address will not be published. Required fields are marked *