####################3.用户和访问权限####################
[root@mariadb ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| 127.0.0.1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| ::1       | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> CREATE USER westos@localhost identified by 'westos';
Query OK, 0 rows affected (0.00 sec)
##创建用户westos密码westos。localhost表示只能本机访问'%'表示远端访问授权

MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+--------+-------------------------------------------+
| Host      | User   | Password                                  |
+-----------+--------+-------------------------------------------+
| localhost | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| 127.0.0.1 | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| ::1       | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| localhost | westos | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+--------+-------------------------------------------+
4 rows in set (0.00 sec)
##多了一行"westos"的信息

MariaDB [(none)]> Ctrl-C -- exit!        ##按"ctrl+c"退出
Aborted
[root@mariadb ~]# mysql -uwestos -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> CREATE DATABASE westos;
ERROR 1044 (42000): Access denied for user 'westos'@'localhost' to database 'westos'
MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@mariadb ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT Host,User,Password,Create_priv FROM mysql.user;
+-----------+--------+-------------------------------------------+-------------+
| Host      | User   | Password                                  | Create_priv |
+-----------+--------+-------------------------------------------+-------------+
| localhost | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
| 127.0.0.1 | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
| ::1       | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
| localhost | westos | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | N           |
+-----------+--------+-------------------------------------------+-------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> GRANT CREATE on *.* to westos@localhost;
Query OK, 0 rows affected (0.00 sec)
##用户授权CREATE*.*表示所有库的所有表

MariaDB [(none)]> SHOW GRANTS FOR westos@localhost;
+----------------------------------------------------------------------------------------------------------------+
| Grants for westos@localhost                                                                                    |
+----------------------------------------------------------------------------------------------------------------+
| GRANT CREATE ON *.* TO 'westos'@'localhost' IDENTIFIED BY PASSWORD '*28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96' |
+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT Host,User,Password,Create_priv FROM mysql.user;
+-----------+--------+-------------------------------------------+-------------+
| Host      | User   | Password                                  | Create_priv |
+-----------+--------+-------------------------------------------+-------------+
| localhost | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
| localhost | westos | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
| 127.0.0.1 | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
| ::1       | root   | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 | Y           |
+-----------+--------+-------------------------------------------+-------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@mariadb ~]# mysql -uwestos -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE westos;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@mariadb ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT Host,User,Create_priv,Insert_priv FROM mysql.user;
+-----------+--------+-------------+-------------+
| Host      | User   | Create_priv | Insert_priv |
+-----------+--------+-------------+-------------+
| localhost | root   | Y           | Y           |
| 127.0.0.1 | root   | Y           | Y           |
| ::1       | root   | Y           | Y           |
| localhost | westos | Y           | N           |
+-----------+--------+-------------+-------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> GRANT INSERT on *.* to westos@localhost;    ##用户授权INSERT
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW GRANTS FOR westos@localhost;+------------------------------------------------------------------------------------------------------------------------+
| Grants for westos@localhost                                                                                            |
+------------------------------------------------------------------------------------------------------------------------+
| GRANT INSERT, CREATE ON *.* TO 'westos'@'localhost' IDENTIFIED BY PASSWORD '*28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96' |
+------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT Host,User,Create_priv,Insert_priv FROM mysql.user;
+-----------+--------+-------------+-------------+
| Host      | User   | Create_priv | Insert_priv |
+-----------+--------+-------------+-------------+
| localhost | root   | Y           | Y           |
| 127.0.0.1 | root   | Y           | Y           |
| ::1       | root   | Y           | Y           |
| localhost | westos | Y           | Y           |
+-----------+--------+-------------+-------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;    ##重载授权表。没起作用使用此命令前权限已变成"Y"
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> REVOKE CREATE on *.* from westos@localhost;        ##移除权限CREATE
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SELECT Host,User,Create_priv,Insert_priv FROM mysql.user;
+-----------+--------+-------------+-------------+
| Host      | User   | Create_priv | Insert_priv |
+-----------+--------+-------------+-------------+
| localhost | root   | Y           | Y           |
| 127.0.0.1 | root   | Y           | Y           |
| ::1       | root   | Y           | Y           |
| localhost | westos | N           | Y           |
+-----------+--------+-------------+-------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> DROP USER westos@localhost;        ##删除用户westos
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SELECT Host,User,Create_priv,Insert_priv FROM mysql.user;
+-----------+------+-------------+-------------+
| Host      | User | Create_priv | Insert_priv |
+-----------+------+-------------+-------------+
| localhost | root | Y           | Y           |
| 127.0.0.1 | root | Y           | Y           |
| ::1       | root | Y           | Y           |
+-----------+------+-------------+-------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!
Aborted

