Published on

Change your Relayhost on Postfix using Sed


a Quick post on how to change your relayhost on Postfix to a External SMTP Provider and aswell how to revert back the changes so the Relay server sends out mail directly.

Checking your current relayhost configuration:

We will assume your /etc/postfix/ has a relayhost entry of #relayhost =, in my example it will look like this:

#relayhost =

If not, you can just adjust your sed command accordingly.

Changing your relayhost configuration to a External SMTP Provider:

We will use sed to change the relayhost to for example:

$ sed -i 's/#relayhost\ =/relayhost\ =\ \[\]/g' /etc/postfix/

to verify that we have set the config, look at the config:

$ cat /etc/postfix/ | grep relayhost 
relayhost = []

Once you see the changes looks as expected, you can restart postfix:

$ /etc/init.d/postfix restart

Then you can tail the logs to see if the mail gets delivered:

$ tail -f /var/log/maillog

Revert your changes so that postfix sends out directly:

To revert your changes, let's change the config back to what it was:

$ sed -i 's/relayhost\ =\ \[\]/#relayhost\ =/g' /etc/postfix/

To verify your changes:

$ cat /etc/postfix/ | grep relayhost
#relayhost =

As you can see the relayhost is commented out, meaning that the relayhost property will not be active, go ahead and restart the service for the changes to take effect:

$ /etc/init.d/postfix restart

Same as before, look at the logs to confirm mailflow is as expected:

$ tail -f /var/log/maillog

Thank You

Thanks for reading, feel free to check out my website, feel free to subscribe to my newsletter or follow me at @ruanbekker on Twitter.