Postfix completely ignores relayhost setting in main.cf

Solution 1:

It appears postfix completely ignores /etc/hosts.

If the setting relayhost = FQDN instead of an IP address, it will try to resolv the FQDN for its IP address and will not use /etc/hosts to find it. It will use the system configured resolv.conf file if smtp_host_lookup is set to native.

Since it could not resolve my relay host it instead just attempted to deliver the mail straight out into the ether. I cannot find details as to if this is the correct behaviour.

Setting the relayhost to an IP address OR ensuring the relayhost FQDN is resolvable WITHOUT using /etc/hosts fixes this problem.

Solution 2:

1st Option

If you want to relay ALL email to a given /etc/hosts host and not receive any email, add this to /etc/postfix/main.cf :

mydestination= # Don't deliver anything locally

smtp_dns_support_level = disabled # native lookup only (/etc/hosts)

relayhost = hostname.mydomain.tld # your relay here. add IP/hostname to /etc/hosts

This will override DNS/MX lookups and use /etc/hosts only.
It will never use DNS lookups.

Please note: The lookup is not performed on incoming either. This is for an outbound (only) server to a relay that is found in /etc/hosts.

This is appropriate when you want a server to send LOCAL emails (root/cronjobs/etc) to an actual email server for delivery and never receive outside email itself.

Reference: smtp_dns_support_level


2nd Option

smtp_host_lookup = native can be used in place of smtp_dns_support_level above.

This will use "native naming service only" aka 'system' lookups.

It will normally first query /etc/hosts (via /etc/nsswitch.conf*). If not found, it will query a DNS server (default on most system*). In this regard, /etc/hosts can still override a lookup.

This appropriate if your server is also receiving outside email, or needs to use a DNS lookup for anything else not found in /etc/hosts. Please note that there is also a lmtp_host_lookup option.

* nsswitch.conf typical example:
hosts: files dns (Queries 'files' (/etc/hosts, normally) then the system DNS.)

Reference smtp_host_lookup , lmtp_host_lookup