####################4.密码恢复####################
[root@mariadb ~]# systemctl stop mariadb
[root@mariadb ~]# mysqld_safe --skip-grant-tables &    ##相当于开启mysql的单用户模式
[1] 12220
[root@mariadb ~]# 161129 02:47:38 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
161129 02:47:38 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

[root@mariadb ~]# mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| 127.0.0.1 | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
| ::1       | root | *28C1E2BE21B45562A34B6CC34A19CFAFC2F88F96 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> UPDATE mysql.user set Password='redhat' WHERE User='root';    ##更新密码
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+----------+
| Host      | User | Password |
+-----------+------+----------+
| localhost | root | redhat   |
| 127.0.0.1 | root | redhat   |
| ::1       | root | redhat   |
+-----------+------+----------+
3 rows in set (0.00 sec)
##密码全部变成明文不安全

MariaDB [(none)]> UPDATE mysql.user set Password=password ('redhat') WHERE User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
##将密码修改成密文

MariaDB [(none)]> SELECT Host,User,Password FROM mysql.user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| 127.0.0.1 | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| ::1       | root | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@mariadb ~]# jobs
[1]+  Stopped                 mysqld_safe --skip-grant-tables
[root@mariadb ~]# killall -9 mysqld_safe
[1]+  Killed                  mysqld_safe --skip-grant-tables
[root@mariadb ~]# ps aux | grep mysql
mysql    12375  0.1  9.9 924612 100800 pts/0   Sl   02:47   0:01 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root     12837  0.0  0.0 112640   940 pts/0    S+   03:04   0:00 grep --color=auto mysql
[root@mariadb ~]# kill -9 12375
[root@mariadb ~]# ps aux | grep mysql
root     12863  0.0  0.0 112640   936 pts/0    S+   03:06   0:00 grep --color=auto mysql
[root@mariadb ~]# systemctl restart mariadb
[root@mariadb ~]# mysql -uroot -predhat
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> Ctrl-C -- exit!
Aborted
[root@mariadb ~]# mysqladmin -uroot -predhat password westos        ##修改密码
[root@mariadb ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> Ctrl-C -- exit!
Aborted

####################5.备份####################
==准备工作==
[root@mariadb ~]# mysql -uroot -pwestos
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.35-MariaDB MariaDB Server

Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> USE westos;
Database changed
MariaDB [westos]> SHOW TABLES;
Empty set (0.00 sec)

MariaDB [westos]> CREATE TABLE linux (
    -> username varchar(10) not null,
    -> password varchar(50) not null,
    -> class varchar(5) );
Query OK, 0 rows affected (0.12 sec)

MariaDB [westos]> DESC linux;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO   |     | NULL    |       |
| password | varchar(50) | NO   |     | NULL    |       |
| class    | varchar(5)  | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

MariaDB [westos]> INSERT INTO linux VALUES ('lee','123','18');
Query OK, 1 row affected (0.09 sec)

MariaDB [westos]> INSERT INTO linux VALUES ('linuxc','123','');
Query OK, 1 row affected (0.08 sec)

MariaDB [westos]> SELECT * FROM linux;
+----------+----------+-------+
| username | password | class |
+----------+----------+-------+
| lee      | 123      | 18    |
| linuxc   | 123      |       |
+----------+----------+-------+
2 rows in set (0.00 sec)

MariaDB [westos]> Ctrl-C -- exit!
Aborted

==备份==
[root@mariadb ~]# mysqldump -uroot -pwestos westos
--------------------------------------------------
>太多截取重要信息

--
-- Table structure for table `linux`
--

DROP TABLE IF EXISTS `linux`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `linux` (
  `username` varchar(10) NOT NULL,
  `password` varchar(50) NOT NULL,
  `class` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `linux`
--

LOCK TABLES `linux` WRITE;
/*!40000 ALTER TABLE `linux` DISABLE KEYS */;
INSERT INTO `linux` VALUES ('lee','123','18'),('linuxc','123','');
/*!40000 ALTER TABLE `linux` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
--------------------------------------------------
##不加"--no-data"备份表的数据结构和表的内容
[root@mariadb ~]# mysqldump -uroot -pwestos --no-data westos
--------------------------------------------------
>太多截取重要信息

--
-- Table structure for table `linux`
--

DROP TABLE IF EXISTS `linux`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `linux` (
  `username` varchar(10) NOT NULL,
  `password` varchar(50) NOT NULL,
  `class` varchar(5) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
--------------------------------------------------
##加上"--no-data"只备份表的数据结构
[root@mariadb ~]# mysqldump -uroot -pwestos westos > /mnt/westos.sql
[root@mariadb ~]# mysql -uroot -pwestos -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
##"-e"非交互式可以用来制作脚本
[root@mariadb ~]# mysql -uroot -pwestos -e "DROP DATABASE westos;"
[root@mariadb ~]# mysql -uroot -pwestos -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
[root@mariadb ~]# mysql -uroot -pwestos westos < /mnt/westos.sql
ERROR 1049 (42000): Unknown database 'westos'
##必须先创建一个westos库
[root@mariadb ~]# mysql -uroot -pwestos -e "CREATE DATABASE westos;"
[root@mariadb ~]# mysql -uroot -pwestos westos < /mnt/westos.sql    ##恢复数据
[root@mariadb ~]# mysql -uroot -pwestos -e "SHOW DATABASES;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
[root@mariadb ~]# mysql -uroot -pwestos -e "SELECT * FROM westos.linux"
+----------+----------+-------+
| username | password | class |
+----------+----------+-------+
| lee      | 123      | 18    |
| linuxc   | 123      |       |
+----------+----------+-------+

####################6.web管理####################
yum install php php-mysql httpd -y
systemctl start httpd
systemctl enable httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --reload

tar -jxf phpMyAdmin-3.4.0-all-languages.tar.bz2 -C /var/www/html
cd /var/www/html
mv phpMyAdmin-3.4.0-all-languages myadmin
cd myadmin/
cp -p config.sample.inc.php config.inc.php
vim config.inc.php
 17 $cfg['blowfish_secret'] = 'test'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH    ! */

systemctl restart httpd
http://172.25.50.100/myadmin

#####################
#####   SMTP    #####
#####################

####################DNS配置####################
172.25.50.100-->maillinux
172.25.50.200-->mailwestos

[root@localhost ~]# hostnamectl set-hostname maillinux.linux.com
[root@localhost ~]# vim /etc/yum.repos.d/rhel_dvd.repo
[root@localhost ~]# yum clean all
Loaded plugins: langpacks
Cleaning repos: rhel_dvd
Cleaning up everything
[root@localhost ~]# reboot
等待重启

[root@localhost ~]# hostnamectl set-hostname mailwestos.westos.com
[root@localhost ~]# vim /etc/yum.repos.d/rhel_dvd.repo
[root@localhost ~]# yum clean all
Loaded plugins: langpacks
Cleaning repos: rhel_dvd
Cleaning up everything
[root@localhost ~]# reboot
等待重启

[root@maillinux ~]# yum install bind -y
......
[root@maillinux ~]# vim /etc/named.conf
--------------------------------------------------
 11         listen-on port 53 { any; };

 17         allow-query     { any; };

 32         dnssec-validation no;
:wq
--------------------------------------------------
[root@maillinux ~]# vim /etc/named.rfc1912.zones
--------------------------------------------------
 25 zone "linux.com" IN {
 26         type master;
 27         file "linux.com.zone";
 28         allow-update { none; };
 29 };
 30
 31 zone "westos.com" IN {
 32         type master;
 33         file "westos.com.zone";
 34         allow-update { none; };
 35 };
 36
:wq
--------------------------------------------------
[root@maillinux ~]# cd /var/named
[root@maillinux named]# cp -p named.localhost westos.com.zone
[root@maillinux named]# vim westos.com.zone
--------------------------------------------------
  1 $TTL 1D
  2 @       IN SOA  dns.westos.com. root.westos.com. (
  3                                         0       ; serial
  4                                         1D      ; refresh
  5                                         1H      ; retry
  6                                         1W      ; expire
  7                                         3H )    ; minimum
  8                 NS      dns.westos.com.
  9 dns             A       172.25.50.100
 10 westos.com.     MX 1    172.25.50.200.
:wq
--------------------------------------------------
[root@maillinux named]# cp -p westos.com.zone linux.com.zone
[root@maillinux named]# vim linux.com.zone
--------------------------------------------------
:%s/westos/linux/g

 10 linux.com.      MX 1    172.25.50.100.
:wq
--------------------------------------------------
[root@maillinux named]# systemctl start named
[root@maillinux named]# systemctl enable named
ln -s '/usr/lib/systemd/system/named.service' '/etc/systemd/system/multi-user.target.wants/named.service'
[root@maillinux named]# systemctl stop firewalld
[root@maillinux named]# systemctl disable firewalld
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'
[root@maillinux named]# vim /etc/resolv.conf
--------------------------------------------------
  4 nameserver 172.25.50.100
:wq
--------------------------------------------------
[root@maillinux named]# dig -t mx linux.com |grep MX
;linux.com.            IN    MX
linux.com.        86400    IN    MX    1 172.25.50.100.
[root@maillinux named]# dig -t mx westos.com |grep MX
;westos.com.            IN    MX
westos.com.        86400    IN    MX    1 172.25.50.200.

[root@mailwestos ~]]# vim /etc/resolv.conf
--------------------------------------------------
  4 nameserver 172.25.50.100
:wq
--------------------------------------------------
[root@mailwestos ~]# dig -t mx linux.com |grep MX
;linux.com.            IN    MX
linux.com.        86400    IN    MX    1 172.25.50.100.
[root@mailwestos ~]# dig -t mx westos.com |grep MX
;westos.com.            IN    MX
westos.com.        86400    IN    MX    1 172.25.50.200.

####################SMTP配置####################
smtp用于邮件投递默认使用25端口

是谁发送的邮件
本机登陆web页面控制服务器发送邮件
所以发送邮件的是服务器

MX记录        ##邮件交换记录

http的服务由iiswindowsapachelinux等提供
stmp的服务由sendmailqmailpostfixredhat集成等提供

popimap用于邮件接收其服务由Dovecot豆腐块等提供

MTA邮件传输代理就是邮件服务器用于寄信和收信
MDA邮件接受代理将从MTA接收到的邮件放入正确的本地邮箱
MUA邮件用户代理是用在Client端的软件比如OutLook

mail root@linux.com
Subject: 111
222
333
.        ##"."加"回车"表示结束编辑
EOT

1)基本配置
[root@maillinux ~]# netstat -antlpe | grep :25
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      0          21796      1468/master         
tcp6       0      0 ::1:25                  :::*                    LISTEN      0          21797      1468/master     
##只开放了环回地址的25端口
[root@maillinux ~]# vim /etc/postfix/main.cf
-----------------------------------------------
 75 myhostname = maillinux.linux.com
 83 mydomain = linux.com
 99 myorigin = $mydomain
