Linux課程第二十天學習筆記

####################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 [email protected]
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  [email protected]
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         [email protected]

-- 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 [email protected]  Wed Nov 30 21:44:45 2016
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Date: Wed, 30 Nov 2016 21:44:45 -0500
To: [email protected]
Subject: aaa
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R

aaaaaa
aaaaaa

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

[root@maillinux ~]# scp /etc/postfix/main.cf [email protected]:/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.
[email protected]'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 [email protected]
Subject: bbb
bbbbbb
bbbbbb
.
EOT
[root@maillinux ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
11DD917E85C      434 Wed Nov 30 21:47:14  [email protected]
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         [email protected]

-- 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 [email protected]
Subject: ccc
cccccc
cccccc
.
EOT
[root@maillinux ~]# mailq
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
18E4717E85C      434 Wed Nov 30 21:47:56  [email protected]
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         [email protected]

-- 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  [email protected]
                (connect to 172.25.50.200[172.25.50.200]:25: No route to host)
                                         [email protected]

-- 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 [email protected]  Wed Nov 30 21:48:21 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Wed, 30 Nov 2016 21:47:55 -0500
To: [email protected]
Subject: ccc
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R

cccccc
cccccc

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

=====測試:westos回信=====
[root@mailwestos ~]# mail [email protected]
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 [email protected]  Wed Nov 30 22:34:36 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Wed, 30 Nov 2016 22:34:36 -0500
To: [email protected]
Subject: ddd
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (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 [email protected]  Wed Nov 30 22:47:56 2016
Return-Path: <[email protected]>
X-Original-To: ""@westos.com
Delivered-To: [email protected]
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: [email protected] (root)
Status: R

eeeeee
eeeeee

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

=====測試:發給普通用戶加域名=====
[root@maillinux ~]# mail [email protected]
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 [email protected]  Wed Nov 30 22:53:15 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Wed, 30 Nov 2016 22:53:14 -0500
To: [email protected]
Subject: fff
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (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

=====測試:[email protected]=====
[root@maillinux ~]# mail [email protected]
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 [email protected]  Thu Dec  1 01:47:58 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 01 Dec 2016 01:47:57 -0500
To: [email protected]
Subject: 111
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R

111111
111111

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

=====測試:[email protected]=====
[root@maillinux ~]# mail [email protected]
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 [email protected]  Thu Dec  1 01:49:41 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 01 Dec 2016 01:49:40 -0500
To: [email protected]
Subject: 222
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (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 [email protected]  Thu Dec  1 01:49:41 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 01 Dec 2016 01:49:40 -0500
To: [email protected]
Subject: 222
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R

222222
222222

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

6)發件人地址欺騙
[root@maillinux ~]# vim /etc/postfix/generic
-----------------------------------------------
241 [email protected] [email protected]
: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^[email protected]@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 [email protected]
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 [email protected]  Thu Dec  1 02:25:11 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 01 Dec 2016 02:25:10 -0500
To: [email protected]
Subject: 333
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (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:[email protected]
250 2.1.0 Ok
rcpt to:[email protected]
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 [email protected]      Thu Dec  1 02:37  14/511   
& 4
Message  4:
From [email protected]  Thu Dec  1 02:37:38 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
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 [email protected] [email protected]
: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 [email protected]
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 [email protected]  Thu Dec  1 04:01:47 2016
Return-Path: <[email protected]>
X-Original-To: [email protected]
Delivered-To: [email protected]
Date: Thu, 01 Dec 2016 04:01:46 -0500
To: [email protected]
Subject: 000
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
From: [email protected] (root)
Status: R

000000
000000

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章