Solved

CentOS:  Setup email server and configure email forwarders

Posted on 2011-03-13
62
945 Views
Last Modified: 2013-11-08
Hi All,

I have a CentOS VPS and would like to setup email forwarding.


The server has YUM setup and i have root SSH access.
0
Comment
Question by:detox1978
  • 32
  • 28
  • +1
62 Comments
 
LVL 76

Expert Comment

by:arnold
Comment Utility
yum install postfix #mailserver
yum install webmin #web GUI based system administration tool.
Or Download from http://www.webmin.com/download.html the most current version

0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
You can configure one of sendmail or postfix.

Here are the instructions if you want to  configure sendmail.

http://www.wikihow.com/Configure-Sendmail

For mail forwarding, you can do it at both server or client level.

For server level mail forwarding after installing sendmail server, you would need to edit a few entries in the /etc/mail/virtusertable.  With entries in this table you can forward mail in the most flexible way.
For example, you can do this:

@abc.com             %1@def.com

The above would forward all emails of a domain to their counterpart on the other domain.  Or you can forward to another user.

For client level forwarding, you need to create a ".forward" file the the home directory of the forwarding user and write a comma separated list of emails where the user wishes to forward it.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I've installed webmin and postfix, but cant see the option for email forwarding.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Try editing /etc/postfix/virtusertable

And enter the following
username@somedomain.dom realuser@someotherdomain.dom

Depends upon your specifics
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
that file doesn't exist, but the folder does, should i create it?

Also, what is the syntax for a catch all?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
In your /etc/postfix/main.cf file, check the virtual_alias_maps

grep "virtual_alias_maps" /etc/postfix/main.cf


It should tell you the name of the file where you should put forwarding information.

For syntax, you should be able to do "man" on it.

Try the syntax I gave you above.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
It looks like there is no entry;


-bash-3.2# grep "virtual_alias_maps" /etc/postfix/main.cf
#   /etc/passwd, /etc/aliases, or the $virtual_alias_maps files.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
By above, I meant

@abc.com             %1@def.com


Try this:
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
0
 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
farzani, i couldn't find any virtual alias reference, but did find;

alias_maps = hash:/etc/aliases



insoftservice, I dont have access to the GUI that link requires, just SSH.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Is it something that you satisfies your requirements?

http://www.cyberciti.biz/tips/how-to-redirect-one-users-mail-to-another-user-with-postfix.html

Otherwise, just create virtual_alias_maps in your config file which should not be terribly hard.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
Can you give me a sample of what to write in  /etc/postfix/main.cf to setup virtual_alias_maps
0
 
LVL 31

Accepted Solution

by:
farzanj earned 500 total points
Comment Utility
Two lines go in /etc/postfix/main.cf

virtual_alias_domains = mydomain.com                           <---- Change this domain name for your own
virtual_alias_maps = hash:/etc/postfix/virtual

Then open /etc/postfix/virtual

And enter
@mydomain.com             %1@anotherdomain.com
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
Thanks,

what about directing single addresses?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Just write single addresses

First column is from:  second column is to
One to one
sender@gmail.com            receiver@yahoo.com

Many to one (all of mydomain)
@mydomain.com              admin@anotherdomain.com
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I've add the follow;

#  vi /etc/postfix/main.cf

virtual_alias_domains = mydomain.com
virtual_alias_maps = hash:/etc/postfix/virtual

#  vi /etc/postfix/virtual
@mydomain.com              myemail@yahoo.com


restarted the service and sent myself an email, but nothing has come through.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Can you generally email from hostA to hostB and receive it without a trouble?
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I've only just installed it.

How do i test?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Test 1:

Send mail using command line utilities.

echo "this is a test message" | mail -s "test1" email@targetdomain.com


Test 2:
A: Where you configured your server and want it to forward.
B: Where you want to receive it

On A: user --> is the user who receives email.  And forwarding address is email@targetdomain.com

Then
echo 'email@targetdomain.com' > ~user/.forward

Now send email to user@domain.com (On A)
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
no email arrived with test 1
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
So, your server is simply not working.  So we cannot blame forwarding yet

Now use email address with IP addresses NOT with domain name.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I want to forward to a yahoo account, so i dont know about Ip addresses.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Are you sending email from Yahoo also?

Can you do this successfully: nslookup mail.yahoo.com
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
i'm logging into the yahoo webmail and sending an email to test@mydomain.com, the forwarder should forward it to my other Yahoo account
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Do you have a registered domain name?  And, do you have a live IP address?  If you don't know what I am talking about then you don't have it.

In general it is a bad idea to test it this way.  You should have at least two computers in your local network or connected together NIC to NIC.  If you cannot do that, then make three email accounts from one computer to another and see if forwarding and mailing works.   It can be tested in one single machine.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
Yeah i have a domain that i am forwarding.  I've created an MX record mail.mydomain.com

