Skip to main content

How to change default system email for root@hostname for Postfix in CentOS 7?

By default, any system email is generated by system is sent to root@hostname or system-user@hostname. So critical server errors, log errors, corn jobs alerts etc. all are sent to this default email address. Most likely you want to receive all the emails from different daemons and system components not to the user ‘root’ but in another user that has access to emails. To change it to different appropriate email id, we can do this by two ways. 
Step 1: To do this, open /etc/aliases with your favourite editor and navigate to the end of the file. You will find something like this:
# vi /etc/aliases
root:           postmaster@awsmonster.com
To add multiple email ids, we can simply separate them by comma.
root:           postmaster@awsmonster.com, system@awsmonster.com
Run the aliases command, to compile aliases file.
# newaliases
# service postfix restart
Step 2: We can simply create .forward file to the folder root and add email address there.
# vi /root/.forward
serveradmin@awsmonster.com
# service postfix restart
Step 3: Configure alias addresses (Optional): In the /etc/postfix/main.cf add the following part to define the mapping file: Defines a lookup tables that alias specific mail addresses or domains to other local or remote address.
virtual_alias_maps =  proxy:mysql:/etc/postfix/mysql-virtual_alias_default_maps.cf,
                      proxy:mysql:/etc/postfix/mysql-virtual_alias_maps.cf,
                      regexp:/etc/postfix/virtual_regexp
This file can now be filled with the aliases. These aliases can then be forwarded to another external email or to a local user. The format of the mapping file is the following:
# vi /etc/postfix/virtual_users
# alias-address                         destination user / email
friendlyname@example.com                user1
info@example.com                        user1
webmaster@example.com                   user2
bob@example.com                         user2

Every time you change this mapping file, the postmap command needs to be executed. Without executing this command the db file will remain unchanged and your changed configuration will not have any effect. After the mapping file is created execute the following command to create the lookup table database out of this file:
#postmap /etc/postfix/virtual_users
Step 3 is not recommended if you use MySQL/MariaDB database as backend.

Comments