Android

Cài đặt và cấu hình postfix và dovecot

Building an Ubuntu mail server with Postfix, Amavis, SpamAssassin, ClamAV, Dovecot, and OpenDMARC

Building an Ubuntu mail server with Postfix, Amavis, SpamAssassin, ClamAV, Dovecot, and OpenDMARC

Mục lục:

Anonim

Đây là bài viết thứ hai về Thiết lập và cấu hình loạt máy chủ thư của chúng tôi. Trong bài đăng này, chúng tôi sẽ chỉ cho bạn cách cài đặt và cấu hình Postfix và Dovecot, hai thành phần chính của hệ thống thư của chúng tôi.

Postfix là một đại lý chuyển thư nguồn mở (MTA), một dịch vụ được sử dụng để gửi và nhận email. Dovecot là một máy chủ IMAP / POP3 và trong thiết lập của chúng tôi, nó cũng sẽ xử lý phân phối cục bộ và xác thực người dùng.

Hướng dẫn này được viết cho Ubuntu 16.04, tuy nhiên các bước tương tự với các sửa đổi nhỏ sẽ hoạt động trên mọi phiên bản Ubuntu mới hơn.

Điều kiện tiên quyết

Trước khi tiếp tục với hướng dẫn này, hãy đảm bảo bạn đã đăng nhập với tư cách là người dùng có quyền sudo.

Cài đặt Postfix và Dovecot

Các gói Dovecot trong kho lưu trữ mặc định của Ubuntu đã lỗi thời. Để tận dụng mô-đun imap_sieve chúng tôi sẽ cài đặt Dovecot từ kho lưu trữ cộng đồng Dovecot.

Thêm khóa GPG của kho lưu trữ vào khóa nguồn apt của bạn bằng lệnh wget sau:

wget -O- https://repo.dovecot.org/DOVECOT-REPO-GPG | sudo apt-key add -

Kích hoạt kho lưu trữ cộng đồng Dovecot bằng lệnh sau:

echo "deb https://repo.dovecot.org/ce-2.3-latest/ubuntu/$(lsb_release -cs) $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/dovecot.list

sudo apt update sudo debconf-set-selections <<< "postfix postfix/mailname string $(hostname -f)" sudo debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'" sudo apt install postfix postfix-mysql dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-mysql

Cấu hình Postfix

Chúng tôi sẽ thiết lập Postfix để sử dụng hộp thư và tên miền ảo.

Bắt đầu bằng cách tạo các tệp cấu hình sql sẽ hướng dẫn postfix cách truy cập cơ sở dữ liệu MySQL, được tạo trong phần đầu tiên của loạt bài này.

sudo mkdir -p /etc/postfix/sql

Mở trình soạn thảo văn bản của bạn và tạo các tệp sau:

/etc/postfix/sql/mysql_virtual_domains_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT domain FROM domain WHERE domain='%s' AND active = '1' /etc/postfix/sql/mysql_virtual_alias_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias WHERE address='%s' AND active = '1' /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias, alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1' /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT goto FROM alias, alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1' /etc/postfix/sql/mysql_virtual_mailbox_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1' /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf

user = postfixadmin password = P4ssvv0rD hosts = 127.0.0.1 dbname = postfixadmin query = SELECT maildir FROM mailbox, alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'

Khi các tệp cấu hình SQL được tạo, hãy cập nhật tệp cấu hình hậu tố chính để bao gồm thông tin về các miền ảo, người dùng và bí danh được lưu trữ trong cơ sở dữ liệu MySQL.

sudo postconf -e "virtual_mailbox_domains = mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf" sudo postconf -e "virtual_alias_maps = mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf" sudo postconf -e "virtual_mailbox_maps = mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf" Lệnh postconf hiển thị các giá trị thực của các tham số cấu hình, thay đổi giá trị tham số cấu hình hoặc hiển thị thông tin cấu hình khác về hệ thống thư Postfix.

Đại lý chuyển phát cục bộ sẽ gửi các email đến vào hộp thư của người dùng. Chạy lệnh sau để đặt dịch vụ LMTP của Dovecot làm vận chuyển gửi thư mặc định:

sudo postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"

Đặt tham số TL bằng chứng chỉ SSL mã hóa được tạo trước đó:

sudo postconf -e 'smtp_tls_security_level = may' sudo postconf -e 'smtpd_tls_security_level = may' sudo postconf -e 'smtp_tls_note_starttls_offer = yes' sudo postconf -e 'smtpd_tls_cert_file = /etc/letsencrypt/live/mail.linuxize.com/fullchain.pem' sudo postconf -e 'smtpd_tls_key_file = /etc/letsencrypt/live/mail.linuxize.com/privkey.pem'

