Ubuntu下配置sendmail邮件服务器
@
环境准备
部署好dns邮件域名
邮件域名 | ip |
---|---|
www.hoyeong.com | 192.168.200.160 |
root@huhy:~# host www.hoyeong.com
www.hoyeong.com has address 192.168.200.160
软件环境;
ubuntu22服务端
ubuntu22客户端测试
服务端部署
安装sendmail软件包
apt install -y sendmail
使用sendconfig命令配置
sendmailconfig
#默认直接回车即可
y
#输入n回车,表示不使用默认配置的sendmail,而是自定义
n
#直接回车
ENTER回车
#输入邮件输入域名,2级即可
hoyeong.com
一直回车到底然后会生效配置文件(然后需要等待一会时间)
#然后直接回车即可,默认会重启sendmail
Reload the running sendmail now with the new configuration?
修改主机名为域名
root@huhy:~# vim /etc/hostname
root@huhy:~# cat /etc/hostname
hoyeong.com
修改配置文件
vim /etc/mail/sendmail.mc
将监听范围设置为本机
#127.0.0.1改为0.0.0.0
DAEMON_OPTIONS(`Family=inet, Name=MTA-v4, Port=smtp, Addr=0.0.0.0')dnl
DAEMON_OPTIONS(`Family=inet, Name=MSP-v4, Port=submission, M=Ea, Addr=0.0.0.0')dnl
vim /etc/mail/local-host-names
hoyeong.com
192.168.200.160
修改access文件,提前备份好习惯
cp /etc/mail/access{,.bak}
cat > /etc/mail/access <<eof
Connect:hoyeong.com RELAY
Connect:192.168.200.160 RELAY
eof
在/etc/mail/目录下配置生效
make
重启生效
systemctl restart sendmail
服务端创建两个mail用户,用于测试
root@huhy:~# useradd -m mail1
root@huhy:~# useradd -m mail2
root@huhy:~# passwd mail1
New password:
Retype new password:
passwd: password updated successfully
root@huhy:~# passwd mail2
New password:
Retype new password:
passwd: password updated successfully
客户端测试
首先修改dns为服务端ip
root@slave1:~# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: no
addresses: [192.168.200.101/24]
gateway4: 192.168.200.2
nameservers:
addresses: [192.168.200.160,8.8.8.8]
version: 2
root@slave1:~#
#重启生效
root@slave1:~# netplan apply
root@slave1:~# ping www.hoyeong.com
PING www.hoyeong.com (192.168.200.160) 56(84) bytes of data.
64 bytes from www.hoyeong.com (192.168.200.160): icmp_seq=1 ttl=64 time=0.498 ms
64 bytes from www.hoyeong.com (192.168.200.160): icmp_seq=2 ttl=64 time=0.969 ms
^C
--- www.hoyeong.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1003ms
rtt min/avg/max/mdev = 0.498/0.733/0.969/0.235 ms
root@slave1:~#
ubuntu默认有telnet工具
命令解释;
telnet 邮件域名 25端口
helo 域名(测试是否可以连接)
mail from:"邮件主题"<服务端用户@域名> (邮件发送者)
rcpt to:mail1@域名 (邮件接收者,这里接收者和发送者都为mail1)
data (回车后输入正文)
. (以点结尾)
quit退出
telnet www.hoyeong.com 25
helo www.hoyeong.com
mail from:"Hello mail"<mail1@www.hoyeong.com>
rcpt to:mail1@www.hoyeong.com
data
354 Enter mail, end with "." on a line by itself
hello huhy,welcome!!!!
.
quit
ubuntu客户端具体操作如下
root@slave1:~# telnet hoyeong.com 25
Trying ::1...
telnet: Unable to connect to remote host: Connection refused
root@slave1:~# telnet www.hoyeong.com 25
Trying 192.168.200.160...
Connected to www.hoyeong.com.
Escape character is '^]'.
220 huhy ESMTP Sendmail 8.15.2/8.15.2/Debian-22ubuntu3; Tue, 22 Nov 2022 14:08:36 GMT; (No UCE/UBE) logging access from: [192.168.200.101](FAIL)-[192.168.200.101]
helo www.hoyeong.com
250 huhy Hello [192.168.200.101], pleased to meet you
mail from:"Hello mail"<mail1@www.hoyeong.com>
250 2.1.0 "Hello mail"<mail1@www.hoyeong.com>... Sender ok
rcpt to:mail1@www.hoyeong.com
250 2.1.5 mail1@www.hoyeong.com... Recipient ok
data
354 Enter mail, end with "." on a line by itself
hello huhy,welcome!!!!
.
250 2.0.0 2AME8a4K004428 Message accepted for delivery
quit
221 2.0.0 huhy closing connection
Connection closed by foreign host.
root@slave1:~#
服务端验证查看,安装mailutils工具
apt install -y mailutils
由于是发送到mail1用户,需要登录到mail1
root@huhy:~# su mail1
$ mail
"/var/mail/mail1": 1 message 1 new
>N 1 mail1@www.hoyeong. Tue Nov 22 14:11 10/352
? 1
Return-Path: <mail1@www.hoyeong.com>
Received: from www.hoyeong.com ([192.168.200.101])
by huhy (8.15.2/8.15.2/Debian-22ubuntu3) with SMTP id 2AME8a4K004428
for mail1@www.hoyeong.com; Tue, 22 Nov 2022 14:10:30 GMT
Date: Tue, 22 Nov 2022 14:08:36 GMT
From: mail1@www.hoyeong.com
Message-Id: <202211221410.2AME8a4K004428@huhy>
hello huhy,welcome!!!!
?
#使用exit退出
安装pop3协议
达到不登录到服务端服务器,在客户端服务器就可以查看邮件的目的
服务端安装以下软件包
apt install -y dovecot-imapd
apt install -y dovecot-pop3d
修改配置文件
vim /etc/dovecot/dovecot.conf
#解开注释,添加dns地址网段
login_trusted_networks = 192.168.200.0/24
重启服务
systemctl restart dovecot
再次发送邮件验证测试
root@slave1:~# telnet www.hoyeong.com 25
Trying 192.168.200.160...
Connected to www.hoyeong.com.
Escape character is '^]'.
220 huhy ESMTP Sendmail 8.15.2/8.15.2/Debian-22ubuntu3; Tue, 22 Nov 2022 14:33:51 GMT; (No UCE/UBE) logging access from: [192.168.200.101](FAIL)-[192.168.200.101]
helo www.hoyeong.com
250 huhy Hello [192.168.200.101], pleased to meet you
mail from:mail@www.hoyeong.com
250 2.1.0 mail@www.hoyeong.com... Sender ok
rcpt to:mail2@www.hoyeong.com
250 2.1.5 mail2@www.hoyeong.com... Recipient ok
data
354 Enter mail, end with "." on a line by itself
huhy huhy huhy huhy huhy
.
250 2.0.0 2AMEXpVF008699 Message accepted for delivery
quit
221 2.0.0 huhy closing connection
Connection closed by foreign host.
root@slave1:~#
客户端进行查看验证
进入交互式后;
回车
user 用户名 (选择用户)
pass 用户密码
quit (退出)
root@slave1:~# telnet www.hoyeong.com 110
Trying 192.168.200.160...
Connected to www.hoyeong.com.
Escape character is '^]'.
+OK [XCLIENT] Dovecot (Ubuntu) ready.
user mail2
+OK
pass 000000
+OK Logged in.
list
+OK 1 messages:
1 367
.
retr 1
+OK 367 octets
Return-Path: <mail@www.hoyeong.com>
Received: from www.hoyeong.com ([192.168.200.101])
by huhy (8.15.2/8.15.2/Debian-22ubuntu3) with SMTP id 2AMEXpVF008699
for mail2@www.hoyeong.com; Tue, 22 Nov 2022 14:35:02 GMT
Date: Tue, 22 Nov 2022 14:33:51 GMT
From: mail <mail@www.hoyeong.com>
Message-Id: <202211221435.2AMEXpVF008699@huhy>
huhy huhy huhy huhy huhy
.
quit
+OK Logging out.
Connection closed by foreign host.
root@slave1:~#
Ubuntu一键部署
#!bin/bash
ip=192.168.200.160
domain=hoyeong.com
mailuser1=mail1
userpd1=000000
mailuser2=mail2
userpd2=000000
apt install -y sendmail
#博主空了完善
echo "####交互式配置sendmail####"
sendmailconfig
y
#输入n回车,表示不使用默认配置的sendmail,而是自定义
n
#直接回车
ENTER回车
#输入邮件域名,2级即可
hoyeong.com
一直回车到底然后会生效配置文件(然后需要等待一会时间)
#然后直接回车即可,默认会重启sendmail
Reload the running sendmail now with the new configuration?
echo "#####修改配置文件#####"
cat > /etc/hostname <<eof
$domain
eof
sed -i 's/Addr=127.0.0.1/Addr=0.0.0.0/'g /etc/mail/sendmail.mc
cat > /etc/mail/local-host-names <<eof
$domain
$ip
eof
cp /etc/mail/access{,.bak}
cat > /etc/mail/access <<eof
Connect:$doamin RELAY
Connect:$ip RELAY
eof
cd /etc/mail
make
systemctl restart sendmail
echo "#####创建用户#####"
useradd -m $mailuser1
useradd -m $mailuser2
passwd $mailuser1 <<eof
$userpd1
$userpd1
eof
passwd $mailuser2 <<eof
$userpd2
$userpd2
eof
echo "#####服务端配置完毕#####"