Automate adding domains and IP addresses to Postfix and Postgrey whitelists on Mail-in-a-Box servers with a single, universal Bash script.
- Overview
- Prerequisites
- Installation
- Configuration
- Usage
- Backup & Safety
- Examples
- Contributing
- License
add_whitelists.sh is a universal script to add domains and IP addresses to Postfix and Postgrey whitelists. It supports single-entry and bulk-from-file modes. The script auto-creates missing files/directories, makes timestamped backups before changes, ignores blank lines and # comments, deduplicates entries, and restarts Postfix/Postgrey only when changes are made. At the end it prints how many entries were added and a list of what was actually added.
Single entry
sudo ./add_whitelists.sh example.com
# or
sudo ./add_whitelists.sh 203.0.113.7
-
Create a file
whitelists.txtwith one domain or IP per line (blank lines and#comments are ignored):example.com
mail.example.org
192.168.1.10 -
Run:
sudo ./add_whitelists.sh -f whitelists.txt
- Auto-creates required files if missing:
/etc/postfix/client_whitelist/etc/postgrey/whitelist_clients.local
- Backs up whitelist files with timestamps before modifying them.
- Skips duplicates (doesn’t add the same entry twice).
- Rebuilds Postfix hash map (
postmap) and restarts Postfix/Postgrey only when changes occurred. - Shows a summary: totals added to Postfix/Postgrey and a list of actually added entries.
Keeping your whitelist entries in a separate file lets you safely publish this script on GitHub without exposing private data.
- OS: Debian / Ubuntu
- Services: Postfix & Postgrey installed
- Permissions: Root or
sudoto modify/etc/postfixand/etc/postgrey
-
Clone the repo
git clone https://github.com/Anton-Babaskin/miab-whitelists.git cd miab-whitelists -
Create your whitelist file
# whitelist.txt example.com 198.51.100.0/24 mail.partner-domain.org -
Make the script executable
chmod +x add_whitelists.sh
Customize paths in the script header if needed:
# add_whitelists.sh
POSTFIX_FILE="/etc/postfix/client_whitelist"
POSTGREY_FILE="/etc/postgrey/whitelist_clients.local"Run the script with your whitelist file:
sudo ./add_whitelists.sh -f whitelist.txtQuick add: add any single domain or IP with one command, no file needed:
sudo ./add_whitelists.sh YOURDOMAIN.comWhat happens under the hood:
-
Backups:
/etc/postfix/client_whitelist.bak_YYYY-MM-DD_HH:MM:SS /etc/postgrey/whitelist_clients.local.bak_YYYY-MM-DD_HH:MM:SS
-
Reads each line:
- If missing in Postfix: appends
ENTRY OK. - If a domain (not IP/CIDR) and missing in Postgrey: appends
ENTRY.
- If missing in Postfix: appends
-
Applies changes:
postmap "$POSTFIX_FILE" systemctl restart postfix systemctl restart postgrey
Before making changes, the script creates timestamped backups. To restore from backup:
sudo cp /etc/postfix/client_whitelist.bak_YYYY-MM-DD_HH:MM:SS /etc/postfix/client_whitelist
sudo cp /etc/postgrey/whitelist_clients.local.bak_YYYY-MM-DD_HH:MM:SS /etc/postgrey/whitelist_clients.local
sudo postmap /etc/postfix/client_whitelist
sudo systemctl restart postfix postgreyBackup rotation: To automatically delete backups older than 30 days, add this line at the end of your script or run it separately:
find /etc/postfix -name "client_whitelist.bak_*" -mtime +30 -deleteOne-liner:
echo -e "partner.com
198.51.100.0/24" > whitelist.txt
sudo ./add_whitelists.sh whitelist.txtAutomate via cron:
# /etc/cron.daily/miab-whitelist
#!/bin/bash
cd /opt/miab-whitelists
git pull --ff-only
/opt/miab-whitelists/add_whitelists.sh /opt/miab-whitelists/whitelist.txtPull requests and issues welcome! Please follow the standard fork → branch → PR workflow.
MIT © Anton Babaskin. See LICENSE for details.