首页 > 系统相关 >Ubuntu Linux 搭建邮件服务器(postfix + dovecot)

Ubuntu Linux 搭建邮件服务器(postfix + dovecot)

时间:2024-08-07 21:27:15浏览次数:10  
标签:tls mail postfix sudo server Ubuntu key smtpd dovecot

准备工作

1. 一台公网服务器(需要不被服务商限制发件收件的,也就是端口25、110、143、465、587、993、995不被限制),如有防火墙或安全组需要把这些端口开放

2. 一个域名,最好是com cn org的一级域名

3. 域名备案(如果服务器是国外的则不需要备案)

一、配置域名解析

1. 登录阿里云,找到云解析DNS

选择域名,如abc.com

①. 添加MX记录

- 记录类型:MX

- 主机记录:@

- 记录值:mail.abc.com

- MX优先级:1

②添加A记录

- 记录类型:A

- 主机记录:mail

- 记录值:服务器IP

- TTL:10分钟

③添加SPF记录

- 记录类型:TXT

- 主机记录:@

- 记录值:v=spf1 mx:mail.abc.com ip4:服务器IP -all

- TTL:10分钟

最后完成如下图

2. 安装并配置Postfix

apt-get update
sudo apt install -y postfix

安装过程中有选项需要选择,先选择1(No configuration),等下一步再单独配置

安装完成,输入下面命令配置Postfix

sudo dpkg-reconfigure postfix

将显示配置界面,选择 Internet Site ,然后分别输入或者选择以下值

  •  mail.abc.com
  • steve
  • mail.abc.com, abc.com, localhost.localdomain, localhost
  • no
  • 127.0.0.0/8
  • 0
  • +
  • ipv4

配置邮箱目录

sudo postconf -e 'home_mailbox = Maildir/'

使用 SASL(Dovecot SASL)配置 Postfix 的 SMTP-AUTH,在终端提示符下运行这些命令

sudo postconf -e 'smtpd_sasl_type = dovecot'
sudo postconf -e 'smtpd_sasl_path = private/auth'
sudo postconf -e 'smtpd_sasl_local_domain = $myhostname'
sudo postconf -e 'smtpd_sasl_security_options = noanonymous,noplaintext'
sudo postconf -e 'smtpd_sasl_tls_security_options = noanonymous'
sudo postconf -e 'broken_sasl_auth_clients = yes'
sudo postconf -e 'smtpd_sasl_auth_enable = yes'
sudo postconf -e 'smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination'

配置TLS(使用自签名证书)

[root@mail ~]# cd
 
# 生成私钥
[root@mail ~]# openssl genrsa -des3 -out server.key 2048
Enter pass phrase:    #输入密码
Verifying - Enter pass phrase:  #重复输入密码
 
#下面这个步骤是删除server.key中的密码
[root@mail ~]# openssl rsa -in server.key -out server.key.insecure
Enter pass phrase for server.key:  #输入刚才设置的密码
writing RSA key

#重命名证书
[root@mail ~]# mv server.key server.key.secure
[root@mail ~]# mv server.key.insecure server.key

