SMTP服务部署-进阶篇
紧接着上周的内容。
一.远程登陆限制
由于当前所有虚拟机都可以远程连接实现发送邮件的服务,很明显这是步安全的。
如何实现登陆限制:
在我的客户机(desktop)上
1.cd /etc/postfix/
在最后一行添加:拒绝登陆的ip REJECT
如:172.25.254.50 REJECT
2.postmap access #加密文件,这样会生成一个access.db文件
3.postconf -d | grep client #筛选配置项目。并查询
4. postconf -e "smtpd_client_restrictions = check_client_access hash:/etc/postfix/access"#写入配置文件
这个配置文件在:/etc/postfix/main.cf的最后面
5.重启服务
systemctl restart postfix.service
测试:
在我的ip为50的主机上:
说明:这里的限制登陆实际上是可以通过telnet连接的,但是邮件是发不出去的
@还原操作:删除/etc/postfix/main.cf的最后一行,
Postmap access #重新加密,最后重启服务即可
二.冻结用户邮件服务
在/etc/postfix/目录下
1.vim sender
输入内容:[email protected] REJECT
2.postmap sender
3.postconf -e "smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender"
4.systemctl restart postfix.service
测试:
在另外一台虚拟机上测试:
[[email protected] Desktop]$ telnet 172.25.254.166 25
Trying 172.25.254.166...
Connected to 172.25.254.166.
Escape character is ‘^]‘.
220 maillyitx.lyitx.com ESMTP Postfix
mail from:[email protected]
250 2.1.0 Ok
rcpy to:[email protected]
502 5.5.2 Error: command not recognized
rcpt to:[email protected]
554 5.7.1 <[email protected]>: Sender address rejected: Access denied
三.拒绝用户接收邮件
1.vim recip #写入要拒绝接收邮件的用户全名
[email protected] REJECT
2.postmap recip#加密
3.postconf -e "smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recip"
4. systemctl restart postfix.service
测试:
在50主机:
[[email protected] Desktop]$ telnet 172.25.254.166 25
以上操作在/etc/postfix/main.cf的最后一行可以看到
readme_directory = /usr/share/doc/postfix-2.10.1/README_FILES
smtpd_sender_restrictions = check_sender_access hash:/etc/postfix/sender
smtpd_recipient_restrictions = check_recipient_access hash:/etc/postfix/recip
@@不干扰后续实验,在这里进行操作的还原
具体方法:
删除/etc/postfix/main.cf文件的最后两行
然后重启服务即可
四.豆腐块(dovecot)
在desktop主机(172.25.254.166)上
1.yum install dovecot -y
2.vim /etc/dovecot/dovecot.conf
将24行注释取消
24 protocols = imap pop3 lmtp
49 disable_plaintext_auth = no
50 login_trusted_networks = 0.0.0.0/0#允许外网登陆
3.cd /etc/dovecot/conf.d/
vim 10-mail.conf
加入以下内容
在30行: mail_location = mbox:~/mail:INBOX=/var/mail/%u
4.systemctl start dovecot
@以上几步操作目的在于开放端口
5.su - westos####在这里需要手动建立文件(已经存在的用户)
6.mkdir -p /home/westos/mail/.imap
7.touch /home/westos/mail/.imap/INBOX
8.su - root
这里有一个一劳永逸的方法:
在超级用户下:
cd /etc/skel/
mkdir mail/.imap/INBOX
(如此设置可以让每一个新建用户创建时系统都会自动创建目录)
最后重启服务:
systemctl restart dovecot.service
测试:
在其他主机上测试:
1.yum install mutt -y
2.mutt -f imap://[email protected]
根据提示输入密码后就可以看到westos用户的邮件
[[email protected] Desktop]# telnet 172.25.254.166 110
Trying 172.25.254.166...
Connected to 172.25.254.166.
Escape character is ‘^]‘.
+OK [XCLIENT] Dovecot ready.
user westos
+OK
pass redhat
+OK Logged in.
操作成功
五.用雷鸟接收邮件
打开雷鸟软件
这样就可以在软件中受到邮件了
####数据库虚拟用户接收邮件###
1.安装数据库
yum install mysql-server -y
systemctl start mariadb
vim /etc/my.cnf
配置文件改为:
symbolic-links=1
systemctl restart mariadb##
mysql_secure_installation#安全初始化,设置root密码。然后一直空格
2.phpmyadmin的安装—在网页上显示操作数据库
yum install httpd -y
systemctl start httpd
mv phpMyAdmin-3.4.0-all-languages.tar.bz2 /var/www/html/
cd /var/www/html/
tar -jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 ##解压文件
mv phpMyAdmin-3.4.0-all-languages myadmin ##改个简单的名字
systemctl stop firewalld.service ##关闭火墙
yum install php -y##安装php,没有php环境显示不了
systemctl restart httpd.service ##安装过后需要重新启http
cd myadmin/
cp -p config.sample.inc.php config.inc.php##将模板文件复制成一个真正的配置文件
vim config.inc.php ##在文件中添加如cookie
$cfg[‘blowfish_secret‘] = ‘westos‘; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
yum install php-mysql.x86_64 -y##关联数据库
systemctl restart httpd.service
浏览器中输入:安装数据库的服务器的ip地址/myadmin
在创建一个名为:email的数据库,创建一个为muser的表,字段数设置为4.
详细设置如下:
然后在本地数据库中新建用户:
MariaDB [(none)]> select * from email.muser;
+-----------------+----------+-----------+------------------+
| username | password | domain | maildir |
+-----------------+----------+-----------+------------------+
| [email protected] | 123 | lyitx.org | lyitx.org/admin/ |
+-----------------+----------+-----------+------------------+
1 row in set (0.00 sec)
创建用户:CREATE USER [email protected] identified by ‘postfix’
再进行授权,(图中)
GRANT INSERT,UPDATE,SELECT on emial.* to [email protected];
然后退出
3.文件的编写:
再安装有数据库的主机上:
cd /etc/postfix/
vim mysql-users.cf
手动编写以下内容
hosts = localhost
user = postfix
password = postfix
dbname = email
table = muser
select_field = username
where_field = username
保存退出
vim mysql-domain.cf
编写:
hosts = localhost
user = postfix
password = postfix
dbname = email
table = muser
select_field = domain
where_field = domain
vim mysql-maildir.cf
hosts = localhost
user = postfix
password = postfix
dbname = email
table = muser
select_field = maildir
where_field = username
然后对这三个文件进行加密处理
纠正一下:图中显示的内容应为
lyitx.org/admin/
4.创建vmail用户,mail组
groupadd -g 666 vmail
useradd -g 666 -u 666 -s /sbin/nologin vmail
细节:参数不能少,如果出错要重新建立用户需要执行:
userdel -r vmail
5.写入配置文件
在配置文件/etc/postfix/main.cf的最后可以看到写入的内容
virtual_gid_maps = static:666
virtual_uid_maps = static:666
virtual_mailbox_base = /home/vmail
virtual_alias_maps = mysql:/etc/postfix/mysql-users.cf
virtual_mailbox_domains = mysql:/etc/postfix/mysql-domain.cf
virtual_mailbox_maps = mysql:/etc/postfix/mysql-maildir.cf
6.重启postfix服务
Systemctl restart postfix.severice
测试:就在当前主机给[email protected] 用户发送邮件
如果在/home/vmail/lyitx.org/admin/new目录下查看到邮件,说明操作成功!
[[email protected] new]# pwd
/home/vmail/lyitx.org/admin/new
[[email protected] new]# ls
1481167802.Vfd01I280a514M732414.maillyitx.lyitx.com
1481167902.Vfd01I280a515M599277.maillyitx.lyitx.com
1481167977.Vfd01I280a516M115286.maillyitx.lyitx.com
1481185764.Vfd01I280a51eM96871.maillyitx.lyitx.com
[[email protected] new]#
豆腐块&数据库
cp /usr/share/doc/dovecot-2.2.10/example-config/dovecot-sql.conf.ext /etc/dovecot/
cd /etc/dovecot
vim dovecot-sql.conf.ext
32 driver = mysql
71 connect = host=localhost dbname=email user=postfix password=postfix
78 default_pass_scheme = PLAIN
107 password_query = \
108 SELECT username, domain, password \
109 FROM muser WHERE username = ‘%n‘ AND domain = ‘%d‘
125 user_query = SELECT maildir, 666 AS uid, 666 AS gid FROM muser WHERE userid = ‘%u‘
Vim /etc/dovecot/conf.d/10-mail.conf
30 mail_location = maildir:/home/vmail/%d/%n
168 first_valid_uid = 666
175 first_valid_gid = 666
最后重启服务
Ststemctl restart dovecot
在其他主机上,用telnet连接上,就表示实验成功