Hướng dẫn cách cài đặt Mail Server với Postfix, Dovecot & Cyrus SASL

Nếu bạn đang muốn xây dựng một hệ thống Mail Server cho công ty của bạn thì bài viết này của xTraffic.pep.vn sẽ hướng dẫn bạn cách xây dựng hệ thống Mail Server tốt nhất với Postfix, Dovecot & Cyrus SASL trên Linux

Hướng dẫn cách cài đặt Mail Server với Postfix, Dovecot & Cyrus SASL - Cyrus SASL Dovecot Linux Mail Server Postfix - Webmasters Tools Phát triển website
Email Server

Thông tin về Postfix, Dovecot & Cyrus SASL

Postfix là gì? ( http://www.postfix.org/ )

Postfix là chương trình mã nguồn mở và miễn phí (free and open-source) dùng để gửi thư điện tử (Mail Transfer Agent – MTA) được tạo ra ban đầu tại IBM với mục tiêu là thay thế chương trình gửi mail phổ biến là Sendmail. Postfix được phát triển dựa trên mục tiêu là nhanh, dễ quản lý và bảo mật.

Dovecot là gì? ( http://dovecot.org/ )

Dovecot cũng là phần mềm miễn phí mã nguồn mở được dùng để nhận email bằng IMAP và POP3. Dovecot nhanh, dễ dàng cài đặt, dễ quản lý, bảo mật cao và sử dụng rất ít bộ nhớ. (Website : )

Cyrus SASL là gì? ( http://www.cyrusimap.org/ )

Cyrus SASL (Simple Authentication Security Layer) là một thư viện được dùng để xác thực giữa máy khách và máy chủ bằng nhiều phương pháp mã hoá.

Hướng dẫn cài đặt Mail Server trên Linux (CentOS)

Trong bài này mình sẽ lấy mail.pep.vn để làm ví dụ, các bạn hãy thay mail.pep.vn bằng tên miền của bạn trong lúc thực hiện nhé.

Đầu tiên, bạn cần mở file “/etc/sysconfig/network” và thêm vào dòng sau (nếu chưa có) :

HOSTNAME="pep.vn"

Bạn cần cài đặt các gói Repository cho YUM CentOS được hướng dẫn tại đây.

Bạn chạy lệnh sau để cài đặt Postfix bằng YUM :

sudo yum -y install postfix

Cài đặt thư viện Cyrus SASL bằng lệnh :

sudo yum -y install cyrus-sasl cyrus-sasl-devel cyrus-sasl-gssapi cyrus-sasl-md5 cyrus-sasl-plain

Bạn tạo file SSL certificates bằng cách nhập lần lượt từng lệnh sau (theo thứ tự từ trên xuống dưới, nhập đầy đủ các thông tin được yêu cầu khi thực hiện mỗi lệnh) :

mkdir /etc/postfix/ssl
cd /etc/postfix/ssl/
openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024
chmod 600 smtpd.key
openssl req -new -key smtpd.key -out smtpd.csr
openssl x509 -req -days 365 -in smtpd.csr -signkey smtpd.key -out smtpd.crt
openssl rsa -in smtpd.key -out smtpd.key.unencrypted
mv -f smtpd.key.unencrypted smtpd.key
openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 365

Bạn mở file “/etc/postfix/main.cf” rồi thêm dấu # vào trước các dòng sau :

#inet_interfaces = localhost #line 116
#mydestination = $myhostname, localhost.$mydomain, localhost #--> line 164

Thêm vào cuối file “/etc/postfix/main.cf” các dòng sau :

myhostname = mail.pep.vn
mydomain = pep.vn
myorigin = $mydomain
home_mailbox = mail/
mynetworks = 127.0.0.0/8
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = cyrus
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_authenticated_header = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtpd_tls_auth_only = no
smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key
smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt
smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom

Bạn mở file “/etc/postfix/master.cf” và thêm vào các dòng sau (ngay phía dưới smtp inet n – n – – smtpd ) :

smtps     inet  n       -       n       -       -       smtpd
	-o smtpd_sasl_auth_enable=yes
	-o smtpd_reject_unlisted_sender=yes
	-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
	-o broken_sasl_auth_clients=yes

Khởi động postfix & saslauthd :

sudo service postfix restart;
sudo service saslauthd restart;
sudo chkconfig --level 235 postfix on;
sudo chkconfig --level 235 saslauthd on;

Bạn chạy lệnh sau để kiểm tra Postfix :

telnet localhost 25

Ví dụ :

[root@ramnode-xtraffic ~]# telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.pep.vn ESMTP Postfix
ehlo localhost ###

Nếu bạn thấy hiển thị tương tự như trên thì có nghĩa là Postfix đã hoạt động tốt.

Bạn cài đặt Dovecot bằng lệnh sau :

sudo yum -y install dovecot

Bạn mở file “/etc/dovecot/dovecot.conf” và thêm các dòng sau vào cuối file :

protocols = imap pop3
mail_location = maildir:~/mail
pop3_uidl_format = %08Xu%08Xv

Khởi động Dovecot :

sudo service dovecot restart;
sudo chkconfig --level 235 dovecot on;

Kiểm tra Dovecot bằng lệnh sau :

telnet localhost 110

Ví dụ :

[root@ramnode-xtraffic ~]# telnet localhost 110
Trying ::1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
quit ###

Nếu bạn thấy tương tự như ví dụ thì Dovecot đã hoạt động tốt.

Vậy là xong, bây giờ bạn đã có hệ thống Mail Server để sử dụng cho công ty của bạn. Bạn có thể dùng các chương trình email client để gửi và nhận email như :

Leave a Comment