如何在服务器上搭建mail服务器邮件服务器

Ubuntu 邮件服务器部署指南

本文档详细介绍了在 Ubuntu 系统上部署完整邮件服务器的流程,包括安装步骤、配置方法以及使用说明。

目录

  1. 概述
  2. 准备工作
  3. 安装主要组件
  4. [配置 Postfix](#配置 Postfix)
  5. [配置 Dovecot](#配置 Dovecot)
  6. [配置 SpamAssassin 和 ClamAV](#配置 SpamAssassin 和 ClamAV)
  7. [配置 SSL/TLS](#配置 SSL/TLS)
  8. [DNS 记录配置](#DNS 记录配置)
  9. 用户管理
  10. 测试邮件服务器
  11. 使用方法
  12. 故障排除

概述

一个完整的邮件服务器通常由以下组件组成:

  • Postfix:SMTP 服务器,负责邮件的发送和接收
  • Dovecot:IMAP/POP3 服务器,允许用户通过邮件客户端访问邮件
  • SpamAssassin:垃圾邮件过滤系统
  • ClamAV:病毒扫描工具
  • Let's Encrypt:提供免费的 SSL/TLS 证书

准备工作

系统要求

  • Ubuntu 22.04 LTS 或更高版本
  • 固定的公网 IP 地址
  • 完全限定域名 (FQDN),如 mail.yourdomain.com
  • 打开的网络端口:25 (SMTP)、465 (SMTPS)、587 (Submission)、143 (IMAP)、993 (IMAPS)、110 (POP3)、995 (POP3S)

更新系统

bash 复制代码
sudo apt update
sudo apt upgrade -y

设置正确的主机名

bash 复制代码
sudo hostnamectl set-hostname mail.yourdomain.com

编辑 /etc/hosts 文件:

bash 复制代码
sudo nano /etc/hosts

添加以下内容:

复制代码
127.0.0.1 localhost
YOUR_SERVER_IP mail.yourdomain.com mail

安装主要组件

安装 Postfix

bash 复制代码
sudo apt install postfix -y

在安装过程中,选择"Internet Site",并输入你的域名(例如 yourdomain.com)。

安装 Dovecot

bash 复制代码
sudo apt install dovecot-core dovecot-imapd dovecot-pop3d dovecot-lmtpd -y

安装垃圾邮件和病毒防护

bash 复制代码
sudo apt install spamassassin spamc clamav clamav-daemon -y

安装邮件工具和 SSL 证书工具

bash 复制代码
sudo apt install mailutils certbot -y

配置 Postfix

编辑主配置文件

bash 复制代码
sudo nano /etc/postfix/main.cf

替换为以下配置(记得替换域名):

复制代码
# 基本设置
smtpd_banner = $myhostname ESMTP $mail_name
biff = no
append_dot_mydomain = no
readme_directory = no
compatibility_level = 2

# TLS 参数
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
smtpd_tls_security_level = may
smtp_tls_security_level = may
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_tls_protocols = !SSLv2, !SSLv3
smtp_tls_protocols = !SSLv2, !SSLv3

# 基本网络设置
myhostname = mail.yourdomain.com
myorigin = yourdomain.com
mydomain = yourdomain.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
inet_interfaces = all
inet_protocols = all

# 邮件传输设置
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/
mailbox_command = 
mailbox_size_limit = 0
recipient_delimiter = +
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases

# SMTP 身份验证设置
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

# 反垃圾邮件和病毒设置
smtpd_helo_required = yes
smtpd_helo_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_invalid_helo_hostname, reject_non_fqdn_helo_hostname
smtpd_sender_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_non_fqdn_sender, reject_unknown_sender_domain
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, defer_unauth_destination
disable_vrfy_command = yes

配置 Postfix 的子配置文件

bash 复制代码
sudo nano /etc/postfix/master.cf

在文件末尾添加或取消注释以下行:

复制代码
# 安全 SMTP(SMTPS)设置
smtps     inet  n       -       y       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_tls_auth_only=yes

# 提交端口(587)设置
submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_tls_auth_only=yes

启用 SpamAssassin 过滤

编辑 Postfix 配置以使用 SpamAssassin:

bash 复制代码
sudo nano /etc/postfix/master.cf

在文件末尾添加:

复制代码
# SpamAssassin 过滤
smtp      inet  n       -       y       -       -       smtpd
  -o content_filter=spamassassin
spamassassin unix -     n       n       -       -       pipe
  user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}

配置 Dovecot

编辑主配置文件

bash 复制代码
sudo nano /etc/dovecot/dovecot.conf

确保以下行存在:

复制代码
protocols = imap pop3 lmtp
listen = *, ::

配置 SSL 设置

bash 复制代码
sudo nano /etc/dovecot/conf.d/10-ssl.conf

修改以下设置:

复制代码
ssl = required
ssl_cert = </etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.yourdomain.com/privkey.pem
ssl_min_protocol = TLSv1.2

配置邮箱设置

bash 复制代码
sudo nano /etc/dovecot/conf.d/10-mail.conf

修改邮箱格式为 Maildir:

复制代码
mail_location = maildir:~/Maildir

配置用户认证

bash 复制代码
sudo nano /etc/dovecot/conf.d/10-auth.conf

修改以下内容:

复制代码
disable_plaintext_auth = yes
auth_mechanisms = plain login

取消注释并设置:

复制代码
!include auth-system.conf.ext

配置 LMTP

bash 复制代码
sudo nano /etc/dovecot/conf.d/20-lmtp.conf

确保含有:

复制代码
service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    mode = 0600
    user = postfix
    group = postfix
  }
}

配置与 Postfix 的认证

bash 复制代码
sudo nano /etc/dovecot/conf.d/10-master.conf

找到并编辑 service auth 部分:

复制代码
service auth {
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0666
    user = postfix
    group = postfix
  }
}

配置 SpamAssassin 和 ClamAV

启用 SpamAssassin

bash 复制代码
sudo nano /etc/default/spamassassin

修改以下设置:

复制代码
ENABLED=1
OPTIONS="--create-prefs --max-children 5 --helper-home-dir"

配置 SpamAssassin

bash 复制代码
sudo nano /etc/spamassassin/local.cf

添加或修改以下内容:

复制代码
rewrite_header Subject *****SPAM*****
report_safe 0
required_score 5.0
use_bayes 1
bayes_auto_learn 1

启用 ClamAV

bash 复制代码
sudo systemctl enable clamav-daemon
sudo systemctl start clamav-daemon

配置 SSL/TLS

使用 Let's Encrypt 获取 SSL 证书:

bash 复制代码
sudo certbot certonly --standalone -d mail.yourdomain.com

按照提示完成证书申请过程。

DNS 记录配置

在你的 DNS 提供商处添加以下记录:

MX 记录

复制代码
Type: MX
Host: @
Value: mail.yourdomain.com
Priority: 10

A 记录

复制代码
Type: A
Host: mail
Value: YOUR_SERVER_IP

SPF 记录

复制代码
Type: TXT
Host: @
Value: "v=spf1 mx a ip4:YOUR_SERVER_IP ~all"

DKIM 记录(需要额外配置 OpenDKIM,这里是基本设置)

安装 OpenDKIM:

bash 复制代码
sudo apt install opendkim opendkim-tools -y

配置 OpenDKIM(详细配置较复杂,需要单独设置)。

DMARC 记录

复制代码
Type: TXT
Host: _dmarc
Value: "v=DMARC1; p=none; sp=none; rua=mailto:[email protected]"

用户管理

创建邮箱用户

添加系统用户并设置邮箱目录:

bash 复制代码
sudo useradd -m -s /bin/false username
sudo passwd username
sudo mkdir -p /home/username/Maildir/{new,cur,tmp}
sudo chown -R username:username /home/username/Maildir
sudo chmod -R 700 /home/username/Maildir

启动和启用服务

bash 复制代码
sudo systemctl restart postfix
sudo systemctl restart dovecot
sudo systemctl restart spamassassin
sudo systemctl enable postfix
sudo systemctl enable dovecot
sudo systemctl enable spamassassin

测试邮件服务器

测试本地邮件发送

bash 复制代码
echo "This is a test email" | mail -s "Test Email" [email protected]

检查日志

bash 复制代码
sudo tail -f /var/log/mail.log

使用方法

邮件客户端配置

以下是用户在邮件客户端中配置账户的参数:

收件服务器 (IMAP)

  • 服务器:mail.yourdomain.com
  • 端口:993 (SSL/TLS)
  • 用户名:用户名或完整邮箱地址
  • 密码:用户密码
  • 安全类型:SSL/TLS

发件服务器 (SMTP)

  • 服务器:mail.yourdomain.com
  • 端口:587 (STARTTLS) 或 465 (SSL)
  • 用户名:用户名或完整邮箱地址
  • 密码:用户密码
  • 安全类型:STARTTLS 或 SSL/TLS
  • 身份验证:需要认证

Webmail 客户端(可选)

为了提供网页版邮箱界面,可以安装 Roundcube:

bash 复制代码
sudo apt install apache2 php php-xml php-mbstring php-intl php-mysql mariadb-server -y
sudo apt install roundcube roundcube-mysql -y

设置 Apache 虚拟主机和配置 Roundcube 数据库需要额外的配置步骤。

故障排除

常见问题

  1. 邮件发送失败

    • 检查 /var/log/mail.log
    • 确保端口 25、465、587 已开放
    • 检查 DNS 记录配置
  2. 无法接收邮件

    • 检查 MX 记录配置
    • 检查防火墙设置
    • 确认域名 DNS 解析正常
  3. 身份验证失败

    • 检查用户名和密码
    • 检查 Dovecot 认证配置
    • 查看 Dovecot 日志
  4. SSL 证书错误

    • 确保证书路径正确
    • 确保证书未过期
    • 检查证书权限

有用的调试命令

bash 复制代码
# 测试 SMTP 连接
telnet mail.yourdomain.com 25

# 测试 IMAP 连接
openssl s_client -connect mail.yourdomain.com:993

# 测试邮件队列
mailq

# 检查 Postfix 配置语法
sudo postfix check

# 检查 SpamAssassin 规则
sudo spamassassin --lint

# 查看邮件服务器状态
sudo systemctl status postfix dovecot spamassassin clamav-daemon

维护

自动更新 SSL 证书

添加 crontab 任务以自动更新证书:

bash 复制代码
sudo crontab -e

添加以下内容:

复制代码
0 3 * * * certbot renew --quiet && systemctl restart postfix dovecot

备份配置

定期备份关键配置文件:

bash 复制代码
sudo cp -r /etc/postfix /backup/postfix
sudo cp -r /etc/dovecot /backup/dovecot
sudo cp -r /etc/letsencrypt /backup/letsencrypt

监控邮件服务器

安装监控工具(如 Monit):

bash 复制代码
sudo apt install monit -y

配置 Monit 监控邮件服务:

bash 复制代码
sudo nano /etc/monit/conf.d/mail

添加以下内容:

复制代码
check process postfix with pidfile /var/spool/postfix/pid/master.pid
  start program = "/etc/init.d/postfix start"
  stop program = "/etc/init.d/postfix stop"
  if failed port 25 protocol smtp then restart

check process dovecot with pidfile /var/run/dovecot/master.pid
  start program = "/etc/init.d/dovecot start"
  stop program = "/etc/init.d/dovecot stop"
  if failed port 993 type tcpssl protocol imap then restart

文章创作者风车 https://xoxome.online

关于搭建好以后的操作

手动操作:

登录到user1

复制代码
`su - user1` 

查看邮件

复制代码
mutt

测试发送

复制代码
echo "这是发送到 [email protected] 的测试邮件" | mail -s "测试 sales 邮箱" [email protected]

添加自定义邮箱

要添加更多邮箱地址,你需要修改虚拟邮箱映射文件。以下是添加新邮箱的步骤:

1. 添加新虚拟邮箱映射

bash 复制代码
# 编辑虚拟邮箱映射文件
nano /etc/postfix/virtual

在文件中添加新的邮箱映射,例如:

复制代码
# 现有邮箱
[email protected] user1
[email protected] user1

# 添加新邮箱
[email protected] user1
[email protected] user1
[email protected] user1

保存并退出(按 Ctrl+O,然后 Enter,再按 Ctrl+X)

2. 更新虚拟邮箱数据库

bash 复制代码
# 更新数据库文件
postmap /etc/postfix/virtual

3. 重新加载 Postfix 配置

bash 复制代码
# 重新加载配置
systemctl reload postfix

4. 创建不同的用户(可选)

如果想将不同的邮箱分配给不同的用户账户,首先创建新用户:

bash 复制代码
# 创建新用户 user2
useradd -m -s /bin/bash user2
echo "user2:密码" | chpasswd

# 创建邮箱目录
mkdir -p /home/user2/Maildir/{cur,new,tmp}
chmod -R 700 /home/user2/Maildir
chown -R user2:user2 /home/user2/Maildir

然后在虚拟邮箱映射中指定新用户:

bash 复制代码
# 编辑虚拟邮箱文件
nano /etc/postfix/virtual

添加:

复制代码
# user1 的邮箱
[email protected] user1
[email protected] user1

# user2 的邮箱
[email protected] user2
[email protected] user2

然后更新数据库并重新加载:

bash 复制代码
postmap /etc/postfix/virtual
systemctl reload postfix

5. 创建邮件别名(可选)

如果想让某个邮箱地址转发到另一个邮箱:

bash 复制代码
# 编辑别名文件
nano /etc/aliases

添加:

复制代码
# 将 webmaster 转发到 admin
webmaster: [email protected]

保存后更新别名数据库:

bash 复制代码
newaliases

6. 创建邮件列表(可选)

如果需要发送到多个收件人的邮件列表:

bash 复制代码
# 编辑别名文件
nano /etc/aliases

添加:

复制代码
# 团队邮件列表
team: [email protected], [email protected], [email protected]

更新别名数据库:

bash 复制代码
newaliases

7. 测试新邮箱

可以发送测试邮件到新添加的地址:

bash 复制代码
echo "测试新邮箱" | mail -s "测试" [email protected]

8. 使用域别名添加更多域(可选)

如果你有多个域名想使用相同的邮箱配置:

bash 复制代码
# 编辑 Postfix 配置
nano /etc/postfix/main.cf

添加:

复制代码
# 虚拟域别名
virtual_alias_domains = xmyy.shop another-domain.com third-domain.net

然后在虚拟邮箱映射中添加其他域的邮箱:

bash 复制代码
# 编辑虚拟邮箱文件
nano /etc/postfix/virtual

添加:

复制代码
# xmyy.shop 邮箱
[email protected] user1

# another-domain.com 邮箱
[email protected] user1

别忘了更新数据库并重启:

bash 复制代码
postmap /etc/postfix/virtual
systemctl reload postfix

9. 添加邮箱后查看状态

可以通过查看邮件日志来确认新邮箱是否正常工作:

bash 复制代码
tail -f /var/log/mail.log

现在你应该能够使用任何添加的新邮箱地址收发邮件了。

你提到的要查看 [email protected] 的邮件(注意你的拼写是 [email protected],我假设是 [email protected])。根据你前面的配置,这个邮箱应该映射到了某个系统用户(可能是 user1 或其他用户)。

查看 [email protected] 邮件的步骤:

1. 首先确认这个邮箱映射到哪个用户
bash 复制代码
# 查看虚拟邮箱映射
cat /etc/postfix/virtual | grep sales

假设结果显示 [email protected] user1,则表示这个邮箱映射到了 user1 用户。

2. 以该用户身份查看邮件

如果邮箱映射到 user1:

bash 复制代码
# 切换到 user1 用户
su - user1

# 使用 mail 命令查看
mail

如果邮箱映射到 user2 或其他用户,则相应切换到该用户。

3. 使用 mutt 查看(推荐,支持中文显示)
bash 复制代码
# 安装 mutt(如果尚未安装)
apt install mutt -y

# 切换到对应用户
su - user1  # 或 user2,取决于邮箱映射

# 启动 mutt
mutt

在 mutt 中,你可以通过上下箭头键选择邮件,按回车键查看内容。

4. 直接查看邮件文件
bash 复制代码
# 切换到对应用户
su - user1  # 或 user2,取决于邮箱映射

# 列出所有新邮件
ls -l ~/Maildir/new/

# 查看具体邮件内容(替换为实际文件名)
cat ~/Maildir/new/1650123456.V801I123456.mail.xmyy.shop
5. 使用之前创建的脚本查看最新邮件

如果你之前按照我的建议创建了 latest_email.sh 脚本:

bash 复制代码
# 切换到对应用户
su - user1  # 或 user2,取决于邮箱映射

# 运行脚本查看最新邮件
~/latest_email.sh
6. 过滤特定主题的邮件

如果你想查找特定主题的邮件:

bash 复制代码
# 切换到对应用户
su - user1  # 或 user2,取决于邮箱映射

# 搜索包含"测试"的邮件
find ~/Maildir -type f -exec grep -l "测试" {} \; | xargs cat
7. 检查邮件是否发送到此地址

可以查看邮件日志,确认是否有发送到 [email protected] 的记录:

bash 复制代码
grep "[email protected]" /var/log/mail.log
8. 发送测试邮件到这个地址

为了测试邮箱是否工作正常,可以发送一封测试邮件:

bash 复制代码
echo "这是发送到 [email protected] 的测试邮件" | mail -s "测试 sales 邮箱" [email protected]

发送后再使用上述方法查看邮件。

如果你发现查看不到邮件,请确保:

  1. 邮箱已正确添加到虚拟映射文件
  2. 已运行 postmap /etc/postfix/virtual 更新数据库
  3. 已重新加载 Postfix 配置 systemctl reload postfix
  4. 邮件服务运行正常 systemctl status postfix dovecot
相关推荐
liyongjun631642 分钟前
CentOS 下 Zookeeper 常用命令与完整命令列表
linux·服务器·zookeeper·centos
facaixxx20242 小时前
什么是巨型帧Jumbo Frames?云服务器开启巨型帧有什么用?
运维·服务器
巨可爱熊4 小时前
高并发内存池(定长内存池基础)
linux·运维·服务器·c++·算法
zkmall4 小时前
ZKmall开源商城静态资源管理:Nginx 配置与优化
运维·nginx·开源
yangang1856 小时前
linuxbash原理
linux·运维·服务器
一一Null7 小时前
关于手机取证中逻辑采集与系统备份的差异
服务器·网络·智能手机
小度爱学习7 小时前
linux中的执行命令格式及命令帮助
linux·运维·chrome
猿小喵7 小时前
记录一次TDSQL网关夯住故障
运维·数据库·mysql
独行soc8 小时前
2025年常见渗透测试面试题-红队面试宝典下(题目+回答)
linux·运维·服务器·前端·面试·职场和发展·csrf