###### tags: `Ubuntu`,`Mail Relay`, `Pstfix` # Ubuntu 20.04 + Postfix 做Mail Relay 1. 更新 ``` csharp=1 sudo apt update ``` 2. 安裝Postfix 套件 ``` csharp=1 sudo apt install postfix ``` 3. 選擇 <font color=#FF6600>Internet Site </font>模式 ![](https://i.imgur.com/zG4tWMk.png) 4. 輸入主機名稱 EX: <font color=#FF6600>mailrealy.example.com</font> ![](https://i.imgur.com/MLUFr4r.png) ### 重點來啦!! 5. 配置主機參數 編輯 /etc/postfix/main.cf,輸入: ```csharp=1 sudo nano /etc/postfix/main.cf ``` main.cf內容: ```csharp=1 # 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 # See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on # fresh installs. compatibility_level = 2 #25 inet n - n - - smtpd # 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_tls_security_level=may smtp_tls_CApath=/etc/ssl/certs smtp_tls_security_level=may smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination myhostname = mailrelay.example.com //mailrelay 主機名稱 alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases myorigin = /etc/mailname #myorigin = $myhostname mydestination = localhost.$mydomain, localhost, $myhostname #mydestination= relayhost = [smtp.office365.com]:587 //此為O365 SMTP伺服器 mynetworks = 127.0.0.0/8 192.168.0.0/24 //輸入區網IP 用空白分隔 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all #inet_protocols = all inet_protocols = ipv4 smtp_sasl_auth_enable = yes #smtp_sasl_password_maps = static:test@example.com:Password smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd smtp_sasl_security_options = noanonymous smtp_tls_security_level = may #header_size_limit = 4096000 ``` #### 需要注意的是 : myhostname : Relay主機名稱 relayhost : 此用O365做SMTP伺服器,若是其他主機就設定該主機上的SMTP伺服器 mynetworks : 區網IP inet_interfaces : 誰可以傳進這台主機 inet_protocols : 使用IPV4即可 smtp_sasl_password_maps : 這裡有兩種寫法, 一種是直接寫死(如53行),另一種是另外做一個檔案把相關參數放在該檔案裡(54行) 帶設定都完成後跳出並重啟Postfix服務: ```csharp=1 sudo systemctl restart postfix ``` #### sasl_passwd 內容: 剛剛說到把smtp_sasl_password_maps放在另一個檔案內所以需要在該檔案(檔案位置在/etc/postfix/)內新增下面內容 [smtp伺服器]:port號 帳號:密碼 ```csharp=1 [smtp.office365.com]:587 test@example.com:Password ``` 當Postfix都設定完成後記得要開啟防火牆 Port 25 ```csharp=1 sudo ufw allow 25/tcp sudo ufw restart //重開防火牆 ``` <br> <br> #### 當都完成後就可以來測試了 1. 首先確認是否有把該主機加入DNS內,因一不小心就可能被當跳板亂發信所以建議設定在內部使用就好。 2. 再來要檢查port 25是否正常開啟運作 指令: ```csharp= sudo netsta -tuln ``` ![](https://i.imgur.com/X3eNdVw.png) telent 測試,開啟命令提示字元 指令: ```csharp= telnet 192.168.XXX.XXX 25 ``` 若成功就下面文字代表已經TELNET進去POSTFIX服務裡 ![](https://i.imgur.com/UBPAxrX.png) 安裝 Mailutils 套件 ```csharp= sudo apt install mailutils ``` 安裝後可測試發信看看,若收到信就代表成功了 指令: ```csharp= echo "This is a test email body." | mail -s "Subject title" -a "From: send@example.com" receive@example.com ``` <br> <br> ### 參考網站: -[連結一](https://apiit.atlassian.net/wiki/spaces/ITSM/pages/1205567492/How+to+configure+postfix+relay+to+Office365+on+Ubuntu) -[連結二](https://magiclen.org/ubuntu-server-email/)