HowTo: Ubuntu virtual mail host
How to be an ISP
HowTo July 2nd, 2008
By Roger Howorth

This article explains how to set up a Ubuntu server as a virtual mail host. The resulting server could be used as a bastion mail server, which accepts email from anywhere on the Internet, provided the email is addressed to a domain hosted by your network.

A further HowTo explains how to convert this configuration so it could be used as an “internal” mail server, which accepts mail from any user that is logged into this server – they could be connected from anywhere on the Internet. The server would deliver email from any logged in users to any addresses on the Internet.

We assume you have created a virtual machine using the Ubuntu CD Installer. If not follow this link for a HowTo for creating a Ubuntu Server VM.

Make a “clone” copy of your VM. For the rest of this HowTo we will work only with the cloned copy, which we will call “upost”.

Step 1: Install the software
Boot your upost VM. As Ubuntu uses the udev subsystem for managing devices, the first thing we’ll need to do is to fix the network device, which will have been broken by making the cloned copy. We’ll also configure the correct hostname for the server. Do this using the following commands:

mv /etc/udev/rules.d/70-persistent-net.rules /tmp
echo upost > /etc/hostname
reboot

Next, we’ll ensure your Ubuntu system is up to date:

apt-get update
apt-get upgrade

Now we’ll install the MySQL database, which will be used to store information used to route mail to its destination, and also to store information about the users of the mail system. This command will also install the MySQL client software.


apt-get install mysql-server

During the installation you will be asked to provide a password for the root MySQL user. You should not use the same password here as you use for the root password of your Ubuntu Linux server.

Now, we’ll install the Postfix mail transfer agent, used to send and receive email. This command will also create a user account and a group called “postfix”.


apt-get install postfix

During the installation of Postfix you will be asked what type of mail server you want to install. Select the option for “Internet site”. You’ll then be asked for the fully qualified domain name of your server. You could use something like upost.mydomain.local.

Now install the libraries needed to link Postfix with MySQL:


apt-get install postfix-mysql

Optional
Now we will install “WebMin” a popular web based server administration package. Before running the wget command you might want to use your web browser to go to the SourceForge WebMin site and ensure you are downloading the latest stable release of WebMin.


wget http://prdownloads.sourceforge.net/webadmin/webmin_1.420_all.deb
dpkg -i webmin_1.420_all.deb

You will probably see some errors from dpkg. Fix them using the following command:


apt-get install -f

This command will take some time to finish, but it should complete without any errors. If you do see errors you have probably configured your VM with too little RAM. It’s probably best to increase the VM’s RAM allocation and start again!

Configure Postfix to use data stored in MySQL
We will store information about our users and domains in the MySQL database, but we need to configure Postfix to use MySQL before it can access this information.

Use your favourite text editor to create the following files:

Filename – /etc/postfix/mysql-virtual


#mysql-virtual
user = maildb
password = password
dbname = maildb
table = virtual
select_field = destination
where_field = email
hosts = unix:/var/run/mysqld/mysqld.sock

Configure the database
Now you need to create the email database and a few entries in the database before your mail server will do anything useful.

First download this script, which we’ll need in a moment. It might be easiest to right-click on the link and choose the “Save” option.

SQL setup script

Now, execute these SQL commands to configure your database:


mysqladmin -u root -p create mailsql
mysql -u root -p mailsql < genericmailsql.sql

..this creates a MySQL user


mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password) VALUES(‘%’,’username’,PASSWORD(‘password’));
mysql> flush privileges;

..this grants the appropriate database access privileges:


mysql> grant SELECT,INSERT,UPDATE,DELETE on mailsql.* to mailsql@localhost;
mysql> flush privileges;

For each of the above, you should get s a response back from MySQL that looks something like this:


Query OK, 0 rows affected (0.01 sec)

Now close the MySQL console:


mysql> quit

Finally verify that the your mailsql user can connect to the mysql server.


mysql -u mailsql -p mailsql

Now you can use WebMin to add some entries to the “virtual” table in your MySQL “mailsql” database.

Continue to part 2.

Leave a Response