Instructor: Hey, beautiful people.
in this lecture,
we will learn about mail servers in Linux.
So what is a mail server and why do we need it?
A mail server is a computer system
that sends and receive emails.
It functions like a digital post office,
ensuring messages reach the correct recipients.
The purpose of mail servers is to handle the storage,
processing, and delivery of emails
between users and domains.
CentOS offers various mail servers,
each catering to different needs.
For example, Postfix is a popular and secure MTA,
known for its simplicity,
and is the preferred mail server for CentOS v8,
or Red Hat, and above.
The next one is Sendmail,
which is one of the oldest MTAs.
It is used in CentOS v7 or earlier,
but is deprecated in newer versions of CentOS.
Sendmail is highly configurable, but a bit complex.
After that, Linux has Exim Mail Server.
It is flexible and highly configurable.
Then qmail, which is secure, reliable,
and known for its performance.
Then we have OpenSMTPD,
which is a lightweight and easy to configure.
Dovecot is another popular IMAP
and POP3 server known for its performance and security.
Courier provides IMAP and POP3 services.
Then Zimbra is a full feature collaboration suite,
though not available directly from CentOS repository.
Additionally, SpamAssassin filters out spam emails,
Amavis interfaces between MTAs and content checkers,
and ClamAV detects various malware threats.
For our purposes, we will use only Postfix
due to its strong security features,
ease of configuration, excellent performance,
and active community support.
So making it reliable choice
for various email handling needs.
Okay, so let's learn how to install
and configure Postfix mail server
on our CentOS machine.
Postfix is a program that runs on your Linux system
to send and receive emails,
but primarily it is used to send emails.
When you have a program or a workflow
running on your system
and you want to be notified if the process
or the entire workflow finishes or not
or successfully done,
you need some kind of mailing program, right,
or some kind of notification that will tell you,
"Hey, the program is done."
So the most readily used program for this
on Linux platform is Postfix.
So the configuration files for Postfix
are located in the /etc/postfix/ directory,
with the main configuration file being /etc/postfix/main.cf,
for shortest configuration.
You configure everything in this file.
Once you save the configuration,
you can restart the Postfix service to apply those changes.
Many people go straight
to /etc/postfix/main.cf file,
which is fine,
it's nothing that will break anything.
But it is standard and practical
to always use main configuration file.
The service that you need to start or restart
is managed with the systemctl command,
and that is systemctl restart, stop, or start,
and then the name of the service, which is Postfix.
Postfix is one of those program
that does not have a "d" at the end,
but it still runs as a daemon in the background.
So like chrony, which is a NTP service,
has chronyd at the end,
just like that Postfix does not have that "d",
that's what I'm trying to clarify.
Now, to send an email from your server,
you would use a mail command with -s option,
followed by the subject line,
and the email address you are sending it to.
Once you send it and hit Enter on your keyboard,
it will bring you into an interactive mode
similar to like a Vim or Vi,
where you will type your message.
When you are done typing,
you simply press Control + D to send the email.
To explain that further,
we will go into our Linux machine
and look at some of the files and commands
related to Postfix.
So here is my server,
and I'm already logged in root.
How do I verify it?
We will just run it, what command?
whoami, yes, which is a command
that tells you which user is logged in.
You could also run the id command,
which will provide you the same information.
So the configuration file for Postfix is,
as I said, main.cf.
But of course, before we get into this file,
the first thing we need to do
is to find out whether we have a Postfix program itself
installed in our system,
and how do we need to find that out?
Well, there's a command that you'll run,
which is rpm -qa,
and then you grep for that program that you're looking for,
and in our case, it's Postfix.
So rpm -qa will list every single program
or repository or package
that you have installed in your system.
So I ran it, and I see it's not there, right?
So the next thing we'll do is to install it.
To install this package,
you will run a dnf command,
space install, space the program or package name,
which is Postfix.
And of course, remember that this is going to go
outside on the internet
where all the CentOS has repositories are located,
and it will get the package from there.
Now, if you do not have internet access
on your Linux machine,
it's actually gonna fail, and it will read,
"Cannot reach the internet,"
or "Cannot reach the repository mirror."
So once I run: dnf install postfix command,
it tells me that it found one package,
and it shows its information and the size.
Now it asks, "Do you want to install it?"
Yes, of course, I want to install it.
I will click Yes,
and it will start installing it and will do its magic.
So it will go through the process of downloading,
installing, verifying, and cleaning up.
Okay, the installation of Postfix is completed successfully.
All right, sweet.
So once you have the package installed,
it means it already creates your directories
for where it belong.
It already creates a configuration file for you.
So the first thing we have to do
is go to configuration file
to define its configuration parameters.
There is another package that you need to install,
and that is s- or -nail,
which stands for Simple Mail.
The question is here,
why do we need this s-nail package
while or when we have the Postfix already?
Well, good question,
but the answer to that is Postfix
handles the delivery of emails on the server,
while s-nail provides the mail command
that you use to write and send emails from the terminal.
So together, they let you easily send emails
from the command line,
ensures those emails are properly delivered.
It's just like you have a mail office, post office now,
now you need a mailman to deliver the mail, right?
So in our case, that mailman is the s-nail package.
Okay, let's install it
by typing command: dnf install s-nail.
You click Yes to install it.
All right, it's completed, awesome.
So the configuration files for Postfix
are all located in /etc/postfix.
Right here, the third one from the bottom is main.cf.
This is where we define our mail relay server.
Now, what is the mail relay server?
The mail relay server is a server
that set up in the company environment
that accepts mails or emails from any server
and then delivers them.
It acts as a middleman, receiving the message on one side
and delivering it on the other side.
It can be either a Windows server or a Linux server,
depending on your environment it,
but it functions as a mail relay server.
All right, let's go into the main.cf file.
Okay, here you can see that most of the lines start
with a hashtag or pound sign,
which means that is just a comment,
so the comments can be ignored.
We have to find the line where it says "#relayhost."
Here you can find these lines.
The relayhost parameter specifies
our SMTP server or relay server
where we are going to send our emails,
and it will accept them.
These lines are initially commented
with the hashtag or pound sign,
meaning they are inactive.
So what you'll need to do is to uncomment these lines
by removing the the # sign,
and then add the appropriate relay host information.
Now, for example, you can use $ my domain
to set the relay host to the local domain of the server.
To specify a fully qualified domain name, or FQDN,
use gateway.my.domain
or mailserver.isp.tld,
like mail.yourdomain.com.
If you prefer to use an IP address,
you can set it as an IP address in brackets,
for example, [192.168.1.100],
and then close the bracket.
For a uucphost,
you can simply specify the host name,
such as my uucphost.
Okay, once that is added,
the next thing you have to do
is to start or restart the service
with: systemctl restart postfix command.
Okay, the Postfix service has been restarted successfully.
Nice, let's check it out.
All right, it says it's up and running.
You could also check the status of Postfix
by running the command, simple command,
which is: ps -ef,
and then you could just simply grep for Postfix.
Here, the first line shows the master process,
which is the main process responsible
for managing Postfix and accepting connections.
You could terminate it by typing the command kill,
followed by the process ID,
in our case, its process ID is 4135.
Now, you can send emails,
and the command to send email is: mail -s.
The subject line is "mail setup,"
and I wanted to send it to myself.
So I will put in my email address,
which is ia@utcisolutions.com,
and hit Enter.
After hitting Enter,
you can type the body of the email,
whatever text you want to type to include.
For example, like, "Hi," "Hello," "Goodbye."
Once you do that, then simply type Control + D.
When Control + D is typed, it prompts you
to confirm sending an email to the recipient,
in this case is ia@utcisolutions.com,
with the subject mail setup.
You need to respond with Yes
to send the message, and of course, No,
to cancel or press Enter to edit the message.
All right, let's say, let's type a Y and hit Enter.
Now, you will exit the mail program,
and that's how you send and configure your mail.
Mail ,once again, is one of the critical services
that you often have to configure on your system.
So familiarize yourself with the mail files,
the configuration, how to start and stop the mail service,
how to monitor it, and how to send emails.
You being a system administrator,
you have to, as a your primary responsibility,
to manage the mail server
because if you are managing a lot of Linux machines,
you need to be notified,
and the only way to be notified is either you have
some kind of monitoring solution installed on your computer,
which will notify you,
or you could rely on the local services
of the Linux machine.
And one of the local services
is this mail service Postfix service
that we all went through.
All right, I'll see you in the next lecture.