Khi cấu hình dovecot trên Centos 7 để xác định vì trị thư mục Maildir thì ta mở tập tin nào dưới đây

Trong hướng dẫn này, bạn sẽ học cách thiết lập một Mail Server /taxonomy/term/41với Postfix, Dovecot và MariaDB (một bản thay thế cho MySQL) trên CentOS 7. Chúng tôi sẽ giải thích cách tạo và gửi các hộp thư người dùng mới. hoặc nhận email đến và cấu hình domain.

Bước 1 - Cấu hình DNS

Cập nhật DNS và bắt đầu gửi thư đến server, hãy chỉnh sửa bản ghi MX của miền để nó trỏ đến miền hoặc địa chỉ IP của bạn, tương tự như ví dụ bên dưới:

example.com A 10 12.34.56.78 example.com MX 10 example.com mail.example.com MX 10 example.com

Bước 2 - Cập nhập file hosts

Xác minh rằng file hosts chứa một dòng cho địa chỉ IP của domain và được liên kết với Fully Qualified Domain Name (FQDN). Ví dụ: 192.0.2.0 là địa chỉ IP, hostname là local và hostname.example.com là FQDN.

File: /etc/hosts

127.0.0.1 localhost.localdomain localhost 192.0.2.0 hostname.example.com hostname

Bước 3 - Cài đặt SSL

Bạn sẽ cần cài đặt chứng chỉ SSL trên mail server của mình trước khi hoàn tất các bước cấu hình Dovecot. Chứng chỉ SSL sẽ xác thực danh tính của mail server cho người dùng và mã hóa dữ liệu được truyền giữa ứng dụng thư khách của người dùng và mail server.

3.1. Bật EPEL repository:

sudo yum install epel-release sudo yum update

3.2. Cài đặt Certbot và các packages

sudo yum install python2-certbot-nginx nginx sudo certbot --nginx

3.3. Chạy lệnh và nhập thông tin cần thiết để nhận certificate

# sudo certbot --nginx Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator apache, Installer apache Which names would you like to activate HTTPS for? ------------------------------------------------------------------------------- 1: example.com 2: www.example.com ------------------------------------------------------------------------------- Select the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shown (Enter 'c' to cancel):

Chú ý: Nên chọn option  tự động redirect HTTP qua HTTPs

Sau khi cài đặt xong bạn sẽ nhận được certificates tại /etc/letsencrypt/live/$domain ($domain là tên miền của bạn)

Chú ý: Certbot khuyên bạn nên trỏ cấu hình máy chủ web (web server ) của bạn đến thư mục chứng chỉ mặc định hoặc tạo liên kết tượng trưng(symlinks). Các key và certificate không được chuyển sang một thư mục khác.

3.4. Cầu hình Firewall 

Firewall để cho phép các kết nối đến và đi đến với HTTPS. Trên CentOS, firewalld là công cụ mặc định để quản lý các quy tắc tường lửa. Định cấu hình firewalld cho lưu lượng HTTP và HTTPS như sau:

sudo firewall-cmd --zone=public --permanent --add-service=http sudo firewall-cmd --zone=public --permanent --add-service=https sudo firewall-cmd --reload

Bước 4 - Cài đặt các Package

#cập nhập yum update #cài đặt các packages yum install postfix dovecot mariadb-server dovecot-mysql

Thao tác này sẽ cài đặt mail server Postfix, cơ sở dữ liệu MariaDB, IMAP và POP -Dovecot  và một số gói hỗ trợ cung cấp các dịch vụ liên quan đến xác thực.

Sau khi cài đặt bạn sẽ nhận:

  • Postfix 2.10.1
  • Dovecot 2.2.10
  • MariaDB 5.5.60

Bước 5 - Cấu hình MariaDB

5.1. Tạo Database và Tables

1. Khởi động và kích hoạt MariaDB

sudo systemctl start mariadb sudo systemctl enable mariadb

2. Sử dụng mysql_secure_installation để cấu hình MariaDB

sudo mysql_secure_installation

Trả lời Y cho các câu hỏi:

  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?

3. Tạo một database

sudo mysqladmin -u root -p create mailserver

4. Đăng nhập MySQL

sudo mysql -u root -p

