Restricting SASL Accounts by IP Addresses in Postfix on Ubuntu 22.04

Abstract: Restrict SASL authentication to specific IP addresses in Postfix on Ubuntu 22.04 by configuring smtpd_client_restrictions in master.cf and using a hash table for access control.

Created: 2026-08-15 by
You can restrict SASL authentication to specific IP addresses in Postfix on Ubuntu 22.04 by configuring the smtpd_client_restrictions parameter within your master.cf file. By adding a check against a custom hash table, you ensure that only authorized network ranges can utilize SMTP AUTH for sending mail, effectively locking down your ten allowed accounts from unauthorized access attempts while maintaining secure email delivery.

Understanding SASL Authentication Context

Before modifying configuration files, it is crucial to understand how Postfix handles authentication. Simple Authentication and Security Layer (SASL) allows users to authenticate themselves before sending emails. This process is typically handled by daemons like Dovecot or Cyrus. In a standard setup, if a user provides valid credentials, Postfix grants them permission to relay mail. However, this creates a security risk if those credentials are compromised, as attackers can send spam from any location. Restricting these accounts by IP address adds a layer of defense-in-depth. It ensures that even if a password is leaked, the attacker cannot use the account unless they are connecting from a pre-approved network. This is particularly important for small mail servers with limited accounts, where each credential holds significant value. The goal is not to block all external connections but to limit the *authentication* capability to trusted sources.

Creating the Access Control List

The first step involves creating a text file that maps IP addresses or subnets to their permission status. Create a new file named /etc/postfix/sasl_access. Inside this file, list the IP addresses or CIDR notation for your allowed networks. For example, if your office has the static IP 192.168.1.10, you would add that entry. You should also include your home IP if you send mail remotely. Each line should follow the format:
IP_ADDRESS OK
For IPv4, this might look like:
192.168.1.10 OK
10.0.0.5 OK
If you want to explicitly deny all other IPs later, you will need a catch-all rule. However, Postfix processes restrictions in order, so the order matters significantly. After editing the file, you must convert it into a database format that Postfix can read efficiently. Run the following command in your terminal:
sudo postmap /etc/postfix/sasl_access
This generates a sasl_access.db file in the same directory. Ensure that the permissions on both files are correct, typically readable by the postfix user. If you change the allowed IPs, remember to run postmap again after every modification.

Configuring Master.cf

The most common mistake is editing main.cf directly for this specific restriction. Instead, you must modify master.cf, which controls the services Postfix runs. Open /etc/postfix/master.cf with a text editor. Look for the submission service, which usually starts with submission inet n - - - - smtpd. This service handles client submissions on port 587. You need to add the -o smtpd_client_restrictions=check_client_access hash:/etc/postfix/sasl_access option to this service line. The section should look similar to this:
submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=check_client_access hash:/etc/postfix/sasl_access
  -o smtpd_relay_restrictions=permit_sasl_authenticated,reject
Notice the last two lines. The smtpd_sasl_auth_enable=yes line turns on authentication for this service. The critical addition is the smtpd_client_restrictions line. This tells Postfix to check the client's IP against your sasl_access database before allowing any further processing. If the IP is not listed or denied, the connection is refused immediately. Do not apply this restriction to the smtp service (port 25) intended for server-to-server communication, as other mail servers have dynamic IPs. This restriction is strictly for client submission. For more information on handling dynamic IP issues in general delivery contexts, see our guide on Handling Email Delivery with Dynamic IP Addresses on Ubuntu Server using Postfix.

Applying and Verifying Changes

After saving master.cf, reload the Postfix configuration to apply the changes without restarting the entire system. Use the following command:
sudo systemctl reload postfix
To verify that the restriction is working, attempt to connect to port 587 from an IP address that is NOT in your sasl_access file. You can use telnet or nc from a different machine. When you try to authenticate, the server should reject the connection or the authentication attempt with a 5xx error code indicating client rejection. Conversely, test from an allowed IP. The connection should succeed, and you should be able to send mail normally. If you encounter issues, check the /var/log/mail.log for detailed messages. Look for entries containing "reject" or "access" to see if Postfix is correctly evaluating the rules. It is also wise to review your overall server security. Restricting SASL is one part of a broader strategy. Consider implementing additional hardening measures, such as disabling unused kernel modules, as discussed in our article on Understanding Ubuntu 22.04 VPS Security.

Maintaining the Allowlist

As your needs change, you may need to add or remove IPs. Always edit the plain text sasl_access file, then re-run postmap, and finally reload Postfix. Never edit the .db file directly. If you manage multiple domains, ensure that this restriction applies uniformly if you want consistent security across all accounts. For scenarios where you need to adjust sender headers rather than client access, refer to our tutorial on Change Postfix Sender Envelope MAIL FROM Address To Match Return Path Header Email For Multiple Domains. This ensures your outbound mail looks professional while remaining secure.

Frequently Asked Questions

Can I allow specific IPs for some accounts but not others?

Postfix does not natively support per-account IP restrictions in the basic configuration. You would need to implement virtual alias maps or use a more complex policy daemon to achieve granular per-user control. For most small setups, applying the restriction globally to the submission service is sufficient and simpler to maintain.

What happens if my ISP assigns me a dynamic IP?

If your home IP changes frequently, you cannot rely on static IP whitelisting. Instead, consider using a VPN or a tunneling service to connect to your mail server. Alternatively, you can use strong passwords and enable two-factor authentication if your SASL backend supports it, reducing the reliance on IP-based security.

Does this affect server-to-server mail delivery?

No, because the restriction is applied only to the submission service (port 587). Standard SMTP traffic on port 25 remains unrestricted for incoming mail from other mail servers. This ensures that your server can still receive emails from the public internet while keeping outgoing authenticated mail secure.

How do I troubleshoot if legitimate users are blocked?

Check the /var/log/mail.log file for rejection messages. Look for lines containing "client=..." followed by "reject". Verify that the IP address reported in the log matches the one you added to sasl_access. Remember that if you are behind a NAT or proxy, the logged IP might be the gateway's IP, not the user's actual device IP.

Is it safe to leave the default reject rule out?

If you do not add a default REJECT rule to sasl_access, Postfix will proceed to the next restriction or allow the connection if no other rules match. It is best practice to add a final line like "REJECT" at the end of your sasl_access file to explicitly deny any IP not previously listed as OK.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More