Configuring Linux Mail
Configuring Linux Mail
Configuring Linux Mail
Chapter 21
===========================================
This chapter will help to show you how to set up a mail server for your home network. It covers
Sendmail which is responsible for relaying your mail to a remote user’s mailbox and also POP
mail which is used to retrieve the mail from the mail box to your local PC via a mail client such as
outlook Express.
Configuring Sendmail
Configuring DNS
Remember that you will never receive mail unless you have configured DNS for your
domain to make your new Linux box mail server the target of the DNS domain's MX record.
See either the Static DNS or Dynamic DNS pages on how to do this.
You can use the chkconfig command to get Sendmail configured to start at boot:
Remember to restart the sendmail process every time you make a change to the
configuration files for the changes to take effect on the running process. You can also test
whether the sendmail process is running with the pgrep command, you should get a
response of plain old process ID numbers:
You’ll then need to run the script each time you change any of the sendmail configuration
files described in the sections to follow.
The line in the script that restarts sendmail is only needed if you have made changes to the
/etc/mail/sendmail.mc file, but it has been included so that you don’t forget. This may not be
a good idea in a production system. Delete the appropriate "m4" line depending on your
version of RedHat.
Both the newaliases and m4 commands depend on the sendmail-cf RPM package. This
must be installed, if not, you'll get errors like this when running the script:
All Linux systems have a virtual loopback interface that only lives in memory with an IP
address 127.0.0.1. As mail must be sent to a target IP address even when there is no
NIC in the box, Sendmail therefore uses the loopback address to send mail to users on
the local box. To become a server, and not a client, Sendmail needs to be also
configured to listen for messages on the NIC interface.
We can verify that sendmail is running by first using the pgrep command which will return
the sendmail process ID number once sendmail is running. If it isn't running, then the
return value will be blank.
[root@bigboy tmp]# pgrep sendmail
22131
[root@bigboy tmp]#
Sendmail would assume the server's name was my-site and that the domain was all of
".com". The server would therefore be open to relay all mail from any ".com" domain and
would ignore the security features of the access and relay-domains files we'll describe
below.
If you fail to put the IP address of your NIC in the /etc/hosts file altogether, then you run
the risk of having all your mail appear to come from localhost.localdomain and not
bigboy.my-site.com.
As discussed above, a poorly configured /etc/hosts file can make mail sent from your
server to the outside world appear as if it came from users at localhost.localdomain and
not bigboy.my-site.com.
Use the sendmail program to send a sample email to someone in verbose mode. Enter
some text after issuing the command and end your message with a single "." all by itself
on the last line.
[root@bigboy tmp]# sendmail -v [email protected]
test text
test text
.
[email protected]... Connecting to mail.another-site.com.
via esmtp...
220 ltmail.another-site.com LiteMail v3.02(BFLITEMAIL4A); Sat,
05 Oct 2002 06:48:44 -0400
>>> EHLO localhost.localdomain
250-mx.another-site.com Hello [67.120.221.106], pleased to meet
you
250 HELP
>>> MAIL From:<[email protected]>
250 <[email protected]>... Sender Ok
>>> RCPT To:<[email protected]>
250 <[email protected]>... Recipient Ok
>>> DATA
354 Enter mail, end with "." on a line by itself
>>> .
250 Message accepted for delivery
[email protected]... Sent (Message accepted for delivery)
Closing connection to mail.another-site.com.
>>> QUIT
[root@bigboy tmp]#
Note: You may also get this error if you are using a SPAM prevention program, for
example a script based on the PERL module Mail::Audit. An error in the script could
cause this type of message too.
Another set of tell tale errors caused by the same problem can be generated when trying
to send mail to a user , in this example "root", or creating a new alias database file. (The
newalias command will be explained later):
my-super-duper-site.com
One disadvantage of this file is that it can only control mail based on the source domain.
The /etc/mail/access file has more capabilities, such as restricting relaying by IP address
or network range and is more commonly used. If you delete /etc/mail/relay-domains, then
relay access is fully determined by the /etc/mail/access file.
Sendmail has to be restarted after editing this file for the changes to take effect.
You'll then have to convert this text file into a Sendmail readable database file named
/etc/mail/access.db. Here are the commands to do that:
Remember that the relay security features of this file may not work if you don't have a
correctly configured /etc/hosts file.
my-site.com
my-other-site.com
In this case, remember to modify the MX record of the "my-other-site.com" DNS zonefile
point to my-site.com. Here is an example (Remember each "." is important):
This file has two columns too. It could be viewed as a mailing list file. The first column has
the mailing list name (sometimes called a virtual mailbox) and the second column has the
members of the mailing list separated by commas.
o If the mailing list member doesn't have an "@" in the name, then sendmail assumes
the recipient is on the local box.
o It will then search the first column of the aliases file to see if the recipient isn't on yet
another mailing list.
o If it doesn't find a duplicate, it assumes the recipient is a local user.
o If the recipient is a mailing list, then it goes through the process all over again to
determine each individual in the mailing list and when it is all finished, they will all get a
copy of the email message.
[email protected] webmasters
@my-other-site.com marc
[email protected] [email protected]
[email protected] paul
[email protected] paul
@my-site.com error:nouser User unknown
After editing this file you'll have to convert it into a sendmail readable database file named
/etc/mail/virtusertable.db. Here are the commands to do that:
[root@bigboy tmp]# cd /etc/mail
[root@bigboy mail]# make
Note: The default /etc/aliases file installed with RedHat has the last line of this sample
commented out with a "#", you may want to delete the comment and change user "marc" to
another user.
Notice that there are no spaces between the mailing list entries for “root”. This is important
as you will get errors if you add spaces.
After editing this file you'll have to convert it into a sendmail readable database file named
/etc/aliases.db. Here is the command to do that:
# My family
family: grandma,brother,sister
Mail sent to admin-list gets sent to all the users listed in the file /usr/home/admin/admin-list.
The advantage of using mailing list files is that the admin-list file can be a file that trusted
users can edit, user “root” is only needed update the aliases file. Despite this, there are
some problems with mail reflectors. One is that bounce messages from failed attempts to
broadcast goes to all users. Another is that all subscriptions and unsubscriptions have to be
done manually by the mailing list administrator. If either of these are a problem for you,
then consider using a mailing list manager like majordomo.
After editing this file, you'll have to convert it into a sendmail readable database file named
/etc/aliases.db. Here is the command to do that:
Configuring masquerading
In the DNS configuration, we made bigboy the mailserver for the domain my-site.com.
You now have to tell bigboy in the sendmail configuration file sendmail.mc that all
outgoing mail originating on bigboy should appear to be coming from my-site.com, if not,
based on our settings in the /etc/hosts file, it will appear to come from mail.my-site.com.
This isn't terrible, but you may not want your website site to be remembered with the
word "mail" in front of it. In other words you may want your mail server to handle all email
by assigning a consistent return address to all outgoing mail, no matter which server
originated the email.
This can be solved by editing your sendmail.mc configuration file and adding some
masquerading commands and directives. These are explained below:
The MASQUERADE_AS directive will make all mail originating on bigboy appear to
come from a server within the domain my-site.com by rewriting the email header.
The MASQUERADE_DOMAIN directive will make mail relayed via bigboy from all
machines in the my-other-site.com domain appear to come from the MASQUERADE_AS
domain of my-site.com.
Feature "allmasquerade" will make sendmail rewrite both recipient addresses and
sender addresses relative to the local machine. If you cc: yourself on an outgoing mail,
the other recipient will see a cc: to an address he knows instead of one on
localhost.localdomain.
The email header is what email clients, such as Outlook Express, say the "to:" and
"from:" should be. The "to:" and "from:" in the header is what is used when you use
Outlook Express to do a "reply" or "reply all". It is easy to fake the header, as Spammers
often do, it is detrimental to email delivery to fake the envelope.
The email envelope contains the "to:" and "from:" used by mailservers for protocol
negotiation. It is the envelope's "from:" which is used when email rejection messages are
sent between mail servers.
A fully functional and commented /etc/mail/sendmail.mc with masquerading statements
can be found in the Appendix.
Testing Masquerading
The best way of testing masquerading from the Linux command line is to use the "mail
-v username" command. I have noticed that "sendmail -v username" ignores
masquerading altogether.
Tail the /var/log/maillog file to verify that the masquerading is operating correctly
Check the envelope and header of test email received by test email accounts
EXPOSED_USER(`root')dnl
command in /etc/mail/sendmail.mc. You can comment this out if you like with a "dnl" at
the beginning of the line and recompiling / restarting sendmail
I have written a script called mail-filter.pl that effectively filters out SPAM email for my home
system. There are a few steps required to make the script work:
o Install PERL and the PERL modules listed above.
o Place an executable version of the script in your home directory and modify the script’s
$FILEPATH variable point to your home directory
o Update the two configuration files:
mail-filter.accept, which specifies the subjects and email addresses to accept,
mail-filter.reject that specifies those that you should reject.
o Update your “.forward” file and place an entry in /etc/smrsh
Mail-filter will first reject all email based on the “reject” file and will then accept all mail
found in the “accept” file. It will then deny everything else.
I have included a simple script with instructions on how to install the PERL modules in the
Appendix.
POP mail is started by xinetd. Therefore to get POP mail configured to start at boot you
have to use the chkconfig command to make sure xinetd starts up on booting.
Remember to restart the POP mail process every time you make a change to the
configuration files for the changes to take effect on the running process
# default: off
# description: The POP3 service allows remote users
# to access their mail \
# using an POP3 client such as Netscape Communicator, mutt, \
# or fetchmail.
service pop3
{
socket_type = stream
wait = no
user = root
server = /usr/sbin/ipop3d
log_on_success += HOST DURATION
log_on_failure += HOST
disable = no
}
You will then have to restart xinetd for these changes to take effect using the startup script
in the /etc/init.d directory.
Naturally, to disable POP Mail once again, you’ll have to edit the /etc/xinetd.d/ipop3 file, set
“disable” to “yes” and restart xinetd.
How To Configure Your Windows Mail Programs
All your POP email accounts are really only regular Linux user accounts in which Sendmail
has deposited mail. You can now configure your email client such as Outlook Express to
use your use your new POP / SMTP Mail Server quite easily. Here’s how:
POP Mail
Set your POP mail server to be the IP address of your Linux mail server. Use your Linux
user username and password when prompted.
SMTP
Set your SMTP mail server to be the IP address / domain name of your Linux mail server.
Make the user part of the email address is different. For example: [email protected] and
[email protected]. Create Linux accounts "john1" and "john2". If the
users insist on overlapping names then you may need to modify your virtusertable file.
Create the user accounts "john1" and "john2". Have a virtusertable entries for john@my-
site.com pointing to account "john1" and [email protected] pointing to account
"john2". The POP configuration in Outlook Express for each user should POP using "john1"
and "john2" respectively.