NCNU-OpenSource
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    [TOC] ###### tags: `lsa` # Mail Server - 什麼是 mail server ? - 專門寄送、接收 email 的服務 - 你就可以寄信給世界各地的任何人 <!-- > 當你寄 email 的時候,它會經過一連串 server 才會到達最終目的地 --> - Email 的位址寫法 - `帳號@主機名稱` - 例如:`lin@gmail.com` ## 郵件傳輸過程中經過的節點與使用的 Protocol ![](https://hackmd.io/_uploads/SyURHUCEn.png) ### 寄信 - MUA (Mail User Agent) - 郵件使用者代理人 - 功能 - 寄送、接收 mail server 的電子郵件 - 例如:Linux 的 Kmail - MTA (Mail Transfer Agent) - 郵件傳送代理人 - 使用 SMTP (port 25) 或是 SMTPs (port 465/port 587) - 功能 - 轉遞信件 - 如果信的目的地是本機上的使用者,那信就直接送到該使用者的 mailbox 下 - 如果信的目的地不是本機上的使用者,那就需要將信轉送給下一台 MTA :::info **MX (Mail eXchanger)** - DNS 上的一個記錄,用來解析 Email 中 `@` 後的主機名稱 - 當郵件傳送出去時,會先看這個 domain name 的 MX 紀錄是甚麼 - 找到 MX server 後,就將信傳送過去 - 假設寄信給 BT (`BlueT@BlueT.org`) - ![](https://i.imgur.com/uLiFrVu.png) - ![](https://i.imgur.com/s63qYGf.png) - ![](https://i.imgur.com/9kJ4NCZ.png) <!-- 有很多台的話就依照優先權選一台,作為送信的目的,如果都沒有的話,就會用最原本的 BlueT.org 當作目的,如果還是沒有就回復失敗,給寄件人 --> ::: - MDA (Mail Delivery Agent) - 郵件遞送代理人 - 掛在 MTA 下的一個小**程式** - 功能 - 分析 MTA 收到這封信的 header 或內容等資料,決定這封郵件的去向 - 過濾垃圾信件 - 根據這封信件的 header,或是特定的信件內容來分析過濾 - 例如:AV 情色 - 自動回覆 - Mailbox - 電子信箱 - Linux 系統預設的信箱放在 `/var/spool/mail/使用者帳號` - Protocol - SMTP (Simple Mail Transfer Protocol) - port - 25:預設的 SMTP port 號 <!-- 但很多 ISP (Internet Service Provider 網際網路連線服務公司) 避免客戶發送垃圾電郵,很多 ISP 將客戶的 port 25 封鎖,現在 port 25 主要用作 server to server 傳送郵件 --> - 465:透過 SSL 加密 - 587:透過 TLS 加密 <!--最新的標準是使用 port 587--> - 用途:寄送電子郵件 #### 一般寄信法 ```s= telnet localhost 25 # 使用 localhost,25 port 進行連線 # 220 squidxoxo-VirtualBox ESMTP Postfix 代表成功連線 helo gmail.com # helo 跟他說個嗨 誰? gmail.com # 250 squid-VirtualBox mail from: {username} # 信件來自誰 @ 前面的名稱 rcpt to: {收件人 email} # 信件給誰 完整的 email 名稱 data # 信件內容 Subject: Title # 標題 content # 內容 # 兩個換行 + 一個 . 結束這回合 # 離開 quit ``` ### 寄信過程 :::success **情境** - 秋分要寄信給魷魚 - 寄信人:chofinn@gmail.com - 收信人:squid@yahoo.com.tw ::: ![](https://i.imgur.com/5oNS0tT.png) - 步驟 0:寄件人取得 MTA 的使用權限,註冊一組可使用的帳號、密碼 - 步驟 1:寄件人透過 MUA 寫信,然後傳送至自己的 MTA 上 - 信件的主要資料 - 信件標頭:包括寄件者與收件者的 email 位址、信件的主旨等 - 信件內容:你要跟對方說明的內容 - 編寫完畢,按下傳送,這封信就會送至你的 MTA 伺服器上 - 注意:是你的 MTA 而不是對方的 MTA ! - 步驟 2-1:如果這封信的目的地是本機 MTA 使用者的帳號,MDA 就直接將這封信送到該使用者地 Mailbox - 步驟 2-2:如果這封信目的地為其他 MTA ,則開始 Relay - MTA 開始分析該封信是否具有合法的使用權限 - 有權限:MTA 將信寄給下一部 MTA 的 SMTP (port 25) - 步驟 3:收件方的 MTA 接收寄件方發出的信,並將信放到正確的使用者信箱中 - 收件方的 MTA 收到寄件方的信後,MDA 會將這封信送到魷魚的 Mailbox,等魷魚登入讀取、下載 <!-- :::info 這封信最後是停留在收件方 MTA 上,並不是在收件人 MUA 上! 所以 MUA 不必一直開著!要收信時再打開就可以 ::: --> ### 收信 - MRA (Mail Retrieval Agent) - 透過 POP3 或 IMAP 接收信件 <!-- - 包含在 mail server 中 --> - Protocol - POP3 (Post Office Protocol version 3) - port - 不安全:110 - 安全:995,透過 TLS/SSL 加密 - 從 mail server 下載所有郵件 - 郵件下載到電腦上後,mail server 就會將使用者 mailbox 內的資料刪除 - 已傳送的郵件不會存在 mail server 中,而是存在你的本機上 <!-- 所以只能使用同一台電腦來存取該郵件,如果用不同裝置存取電子郵件,你就看不到之前下載的信了~ --> - IMAP (Internet Message Access Protocol) - port - 不安全:143 - 安全:993,透過 TLS/SSL 加密 - 從 mail server 上讀取信件 - 讓你在任何地方、使用任何裝置,都能夠存取電子郵件 - 除非你點下載的按鈕,要不然不會自動下載信件 #### 一般收信法 ```s= # postfix 是透過 SMTP 的 MTA,沒有 POP3 及 IMAP,要另外安裝 dovecot 等 mail server telnet localhost 110 user {UserName} pass {明文寫的密碼} list # 列出收件匣內的信件,以編號顯示 retr [Num] # 選擇想要看的信件 ``` :::info - SMTP、POP3、IMAP 明文傳送 :point_right: 不安全 - 可能會因為 Man-in-the-middle 導致電子郵件內容改變 - 郵件偽造 <!-- 假裝自己是校長宣布現在開始放寒假--> - 病毒郵件 - 帳號密碼會被攔截 - 所以發展出加密的通訊協定 SMTPs、POP3s、IMAPs ::: ### 收信過程 :::success **情境** - 魷魚要收信 - 寄信人:chofinn@gmail.com - 收信人:squid@yahoo.com.tw ::: ![](https://i.imgur.com/5oNS0tT.png) - 步驟 1:MUA 透過 POP3(port 110)/IMAP(port 143) 協定連接到 MRA, 並且輸入帳號與密碼來取得正確的認證與授權 - 步驟 2:MRA 確認使用者帳號、密碼沒有問題後,會前往該使用者的 Mailbox (`/var/spool/mail/使用者帳號`) 取得使用者的信件並傳送到使用者的 MUA 軟體上 ## Postfix - 為了比 sendmail 更好而開發的 - sendmail 缺點:效能不理想、不夠安全、設定檔 `sendmail.cf` 難以理解 - MTA、SMTP server/client - 運行在 Unix-like OS 上 - 第二常用的 mail server(第一名是 Exim) - Exim 自訂性高,可以根據需求進行複雜的設定 - Postfix 新手友善,容易配置 - 設計目標: - 兼容性 - 基本 100% 相容 sendmail 的檔案 - 穩定性 - 在 overload 的情況下仍可保證程式的可靠性。當沒有足夠的空間或記憶體時,postfix 會選擇直接放棄,而不是重試 - 靈活性 - 由多個 module 組成,可依照需求安裝需要的 module - `apt search postfix | grep postfix` - 安全性 ### 安裝 - `sudo apt install postfix` - ![](https://i.imgur.com/Qo06pI6.png) - `No configuration` - `Internet site` 透過 SMTP 進行寄信,可以收外面的信,也可以收 local 內使用者互相寄的信 - 一般公司或單位用 - `Internet with smarthost` 要寄到外面的信件會經由其他 mail server 來轉發 - 想要透過外部 server(google.com 之類的)來幫忙寄信 - `Satellite system` local 內及外面的信件都由其他 mail server 轉發 - 跨國公司常用,假設主機在美國,可以透過其他地方的 Satellite System 來寄送 - `Local only` 只有主機內使用者可以互相寄信 - 一般主機、家用電腦、測試用 - `sudo dpkg-reconfigure postfix` 看詳細設定檔(直接看 main.cf 也可以) - `System mail name` 寄信出去時要顯示的名稱 - ![](https://i.imgur.com/A9AxLsj.png) - ![](https://i.imgur.com/DNmhv4y.png) - 寄到 root 跟 postmaster 的信件會被轉寄到哪個使用者 - ![](https://i.imgur.com/UuoArci.png) - 寄到主機內的信件是這些 domain 的才會接收 - ![](https://i.imgur.com/5RfAiBw.png) - 這些位址可以直接寄信給這台 mail server - Local address extension character - 表示忽略**符號**後的內容 - 可用於內部信件分流 > ta+squidxoxo@lsa.kija => ta@lsa.kija > ta+s110213518@lsa.kija => ta@lsa.kija - `netstat -ntupl | grep :25` 有看到 TCP 在聽 25 的話,恭喜你 postfix 安裝成功了 :::warning Postfix 重要檔案/目錄 - `/etc/postfix/main.cf` 主要設定檔 - `myorigin=/etc/mailname` 希望用在 **@** 後面的名稱 - 配置 SMTP 的 domain name - `myhostname` email 中 **@** 後面的部分,主機名稱 - 安裝 postfix 這台主機的名稱 - `inet_interfaces` SMTP 要聽的 IP 位址。**all** 代表聽所有的網路介面。如果有重複設定項目時,會以最晚出現的設定值為準 - `mydestination` 在許多的主機名稱中,寫入這個設定值的名稱才能被作為 email 的主機名稱 - DNS 的設定裡有 MX 的話,指向的那台主機一定要寫在 mydestiantion 裡面 - `mynetworks` 設定可以用這個 server 做轉發的用戶 - `relayhost` 設定將所有寄出的信件給某台 server 做轉發 - `/etc/mailname` 系統預設的 server name - `/etc/aliases` 信箱的別名,可以設成群組,一次寄給很多人 - `/var/mail/{userName}` 收件匣 - `/var/spool/postfix` 寄件 Queue ::: ### 利用 telnet 寄信 #### 群組寄信法 - 新增 alias 紀錄 ```s= sudo vim /etc/aliases jobs: {user_name}, {完整 email 地址}, root ``` - renew aliases.db & reload postfix ```s= sudo postalias hash:/etc/aliases # postalias 用來更新 db 的指令 後面是要更新的 db sudo service postfix reload ``` ### mbox vs. Maildir - mbox 是個檔案,如果掛掉了,所有信件都沒了,因此轉為 Maildir (目錄),單一郵件檔毀損的話也沒關係 - 先看 /var/mail 裡面,自己的信件紀錄 - 新增一個要拿來放 Maildir 的資料夾 - mbox to maildir `sudo apt install mb2md` - [不會用就看 man](https://www.unix.com/man-page/debian/1/MB2MD/) - `sudo mb2md -s /var/mail/squidxoxo -d /var/mail/squidxoxo-dir/` - 可以下載一個指令 `tree` 看資料夾下的東西更方便 ## 參考連結 - [email history](https://phrasee.co/blog/a-brief-history-of-email/) - [前人筆記 - Mail Server 介紹](https://hackmd.io/52PTLHqTRl-lusp9z9dlDQ?view) - [鳥哥 - 郵件伺服器: Postfix](https://linux.vbird.org/linux_server/centos6/0380mail.php#whatmail_dns) - [前人筆記 - Mail Server 實作](https://hackmd.io/HfnUzUQjSqeXF-u8t_CiYA?view) - [鳥哥 - 設定郵件伺服器(Mail Server)](https://linux.vbird.org/linux_server/redhat6.1/linux_25mailsendmail.php) - [What Is an Email Server? Definition, How It Works, Why You Need One](https://www.axigen.com/articles/what-is-an-email-server_107.html) - [Simple Mail Transfer Protocol (SMTP)](https://www.geeksforgeeks.org/simple-mail-transfer-protocol-smtp/) - [What is the difference between SMTP and SMTPS?](https://www.agari.com/blog/smtps-how-to-secure-smtp-with-ssl-tls-which-port-to-use) - [Introduction to POP3 & IMAP ](https://www.knownhost.com/kb/introduction-to-pop3-imap/) - [何謂 Mail Exchanger (MX) ?](http://channel.synnex.com.tw/asp/fae_qaDetail.asp?from_prg=&topic=FAE&group=&parent=&classifyid=01534&seqno=17451)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully