Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1.

yum install dovecot mariadb-server dovecot-mysql

MariaDB

In this section you will set up a MariaDB database to store virtual domains, users and passwords.
Dovecot and Postfix require this data.

Creating the Database and Tables


Follow the steps below to create the database tables for virtual users, domains and aliases:
1. Ensure the MariaDb server is running and enabled to start automatically on reboot:
sudo systemctl start mariadb
sudo systemctl enable mariadb

2. Use the mysql_secure_installation tool to configure additional security options. This tool
will ask if you want to set a new password for the MySQL root user, but you can skip that
step:
sudo mysql_secure_installation

Answer Y at the following prompts:


 Remove anonymous users?
 Disallow root login remotely?
 Remove test database and access to it?
 Reload privilege tables now?
3. Create a new database:
sudo mysqladmin -u root -p create mailserver

4. Log in to MySQL:
sudo mysql -u root -p

5. Create the MySQL user and grant the new user permissions over the database. Replace
mailuserpass with a secure password:
GRANT SELECT ON mailserver.* TO 'mailuser'@'127.0.0.1' IDENTIFIED BY
'mailuserpass';

6. Flush the MySQL privileges to apply the change:


FLUSH PRIVILEGES;

7. Switch to the new mailsever database:


USE mailserver;
8. Create a table for the domains that will receive mail on the Linode:
CREATE TABLE `virtual_domains` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

9. Create a table for all of the email addresses and passwords:


CREATE TABLE `virtual_users` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`password` varchar(106) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

10. Create a table for the email aliases:


CREATE TABLE `virtual_aliases` (
`id` int(11) NOT NULL auto_increment,
`domain_id` int(11) NOT NULL,
`source` varchar(100) NOT NULL,
`destination` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Adding Data
You can now add data to the database and tables that were created in the previous section.
1. Add the domains to the virtual_domains table. Replace the values for example.com
and hostname with your own settings:
INSERT INTO `mailserver`.`virtual_domains`
(`id` ,`name`)
VALUES
('1', 'example.com'),
('2', 'hostname.example.com'),
('3', 'hostname'),
('4', 'localhost.example.com');

Note
Note which id corresponds to which domain, the id value is necessary for the
next two steps.

2. Add email addresses to the virtual_users table. The domain_id value references the
virtual_domain table’s id value. Replace the email address values with the addresses
that you wish to configure on the mailserver. Replace the password values with strong
passwords.
INSERT INTO `mailserver`.`virtual_users`
(`id`, `domain_id`, `password` , `email`)
VALUES
('1', '1', ENCRYPT('password', CONCAT('$6$', SUBSTRING(SHA(RAND()), -
16))), '[email protected]'),
('2', '1', ENCRYPT('password', CONCAT('$6$', SUBSTRING(SHA(RAND()), -
16))), '[email protected]');

3. An email alias will forward all email from one email address to another. To set up an email
alias, add it to the virtual_aliases table:
INSERT INTO `mailserver`.`virtual_aliases`
(`id`, `domain_id`, `source`, `destination`)
VALUES
('1', '1', '[email protected]', '[email protected]');

Testing
In the previous section, data was added to the MySQL mailserver database. The steps below
will test that the data has been stored and can be retrieved.
1. Log in to MySQL:
sudo mysql -u root -p

2. Check the contents of the virtual_domains table:


SELECT * FROM mailserver.virtual_domains;

3. Verify that the output displays the domains you add to the virtual_domains table:

+----+-----------------------+
| id | name |
+----+-----------------------+
| 1 | example.com |
| 2 | hostname.example.com |
| 3 | hostname |
| 4 | localhost.example.com |
+----+-----------------------+
4 rows in set (0.00 sec)

4. Check the virtual_users table:


SELECT * FROM mailserver.virtual_users;

5. Verify that the output displays the email addresses you added to the virutal_users
table. Your hashed passwords will appear longer than they are displayed below:

+----+-----------+-------------------------------------
+--------------------+
| id | domain_id | password | email
|
+----+-----------+-------------------------------------
+--------------------+
| 1 | 1 | $6$574ef443973a5529c20616ab7c6828f7 |
[email protected] |
| 2 | 1 | $6$030fa94bcfc6554023a9aad90a8c9ca1 |
[email protected] |
+----+-----------+-------------------------------------
+--------------------+
2 rows in set (0.01 sec)

6. Check the virtual_aliases table:


SELECT * FROM mailserver.virtual_aliases;

7. Verify that the output displays the aliases you added to the virtual_aliases table:

+----+-----------+-------------------+--------------------+
| id | domain_id | source | destination |
+----+-----------+-------------------+--------------------+
| 1 | 1 | [email protected] | [email protected] |
+----+-----------+-------------------+--------------------+
1 row in set (0.00 sec)

8. If all the desired data displays as expected, exit MySQL:


exit

You might also like