5. Tạo user cho MySQL và phân quyền cho user

GRANT SELECT ON mailserver.* TO 'mailuser'@'127.0.0.1' IDENTIFIED BY 'mailuserpass';

Thay đổi mailuserpass bằng mật khẩu của bạn

6. Áp dụng thay đổi MySQL

FLUSH PRIVILEGES;

7. Chuyển đến database mailserver

USE mailserver;

8. Tạo table cho domain nhận email

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. Tạo table cho các địa chỉ email và password

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. Tạo table cho  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;

5.2. Thêm dữ liệu vào database

1. Thêm domain vào virtual_domains 

INSERT INTO `mailserver`.`virtual_domains` (`id` ,`name`) VALUES ('1', 'example.com'), ('2', 'hostname.example.com'), ('3', 'hostname'), ('4', 'localhost.example.com');

Chú ý : Lưu ý id nào tương ứng với miền nào sẽ được sử dụng cho hai bước tiếp theo.

2. Thêm địa chỉ email vào bảng virtual_users. Giá trị domain_id tham chiếu đến giá trị id của bảng virtual_domain. Thay thế các giá trị địa chỉ email bằng các địa chỉ mà bạn muốn cấu hình trên mail server. Thay thế các giá trị mật khẩu bằng mật khẩu của bạn.

INSERT INTO `mailserver`.`virtual_users` (`id`, `domain_id`, `password` , `email`) VALUES ('1', '1', ENCRYPT('password', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), ''), ('2', '1', ENCRYPT('password', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), '');

3. Email alias  sẽ chuyển tiếp tất cả email từ địa chỉ email này sang địa chỉ email khác. Để thiết lập email alias , hãy thêm nó vào bảng virtual_aliases:

INSERT INTO `mailserver`.`virtual_aliases` (`id`, `domain_id`, `source`, `destination`) VALUES ('1', '1', '', '');

5.3. Kiểm tra 

1. Vào lại MySQL

sudo mysql -u root -p

2.  Kiểm tra virtual_domains 

SELECT * FROM mailserver.virtual_domains;

Kết quả:

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

3. Kiểm tra virtual_users 

SELECT * FROM mailserver.virtual_users;

Kết quả

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

4. Kiểm tra virtual_aliases 

SELECT * FROM mailserver.virtual_aliases;

Kết quả

+----+-----------+-------------------+--------------------+ | id | domain_id | source | destination | +----+-----------+-------------------+--------------------+ | 1 | 1 | | | +----+-----------+-------------------+--------------------+ 1 row in set (0.00 sec)

Dùng exit để thoát.

Bước 6 - Cấu hình Postfix

Postfix là một Mail Transfer Agent (MTA), nó chuyển tiếp thư giữa mail server và internet

6.1. Cấu hình file main.cf

File main.cf là tệp cấu hình chính được Postfix sử dụng.

1. Lưu lại file mặc định của postfix

sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.orig

2. Thay đổi main.cf như dưới

File: /etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version # Debian specific: Specifying a file name will cause the first # line of that file to be used as the name. The Debian default # is /etc/mailname. #myorigin = /etc/mailname smtpd_banner = $myhostname ESMTP $mail_name (CentOS) biff = no # appending .domain is the MUA's job. append_dot_mydomain = no # Uncomment the next line to generate "delayed mail" warnings #delay_warning_time = 4h readme_directory = no # TLS parameters smtpd_tls_cert_file=/etc/letsencrypt/live/example.com/fullchain.pem smtpd_tls_key_file=/etc/letsencrypt/live/example.com/privkey.pem smtpd_use_tls=yes smtpd_tls_auth_only = yes smtp_tls_security_level = may smtpd_tls_security_level = may smtpd_sasl_security_options = noanonymous, noplaintext smtpd_sasl_tls_security_options = noanonymous # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for # information on enabling SSL in the smtp client. smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination myhostname = example.com alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases mydomain = example.com myorigin = $mydomain mydestination = localhost, localhost.$mydomain relayhost = mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all inet_protocols = all # Handing off local delivery to Dovecot's LMTP, and telling it where to store mail virtual_transport = lmtp:unix:private/dovecot-lmtp # Virtual domains, users, and aliases virtual_mailbox_domains = mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf virtual_mailbox_maps = mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf, mysql:/etc/postfix/mysql-virtual-email2email.cf

