Installing and Configuring SMTP Server on Ubuntu
Objective: How To Install and Configure SMTP Server This tutorial will guide you through the installation and configuration of an SMTP (Simple Mail Transfer Protocol) server on Ubuntu using Postfix. We will cover installation, configuration, security measures, and testing.
Introduction to SMTP
SMTP is a protocol used to send email messages between servers. It is the most common protocol for sending emails across the Internet. An SMTP server processes outgoing messages and routes them to the recipient’s mail server, making it essential for email delivery.
Prerequisites
Before proceeding, ensure you have:
- An Ubuntu server (20.04 or later).
- Sudo access to install packages.
- A registered domain name and access to DNS settings (for MX records).
- Basic knowledge of command-line operations.
Installing Postfix
Start by updating your package list:
sudo apt update
sudo apt upgrade -y
Now, install Postfix:
sudo apt install postfix mailutils -y
During installation, you will be prompted to configure Postfix. Choose “Internet Site” and enter your domain name when prompted (e.g., mail.yourdomain.com).
Configuring Postfix – How To Install and Configure SMTP Server
Basic Configuration
Edit the main configuration file:
sudo nano /etc/postfix/main.cf
Update the following parameters:
# Basic Settings
myhostname = mail.yourdomain.com
mydomain = yourdomain.com
myorigin = /etc/mailname
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
relayhost =
mynetworks = 127.0.0.0/8
inet_interfaces = all
inet_protocols = all
# Mailbox Settings
home_mailbox = Maildir/
These settings define the hostname, domain, accepted mail destinations, and the mailbox format.
Setting Up Domains
To allow your server to send and receive emails for multiple domains, add the additional domains to mydestination. For example:
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain, additionaldomain.com
Setting Up Virtual Users – How To Install and Configure SMTP Server
To manage mail for virtual users, you’ll need to set up Postfix with a database for user authentication. Install the required packages:
sudo apt install postfix-mysql
Create a MySQL database for virtual users:
sudo mysql -u root -p
CREATE DATABASE mailserver;
CREATE USER 'mailuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON mailserver.* TO 'mailuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Create tables for users and domains:
USE mailserver;
CREATE TABLE virtual_domains (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE virtual_users (
id INT NOT NULL AUTO_INCREMENT,
domain_id INT NOT NULL,
password VARCHAR(106) NOT NULL,
email VARCHAR(100) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
);
Populate the domains and users:
INSERT INTO virtual_domains (name) VALUES ('yourdomain.com');
INSERT INTO virtual_users (domain_id, password, email) VALUES (1, ENCRYPT('userpassword'), '[email protected]');
Now, configure Postfix to use MySQL for virtual domains and users. Open main.cf again:
sudo nano /etc/postfix/main.cf
Add the following lines:
# MySQL Configuration for Virtual Domains
virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-domains.cf
virtual_mailbox_base = /var/mail/vhosts
virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox.cf
virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias.cf
Create the MySQL configuration files for domains, users, and aliases in /etc/postfix/.
Examples: /etc/postfix/mysql-virtual-domains.cf
user = mailuser
password = yourpassword
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_domains WHERE name='%s'
Example: /etc/postfix/mysql-virtual-mailbox.cf
user = mailuser
password = yourpassword
hosts = 127.0.0.1
dbname = mailserver
query = SELECT 1 FROM virtual_users WHERE email='%s'
Example: /etc/postfix/mysql-virtual-alias.cf
user = mailuser
password = yourpassword
hosts = 127.0.0.1
dbname = mailserver
query = SELECT email FROM virtual_users WHERE email='%s'
Securing Postfix
Enabling TLS
To secure email transmission, enable TLS in Postfix. Generate a self-signed certificate:
sudo mkdir /etc/ssl/private
sudo openssl req -new -x509 -days 365 -nodes -out /etc/ssl/certs/postfix.crt -keyout /etc/ssl/private/postfix.key
Edit main.cf to add TLS settings:
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/ssl/certs/postfix.crt
smtpd_tls_key_file = /etc/ssl/private/postfix.key
Configuring SASL Authentication
For secure authentication, configure SASL:
sudo apt install libsasl2-modules sasl2-bin
Edit /etc/default/saslauthd:
START=yes
MECHANISMS="pam"
Now, edit /etc/postfix/main.cf to enable SASL:
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain =
smtpd_sasl_security_options = noanonymous
Restart services:
sudo systemctl restart saslauthd
sudo systemctl restart postfix
Testing the SMTP Server – How To Install and Configure SMTP Server
Sending Email via Command Line
You can test sending emails using the command line:
echo "Test email body" | mail -s "Test Subject" [email protected]
Check if the email is delivered successfully.
Using Telnet
You can also test your SMTP server using Telnet:
- Install Telnet if not already installed:
sudo apt install telnet - Connect to your SMTP server:
telnet mail.yourdomain.com 25 - Enter the following commands to simulate sending an email:
HELO yourdomain.com MAIL FROM: <[email protected]> RCPT TO: <[email protected]> DATA Subject: Test Email This is a test email. . QUIT
Check your email client for the received email.
Monitoring and Troubleshooting – How To Install and Configure SMTP Server
- Logs: Check the Postfix logs for errors:
sudo tail -f /var/log/mail.log - Check Mail Queue: If emails are not being sent, check the mail queue:
mailq - Testing with
swaks: Installswaks(a Swiss Army Knife for SMTP) for advanced testing:sudo apt install swaksUse it for testing SMTP:
swaks --to [email protected] --from [email protected] --server mail.yourdomain.com
FAQs
Q1: Why is my email not being delivered?
- Check the mail logs for any errors. Ensure that your DNS settings, particularly MX records, are correct.
Q2: How can I enable user authentication for outgoing emails?
- Ensure you have configured SASL authentication correctly in Postfix and Dovecot.
Q3: What if my emails are going to the spam folder?
- Check your SPF, DKIM, and DMARC records. Ensure your server’s IP address is not black listed.
Q4: How can I manage multiple domains?
- Update the
mydestinationparameter inmain.cfto include additional domains, and configure virtual domains and users in your MySQL database.
Q5: How do I secure my email server further?
- Regularly update your software, use strong passwords, configure firewalls, and consider using fail2ban to block repeated login attempts.
By following this tutorial, you should have a fully functional and secure SMTP server on your Ubuntu system. If you have any questions or need further assistance, feel free to ask!