Định cấu hình cài đặt SMTP đã xác thực và tắt xác thực cho Dovecot:

sudo postconf -e 'smtpd_sasl_type = dovecot' sudo postconf -e 'broken_sasl_auth_clients = yes' sudo postconf -e 'smtpd_sasl_auth_enable = yes' sudo postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated, permit_mynetworks, reject_unauth_destination'

Chúng ta cũng cần chỉnh sửa tệp cấu hình chính Postfix master.cf và kích hoạt cổng gửi ( 587 ) và cổng smtps ( 465 ).

Mở tệp bằng trình soạn thảo văn bản của bạn và bỏ ghi chú / chỉnh sửa các dòng sau:

/etc/postfix/master.cf

submission inet n - y - - smtpd -o syslog_name=postfix/submission -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no -o smtpd_client_restrictions=permit_sasl_authenticated, reject # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated, reject -o milter_macro_daemon_name=ORIGINATING smtps inet n - y - - smtpd -o syslog_name=postfix/smtps -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no -o smtpd_client_restrictions=permit_sasl_authenticated, reject # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated, reject -o milter_macro_daemon_name=ORIGINATING

Khởi động lại dịch vụ postfix để các thay đổi có hiệu lực.

sudo systemctl restart postfix

Tại thời điểm này, bạn đã cấu hình thành công dịch vụ Postfix.

Cấu hình Dovecot

Trong phần này, chúng tôi sẽ cấu hình Dovecot để phù hợp với thiết lập của chúng tôi. Hãy chắc chắn rằng bạn chỉnh sửa các dòng được tô màu vàng.

Bắt đầu bằng cách định cấu hình tệp dovecot-sql.conf.ext hướng dẫn Dovecot cách truy cập cơ sở dữ liệu và cách tìm thông tin về tài khoản email.

/etc/dovecot/dovecot-sql.conf.ext

driver = mysql connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=P4ssvv0rD default_pass_scheme = MD5-CRYPT iterate_query = SELECT username AS user FROM mailbox user_query = SELECT CONCAT('/var/mail/vmail/', maildir) AS home, \ CONCAT('maildir:/var/mail/vmail/', maildir) AS mail, \ 5000 AS uid, 5000 AS gid, CONCAT('*:bytes=', quota) AS quota_rule \ FROM mailbox WHERE username = '%u' AND active = 1 password_query = SELECT username AS user, password FROM mailbox \ WHERE username = '%u' AND active='1'

Đừng quên sử dụng thông tin đăng nhập MySQL chính xác (dbname, người dùng và mật khẩu).

Tiếp theo, chỉnh sửa tệp conf.d/10-mail.conf và chỉnh sửa các biến sau:

/etc/dovecot/conf.d/10-mail.conf

… mail_location = maildir:/var/mail/vmail/%d/%n… mail_uid = vmail mail_gid = vmail… first_valid_uid = 5000 last_valid_uid = 5000… mail_privileged_group = vmail… mail_plugins = quota…

Để làm cho xác thực hoạt động, hãy mở conf.d/10-auth.conf , chỉnh sửa các dòng sau và bao gồm tệp auth-sql.conf.ext :

/etc/dovecot/conf.d/10-auth.conf

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

Mở tệp conf.d/10-master.conf và sửa đổi nó như sau:

/etc/dovecot/conf.d/10-master.conf

… service lmtp { unix_listener /var/spool/postfix/private/dovecot-lmtp { mode = 0600 user = postfix group = postfix }… }… service auth {… unix_listener auth-userdb { mode = 0600 user = vmail group = vmail }… unix_listener /var/spool/postfix/private/auth { mode = 0666 user = postfix group = postfix }… }… service auth-worker { user = vmail }… service dict { unix_listener dict { mode = 0660 user = vmail group = vmail } }…

Mở conf.d/10-ssl.conf và bật SSL / TLS.

/etc/dovecot/conf.d/10-ssl.conf

… ssl = yes… ssl_cert = Make sure you use the correct path to the SSL certificate files.


If you have followed this series from the beginning, you should already have the fullchain.pem , privkey.pem , dhparam.pem files created on your server. For more information about how to create a free Let's encrypt SSL certificate and Diffie–Hellman key check this tutorial.


Thanks to Nevyn for noticing the problem and providing a solution.
… ssl = yes… ssl_cert = Make sure you use the correct path to the SSL certificate files.


If you have followed this series from the beginning, you should already have the fullchain.pem , privkey.pem , dhparam.pem files created on your server. For more information about how to create a free Let's encrypt SSL certificate and Diffie–Hellman key check this tutorial.


Thanks to Nevyn for noticing the problem and providing a solution.