0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
I don't think we are on the same page.

Can any one in the world do nslookup on your server and would get your IP? Is it a public IP address?  The IP address must be registered with some service provider.  You cannot do it yourself.

Just test within your home network, please.  
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
yes i can do an nslookup against my domain name.  I can also see the correct MX record via http://www.mxtoolbox.com/
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I run the SMTP test on http://www.mxtoolbox.com/ and it returned

No connection could be made because the target machine actively refused it myip:25
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
Also i can't telnet to port 25
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Author Comment

by:detox1978
Comment Utility
This is a VPS i hire in a data center, so port 25 will be open on the firewall.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Ok.  Open it.

Do you know the entire SMTP protocol so that you can use it to do an email by using telnet
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
yes i know how to send an email using telnet.

How do check if the port is open?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
telnet shows you can reach it or not.

netstat shows it is even advertised or not

so you can do:

netstat -antp | grep 25

So see if it is even announced.

If it is announced, it is not the fault of postfix.  If it is not announced then, there is something wrong with postfix
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
-bash-3.2# netstat -antp | grep 25
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      19073/master



Should the external IP address be in that list?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
So, the service is running at least
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
There is a  problem:  It is not getting announced on your IP address.  It is only getting advertised on loopback.  This is a problem.
You will have to modify your config file and restart it.  Then show me if the service advertises on correct IP
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
sounds like a good plan.

how do i edit the config file?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
in /etc/postfix/main.cf

Find:
inet_interfaces

It may be commented

So, set it to your IP address (of the server) the live IP.

inet_interfaces = <IP-ADDR>

service postfix restart

netstat -tulpn | grep master
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I can now telnet to port 25, but can't forward email;
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Do you receive emails now?
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
no nothing being forwarded.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
no. do you get it at least.  not asking about forwarding yet.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I can see the emails in the webmin queue.

However an SMTP test on http://www.mxtoolbox.com results in a timeout.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
It must be in the queue for sending it out on the machine that you used to send it out.  I think it is not sending/receiving.

You should always try simple things first.  A local system should be used to test one service at a time to avoid time wastage in trouble shooting.

0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Use a local system and use IP addresses, to avoid any problem with DNS.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
This is a VPS in a datacenter, so i only have access to the one server.  I think the email may be from the manual SSH tests we sent.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Make three account on the same server and try sending emails.

Email will be tested.  Forwarding will be surely tested.  First account the sendor, second the forwarder, and third the receiver.  
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I'm only interested in the forwarders.

If you could walk me through those steps.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I've just looked at the queue again and none of the emails i sent have been recieved.  they are all the ones i manually created on the server.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
I knew that.  You have way too may troubles on the server.  DNS not fully configured.  Firewall status unknown and who knows what else.

The easy way to test.

create three users
test1: useradd test1
test2: useradd test2
test3: useradd test3
passwd test1
passwd test2
passwd test3

TEST1:
In /etc/postfix/virtual
test2@localhost        test3@localhost

SAVE

su - test1
echo "test message" | mail -s "From 1 to 2" test2@localhost

exit
su - test3

mail

See if you received anything
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
The DNS is working fine.


I removed the catch all forwarder and the manual tests now work;

echo "this is a test message" | mail -s "test1" email@targetdomain.com


Also the SMTP check now works with at http://www.mxtoolbox.com/


So i must have made a typo.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Well, DNS don't have an MX record.  At least it did not show up for me.

Is forwarding working among users on the same machine?
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
I've not posted my domain, as i'm a novice and didn't want to advertise an easy target.  The MX records show up wheni query the domain on http://www.mxtoolbox.com/ and it passes there SMTP test.  But this was only after i took the forwarding off.

Are there any PHP script that can be used to manage forwarding?

0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
ok, I've had another go at setting up a single email forwarder (not a catch all).

I mssage was bounced with the following error;

Remote host said: 554 5.7.1 <test@mydomain.com>: Relay access denied [RCPT_TO]
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
Does this mean the server is unable to forward the message?
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
Yes, it should be enabled.
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
How do I check/enable?
0
 
LVL 2

Author Closing Comment

by:detox1978
Comment Utility
Got it working in the end.  not sure what i was doing wrong, but deleted all the changes and readded them and now it works.
0
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
I am glad it turned out good.

Did this kind of forwarding work too?
@mydomain.com             %1@anotherdomain.com
0
 
LVL 2

Author Comment

by:detox1978
Comment Utility
The bit i was missing is the virtual.db, which holds the forwarder information needs to be reloaded after a change is made to virtual file.

#cd /etc/postfix/
#postmap virtual
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now