温馨提示×

如何配置ubuntu邮件服务器

小樊
34
2026-09-26 05:19:16
栏目: 云计算

下面给你一份从零在 Ubuntu 上搭建邮件服务器的实用指南,偏向可对外收发邮件的中小型服务器(Postfix + Dovecot + 域名配置)。如果你只是想本机发信(如 cron 通知),我也会在最后说明更简单的方式。


一、准备工作

1. 服务器要求

  • Ubuntu 20.04 / 22.04 / 24.04
  • 有公网 IP
  • 拥有一个域名(如 example.com)
  • 能修改 DNS 解析
  • 端口 25 / 465 / 587 / 110 / 143 / 993 / 995 未被封

2. 设置主机名

sudo hostnamectl set-hostname mail.example.com

3. DNS 配置(非常重要)

在域名解析中添加:

类型 主机名 值
A mail 服务器IP
MX @ mail.example.com
TXT @ v=spf1 mx ~all
TXT _dmarc v=DMARC1; p=none
TXT mail 用于 DKIM(稍后生成)

二、安装邮件服务组件

sudo apt update
sudo apt install postfix dovecot-core dovecot-imapd dovecot-pop3d \
openssl mailutils -y

Postfix 安装时选择:

  • Internet Site
  • System mail name:example.com

三、配置 Postfix(SMTP)

1. 主配置文件

编辑:

sudo nano /etc/postfix/main.cf

关键配置:

myhostname = mail.example.com
mydomain = example.com
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain

# SMTP 认证
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_tls_auth_only = yes

# TLS
smtpd_tls_cert_file = /etc/ssl/certs/mail.crt
smtpd_tls_key_file = /etc/ssl/private/mail.key
smtpd_use_tls = yes

# 限制
smtpd_recipient_restrictions =
    permit_sasl_authenticated,
    permit_mynetworks,
    reject_unauth_destination

四、配置 Dovecot(IMAP / POP3)

1. 启用协议

sudo nano /etc/dovecot/dovecot.conf
protocols = imap pop3

2. 邮件存储位置

sudo nano /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir

3. 认证配置

sudo nano /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login

4. SASL 与 Postfix 对接

sudo nano /etc/dovecot/conf.d/10-master.conf
service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

5. SSL(推荐)

sudo nano /etc/dovecot/conf.d/10-ssl.conf
ssl = required
ssl_cert = </etc/ssl/certs/mail.crt
ssl_key = </etc/ssl/private/mail.key

五、创建用户邮箱

sudo adduser user1

用户邮箱目录:

/home/user1/Maildir

六、SSL 证书(自签或 Let’s Encrypt)

Let’s Encrypt(推荐)

sudo apt install certbot -y
sudo certbot certonly --standalone -d mail.example.com

然后修改:

smtpd_tls_cert_file = /etc/letsencrypt/live/mail.example.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.example.com/privkey.pem

七、DKIM 配置(防垃圾邮件)

sudo apt install opendkim opendkim-tools

生成密钥并配置 TXT 记录(可单独说明)


八、启动服务

sudo systemctl restart postfix dovecot
sudo systemctl enable postfix dovecot

测试:

echo "test" | mail -s "hello" user1@example.com

九、仅本机发信(最简方案)

如果你不需要收信:

sudo apt install postfix

选择 Local only

或:

sudo apt install ssmpt

十、常见问题

  • 收不到外网邮件 → 检查 MX、25 端口
  • 被 Gmail 拒收 → 缺 SPF / DKIM / DMARC
  • 认证失败 → 检查 Dovecot SASL

如果你愿意,我可以:

  • 给你 一步一步命令版
  • 帮你 配置 DKIM + DMARC
  • 或只做 内部邮件服务器

你打算用在什么场景?

0 踩