User:SwifT/Wikified but not merged documents/Virtual mail HOWTO
Introduction
For most Gentoo users, a simple mail client and fetchmail will do. However, when hosting a domain a full blown MTA (Mail Transfer Agent) is needed. When hosting multiple domains, then something more robust is required to handle all of the email for the users. This system was designed to be an elegant solution to that problem.
A virtual mail system needs to be able to handle email for numerous domains with multiple users over a variety of interfaces. This presents some issues that must be addressed. For instance, what if two users on different domains want the same user name? If IMAP access and smtp-auth are being provided, how does one combine the various authentication daemons into a single system? How security provided for the numerous components that comprise the system? How will one manage it all?
This guide will provide instructions on how to set up with a mail system capable of handling mail for as many domains as the system hardware can handle, supports virtual mail users that do not require shell accounts, has domain specific user names, can authenticate web, IMAP, SMTP, and POP3 clients against a single database, utilizes SSL for transport layer security, has a web interface, can handle mailing lists for any domain on the machine, and is controlled by a nice, central and easy MYSQL database.
There are quite a variety of ways to go about setting up a virtual mailhosting system. With so may options, another may be the best choice for your specific needs. Consider investigating http://www.qmail.org/ and http://www.exim.org/ to explore possible options.
The following packages will be used in this setup: apache, courier-imap, courier-authlib postfix, mod_php, phpmyadmin, squirrelmail, cyrus-sasl, mysql, php, and mailman.
Make sure to turn on the following USE variables in /etc/portage/make.conf before compiling the packages: USE="mysql imap libwww maildir sasl ssl"
. Otherwise things will most likely have to be recompiled to get the needed support for all the protocols. Further, it is a good idea to turn off any other mail and network variables, like ipv6.
A domain name is required to run a public mail server, or at least an MX record for a domain. Ideally one would have control of at least two domains to take advantage of virtual domain functionality.
Make sure /etc/conf.d/hostname is set to the right hostname for the mail server. You can apply any changes you make to this file by running /etc/init.d/hostname restart . Verify the hostname is set correctly with the hostname command. Also verify that there are no conflicting entries in the /etc/hosts file.
It is recommended this entire document is read in order to gain familiarity with all the steps before attempting the install. If problems incur with any of the steps, check the troubleshooting steps near the end of this document. Also, not all the referenced packages are necessary (this set up is very flexible). For instance, if a web interface is not desired skip the squirrelmail section.
Postfix basics
Install postfix:
root #
emerge --ask mail-mta/postfix
Verify that no other MTA programs have been installed. Possible entries include ssmtp, exim, or netqmail. Installing them at the same time on the same operating system will surely cause BIG problems.
After postfix is installed, it can be configured. Change the following options in /etc/postfix/main.cf Remember to replace $variables
with appropriate names.
myhostname = $host.domain.name
mydomain = $domain.name
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain $mydomain
mynetworks = my.ip.net.work/24, 127.0.0.0/8
home_mailbox = .maildir/
local_destination_concurrency_limit = 2
default_destination_concurrency_limit = 10
Next change the following in /etc/postfix/master.cf. This will turn on verbose output for debugging:
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (50)
#
==========================================================================
## (Just add the "-v" after the smtpd in the following line)
smtp inet n - n - - smtpd -v
Next, edit /etc/mail/aliases to add local aliases. There should at least be an alias for root like: root: your@email.address
.
Starting postfix for the first time:
root #
/usr/bin/newaliases
This will install the new aliases. This step needs to be performed when aliases are updated or installed.
root #
/etc/init.d/postfix start
Now that postfix is running, fire up your favorite console mail client and send a test email. mutt can be used to send mail without leaving the terminal window. Verify that postfix is delivering mail to local users, once confirmed move on to the next step.
It is strongly recommended that this basic postfix setup is functioning before progressing to the next step of this guide.
Courier-imap
Install courier-imap and courier-authlib:
root #
emerge --ask net-mail/courier-imap net-libs/courier-authlib
Courier-imap configuration:
root #
cd /etc/courier-imap
To use the SSL capabilities of courier-imap or POP3 certs will need to be created for this purpose. This step is recommended. If SSL will not be used skip this step.
root #
nano -w pop3d.cnf
root #
nano -w imapd.cnf
- (Change the C, ST, L, CN, and email parameters to match your server.)
- NOTE: C is a 2 char country code.
root #
mkpop3dcert
root #
mkimapdcert
Start the courier services you need:
root #
/etc/init.d/courier-imapd start
root #
/etc/init.d/courier-imapd-ssl start
root #
/etc/init.d/courier-pop3d start
root #
/etc/init.d/courier-pop3d-ssl start
Start up a favorite mail client and verify that all started connections work for receiving and sending mail. Of course, you won't be able to log on to any of the services because authentication hasn't been configured yet, but it is wise to check if the connections themselves work or not.
Now that the basics work, we're going to do a whole bunch of stuff at once to get the rest of the system running. Again, please verify that what we've installed already works before progressing.
Cyrus-sasl
Next we're going to install cyrus-sasl. Sasl is going to play the role of actually passing your auth variables to courier-auth, which will in turn pass that information to mysql for authentication of smtp users. For this howto, we'll not even try to verify that sasl is working until mysql is set up and contains a test user. Which is fine since we'll be authenticating against mysql in the end anyway.
Configuring and installing the cyrus-sasl ebuild:
root #
emerge cyrus-sasl
Next, edit /etc/sasl2/smtpd.conf
mech_list: PLAIN LOGIN
pwcheck_method: saslauthd
}}
# nano -w /etc/conf.d/saslauthd
SASLAUTHD_OPTS="${SASLAUTH_MECH} -a rimap -r"
SASLAUTHD_OPTS="${SASLAUTHD_OPTS} -O localhost"
# /etc/init.d/saslauthd start
SSL Certs for Postfix and Apache
Next we're going to make a set of ssl certificates for postfix and apache.
root #
cd /etc/ssl/
## Change the following default values for your domain:
countryName_default
stateOrProvinceName_default
localityName_default
0.organizationName_default
commonName_default
emailAddress_default.
If the variables are not already present, just add them in a sensible place.
root #
cd misc
root #
./CA.pl -newreq-nodes
root #
./CA.pl -newca
root #
./CA.pl -sign
root #
cp newcert.pem /etc/postfix
root #
cp newkey.pem /etc/postfix
root #
cp demoCA/cacert.pem /etc/postfix
Now we do the same thing for apache:
root #
openssl req -new > new.cert.csr
root #
openssl rsa -in privkey.pem -out new.cert.key
root #
openssl x509 -in new.cert.csr -out new.cert.cert -req -signkey new.cert.key -days 365
Just leave the resulting certificates here for now. We'll install them after Apache is installed.
Adding SSL and SASL support to Postfix
Now edit the postfix config's to make it aware of the new sasl and ssl capabilities. Add the following parameters to the end of the file where they will be easy to find.
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_local_domain =
}}
# (The broken_sasl_auth_clients option and the login auth method are for outlook and outlook express only and are undocumented. Isn't having to hack software for stupid, broken, M$ BS great? smtpd_sasl_local_domain appends a domain name to clients using smtp-auth. Make sure it's blank or your user names will get mangled by postfix and be unable to auth.)
smtpd_recipient_restrictions =
permit_sasl_authenticated,
permit_mynetworks,
reject_unauth_destination
# (The next two options enable outgoing encryption.)
smtp_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_use_tls = yes
#smtpd_tls_auth_only = yes
smtpd_tls_key_file = /etc/postfix/newkey.pem
smtpd_tls_cert_file = /etc/postfix/newcert.pem
smtpd_tls_CAfile = /etc/postfix/cacert.pem
smtpd_tls_loglevel = 3
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom
# (smtpd_tls_auth_only is commented out to ease testing the system. You can turn this on later if you desire.)
# postfix reload
Now we're going to verify that the config's we added were picked up by postfix. For this we are going to use telnet
(provided by for instance net-misc/netkit-telnetd
) although you can also use nc
(provided by net-analyzer/netcat
):
# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.domain.com ESMTP Postfix
EHLO domain.com
250-mail.domain.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-XVERP
250 8BITMIME
^]
telnet> quit
Verify that the above AUTH and STARTTLS lines now appear in your postfix install. As I said before, as it stands now AUTH will not work. that's because sasl will try to auth against it's sasldb, instead of the shadow file for some unknown reason, which we have not set up. So we're going to just plow through and set up mysql to hold all of our auth and virtual domain information.
The vmail user
Before we set up our virtual mailhosting environment, we create a functional user under which the virtual mailboxes will be hosted. For clarity's sake we will call this vmail :
# useradd -d /home/vmail -s /bin/false -m vmail
So now you've set up the vmail account. You can create multiple accounts if you want (to keep some structure in your set of virtual mail accounts). The user id, group id and home dirs are referenced in the MySQL tables.
Next to the user account we also need to create the location where the mailboxes will reside:
# mkdir -p /home/vmail/virt-domain.com/foo
# chown -R vmail:vmail /home/vmail/virt-domain.com
# maildirmake /home/vmail/virt-domain.com/foo/.maildir
MySQL
Next we're going to install and configure MySQL. You'll need the genericmailsql.sql dumpfile for this step.
# emerge mysql
# /usr/bin/mysql_install_db
## (After this command runs follow the onscreen directions
for adding a root password with mysql, otherwise your db will
be wide open.)
# /etc/init.d/mysql start
# mysqladmin -u root -p create mailsql
# mysql -u root -p mailsql < genericmailsql.sql
# mysql -u root -p mysql
mysql> GRANT SELECT,INSERT,UPDATE,DELETE
-> ON mailsql.*
-> TO mailsql@localhost
-> IDENTIFIED BY '$password';
Query OK, 0 rows affected (0.02 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
## (Verify that the new mailsql user can connect to the mysql server.)
# mysql -u mailsql -p mailsql
Your new database has default values and tables set up for two domains. The following tables are included:
- alias - local email alias and mailman alias information.
- relocated - relocated user email address maps
- transport - default mail transport information for all domains you are hosting
- users - all user account information
- virtual - virtual domain email alias maps
id alias destination
1 root foo@bar.com
2 postmaster foo@bar.com
## (Line wrapped for clarity.)
id email clear name uid gid homedir \
maildir quota postfix
10 foo@virt-domain.com $password realname virtid virtid /home/vmail \
/home/vmail/virt-domain.com/foo/.maildir/ y
13 foo@bar.com $password realname localid localid /home/foo \
/home/foo/.maildir/ y
The values of the virtid
uid and gid should be those of the vmail
user and group.
id domain destination
1 bar.com local:
2 virt-domain.com virtual:
id email destination
3 root@virt-domain.com other@email.address
Apache and phpMyAdmin
Next we'll set up apache and add an interface to interact with the database more easily.
# emerge apache phpmyadmin
There are plenty of guides out there about how to set up apache with php, including guides provided by the Gentoo PHP Project . There are also numerous posts on http://forums.gentoo.org detailing how to solve problems with the installation. So, that said, we're not going to cover it here. Set up the apache and php installs, then continue with this howto. Now, a word for the wise: .htaccess the directory that you put phpmyadmin in. If you do not do this, search engine spiders will come along and index the page which in turn will mean that anyone will be able to find your phpmyadmin page via google and in turn be able to come change your database however they want which is BAD! There are many howtos on this including: http://www.csoft.net/docs/micro/htaccess.html.en .
Now we're going to install the Apache certificates we made previously. The Apache-SSL directives that you need to use the resulting cert are:
- SSLCertificateFile /path/to/certs/new.cert.cert
- SSLCertificateKeyFile /path/to/certs/new.cert.key
# cp /etc/ssl/misc/new.cert.cert /etc/apache2/ssl/
# cp /etc/ssl/misc/new.cert.key /etc/apache2/ssl/
# cd /etc/apache2/vhosts.d
## (Check if you have an ssl-vhost template already.
Copy that one instead of the default_vhost if that is the case)
# cp 00_default_vhost.conf ssl-vhost.conf
# nano -w ssl-vhost.conf
## (Change the following parameters)
NameVirtualHost host.domain.name:443
<VirtualHost host.domain.name:443>
ServerName host.domain.name
ServerAdmin your@email.address
DocumentRoot "/var/www/localhost/htdocs/phpmyadmin";
<Directory "/var/www/localhost/htdocs/phpmyadmin">
...
</Directory>
SSLCertificateFile /etc/apache2/ssl/new.cert.cert
SSLCertificateKeyFile /etc/apache2/ssl/new.cert.key
SSLEngine on
...
</VirtualHost>
# nano -w /etc/conf.d/apache2
## (Add -D SSL -D PHP5 to the APACHE2_OPTS)
# /etc/init.d/apache2 restart
Next, configure phpMyAdmin.
# cd /var/www/localhost/htdocs/phpmyadmin
# cp config.sample.inc.php config.inc.php
# nano -w config.inc.php
## (Change the following parameters.)
$cfg['blowfish_secret'] = 'someverysecretpassphraze';
$cfg['Servers'][$i]['host'] = 'localhost'; // MySQL hostname
$cfg['Servers'][$i]['controluser'] = 'mailsql'; // MySQL control user settings
// (this user must have read-only
$cfg['Servers'][$i]['controlpass'] = '$password'; // access to the "mysql/user"
// and "mysql/db" tables)
$cfg['Servers'][$i]['user'] = 'mailsql'; // MySQL user
$cfg['Servers'][$i]['password'] = '$password'; // MySQL password
Now enter the phpmyadmin page and browse the tables. You'll want to add in your local aliases, edit your user table to add a test user, and change your transport table to add information about your domains. The default values supplied with the dumpfile should be a sufficient guide to what values need to go where. Make sure that if you put information in the database that it is accurate. For instance, make sure the local user's home dir exists and that the correct uid/gid values are in place. The maildirs should be created automatically by postfix when the user receives their first email. So, in general, it's a good idea to send a "Welcome" mail to a new user after you setup their account to make sure the .maildir gets created.
Configuring MySQL Authentication and vhosts
Next we'll reconfigure our authentication to use the mailsql database in courier-imap and postfix. In all of the following examples, replace $password
with the password you set for the mailsql mysql user.
# nano -w /etc/courier/authlib/authdaemonrc
authmodulelist="authmysql authpam"
# nano -w /etc/courier/authlib/authmysqlrc
MYSQL_SERVER localhost
MYSQL_USERNAME mailsql
MYSQL_PASSWORD $password
MYSQL_DATABASE mailsql
MYSQL_USER_TABLE users
## (Make sure the following line is commented out since we're storing plaintext.)
#MYSQL_CRYPT_PWFIELD crypt
MYSQL_CLEAR_PWFIELD clear
MYSQL_UID_FIELD uid
MYSQL_GID_FIELD gid
MYSQL_LOGIN_FIELD email
MYSQL_HOME_FIELD homedir
MYSQL_NAME_FIELD name
MYSQL_MAILDIR_FIELD maildir
# /etc/init.d/courier-authlib restart
# /etc/init.d/saslauthd restart
We're almost there, I promise! Next, set up the rest of the necessary configs for postfix to interract with the database for all its other transport needs. Remember to replace each value with the name of your own user, user id, password, alias, email address, and so on.
# nano -w /etc/postfix/mysql-aliases.cf
# mysql-aliases.cf
user = mailsql
password = $password
dbname = mailsql
table = alias
select_field = destination
where_field = alias
hosts = unix:/var/run/mysqld/mysqld.sock
# nano -w /etc/postfix/mysql-relocated.cf
# mysql-relocated.cf
user = mailsql
password = $password
dbname = mailsql
table = relocated
select_field = destination
where_field = email
hosts = unix:/var/run/mysqld/mysqld.sock
# nano -w /etc/postfix/mysql-transport.cf
# mysql-transport.cf
user = mailsql
password = $password
dbname = mailsql
table = transport
select_field = destination
where_field = domain
hosts = unix:/var/run/mysqld/mysqld.sock
# nano -w /etc/postfix/mysql-virtual-gid.cf
# mysql-virtual-gid.cf
user = mailsql
password = $password
dbname = mailsql
table = users
select_field = gid
where_field = email
additional_conditions = and postfix = 'y'
hosts = unix:/var/run/mysqld/mysqld.sock
# nano -w /etc/postfix/mysql-virtual-maps.cf
# mysql-virtual-maps.cf
user = mailsql
password = $password
dbname = mailsql
table = users
select_field = maildir
where_field = email
additional_conditions = and postfix = 'y'
hosts = unix:/var/run/mysqld/mysqld.sock
# nano -w /etc/postfix/mysql-virtual-uid.cf
# mysql-virtual-uid.cf
user = mailsql
password = $password
dbname = mailsql
table = users
select_field = uid
where_field = email
additional_conditions = and postfix = 'y'
hosts = unix:/var/run/mysqld/mysqld.sock
# nano -w /etc/postfix/mysql-virtual.cf
# mysql-virtual.cf
user = mailsql
password = $password
dbname = mailsql
table = virtual
select_field = destination
where_field = email
hosts = unix:/var/run/mysqld/mysqld.sock
Lastly, edit /etc/postfix/main.cf one more time.
# nano -w /etc/postfix/main.cf
## (Ensure that there are no other alias_maps definitions)
alias_maps = mysql:/etc/postfix/mysql-aliases.cf
relocated_maps = mysql:/etc/postfix/mysql-relocated.cf
local_transport = local
local_recipient_maps = $alias_maps $virtual_mailbox_maps unix:passwd.byname
virtual_transport = virtual
## (The domains listed by the mydestination should not be listed in
the virtual_mailbox_domains parameter)
virtual_mailbox_domains = virt-domain.com, $other-virtual-domain.com
virtual_minimum_uid = 1000
## (Substitute $vmail-gid with the GID of the vmail group)
virtual_gid_maps = static:$vmail-gid
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-maps.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual.cf
## (Substitute $vmail-uid with the UID of the vmail user)
virtual_uid_maps = static:$vmail-uid
virtual_mailbox_base = /
#virtual_mailbox_limit =
For security reasons you should change the permissions of the various /etc/mail/mysql-*.cf :
# chmod 640 /etc/postfix/mysql-*.cf
# chgrp postfix /etc/postfix/mysql-*.cf
As of Postfix 2.0.x, there were a number of significant changes over the 1.1.x release. Notably the transport, virtual-gid, and virtual-uid tables are no longer necessary. The tables are still included if you wish to use them.
It is recommended that you read VIRTUAL_README included with the postfix docs for more information.
# postfix reload
Now, if all went well, you should have a functioning mailhost. Users should be able to authenticate against the sql database, using their full email address, for pop3, imap, and smtp. I would highly suggest that you verify that everything is working at this point. If you run into problems (with as many things as this setup has going on, it's likely that you will) check the troubleshooting section of this howto.
Squirrelmail
# emerge squirrelmail
## (Install squirrelmail to localhost so that it's accessed by http://localhost/mail)
(Substitute 1.4.3a-r2 with the version you use)
# webapp-config -I -h localhost -d /mail squirrelmail 1.4.3a-r2
# cd /var/www/localhost/htdocs/mail/config
# perl ./conf.pl
## (Change your Organization, Server, and Folder settings for squirrelmail.
Now you should be able to login to squirrelmail, again - with your full email address,
and use your new webmail setup.)
Mailman
Last step: mailman. The new version of mailman has very nice virtual domain support, which is why I use it, not to mention it's really a great package. To get this package installed and working correctly for virtual domains is going to require a bit of hacking. I really recommend reading all of the mailman documentation, including README.POSTFIX.gz, to understand what's being done here.
# emerge mailman
# nano -w /usr/local/mailman/Mailman/Defaults.py
## (Change the values below to reflect your primary domain, virtuals will be set next.)
DEFAULT_EMAIL_HOST = 'domain.com'
DEFAULT_URL_HOST = 'www.domain.com'
# nano -w /usr/local/mailman/Mailman/mm_cfg.py
MTA = "Postfix"
POSTFIX_STYLE_VIRTUAL_DOMAINS = ['virt-domain.com', 'virt.domain2.com']
add_virtualhost('www.virt.domain.com', 'virt.domain.com')
add_virtualhost('www.virt.domain2.com', 'virt.domain2.com')
## (This is required for your virtual domains for mailman to function.)
## (Once that's finished, add your first list.)
# su mailman
# cd ~
# ./bin/newlist --urlhost='www.virt-domain.com' --emailhost='virt-domain.com' test
Enter the email of the person running the list: your@email.address
Initial test password:
Hit enter to continue with test owner notification...
## (Virtual domain lists may also be specified with
list@domain.com style list names.)
# ./bin/genaliases
## (Now that your aliases have been generated,
verify that they were added successfully.)
# nano -w data/aliases
# STANZA START: test
# CREATED:
test: "|/usr/local/mailman/mail/mailman post test"
test-admin: "|/usr/local/mailman/mail/mailman admin test"
test-bounces: "|/usr/local/mailman/mail/mailman bounces test"
test-confirm: "|/usr/local/mailman/mail/mailman confirm test"
test-join: "|/usr/local/mailman/mail/mailman join test"
test-leave: "|/usr/local/mailman/mail/mailman leave test"
test-owner: "|/usr/local/mailman/mail/mailman owner test"
test-request: "|/usr/local/mailman/mail/mailman request test"
test-subscribe: "|/usr/local/mailman/mail/mailman subscribe test"
test-unsubscribe: "|/usr/local/mailman/mail/mailman unsubscribe test"
# STANZA END: test
## (Create the required mailman list)
# ./bin/newlist mailman
# ./bin/genaliases
## (Return to the root user)
# exit
# /etc/init.d/mailman start
# rc-update add mailman default
## (To start mailman at once and on every reboot.)
</pre>
# nano -w /etc/postfix/main.cf
owner_request_special = no
recipient_delimiter = +
## (Read README.POSTFIX.gz for details on this.)
alias_maps =
hash:/usr/local/mailman/data/aliases,
mysql:/etc/postfix/mysql-aliases.cf
virtual_alias_maps =
hash:/usr/local/mailman/data/virtual-mailman,
mysql:/etc/postfix/mysql-virtual.cf
## (This adds mailman alias file support to postfix
You may of course use the mysql tables for this,
but I hate doing that by hand. Also, if you are not
using virtual domains, adding the virtual alias maps
to postfix may cause problems, be warned.)
You should now be able to setup mailing lists for any domain on your box. Last note on this, make sure you run all mailman commands as the user mailman ( su mailman
) or else the permissions will be wrong and you'll have to fix them. Read the mailman doc's for more information on setting up and managing mailman lists.
Content Filtering and Anti-Virus
For content filtering and Anti-Virus, please consult our mail filtering gateway guide .
Wrap Up
Ok, you're all set, edit /etc/postfix/master.cf and turn off verbose mode for production use. You'll probably also want to add the services to your startup routine to make sure everything comes back up on a reboot. Make sure to add all the services you're using - apache, mysql, saslauthd, postfix, courier-imapd, courier-imapd-ssl, courier-pop3d, and courier-pop3d-ssl are all up to your decision on what access you want to provide. I generally have all the services enabled.
# postfix reload
# rc-update add $service default
Have fun!
Troubleshooting
Introduction
Troubleshooting: This is a short troubleshooting guide for the set up we've detailed how to install here. It is not exhaustive, but meant as a place to get you started in figuring out problems. With a complicated setup such as this, it's imperative that you narrow down the problem to the particular component that is malfunctioning. In general I do that by following a few steps. Start from the base of the system and work your way up, ruling out components that work along the way until you discover which component is having the problem.
Step 1: Check your config files
Typos are killers, especially when dealing with authentication systems. Scan your config's and mailsql database for typo's. You can debug all you want, but if you're not passing the right information back and forth to your mail system, it's not going to work. If you make a change to a config file for a service, make sure you restart that service so that the config change gets picked up.
# /etc/init.d/service restart
Step 2: Are all the necessary services actually running?
If it's not running, start it up. It's awful hard to debug a service that isn't running. Sometimes a service will act like it's started but still not function. Sometimes, when a bad config is used, or a bad transmission comes into a mail component, the service will hang and keep the port from being used by another process. Sometimes you can detect this with netstat. Or, if you've been at it awhile, just take a break and reboot your box in the meantime. That will clear out any hung services. Then you can come back fresh and try it again.
# /etc/init.d/$service status
# netstat -a | grep $service (or $port)
Step 3: Are all the service using the current config's?
If you've recently made a change to a config file, restart that service to make sure it's using the current version. Some of the components will dump their current config's to you, like postfix.
# apache2ctl fullstatus (needs lynx installed)
# apache2ctl configtest (checks config sanity)
# postconf -n (will tell you exactly what param's postfix is using)
# /etc/init.d/$service restart
Step 4: Check the logs
Repeat after me, logs are my friend. My next troubleshooting stop is always the logs. Sometimes it's helpful to try a failed operation again then check the logs so that the error message is right at the bottom (or top depending on your logger) instead of buried in there somewhere. See if there is any information in your log that can help you diagnose the problem, or at the very least, figure out which component is having the problem.
# kill -USR1 `ps -C metalog -o pid=`(to turn off metalog buffering)
# nano -w /var/log/mail/current
# cat /var/log/mysql/mysql.log
# tail /var/log/apache2/error_log
You may also find the debug_peer parameters in main.cf helpful. Setting these will increase log output over just verbose mode.
# nano -w /etc/postfix/main.cf
debug_peer_level = 5
debug_peer_list = $host.domain.name
## (Uncomment one of the suggested debugger
commands as well.)
Step 5: Talk to the service itself
SMTP, IMAP, and POP3 all respond to telnet sessions. As we've seen earlier when we verified postfix's config. Sometimes it's helpful to open a telnet session to the service itself and see what's happening.
# telnet localhost $port
## (SMTP is 25, IMAP is 143, POP3 is 110. You should receive at least an OK string,
letting you know that the service is running and ready to respond to requests.)
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK Courier-IMAP ready. Copyright 1998-2002 Double Precision, Inc.
Step 6: Sometimes only the big guns will give you the information you need: strace
You should have this installed anyway. This is an invaluable tool for debugging software. You can start commands from the command line with strace and watch all the system calls as they happen. It often dumps a huge amount of information, so you'll either need to watch it realtime as you retry a failed transaction with the mail system, or dump the output to a file for review.
# emerge strace
# strace $command
# strace -p `ps -C $service -o pid=`
Step 7: Research
Once you have the information, if you can diagnose and fix the problem, great! If not, you'll probably need to go digging on the net for information that will help you fix it. Here's a list of sites you can check to see if your error has already been resolved. There's also a really good howto on setting up smtp-auth which contains some great debugging ideas.
- http://forums.gentoo.org/ - Great forums for gentoo users
- http://bugs.gentoo.org/ - Bugs database for gentoo - great place to look for specific errors
- http://postfix.state-of-mind.de/ - smtp-auth howto
- http://marc.theaimsgroup.com/?l=postfix-users - Postfix mailing lists - searchable
- http://sourceforge.net/mailarchive/forum.php?forum_id=6705 - Courier-imap mailing list archives - not searchable
- http://www.google.com/ - If all else fails, there's always google, which has never failed me
- I also spend a lot of time on #gentoo . IRC is a great place to go for help.
Acknowledgements
We would like to thank the following authors and editors for their contributions to this guide:
- Ken Nowack
- Ezra Gorman
- Zack Gilburd
- Scygro
- Sven Vermeulen
- nightmorph