Mở tệp conf.d/20-imap.conf và kích hoạt plugin imap_quota :

/etc/dovecot/conf.d/20-imap.conf

… protocol imap {… mail_plugins = $mail_plugins imap_quota… }…

Mở tệp conf.d/20-lmtp.conf và chỉnh sửa nó như sau:

/etc/dovecot/conf.d/20-lmtp.conf

… protocol lmtp { postmaster_address = [email protected] mail_plugins = $mail_plugins }…

Xác định các Hộp thư mặc định trong tệp conf.d/20-lmtp.conf :

/etc/dovecot/conf.d/15-mailboxes.conf

… mailbox Drafts { special_use = \Drafts } mailbox Spam { special_use = \Junk auto = subscribe } mailbox Junk { special_use = \Junk }…

Có hai loại kích cỡ hạn ngạch khác nhau, một loại được đặt cho toàn bộ tên miền và loại còn lại cho mỗi hộp thư người dùng. Trong phần trước của loạt bài này, chúng tôi đã kích hoạt hỗ trợ hạn ngạch trong PostfixAdmin, điều đó có nghĩa là thông tin hạn ngạch sẽ được lưu trữ trong cơ sở dữ liệu PostfixAdmin.

Bây giờ chúng ta cần cấu hình Dovecot để kết nối với cơ sở dữ liệu, để xử lý các giới hạn hạn ngạch và chạy tập lệnh gửi thư đến người dùng khi hạn ngạch của người dùng vượt quá giới hạn đã chỉ định. Để làm như vậy, hãy mở tệp conf.d/90-quota.conf và sửa đổi nó như sau:

/etc/dovecot/conf.d/90-quota.conf

plugin { quota = dict:User quota::proxy::sqlquota quota_rule = *:storage=5GB quota_rule2 = Trash:storage=+100M quota_grace = 10%% quota_exceeded_message = Quota exceeded, please contact your system administrator. quota_warning = storage=100%% quota-warning 100 %u quota_warning2 = storage=95%% quota-warning 95 %u quota_warning3 = storage=90%% quota-warning 90 %u quota_warning4 = storage=85%% quota-warning 85 %u } service quota-warning { executable = script /usr/local/bin/quota-warning.sh user = vmail unix_listener quota-warning { group = vmail mode = 0660 user = vmail } } dict { sqlquota = mysql:/etc/dovecot/dovecot-dict-sql.conf.ext }

Chúng ta cũng cần nói với dovecot cách truy cập từ điển hạn ngạch SQL. Mở tệp dovecot-dict-sql.conf.ext và chỉnh sửa các dòng sau:

/etc/dovecot/dovecot-dict-sql.conf.ext

… connect = host=127.0.0.1 dbname=postfixadmin user=postfixadmin password=P4ssvv0rD… map { pattern = priv/quota/storage table = quota2 username_field = username value_field = bytes } map { pattern = priv/quota/messages table = quota2 username_field = username value_field = messages }… # map { # pattern = shared/expire/$user/$mailbox # table = expires # value_field = expire_stamp # # fields { # username = $user # mailbox = $mailbox # } # }… Đảm bảo bạn sử dụng thông tin đăng nhập MySQL chính xác (dbname, người dùng và mật khẩu).

Tạo tập lệnh shell sau đây sẽ gửi email đến người dùng nếu hạn ngạch của nó vượt quá giới hạn chỉ định:

/usr/local/bin/quota-warning.sh

#!/bin/sh PERCENT=$1 USER=$2 cat << EOF | /usr/lib/dovecot/dovecot-lda -d $USER -o "plugin/quota=dict:User quota::noenforcing:proxy::sqlquota" From: [email protected] Subject: Quota warning Your mailbox is now $PERCENT% full. EOF

Làm cho tập lệnh thực thi bằng cách chạy chmod sau:

sudo chmod +x /usr/local/bin/quota-warning.sh

Cuối cùng khởi động lại dịch vụ dovecot để các thay đổi có hiệu lực.

sudo systemctl restart dovecot

Phần kết luận

Bây giờ bạn nên có một hệ thống thư đầy đủ chức năng. Trong phần tiếp theo của loạt bài này, chúng tôi sẽ chỉ cho bạn cách cài đặt và tích hợp Rspamd.

bưu điện máy chủ bưu điện dovecot

Bài đăng này là một phần của Thiết lập và định cấu hình loạt máy chủ thư.

Các bài viết khác trong loạt bài này:

• Thiết lập máy chủ thư với PostfixAdmin • Cài đặt và định cấu hình Postfix và Dovecot • Cài đặt và Tích hợp Rspamd • Cài đặt và Cấu hình Roundcube Webmail