Thay đổi example.com bằng domain của bạn

3. Tạo file /etc/postfix/mysql-virtual-mailbox-domains.cf

user = mailuser password = mailuserpass hosts = 127.0.0.1 dbname = mailserver query = SELECT 1 FROM virtual_domains WHERE name='%s'

4. Tạo file /etc/postfix/mysql-virtual-mailbox-maps.cf

user = mailuser password = mailuserpass hosts = 127.0.0.1 dbname = mailserver query = SELECT 1 FROM virtual_users WHERE email='%s'

5. Tạo file /etc/postfix/mysql-virtual-alias-maps.cf

user = mailuser password = mailuserpass hosts = 127.0.0.1 dbname = mailserver query = SELECT destination FROM virtual_aliases WHERE source='%s'

6. Tạo file /etc/postfix/mysql-virtual-email2email.cf

user = mailuser password = mailuserpass hosts = 127.0.0.1 dbname = mailserver query = SELECT email FROM virtual_users WHERE email='%s'

7. Restart Postfix:

sudo systemctl restart postfix

8. Lệnh postmap tạo hoặc truy vấn các bảng tra cứu của Postfix hoặc cập nhật một bảng hiện có. Nhập lệnh sau để đảm bảo rằng Postfix có thể truy vấn bảng virtual_domains.  Lệnh sẽ trả về 1 nếu nó thành công:

sudo postmap -q example.com mysql:/etc/postfix/mysql-virtual-mailbox-domains.cf

9. Kiểm tra Postfix để xác minh rằng nó có thể truy xuất địa chỉ email từ bảng MySQL virtual_users. Bạn sẽ nhận được 1 làm đầu ra:

sudo postmap -q mysql:/etc/postfix/mysql-virtual-mailbox-maps.cf

10. Tương tự kiểm tra Postfix để xác minh truy xuất thành công với các lệnh

sudo postmap -q mysql:/etc/postfix/mysql-virtual-email2email.cf sudo postmap -q mysql:/etc/postfix/mysql-virtual-alias-maps.cf

6.2. Cấu hình file master.cf

Chương trình chính của Postfix bắt đầu và giám sát tất cả các quy trình của Postfix. File cấu hình master.cf liệt kê tất cả các chương trình và thông tin về cách chúng khi được khởi động.

1.  Tạo bảng sao chép gốc master.cf

sudo cp /etc/postfix/master.cf /etc/postfix/master.cf.orig

2. Thay thế nội dung file /etc/postfix/master.cf

# # Postfix master process configuration file. For details on the format # of the file, see the master(5) manual page (command: "man 5 master" or # on-line: //www.postfix.org/master.5.html). # # Do not forget to execute "postfix reload" after editing this file. # # ========================================================================== # service type private unpriv chroot wakeup maxproc command + args # (yes) (yes) (yes) (never) (100) # ========================================================================== smtp inet n - n - - smtpd #smtp inet n - - - 1 postscreen #smtpd pass - - - - - smtpd #dnsblog unix - - - - 0 dnsblog #tlsproxy unix - - - - 0 tlsproxy submission inet n - - - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_sasl_type=dovecot -o smtpd_sasl_path=private/auth -o smtpd_reject_unlisted_recipient=no -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING smtps inet n - - - - smtpd -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes -o smtpd_sasl_type=dovecot -o smtpd_sasl_path=private/auth -o smtpd_client_restrictions=permit_sasl_authenticated,reject -o milter_macro_daemon_name=ORIGINATING ... # để lại nội dung gốc phía sau

3. Phân quyền cho /etc/postfix

sudo chmod -R o-rwx /etc/postfix

4. Restart Postfix

sudo systemctl restart postfix

Bước 7 - Cấu hình Devecot

Dovecot là một Mail Delivery Agent (MDA), đụng sử dụng để chuyển thư từ Postfix và chuyển đến hộp thư ảo (virtual mailbox). Dovecot cần có cấu hình SSL để kết nối.

1. Sao chép tất cả các cầu hình mặc định