113 inet_interfaces = all
116 #inet_interfaces = localhost
164 mydestination = $myhostname, $mydomain, localhost        ##我接收哪些目的地址
:wq
-----------------------------------------------
##localhost表示什么也不加
[root@maillinux ~]# systemctl restart postfix.service
[root@maillinux ~]# netstat -antlpe | grep :25
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN      0          37160      1582/master         
tcp6       0      0 :::25                   :::*                    LISTEN      0          37161      1582/master    

=====测试:不带域名和dns无关=====
[root@maillinux ~]# mail root
Subject: aaa
aaaaaa
aaaaaa
.
EOT
[root@maillinux ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
BD25917E85C      434 Wed Nov 30 21:35:30  root@linux.com
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         root@westos.com

-- 0 Kbytes in 1 Request.
[root@maillinux ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Wed Nov 30 21:44  19/545   "aaa"
& 1
Message  1:
From root@linux.com  Wed Nov 30 21:44:45 2016
Return-Path: <root@linux.com>
X-Original-To: root
Delivered-To: root@linux.com
Date: Wed, 30 Nov 2016 21:44:45 -0500
To: root@linux.com
Subject: aaa
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

aaaaaa
aaaaaa

& q
Held 1 message in /var/spool/mail/root
====================

[root@maillinux ~]# scp /etc/postfix/main.cf root@172.25.50.200:/etc/postfix/main.cf
The authenticity of host '172.25.50.200 (172.25.50.200)' can't be established.
ECDSA key fingerprint is eb:24:0e:07:96:26:b1:04:c2:37:0c:78:2d:bc:b0:08.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.25.50.200' (ECDSA) to the list of known hosts.
root@172.25.50.200's password:
main.cf                                       100%   27KB  26.5KB/s   00:00

[root@mailwestos ~]# vim /etc/postfix/main.cf
-----------------------------------------------
:%s/linux/westos/g
:wq
-----------------------------------------------
[root@mailwestos ~]# systemctl restart postfix.service

=====测试:踢出队列=====
[root@maillinux ~]# mail root@westos.com
Subject: bbb
bbbbbb
bbbbbb
.
EOT
[root@maillinux ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
11DD917E85C      434 Wed Nov 30 21:47:14  root@linux.com
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         root@westos.com

-- 0 Kbytes in 1 Request.
[root@maillinux ~]# postsuper -d 11DD917E85C    ##踢出队列
postsuper: 11DD917E85C: removed
postsuper: Deleted: 1 message
[root@maillinux ~]# mailq
Mail queue is empty
====================

=====测试:刷新队列=====
[root@maillinux ~]# mail root@westos.com
Subject: ccc
cccccc
cccccc
.
EOT
[root@maillinux ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
18E4717E85C      434 Wed Nov 30 21:47:56  root@linux.com
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         root@westos.com

-- 0 Kbytes in 1 Request.

[root@mailwestos ~]# systemctl stop firewalld.service
[root@mailwestos ~]# systemctl disable firewalld.service
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'

[root@maillinux ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
18E4717E85C      434 Wed Nov 30 21:47:56  root@linux.com
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         root@westos.com

-- 0 Kbytes in 1 Request.
[root@maillinux ~]# postqueue -f        ##刷新队列重新发送
[root@maillinux ~]# mailq
Mail queue is empty

[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Wed Nov 30 21:48  22/742   "ccc"
& 1
Message  1:
From root@linux.com  Wed Nov 30 21:48:21 2016
Return-Path: <root@linux.com>
X-Original-To: root@westos.com
Delivered-To: root@westos.com
Date: Wed, 30 Nov 2016 21:47:55 -0500
To: root@westos.com
Subject: ccc
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

cccccc
cccccc

& q
Held 1 message in /var/spool/mail/root
邮件发送成功
====================

=====测试:westos回信=====
[root@mailwestos ~]# mail root@linux.com
Subject: ddd
dddddd
dddddd
.
EOT
[root@mailwestos ~]# mailq
Mail queue is empty

[root@maillinux ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages 1 new
    1 root                  Wed Nov 30 21:44  20/556   "aaa"
>N  2 root                  Wed Nov 30 22:34  22/743   "ddd"
& 2
Message  2:
From root@westos.com  Wed Nov 30 22:34:36 2016
Return-Path: <root@westos.com>
X-Original-To: root@linux.com
Delivered-To: root@linux.com
Date: Wed, 30 Nov 2016 22:34:36 -0500
To: root@linux.com
Subject: ddd
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@westos.com (root)
Status: R

dddddd
dddddd

& q
Held 2 messages in /var/spool/mail/root
====================

=====测试:发给域名=====
[root@maillinux ~]# mail @westos.com
Subject: eee
eeeeee
eeeeee
.
EOT
[root@maillinux ~]# mailq
Mail queue is empty

[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages 1 new
    1 root                  Wed Nov 30 21:48  23/753   "ccc"
>N  2 root                  Wed Nov 30 22:47  22/743   "eee"
& 2
Message  2:
From root@linux.com  Wed Nov 30 22:47:56 2016
Return-Path: <root@linux.com>
X-Original-To: ""@westos.com
Delivered-To: MAILER-DAEMON@westos.com
Date: Wed, 30 Nov 2016 22:47:55 -0500
To: ""@westos.com
Subject: eee
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

eeeeee
eeeeee

& q
Held 2 messages in /var/spool/mail/root
====================

=====测试:发给普通用户加域名=====
[root@maillinux ~]# mail student@westos.com
Subject: fff   
ffffff
ffffff
.
EOT
[root@maillinux ~]# mailq
Mail queue is empty

[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages
>   1 root                  Wed Nov 30 21:48  23/753   "ccc"
    2 root                  Wed Nov 30 22:47  23/754   "eee"
& q
Held 2 messages in /var/spool/mail/root                ##没有收到新消息
[root@mailwestos ~]# mail -u student
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/mail/student": 1 message 1 new
>N  1 root                  Wed Nov 30 22:53  22/752   "fff"
& 1
Message  1:
From root@linux.com  Wed Nov 30 22:53:15 2016
Return-Path: <root@linux.com>
X-Original-To: student@westos.com
Delivered-To: student@westos.com
Date: Wed, 30 Nov 2016 22:53:14 -0500
To: student@westos.com
Subject: fff
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

ffffff
ffffff

& q
Held 1 message in /var/mail/student
====================

2)排错方法
> /var/mail/root
> /var/mail/student
> /var/log/maillog
cat /var/log/maillog

3)mta
[root@maillinux ~]# man alternatives
-----------------------------------------------
       alternatives - maintain symbolic links determining default commands

       --config name
              Present the user with a configuration menu for choosing the mas‐
              ter  link  and slaves for link group name. Once chosen, the link
              group is set to manual mode.

       --display name
              Display information about the link group of which  name  is  the
              master  link.   Information  displayed includes the group's mode
              (auto or manual), which alternative the symlink currently points
              to, what other alternatives are available (and their correspond‐
              ing slave alternatives), and the  highest  priority  alternative
              currently installed.

       --list Display information about all link groups.
-----------------------------------------------
[root@maillinux ~]# alternatives --list |grep mta
mta    auto    /usr/sbin/sendmail.postfix
[root@maillinux ~]# alternatives --display mta        
mta - status is auto.
 link currently points to /usr/sbin/sendmail.postfix
/usr/sbin/sendmail.postfix - priority 30
 slave mta-mailq: /usr/bin/mailq.postfix
 slave mta-newaliases: /usr/bin/newaliases.postfix
 slave mta-pam: /etc/pam.d/smtp.postfix
 slave mta-rmail: /usr/bin/rmail.postfix
 slave mta-sendmail: /usr/lib/sendmail.postfix
 slave mta-mailqman: /usr/share/man/man1/mailq.postfix.1.gz
 slave mta-newaliasesman: /usr/share/man/man1/newaliases.postfix.1.gz
 slave mta-sendmailman: /usr/share/man/man1/sendmail.postfix.1.gz
 slave mta-aliasesman: /usr/share/man/man5/aliases.postfix.5.gz
Current `best' version is /usr/sbin/sendmail.postfix.
[root@maillinux ~]# ll /usr/sbin/sendmail
lrwxrwxrwx. 1 root root 21 May  6  2014 /usr/sbin/sendmail -> /etc/alternatives/mta
[root@maillinux ~]# ll /etc/alternatives/mta
lrwxrwxrwx. 1 root root 26 May  6  2014 /etc/alternatives/mta -> /usr/sbin/sendmail.postfix
[root@maillinux ~]# ll /usr/sbin/sendmail.postfix
-rwxr-xr-x. 1 root root 247832 Jan 26  2014 /usr/sbin/sendmail.postfix

4)postconf
[root@maillinux ~]# postconf -d |grep inet            ##查看默认配置
inet_interfaces = all
inet_protocols = all
local_header_rewrite_clients = permit_inet_interfaces
[root@maillinux ~]# postconf -n |grep inet            ##查看当前配置
inet_interfaces = all
inet_protocols = all
[root@maillinux ~]# postconf -e "inet_interfaces=localhost"    ##修改当前配置
[root@maillinux ~]# postconf -n |grep inet
inet_interfaces = localhost
inet_protocols = all
[root@maillinux ~]# postconf -e "inet_interfaces=all"
[root@maillinux ~]# postconf -n |grep inet
inet_interfaces = all
inet_protocols = all
[root@maillinux ~]# systemctl restart postfix.service

5)收件人别名
[root@mailwestos ~]# vim /etc/aliases
-----------------------------------------------
 97 admin:          root
 98 more:           :include:/etc/moreusers
:wq
-----------------------------------------------
[root@mailwestos ~]# vim /etc/moreusers
-----------------------------------------------
  1 root
  2 student
:wq
-----------------------------------------------
[root@mailwestos ~]# postalias /etc/aliases
[root@mailwestos ~]# systemctl restart postfix.service
[root@mailwestos ~]# > /var/mail/root
[root@mailwestos ~]# > /var/mail/student

=====测试:admin@westos.com=====
[root@maillinux ~]# mail admin@westos.com
Subject: 111
111111
111111
.
EOT
[root@maillinux ~]# mailq
Mail queue is empty

[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Thu Dec  1 01:47  22/746   "111"
& 1
Message  1:
From root@linux.com  Thu Dec  1 01:47:58 2016
Return-Path: <root@linux.com>
X-Original-To: admin@westos.com
Delivered-To: admin@westos.com
Date: Thu, 01 Dec 2016 01:47:57 -0500
To: admin@westos.com
Subject: 111
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

111111
111111

& q
Held 1 message in /var/spool/mail/root
====================

=====测试:more@westos.com=====
[root@maillinux ~]# mail more@westos.com
Subject: 222
222222
222222
.
EOT
[root@maillinux ~]# mailq
Mail queue is empty

[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 2 messages 1 new
    1 root                  Thu Dec  1 01:47  23/757   "111"
>N  2 root                  Thu Dec  1 01:49  25/872   "222"
& 2
Message  2:
From root@linux.com  Thu Dec  1 01:49:41 2016
Return-Path: <root@linux.com>
X-Original-To: more@westos.com
Delivered-To: root@westos.com
Delivered-To: more@westos.com
Date: Thu, 01 Dec 2016 01:49:40 -0500
To: more@westos.com
Subject: 222
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

222222
222222

& q
Held 2 messages in /var/spool/mail/root
[root@mailwestos ~]# mail -u student
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/mail/student": 1 message 1 new
>N  1 root                  Thu Dec  1 01:49  25/875   "222"
& 1
Message  1:
From root@linux.com  Thu Dec  1 01:49:41 2016
Return-Path: <root@linux.com>
X-Original-To: more@westos.com
Delivered-To: student@westos.com
Delivered-To: more@westos.com
Date: Thu, 01 Dec 2016 01:49:40 -0500
To: more@westos.com
Subject: 222
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@linux.com (root)
Status: R

222222
222222

& q
Held 1 message in /var/mail/student
====================

6)发件人地址欺骗
[root@maillinux ~]# vim /etc/postfix/generic
-----------------------------------------------
241 root@linux.com 123456789@qq.com
:wq
-----------------------------------------------
[root@maillinux ~]# postmap /etc/postfix/generic
[root@maillinux ~]# ls /etc/postfix/
access     generic     header_checks  master.cf  transport
canonical  generic.db  main.cf        relocated  virtual
[root@maillinux ~]# cat /etc/postfix/generic.db
]/K&эh^123456789@qq.comroot@linux.com
[root@maillinux ~]# postconf -e "smtp_generic_maps = hash:/etc/postfix/generic"
[root@maillinux ~]# postconf -n | grep smtp_generic_maps
smtp_generic_maps = hash:/etc/postfix/generic
[root@maillinux ~]# systemctl restart postfix.service

=====测试:发件人地址欺骗=====
[root@maillinux ~]# mail root@westos.com
Subject: 333
333333
333333
.
EOT
[root@maillinux ~]# mailq
Mail queue is empty

[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 3 messages 1 new
    1 root                  Thu Dec  1 01:47  23/757   "111"
    2 root                  Thu Dec  1 01:49  26/883   "222"
>N  3 root                  Thu Dec  1 02:25  22/748   "333"
& 3
Message  3:
From 123456789@qq.com  Thu Dec  1 02:25:11 2016
Return-Path: <123456789@qq.com>
X-Original-To: root@westos.com
Delivered-To: root@westos.com
Date: Thu, 01 Dec 2016 02:25:10 -0500
To: root@westos.com
Subject: 333
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: 123456789@qq.com (root)
Status: R

333333
333333

& q
Held 3 messages in /var/spool/mail/root
====================

7)使用telnet登陆并发送邮件
[root@mailwestos ~]# yum install telnet -y
......
[root@mailwestos ~]# telnet 172.25.50.100 25
Trying 172.25.50.100...
Connected to 172.25.50.100.
Escape character is '^]'.
220 maillinux.linux.com ESMTP Postfix
ehlo hello
250-maillinux.linux.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
mail from:root@linux.com
250 2.1.0 Ok
rcpt to:root@westos.com
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
666666
666666
.
250 2.0.0 Ok: queued as 5988117E85F
quit
221 2.0.0 Bye
Connection closed by foreign host.
You have new mail in /var/spool/mail/root
[root@mailwestos ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 4 messages 1 new
    1 root                  Thu Dec  1 01:47  23/757   "111"
    2 root                  Thu Dec  1 01:49  26/883   "222"
    3 root                  Thu Dec  1 02:25  23/759   "333"
>N  4 123456789@qq.com      Thu Dec  1 02:37  14/511   
& 4
Message  4:
From 123456789@qq.com  Thu Dec  1 02:37:38 2016
Return-Path: <123456789@qq.com>
X-Original-To: root@westos.com
Delivered-To: root@westos.com
Status: R

666666
666666

& q
Held 4 messages in /var/spool/mail/root


8)收件人地址转换
[root@maillinux ~]# > /var/mail/root

[root@mailwestos ~]# vim /etc/postfix/virtual
-----------------------------------------------
295 123456789@qq.com root@linux.com
:wq
-----------------------------------------------
[root@mailwestos ~]# postmap /etc/postfix/virtual
[root@mailwestos ~]# ls /etc/postfix/
access     generic        main.cf    relocated  virtual
canonical  header_checks  master.cf  transport  virtual.db
[root@mailwestos ~]# cat /etc/postfix/virtual.db
c/.0<эh^
[root@mailwestos ~]# postconf -e "virtual_alias_maps=hash:/etc/postfix/virtual"
[root@mailwestos ~]# postconf -n virtual_alias_maps
virtual_alias_maps = hash:/etc/postfix/virtual
[root@mailwestos ~]# systemctl restart postfix.service
[root@mailwestos ~]# mail 123456789@qq.com
Subject: 000
000000
000000
.
EOT
[root@mailwestos ~]# mailq
Mail queue is empty

=====测试:收件人地址转换=====
[root@maillinux ~]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 1 message 1 new
>N  1 root                  Thu Dec  1 04:01  22/748   "000"
& 1
Message  1:
From root@westos.com  Thu Dec  1 04:01:47 2016
Return-Path: <root@westos.com>
X-Original-To: root@linux.com
Delivered-To: root@linux.com
Date: Thu, 01 Dec 2016 04:01:46 -0500
To: 123456789@qq.com
Subject: 000
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: root@westos.com (root)
Status: R

000000
000000

& q
Held 1 message in /var/spool/mail/root
====================