discard Command in Linux



In Linux, the discard in Postfix defines a mechanism to discard incoming emails silently. It essentially processes the delivery requests from the queue manager. These requests contain details about the queue file, sender address, domain or hostname, and recipient address. Any of these details can be used as a reason for discarding emails. For instance, emails can be discarded by mentioning the sender’s address in the configuration files.

Table of Contents

Here is a comprehensive guide to the options available with the discard command −

Prerequisites to Use the Postfix discard Feature

To use the Postfix discard feature, you need to have Postfix installed and configured on Linux. Postfix is an open-source mail transfer agent (MTA) that handles the routing and delivery of emails. It is easy to configure, making it a popular choice to manage emails on Linux servers.

To check whether the Postfix is installed or not, run the following command −

sudo postfix status
Prerequisites Postfix discard feature

If it does not show the Postfix status, it is not installed.

To get Postfix on Ubuntu, Debian, and other Debian-based distributions, run the following command −

sudo apt install postfix

To install it on Red Hat-based distros, such as CentOS or Fedora, run −

sudo yum install postfix

After installing Postfix, it needs an initial setup.

On Debian-based distributions including Ubuntu, it can be done interactively through dpkg command −

sudo dpkg-reconfigure postfix

On Red Hat-based distributions, it has to be done by manually configuring the /etc/postfix/main.cf file. To access this file, use the nano editor −

sudo nano /etc/postfix/main.cf

After the Postfix initial configuration reload the service.

sudo postfix reload

Syntax of Postfix discard Service

The discard delivery agent can be configured in the master.cf file using the following syntax −

discard [generic postfix daemon options]

The [generic postfix daemon options] are listed in the following table −

Options Description
service It is the name of the service
type To specify the type of the service from fifo, inet, unix or pass
private (yes) To keep the service private or public (default: private)
unpriv (yes) To keep the service accessible with reduced privileges (default: yes)
chroot (no) To keep the service run in chroot jail (default: no)
wakeup (never) Specifies the time in seconds to wake up service for maintenance (default: never)
maxproc (100) It defines the maximum number of processes for the service (default: 100)
command + args It is used to specify the command with arguments to run

These options are explicitly mentioned in the master.cf file, as shown in the image below −

Options Explicitly Mentioned in master.cf

Configuring Postfix discard Service in Linux

The discard service is configured by default in the master.cf file. However, if it is not present, it can easily be configured as shown in the image below −

Configuring Postfix discard Service

In the above-mentioned configuration −

  • service − It is set to discard
  • type − It is set as unix, which means the service used Unix domain sockets
  • private − It is set to default (-), which means the service is private
  • unpriv − It is also set to default (-), which means the service runs without root privileges
  • chroot − It is set as y, which signifies that the service runs in the chroot jail
  • wakeup − It is set as -, which shows no wake-up time
  • maxproc − It is set as -, which means by default 100 processes are allowed simultaneously
  • command + args − The discard is the given command

Configuring the discard Rules

The final step is to set up the restrictions in the main.cf file. But first, create a file that mentions the rules to discard the email −

sudo nano /etc/postfix/access_control

Now, add rules. For instance, to discard an email from a specific sender, mention its email address −

Configuring discard Rules 1

Two example entries are added to the access_control (any name can be given) file. Save the file and exit the editor.

It is important to note that the rules can also be added using IP addresses, domains, or hostnames. The rules are not necessarily used to discard the emails, other rules such as OK, WARN, or REJECT can also be employed if needed.

Now, open the main.cf file −

sudo nano /etc/postfix/main.cf

Insert the following lines −

smtpd_client_restrictions = check_client_access hash:/etc/postfix/access_control
Configuring discard Rules 2

The smtpd_client_restrictions parameter is used for access control and security. The check_client_access parameter is used to specify the rule file, which is access_control in this case. Save the file and exit the editor.

To apply the changes, reload the Postfix service −

sudo postfix reload

To verify whether the mails are discarded or not, view the log −

less /var/log/mail.log

Note − The discard action is used in Postfix configuration files like header_checks, body_checks, or access_control to silently discard emails based on specified criteria. n the master.cf file, a discard service can be defined to handle the discarding of emails in a specific manner. The discard action and discard service can work independently but can also be used together to manage how emails are discarded.

Conclusion

In Linux, the discard is a mail delivery agent, which discards emails without generating any notifications or bounce messages. Configuring the discard service in master.cf and then defining the discard rules in the main.cf file effectively helps in managing unwanted emails.

This tutorial covered the complete process of installing and configuring the Postfix on Linux, how to set up the discard service, and defining rules to discard emails in Linux.

Advertisements