To block a domain name in Postfix, you can use the sender_access
feature to reject emails from specific domains. Here’s a step-by-step guide:
1. Edit Postfix Configuration
Open the main configuration file:
sudo nano /etc/postfix/main.cf
Look for the smtpd_sender_restrictions
line. If it doesn’t exist, add it, or modify it to include the check_sender_access
directive:
smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender_access, permit
The permit
ensures valid senders are not blocked after checking the list.
2. Create the Sender Access File
Create or edit the sender access file:
sudo nano /etc/postfix/sender_access
Add the domains or email addresses you want to block. For example:
bad-domain.com REJECT
spammer@example.com REJECT You are spam.
bad-domain.com: Blocks all senders from the domain.
spammer@example.com: Blocks a specific sender sending “You are spam” back to the server.
3. Generate the Postfix Hash Database
Compile the sender_access
file into a format Postfix can use:
sudo postmap /etc/postfix/sender_access
This generates /etc/postfix/sender_access.db
.
4. Reload Postfix
Apply the changes by reloading Postfix:
sudo systemctl reload postfix
5. Test the Configuration
Send a test email from the blocked domain or address to verify that the rule is working. Postfix will reject emails from those senders with a message like:
550 5.7.1 <user@bad-domain.com>: Sender address rejected
Additional Notes:
Ensure the hash:
format matches your Postfix installation. If you’re using another database type (e.g., btree
or mysql
), adjust the configuration accordingly.
Always back up configuration files before making changes.
Use permit
or other restrictions cautiously to avoid blocking legitimate traffic.