sudo cp /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig sudo cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.orig sudo cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.orig sudo cp /etc/dovecot/conf.d/auth-sql.conf.ext /etc/dovecot/conf.d/auth-sql.conf.ext.orig sudo cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.orig sudo cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.orig

2. Sửa lại file /etc/dovecot/dovecot.conf

Bỏ comment protocols = imap pop3 lmtp

## Dovecot configuration file ... # Protocols we want to be serving. protocols = imap pop3 lmtp

3. Sửa file /etc/dovecot/conf.d/10-mail.conf

File này kiểm soát cách Dovecot tương tác với với server để lưu và truy xuất messages

... mail_location = maildir:/var/mail/vhosts/%d/%n ... mail_privileged_group = mail ...

4. Tạo file /var/mail/vhosts/example.com để lưu trữ email sent đến domain của bạn

sudo mkdir -p /var/mail/vhosts/example.com

5. Tạo vmail group với ID 5000. Tạo user vmail cho vmail group dùng để đọc mail từ server.

sudo groupadd -g 5000 vmail sudo useradd -g vmail -u 5000 vmail -d /var/mail/

6. Thay đổi ower cho thư mục /var/mail/

sudo chown -R vmail:vmail /var/mail/

7. Sửa đổi  /etc/dovecot/conf.d/10-auth.conf tương tự như:

... disable_plaintext_auth = yes ... auth_mechanisms = plain login ... !include auth-system.conf.ext ... !include auth-sql.conf.ext ...

8. Sửa file /etc/dovecot/conf.d/auth-sql.conf.ext - File xác thực và lưu trữ thông tin. Xóa comment như sau:

... passdb { driver = sql args = /etc/dovecot/dovecot-sql.conf.ext } ... userdb { driver = static args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n } ...

9. Tạo file /etc/dovecot/dovecot-sql.conf.ext - File chứa thông tin connect với MySQL 

driver = mysql connect = host=127.0.0.1 dbname=mailserver user=mailuser password=mailuserpass default_pass_scheme = SHA512-CRYPT password_query = SELECT email as user, password FROM virtual_users WHERE email='%u';

Biến password_query sử dụng địa chỉ email được liệt kê trong bảng virtual_users làm thông tin đăng nhập tên người dùng cho tài khoản email.

Để sử dụng bí danh làm tên người dùng:

  1. Thêm bí danh làm địa chỉ email nguồn (source) và đích (destination) vào bảng virtual_aliases.
  2. Thay đổi giá trị password_query của file /etc/dovecot/dovecot-sql.conf.ext thành
password_query = SELECT email as user, password FROM virtual_users WHERE email=(SELECT destination FROM virtual_aliases WHERE source = '%u');

10. Phân quyền cho /etc/dovecot/

sudo chown -R vmail:dovecot /etc/dovecot sudo chmod -R o-rwx /etc/dovecot

11. Thay đổi service trong file  /etc/dovecot/conf.d/10-master.conf

Vô hiệu hóa IMAP và POP3 không được mã hóa bằng cách đặt các port thành 0. Bỏ ghi chú các biến cổng và ssl:

... service imap-login { inet_listener imap { port = 0 } inet_listener imaps { port = 993 ssl = yes } ... } ... service pop3-login { inet_listener pop3 { port = 0 } inet_listener pop3s { port = 995 ssl = yes } } ...

Chú ý: Nếu có lỗi cú pháp có thể debug trong file /var/log/upstart/dovecot.log

Tìm và thay thế đoạn service lmtp trong 10-master.conf

