部署邮件服务器
概要
某高校组建了校园网,现需要在校园网中部署一台电子邮件服务器,用于发送公文和工作交流。
在诸多早期的Linux系统中,默认使用的发件服务是由Sendmail服务程序提供的,而在RHEL 7系统中已经替换为Postfix服务程序。相较于Sendmail服务程序,Postfix服务程序减少了很多不必要的配置步骤,而且在稳定性、并发性方面也有很大改进。利用基于Linux平台的Postfix邮件服务器既能满足需要,又能节省资金。
在完成该项目之前,首先应当规划好电子邮件服务器的存放位置、所属网段、IP地址、域名等信息;其次,要确定每个用户的用户名,以便为其创建账号等。并且如果想更好地检验电子邮件系统的配置效果,需要先部署bind服务程序,为电子邮件服务器和客户端提供DNS域名解析服务。
这是因为一般而言,信箱地址通常是类似于root@hnsw.com这种形式,也就是按照“用户名@主机地址(域名)”格式来规范的。如果你收到一串“root@192.168.1.100”的信息,可能摸不着头脑,没准会将它当作SSH协议的连接信息。因此,先部署好bind服务程序是必要的。
实验仪器设备(实验环境):
VMware Workstation 虚拟机;
redhat 7 Linux操作系统
部署基础的电子邮件系统
一个最基础的电子邮件系统肯定要能提供发件服务和收件服务,为此需要使用基于SMTP的Postfix服务程序提供发件服务功能,并使用基于POP3协议的Dovecot服务程序提供收件服务功能。这样一来,用户就可以使用Outlook Express或Foxmail等客户端服务程序正常收发邮件了。电子邮件系统的工作流程如图所示。
实验步骤
配置域名解析服务
第1步:设置网卡类型为桥接模式
第2步:手动设置IP地址,注意要与VMnet1处理同一网段,并定义DNS服务器为本机
[root@localhost 桌面]# nmtui
第3步:配置域名解析服务
方法一:连接网卡eno16777736, 配置hostname,配置dnsmasq,在hosts文件中增加本地IP<==>域名映射。
//Dnsmasq为小型网络提供网络基础设施:DNS,DHCP,路由器通告和网络引导。它被设计为轻量级且占用空间小,适用于资源受限的路由器和防火墙。
[root@localhost 桌面]# vim /etc/hostname
mail.hnsw01.com
[root@localhost 桌面]# hostname
mail.hnsw01.com
[root@mail 桌面]# vim /etc/hosts
[root@mail 桌面]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.10 mail.hnsw01.com
[root@mail 桌面]# systemctl start dnsmasq
[root@mail 桌面]# systemctl enable dnsmasq.service
ln -s '/usr/lib/systemd/system/dnsmasq.service' '/etc/systemd/system/multi-user.target.wants/dnsmasq.service'
[root@mail 桌面]# systemctl list-unit-files | grep dns
dnsmasq.service enabled
[root@mail 桌面]# systemctl stop firewalld.service
若使用了方法一配置好了域名解析,方法二部分配置可以忽略。直接跳转至配置Postfix服务程序部分。
方法二:连接网卡eno16777736,配置服务器主机名称,使得服务器主机名称与发信域名保持一致。并搭建yum软件仓库,安装bind程序
[root@localhost 桌面]# nmcli device connect eno16777736
Device 'eno16777736' successfully activated with '4ca43ec3-3017-4700-816b-ca5fbf7bf260'.
[root@localhost 桌面]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fea5:64e8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:a5:64:e8 txqueuelen 1000 (Ethernet)
RX packets 266 bytes 30277 (29.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 338 bytes 33556 (32.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 139 bytes 12080 (11.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 139 bytes 12080 (11.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost 桌面]# vim /etc/hostname
mail.hnsw01.com
[root@localhost 桌面]# hostname
mail.hnsw01.com
[root@localhost 桌面]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 140K 1.9G 1% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 146M 351M 30% /boot
/dev/sr0 3.7G 3.7G 0 100% /run/media/root/RHEL-7.1 Server.x86_64
[root@localhost 桌面]# mkdir /mnt/cdrom && mount /dev/sr0 /mnt/cdrom
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost 桌面]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/rhel-root 18G 3.0G 15G 17% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 140K 1.9G 1% /dev/shm
tmpfs 1.9G 8.9M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 497M 146M 351M 30% /boot
/dev/sr0 3.7G 3.7G 0 100% /mnt/cdrom
[root@localhost 桌面]# vim /etc/yum.repos.d/a.repo
[root@localhost 桌面]# cat /etc/yum.repos.d/a.repo
[a] \\仓库名称:具有唯一性的标识名称,不应与其他软件仓库发生冲突。
name=a \\描述信息(name):可以是一些介绍性的词,易于识别软件仓库的用处。
baseurl=file:///mnt/cdrom \\仓库位置(baseurl):软件包的获取方式,可以使用FTP或HTTP下载,也可以是本地的文件(需要在后面添加file参数)。
enable=1 \\ 是否启用(enabled):设置此源是否可用;1为可用,0为禁用。
gpgcheck=0 \\是否校验(gpgcheck):设置此源是否校验文件;1为校验,0为不校验。
\\公钥位置(gpgkey):若上面的参数开启了校验功能,则此处为公钥文件位置。若没有开启,则省略不写。
[root@localhost 桌面]# yum repolist
已加载插件:langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
a | 4.1 kB 00:00:00
(1/2): a/group_gz | 134 kB 00:00:00
(2/2): a/primary_db | 3.4 MB 00:00:00
源标识 源名称 状态
a a 4,371
repolist: 4,371
[root@localhost 桌面]# yum install -y bind*
[root@localhost 桌面]# vim /etc/named.conf
[root@localhost 桌面]# cat /etc/named.conf
//
// named.conf
//
// Provided by Red Hat bind package to configure the ISC BIND named(8) DNS
// server as a caching only nameserver (as a localhost DNS resolver only).
//
// See /usr/share/doc/bind*/sample/ for example named configuration files.
//
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
……省略部分无关信息
正向解析实现
[root@localhost 桌面]# vim /etc/named.rfc1912.zones
[root@localhost 桌面]# tail -n5 /etc/named.rfc1912.zones
zone "hnsw01.com" IN {
type master;
file "hnsw01.com.zone";
allow-update { none; };
};
[root@localhost 桌面]# cp -a /var/named/named.localhost /var/named/hnsw01.com.zone
[root@localhost 桌面]# cd /var/named/
[root@localhost named]# vim hnsw01.com.zone
[root@localhost named]# cat hnsw01.com.zone
$TTL 1D
@ IN SOA hnsw01.com. root.hnsw01.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns.hnsw01.com.
ns IN A 192.168.1.10
@ IN MX 5 mail.hnsw01.com.
mail IN A 192.168.1.10
[root@localhost named]# named-checkconf // 检查主配置文件语法或参数的错误。
[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl enable named
对主机名执行ping命令,若能ping通,并获得主机名对应的IP地址,则证明上述操作全部正确。
[root@localhost named]# ping mail.hnsw01.com -c4
PING mail.hnsw01.com (192.168.1.10) 56(84) bytes of data.
64 bytes from 192.168.1.10: icmp_seq=1 ttl=64 time=0.008 ms
64 bytes from 192.168.1.10: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 192.168.1.10: icmp_seq=3 ttl=64 time=0.030 ms
64 bytes from 192.168.1.10: icmp_seq=4 ttl=64 time=0.047 ms
--- mail.hnsw01.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3001ms
rtt min/avg/max/mdev = 0.008/0.030/0.047/0.015 ms
最后把DNS协议加入到firewalld防火墙的允许列表中。
[root@localhost named]# firewall-config
配置Postfix服务程序
红帽7中postfix程序是默认安装好了的,其主配置文件路径为/etc/ postfix/main.cf,内容很多,但大多数是注释,我们可以用grep工具过滤出来产生实际效果的参数。
[root@localhost ~]# yum list | grep postfix
postfix.x86_64 2:2.10.1-6.el7 @anaconda/7.1
[root@localhost ~]# grep "#" -v /etc/postfix/main.cf | grep ^$ -v
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
inet_interfaces = localhost
inet_protocols = all
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.10.1/samples
readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
Postfix服务程序主配置文件中的重要参数
参数 | 作用 |
---|---|
myhostname | 邮局系统的主机名 |
mydomain | 邮局系统的域名 |
myorigin | 从本机发出邮件的域名名称 |
inet_interfaces | 监听的网卡接口 |
mydestination | 可接收邮件的主机名或域名 |
mynetworks | 设置可转发哪些主机的邮件 |
relay_domains | 设置可转发哪些网域的邮件 |
进入主配置文件,修改参数,首先需要定义变量myhostname,保存服务器的主机名称;定义变量mydomain,用来保存邮件域的名称。定义变量myorigin调用前面的mydomain变量,用来定义发出邮件的域。
75 #myhostname = host.domain.tld
76 myhostname = mail.hnsw01.com
77
78 # The mydomain parameter specifies the local internet domain name.
79 # The default is to use $myhostname minus the first component.
80 # $mydomain is used as a default value for many other configuration
81 # parameters.
82 #
83 mydomain = hnsw01.com
84
85 # SENDING MAIL
86 #
87 # The myorigin parameter specifies the domain that locally-posted
88 # mail appears to come from. The default is to append $myhostname,
89 # which is fine for small sites. If you run a domain with multiple
90 # machines, you should (1) change this to $mydomain and (2) set up
91 # a domain-wide alias database that aliases each user to
92 # user@that.users.mailhost.
93 #
94 # For the sake of consistency between sender and recipient addresses,
95 # myorigin also specifies the default domain name that is appended
96 # to recipient addresses that have no @domain part.
97 #
98 #myorigin = $myhostname
99 myorigin = $mydomain
……省略
最后定义网卡监听地址。可以指定要使用服务器的哪些IP地址对外提供电子邮件服务;或设置为all,表示所有IP地址都能提供电子邮件服务;并定义可接收邮件的主机名或域名列表。这里可以直接调用前面定义好的myhostname和mydomain变量(如果不想调用变量,也可以直接调用变量中的值):
110 #
111 # Note: you need to stop/start Postfix when this parameter changes.
112 #
113 #inet_interfaces = all
114 #inet_interfaces = $myhostname
115 #inet_interfaces = $myhostname, localhost
116 inet_interfaces = all
……
……
162 # See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
163 #
164 mydestination = $myhostname,$mydomain
165 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
166 #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
167 # mail.$mydomain, www.$mydomain, ftp.$mydomain
重启服务并设置开机自启
[root@localhost ~]# systemctl restart postfix.service
[root@localhost ~]# systemctl enable postfix
[root@localhost ~]# systemctl list-unit-files | grep post
postfix.service enabled
配置Dovecot服务程序
Dovecot是一款能够为Linux系统提供IMAP和POP3电子邮件服务的开源服务程序,安全性极高,配置简单,执行速度快,而且占用的服务器硬件资源也较少。
第1步:安装Dovecot服务程序软件包。
[root@localhost ~]# yum install dovecot -y //若使用的方法一设置的域名解析服务,请先配置好yum仓库再使用该命令
第2步:配置部署Dovecot服务程序。在Dovecot服务程序的主配置文件/etc/dovecot/dovecot.conf中去掉变量protocols的#号,使得Dovecot服务程序支持的电子邮件协议为imap、pop3和lmtp。由于Dovecot服务程序为了保证电子邮件系统的安全而默认强制用户使用加密方式进行登录,而当前还没有加密系统,因此需要添加参数disable_plaintext_auth = no来允许用户的明文登录。然后在主配置文件的第49行,设置允许登录的网段地址,如果想允许所有人都能使用,则不用修改本参数:
[root@mail 桌面]# vim /etc/dovecot/dovecot.conf
22
23 # Protocols we want to be serving.
24 protocols = imap pop3 lmtp
……
……
46 # for authentication checks). disable_plaintext_auth is also ignored for
47 disable_plaintext_auth = no
48 # these networks. Typically you'd specify your IMAP proxy servers here.
49 login_trusted_networks = 192.168.1.0/24
第3步:配置邮件格式与存储路径。在Dovecot服务程序单独的子配置文件中,定义一个路径,用于指定要将收到的邮件存放到服务器本地的哪个位置。这个路径默认已经定义好了,只需要将该配置文件中第25行前面的井号(#)删除即可。
[root@localhost ~]# vim /etc/dovecot/conf.d/10-mail.conf
22 # See doc/wiki/Variables.txt for full list. Some examples:
23 #
24 # mail_location = maildir:~/Maildir
25 mail_location = mbox:~/mail:INBOX=/var/mail/%u
26 # mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
27 #
28 # <doc/wiki/MailLocation.txt>
29 #
然后创建一个普通账户,并在家目录中建立用于保存邮件的目录。记得要重启Dovecot服务并将其添加到开机启动项中。至此,对Dovecot服务程序的配置部署步骤全部结束。
[root@mail 桌面]# systemctl start dovecot
[root@mail 桌面]# systemctl status dovecot.service
dovecot.service - Dovecot IMAP/POP3 email server
Loaded: loaded (/usr/lib/systemd/system/dovecot.service; disabled)
Active: active (running) since 四 2023-10-12 09:06:49 CST; 5s ago
Process: 4743 ExecStartPre=/usr/libexec/dovecot/prestartscript (code=exited, status=0/SUCCESS)
Main PID: 4747 (dovecot)
CGroup: /system.slice/dovecot.service
├─4747 /usr/sbin/dovecot -F
├─4754 dovecot/anvil
├─4755 dovecot/log
└─4757 dovecot/config
10月 12 09:06:49 mail.hnsw01.com systemd[1]: Starting Dovecot IMAP/POP3 email server...
10月 12 09:06:49 mail.hnsw01.com systemd[1]: Started Dovecot IMAP/POP3 email server.
10月 12 09:06:49 mail.hnsw01.com dovecot[4747]: master: Dovecot v2.2.10 starting up for imap, pop3...ed)
Hint: Some lines were ellipsized, use -l to show in full.
[root@mail 桌面]# systemctl enable dovecot.service
ln -s '/usr/lib/systemd/system/dovecot.service' '/etc/systemd/system/multi-user.target.wants/dovecot.service'
[root@mail 桌面]# systemctl list-unit-files | grep dovecot
dovecot.service enabled
[root@mail 桌面]# useradd zhangsan
[root@mail 桌面]# echo redhat | passwd --stdin zhangsan
更改用户 zhangsan 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@mail 桌面]# su zhangsan
[zhangsan@mail 桌面]$ cd ~
[zhangsan@mail ~]$ mkdir -p mail/.imap/INBOX
[zhangsan@mail ~]$ exit
exit
使用FoxMail客户端测试
邮件服务器IP:192.168.1.10
FoxMail客户端IP:192.168.1.10
使用windows主机安装FoxMail客户端测试
第一步配置ip,设置dns服务器
第二步:验证服务器和客户端是否可以正常通信,DNS解析是否正确;
第三步:运行FoxMail程序,选择其他邮箱手动设置,设置登录账号。
第四步:创建账号成功后给root用户写一封邮件
第五步:在服务器内使用mail查看收到的邮件,要想查看邮件的完整内容,只需输入收件人姓名前面的编号即可
[root@mail 桌面]# mail
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/mail/root": 1 messages 1 new
> N 1 zhangsan@hnsw01.com Thu Oct 12 11:29 43/1583 "test"
& 1
Message 1:
From zhangsan@hnsw01.com Thu Oct 12 11:29:02 2023
Return-Path: <zhangsan@hnsw01.com>
X-Original-To: root@hnsw01.com
Delivered-To: root@hnsw01.com
Date: Thu, 12 Oct 2023 11:29:02 +0800
From: "zhangsan@hnsw01.com" <zhangsan@hnsw01.com>
To: root <root@hnsw01.com>
Subject: test
X-Priority: 3
X-Has-Attach: no
X-Mailer: Foxmail 7.2.25.228[cn]
Content-Type: multipart/alternative;
boundary="----=_001_NextPart840863634623_=----"
Status: R
Content-Type: text/plain;
charset="us-ascii"
test mail
zhangsan@hnsw01.com
&
第六步,在服务器上使用mail给用户zhangsan发送一封邮件,
[root@mail 桌面]# mail -s testmail zhangsan@hnsw01.com #-s 指定邮箱标题,zhangsan@hnsw01.com是邮件的接收人,将当前shell当成编辑器来用,进入邮件正文的编写,编辑完内容后 Ctrl-D结束
aaa
EOT
第七步,在客户端点击收取按钮,收取邮件。查看内容