Score:0

Postfix: different smarthosts per server port

se flag
Mat

I need to install Postfix and let it listen on two different ports: 1025 and 1026:

/etc/postfix/master.cf:

1025      inet  n       -       y       -       -       smtpd
1026      inet  n       -       y       -       -       smtpd

I need two different relayhosts for messages coming from these two ports:

  • relay1.domain.com for messages coming to the port 1025
  • relay2.domain.com for messages coming to the port 1026

Relay hosts are using different authentications (for this I know I can use the smtp_sasl_password_maps option).

What I am able to do is enable the two transports on different ports, but what I am not able to do is to specify different relay hosts for different transport: I know I could use the transport_maps option to specify different transport (and relay hosts) for different destinations, but not for different listen ports.

I also know I can create different Postfix instances (with different master.cf configuration files), but I would like to keep a single instance so that I don't have to deal with different systemctl services.

Do you have any idea?

Score:2
za flag

Read man 5 master carefully:

A logical line starts with non-whitespace text. A line that starts with whitespace continues a logical line.

Usually a line which does start with the whitespace continues the last field of the table and that allows to supply argument to the command in that table.

Knowing that you can set up (override) smtpd attributes directly in the master.cf via the command line arguments:

1025      inet  n       -       y       -       -       smtpd
    -o relayhost=[relay1.example.com]
1026      inet  n       -       y       -       -       smtpd
    -o relayhost=[relay2.example.com]

Square brackets suppress MX lookups (e.g. this will use an A record of relay1.example.com instead of looking up MX record of it and then looking A record for that name).

This way you just override the settings of main.cf for this particular instance of the daemon.

There should be no spaces in the arguments of "-o" in the master.cf (in particular, no spaces around "="), and you can't even use quotes for that. If you need spaces there or you just want to be able to set that up in the main.cf, you can declare variables there and reference them in master.cf:

main.cf:

relayhost_1025 = [relay1.example.com]
relayhost_1026 = [relay2.example.com]

master.cf:

1025      inet  n       -       y       -       -       smtpd
    -o relayhost=$relayhost_1025
1026      inet  n       -       y       -       -       smtpd
    -o relayhost=$relayhost_1026

Or, curly brackets could be used to enclose spaces; that described in the manual I linked to, but I never did that myself.

As you probably have guessed, any options can be overridden like this, and you may override multiple options. Even if you look at default master.cf closely you may notice the (probably) commented out submission service which uses that syntax. There are also usually other services defined that span multiple lines like this. Use it as an example.

Mat avatar
se flag
Mat
Thank you! I will try!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.