# 生成CSR(证书签名请求)
[root@mail ~]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key: #输入刚才设置的密码
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN #国家简称
State or Province Name (full name) []:Beijing #省
Locality Name (eg, city) [Default City]:Beijing  #城市
Organization Name (eg, company) [Default Company Ltd]:OPS  #公司名
Organizational Unit Name (eg, section) []:OPS   #部门名
Common Name (eg, your name or your server's hostname) []:mail.abc.com
Email Address []:[email protected]  
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:  #直接按Enter
An optional company name []:  #直接按Enter

#生成自签名证书
[root@mail ~]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt
Enter pass phrase for server.key:  #直接按Enter
Certificate request self-signature ok
subject=C = CN, ST = Beijing, L = Beijing, O = OPS, OU = OPS, CN = mail.abc.com, emailAddress = [email protected]
Getting Private key

#设置权限
[root@mail ~]# chmod 400 server.*

最后把证书拷贝到系统证书目录

sudo cp server.crt /etc/ssl/certs
sudo cp server.key /etc/ssl/private

拥有证书后,配置 Postfix,在终端提示符下运行这些命令:

sudo postconf -e 'smtp_tls_security_level = may'
sudo postconf -e 'smtpd_tls_security_level = may'
sudo postconf -e 'smtp_tls_note_starttls_offer = yes'
sudo postconf -e 'smtpd_tls_key_file = /etc/ssl/private/server.key'
sudo postconf -e 'smtpd_tls_cert_file = /etc/ssl/certs/server.crt'
sudo postconf -e 'smtpd_tls_loglevel = 1'
sudo postconf -e 'smtpd_tls_received_header = yes'
sudo postconf -e 'smtpd_tls_auth_only = yes'

最后查看配置 vim /etc/postfix/main.cf 后面部分如下即可

# TLS parameters
smtpd_tls_cert_file = /etc/ssl/certs/server.crt
smtpd_tls_key_file = /etc/ssl/private/server.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 = mail.abc.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = mail.abc.com, abc.com, localhost.localdomain, localhost
relayhost =
mynetworks = 127.0.0.0/8
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = ipv4
home_mailbox = Maildir/
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous,noplaintext
smtpd_sasl_tls_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_sasl_auth_enable = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
smtp_tls_note_starttls_offer = yes
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_auth_only = yes

配置SMTPS ==> vim /etc/postfix/master.cf 

smtps部分去掉注释

smtps     inet  n       -       -       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

3. 安装并配置Dovecot

sudo apt install -y dovecot-core dovecot-imapd dovecot-pop3d

 安装完成配置信息

vim /etc/dovecot/conf.d/10-auth.conf
auth_mechanisms = plain  改成  auth_mechanisms = plain login 


vim /etc/dovecot/conf.d/10-master.conf
改成如下
service auth {
  # auth_socket_path points to this userdb socket by default. It's typically
  # used by dovecot-lda, doveadm, possibly imap process, etc. Its default
  # permissions make it readable only by root, but you may need to relax these
  # permissions. Users that have access to this socket are able to get a list
  # of all usernames and get results of everyone's userdb lookups.
  unix_listener auth-userdb {
    #mode = 0600
    #user = 
    #group = 
  }
    
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
 }
vim /etc/dovecot/dovecot.conf

listen = *, ::

#!include conf.d/*.conf

!include conf.d/10-auth.conf
!include conf.d/10-master.conf

ssl = no
disable_plaintext_auth = no
mail_location = maildir:~/Maildir

4. 启动邮箱服务并新增邮箱用户

sudo systemctl start postfix
sudo systemctl start dovecot
#重启命令:
sudo systemctl restart postfix
sudo systemctl restart dovecot

#======新建邮箱用户====== useradd -m 用户名 passwd 用户名 #输入密码 #确认密码 #======验证账户====== sudo doveadm auth test 用户名

5. 安装mailutils测试发件

sudo apt install -y mailutils

#======测试发件======
echo '搭建邮件服务器成功啦!!!' | mail -s '通知!' [email protected]

6. 安装系统日志

sudo yum install -y rsyslog
sudo systemctl enable rsyslog
sudo systemctl start rsyslog
后续查看邮件服务日志方式
sudo tail -f /var/log/maillog

 

二、使用Foxmail收发邮件

①windows10版本的Foxmail 新建账号完成后闪退,所以先设置下:

首先找到Foxmail快捷方式,右键属性。
找到兼容性,并且在下方找到“以管理员身份运行此程序”,勾选,确定

②打开Foxmail,新建账号 => 其它邮箱 => 手动设置 => POP3

邮件账号:账号@abc.com

密码:xxxxxx

POP服务器:mail.abc.com    SSL 110(不勾选,如果想勾选走995端口,则需要配置Dovecot的SSL信息,可自行研究)

SMTP服务器:mail.abc.com  SSL 465(勾选)

 注:虽然发件服务(SMTP)启用了SSL走的端口是465,但是实测25端口还得开放,不然无法接收外部邮件

 

 

 

 

 

参考:https://ubuntu.com/server/docs/install-and-configure-postfix#smtps

 

标签:tls,mail,postfix,sudo,server,Ubuntu,key,smtpd,dovecot
From: https://www.cnblogs.com/007sx/p/18347813

相关文章

  • Ubuntu 安装 mysql
    Ubuntu安装mysql参考教程:Ubuntu18.04安装MySQL1.安装MySQL在Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get。(base)simulate@simulate-VirtualBox:~$sudoapt-getupdate(base)simulate@simu......
  • Ubuntu系统Redis无法启动的问题排查
    作者:逍遥Sean简介:一个主修Java的Web网站\游戏服务器后端开发者主页:https://blog.csdn.net/Ureliable觉得博主文章不错的话,可以三连支持一下~如有疑问和建议,请私信或评论留言!Redis在Ubuntu系统上无法启动1.检查Redis状态和日志2.检查配置文件3.查看Redis端口是......
  • Ubuntu防火墙相关命令
    在Ubuntu系统中,启用防火墙可以通过ufw(UncomplicatedFirewall)来完成。以下是如何启用和配置ufw的步骤:1.安装ufw(如果尚未安装)sudoaptupdatesudoaptinstallufw2Ubuntu启用防火墙ufw:sudoufwenable3.Ubuntu检查防火墙状态sudoufwstatus4.设置默认策略(可选,......
  • Ubuntu 24.04 LTS Linux上安装Azure Data Studio
    AzureDataStudio是由Microsoft开发的开源数据库管理和开发工具。它是一种跨平台数据库管理工具,可在所有流行的操作系统(Windows、macOS和Linux)上运行。该软件提供了一个现代编辑器和丰富的界面,用于管理各种数据库系统,例如MicrosoftSQLServer、PostgreSQL等。它还为......
  • Ubuntu 24.04 LTS Noble安装OpenSSH服务器
    OpenSSH服务器在 UbuntuLinux上提供安全外壳(SSH)协议,以便远程管理系统,同时提供高级别的加密,确保安全。虽然许多Linux系统默认配备OpenSSH服务器,但在Ubuntu24.04上,我们必须手动安装它。因此,在本教程中,我们将介绍在Ubuntu24.04系统上安装和配置OpenSSH服务......
  • ubuntu22.04桌面版安装linux搜狗输入法
    本文参考搜狗输入法官方教程,并针对ubuntu22.04做了一些调整https://pinyin.sogou.com/linux/help.php1.安装fcitx#查看fcitx版本aptinfofcitx|grepVersionWARNING:aptdoesnothaveastableCLIinterface.Usewithcautioninscripts.Version:1:4.2.9.8-5#......
  • ubuntu安装
    环境虚拟机:VMwaveWorkstation16.1.216.1.2是完美版,之后的版本,官方允许磁盘映射一次,再映射就报错了。VM17版本已经砍掉了虚拟磁盘映射功能。Linux:Ubuntu16.04虚拟机创建除特殊说明,其他默认直接下一步1.新建虚拟机-选择自定义配置2.选择稍后安装,先不加载镜......
  • Jetson Orin nano 安装ubuntu22.04
    最近项目需要给底盘部署建图以及定位导航,底盘用的是Jetsonorinnano,用的是Ros2humble版本的机器人操作系统,由于humble版本的系统只支持ubuntu22.04,所以需要在orinnano上重新烧录ubuntu22.04,但是这个版本的系统跟之前的有些不一样,也踩了很多坑,查阅了官网的相关资料后,已经......
  • Nessus Professional 10.8.0 Auto Installer for Ubuntu 24.04
    NessusProfessional10.8.0AutoInstallerforUbuntu24.04发布Nessus试用版自动化安装程序,支持macOSSonoma、RHEL9和Ubuntu24.04请访问原文链接:https://sysin.org/blog/nessus-auto-install-for-ubuntu/,查看最新版。原创作品,转载请保留出处。Nessus简介Nessus......
  • Typecho在Ubuntu 22.04上的安装部署
    安装Nginx并配置访问安装PHP并输出脚本结果配置typechoNginx安装并验证aptinstallnginxsystemctlstartnginx正常情况应该可以看到Nginx的欢迎页面了,如果看不到就是防火墙的问题,设置下防火墙放通即可。安装PHP并使用Nginx代理aptinstallphp-fpmphp-curlphp-gd......