如何在 Ubuntu 16.04 上安裝和配置 Postfix


本文將介紹如何在 Ubuntu 16.04 上安裝配置 Postfix。Postfix 是一個非常流行的開源郵件傳輸代理 (MTA),可用於在 Ubuntu Linux 系統上中繼電子郵件。大多數真實世界的公共郵件伺服器都只使用 Postfix。

前提條件

完成此演示需要以下條件:

  • 安裝了 Ubuntu 16.04 的機器,以及具有 sudo 許可權的非 root 使用者。
  • 指向該機器的完全限定域名 (FQDN)。
  • 我們假設域名 FQDN 為 mail.mydomain.com

安裝 Postfix

由於 Postfix 可從 Ubuntu 預設儲存庫獲取,因此可以直接進行安裝。

在開始安裝之前,我們將首先使用以下命令更新 apt-get。

$ sudo apt-get update

現在,我們將使用以下命令安裝 postfix。

$ sudo apt-get install postfix
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed: ssl-cert
Suggested packages:
procmail postfix-mysql postfix-pgsql postfix-ldap postfix-pcre sasl2-bin dovecot-common
postfix-cdb mail-reader postfix-doc openssl-blacklist
The following NEW packages will be installed:
postfix ssl-cert
0 upgraded, 2 newly installed, 0 to remove and 19 not upgraded.
Need to get 1,169 kB of archives.
After this operation, 3,759 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 ssl-cert all 1.0.37 [16.9 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu xenial/main amd64 postfix amd64 3.1.0-3 [1,152 kB]
Fetched 1,169 kB in 2s (434 kB/s)
Preconfiguring packages ...
Selecting previously unselected package ssl-cert.
(Reading database ... 59373 files and directories currently installed.)
Preparing to unpack .../ssl-cert_1.0.37_all.deb ...
Unpacking ssl-cert (1.0.37) ...
Selecting previously unselected package postfix.
Preparing to unpack .../postfix_3.1.0-3_amd64.deb ...
Unpacking postfix (3.1.0-3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu7) ...
…
…
…
/etc/aliases does not exist, creating it.
WARNING: /etc/aliases exists, but does not have a root alias.
Postfix is now set up with a default configuration. If you need to make
changes, edit
/etc/postfix/main.cf (and others) as needed. To view Postfix configuration
values, see postconf(1).
After modifying main.cf, be sure to run '/etc/init.d/postfix reload'.
Running newaliases
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu7) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...

安裝啟動後,將詢問以下內容。

郵件配置的常規型別。我們必須選擇“Internet 站點”。


輸入要與 postfix 一起使用的域名,系統郵件名稱(例如 mail.mydomain.com)。


安裝完成後,我們將檢查服務是否已啟動。為確認,我們將執行以下命令。

$ netstat –ntlp
netstat -ntlp
Active Internet connections (only servers)
Proto    Recv-Q    Send-Q    Local Address    Foreign Address    State    PID/Program name
tcp          0          0    0.0.0.0:22          0.0.0.0:*       LISTEN    4212/sshd
tcp          0          0    0.0.0.0:25          0.0.0.0:*       LISTEN    5747/master
tcp6         0          0    :::22                :::*          LISTEN     4212/sshd
tcp6         0          0    :::25                :::*           LISTEN    5747/master

我們應該看到埠 25 已開啟。

postfix 配置檔案位於 /etc/postfix/main.cf

$ sudo vi /etc/postfix/main.cf
Output:
# 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 (Ubuntu)
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/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
# 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 = ubuntu16.mydomain.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = $myhostname, mail.mydomain.com, localhost.mydomain.com, , localhost
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

設定說明如下:

  • 郵件配置的常規型別?:Internet 站點
  • 系統郵件名稱:mydomain.com(而不是 mail.mydomain.com)
  • Root 和 postmaster 郵件收件人:root
  • 其他接受郵件的目標:$myhostname, mydomain.com, mail.mydomain.com, localhost.mydomain.com, localhost
  • 強制同步更新郵件佇列?:否
  • 本地網路:127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
  • 郵箱大小限制:0
  • 本地地址擴充套件字元:+
  • 要使用的網際網路協議:全部

如果要重新配置,可以使用以下命令更改 postfix 配置。

$ sudo dpkg-reconfigure postfix

將郵箱對映到 Linux 帳戶。

我們必須編輯虛擬對映檔案以將郵箱對映到本地 Linux 帳戶,為此,我們需要編輯以下檔案。

$ sudo nano /etc/postfix/virtual

例如,如果我們想接受 noreply@mydomain.com 和 admin@mydomain.com 的郵件,我們需要將以下幾行新增到 /etc/postfix/virtual 檔案中。

roreply@mydomain.com ubuntu
admin@mydomain.com ubuntu

將設定新增到配置檔案後,我們需要使用以下命令應用這些更改。

$ sudo postmap /etc/postfix/virtual

應用設定後,我們需要重新啟動 postfix 服務。

$ sudo systemctl restart postfix.

配置環境以設定郵件位置

在開始安裝郵件客戶端之前,我們需要確保郵件環境已正確配置。我們必須在幾個位置設定變數 /etc/bash.bashrc 和 /etc/profile.d,以確保使用者已配置郵件。

$ echo 'export MAIL=~/Maildir' | sudo tee -a /etc/bash.bashrc | sudo tee -a /etc/profile.d/mail.sh

配置好設定後,我們需要將這些設定載入到當前會話。

$ source /etc/profile.d/mail.sh

安裝和配置郵件客戶端

預設情況下未安裝郵件客戶端,我們需要使用以下命令安裝郵件客戶端:

$ apt install mailutils –y

使用客戶端工具傳送郵件

要測試郵件,我們將以 root 使用者身份登入並向“ubuntu”使用者傳送郵件,為此,我們可以使用以下命令:

# echo "test " | mail -s 'Test email subject line' ubuntu@mail.mydomain.com

為了驗證郵件,我們已從本地系統的 root 使用者向 ubuntu 使用者傳送郵件。

$ mail
Output:
"/var/mail/ubuntu": 2 messages 2 new
>N 1 Mail Delivery Syst Wed Oct 19 09:44 67/2299 Undelivered Mail Returned to Sender
N 2 root Wed Oct 19 09:45 12/490
? 2
Return-Path: <root@ubuntu16.example.com>
X-Original-To: ubuntu@ubuntu16.example.com
Delivered-To: ubuntu@ubuntu16.example.com
Received: by ubuntu16.example.com (Postfix, from userid 0)
id 5C631FF732; Wed, 19 Oct 2016 09:45:30 +0530 (IST)
To: <test@ubuntu16.example.com>,<ubuntu@ubuntu16.example.com>
X-Mailer: mail (GNU Mailutils 2.99.99)
Message-Id: <20161019041530.5C631FF732@ubuntu16.example.com>
Date: Wed, 19 Oct 2016 09:45:30 +0530 (IST)
From: root@ubuntu16.example.com (root)
test
?

現在您將看到我們可以接收來自 root 使用者的電子郵件。

在本文中,我們學習瞭如何在 Ubuntu 16.04 上安裝和配置 Postfix MTA(郵件傳輸代理),它可以用來發送郵件。使用上述配置,我們擁有了開始使用的基本郵件功能。

更新於:2020年1月20日

瀏覽量:598

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.