... service lmtp { unix_listener /var/spool/postfix/private/dovecot-lmtp { #mode = 0666i mode = 0600 user = postfix group = postfix } ... }

Thay đổi service auth

... service auth { ... unix_listener /var/spool/postfix/private/auth { mode = 0660 user = postfix group = postfix } unix_listener auth-userdb { mode = 0600 user = vmail } ... user = dovecot } ...

Trong đoạn service auth-worker

... service auth-worker { ... user = vmail }

Lưu thay đổi file /etc/dovecot/conf.d/10-master.conf.

12. Sửa file /etc/dovecot/conf.d/10-ssl.conf - File lưu key và certificate của SSL

... # SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt> ssl = required ... ssl_cert = </etc/letsencrypt/live/example.com/fullchain.pem ssl_key = </etc/letsencrypt/live/example.com/privkey.pem

13. Restart Dovecot

sudo systemctl restart dovecot

Bước 8 - Kiểm tra 

1. Trước khi truy cập vào bất kỳ tài khoản email mới được tạo nào, bạn cần gửi thư kiểm tra để tạo hộp thư của người dùng đó (thay thế bằng địa chỉ mà bạn đã định cấu hình trong cơ sở dữ liệu của mình):

yum install mailx mail

Nhập Subject và message, sau đó Ctrl+D để hoàn thành email

2. Kiểm tra log tại /var/log/maillog

tail /var/log/maillog

Kết quả như sau

Mar 18 17:18:47 localhost postfix/cleanup[3427]: B624062FA: message-id=<> Mar 18 17:18:47 localhost postfix/qmgr[3410]: B624062FA: from=<>, size=515, nrcpt=1 (queue active) Mar 18 17:18:47 localhost postfix/pipe[3435]: B624062FA: to=<>, relay=dovecot, delay=0.14, delays=0.04/0.01/0/0.09, dsn=2.0.0, $ Mar 18 17:18:47 localhost postfix/qmgr[3410]: B624062FA: removed

3. Để kiểm tra hộp thư , hãy điều hướng đến thư mục hộp thư /var/mail/vhosts/example.com/email1 và đưa ra lệnh sau:

find

Kết quả

. ./dovecot-uidvalidity ./new ./maildirfolder ./dovecot.index.cache ./cur ./cur/1539202420.M440788P27259.mail,S=450,W=465:2,S ./dovecot.index.log ./dovecot-uidlist ./dovecot-uidvalidity.5bbe5d50 ./tmp

4. Kiểm tra hộp thư bằng cách sử dụng một ứng dụng thư đơn giản.

yum install mutt sudo mutt -f .

Bấm q để thoát mutt

Hướng dẫn tạo thêm email domain

Thêm tên miền, địa chỉ email và bí danh mới

Tên miền - Domain

1. Kết nối MySQL

sudo mysql -u root

2. Kiểm tra table virtual_domains 

SELECT * FROM mailserver.virtual_domains;

Kết quả

+----+-----------------------+ | id | name | +----+-----------------------+ | 1 | example.com | | 2 | hostname.example.com | | 3 | hostname | | 4 | localhost.example.com | +----+-----------------------+

3. Thêm domain mới

INSERT INTO `mailserver`.`virtual_domains` (`name`) VALUES ('newdomain.com');

4. Kiểm tra lại như trên và thoát

SELECT * FROM mailserver.virtual_domains;

Địa chỉ email

1. Kiểm tra table virtual_users 

SELECT * FROM mailserver.virtual_users;

Kết quả:

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

2. Thêm địa chỉ email 

INSERT INTO `mailserver`.`virtual_users` (`domain_id`, `password` , `email`) VALUES ('5', ENCRYPT('newpassword', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))) , '');

Chú ý: Domain_id phải tương ứng với giá trị id của miền trong bảng virtual_domains.

3. Kiểm tra lại

SELECT * FROM mailserver.virtual_users;

Bí danh - Aliases

1. Kiểm tra table virtual_users 

SELECT * FROM mailserver.virtual_aliases;

Kết quả

+----+-----------+-------------------+--------------------+ | id | domain_id | source | destination | +----+-----------+-------------------+--------------------+ | 1 | 1 | | | +----+-----------+-------------------+--------------------+ 1 row in set (0.00 sec)

2. Thêm alias mới

INSERT INTO `mailserver`.`virtual_aliases` (`domain_id`, `source`, `destination`) VALUES ('5', '', '');

3. Bạn có thể tạo bí danh “catch-all” ( nhận tất cả) -  sẽ chuyển tiếp tất cả các email được gửi đến miền phù hợp không có bí danh hoặc người dùng phù hợp. 

INSERT INTO `mailserver`.`virtual_aliases` (`domain_id`, `source`, `destination`) VALUES ('5', '@newdomain.com', '');

4. Kiểm tra 

SELECT * FROM mailserver.virtual_aliases;

Cuối cùng thoát MySQL

quit

Chủ đề