基本知识

重点服务运行维护技能

服务的搭建、调优、维护

掌握apache的各种解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apt install apache2 -y
# 安装apache2
systemctl start apache2
# 启动apache2
systemctl enable apache2
# 设置开机自启
apt install ufw
# 安装防火墙
ufw enable
# 设置开机自启
ufw allow 80
# 放行端口或 ufw allow ssh
[root@UOS /var/www]# mkdir html_1
[root@UOS /var/www]# mkdir html_2
[root@UOS /var/www]# mkdir html_3
# 创建网站目录以及网站首页
[root@UOS /var/www]# echo "this is no.1" > ./html_2/index.html
[root@UOS /var/www]# echo "this is no.2" > ./html_2/index.html
[root@UOS /var/www]# echo "this is no.3" > ./html_3/index.html

基于端口的虚拟主机

修改 /etc/apache2/sites-available/000-default.conf默认配置

若 /etc/apache2/sites-enabled/ vhosts.conf 配置了,则不使用默认配置

这是网页配置文件,决定网页在哪里

1
2
3
4
<VirtualHost *:8080>                                                            
# ServerName 192.168.22.138
DocumentRoot /var/www/heml_1
</VirtualHost>

修改/etc/apache目录下的ports.conf文件,在Listen 80下面加上一条Listen 8080,监听8080端口

1
2
3
4
5
Listen 80                      
Listen 8080
systemctl restart apache2
# 重启apache2服务
打开192.168.22.138 验证

基于IP的虚拟主机

添加网卡,配置另一个IP,

nmtui 图形化配置

1
2
3
4
5
6
7
8
9
10
<VirtualHost 192.168.22.138:8080> 
DocumentRoot /var/www/heml_1
</VirtualHost>
<VirtualHost 192.168.22.140:8080>
DocumentRoot /var/www/heml_2

</VirtualHost>
<VirtualHost 192.168.22.142:8080>
DocumentRoot /var/www/heml_3
</VirtualHost>
1
2
3
systemctl restart apache2
# 重启apache2服务
打开 192.168.22.138 和 192.168.22.140 验证

基于域名的虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
vim /etc/hosts
# 配置域名映射
192.168.22.138 www.uos1.com

<VirtualHost *:8080>
ServerName www.uos1.com
DocumentRoot /var/www/heml_1
</VirtualHost>

<VirtualHost *:8080>
ServerName www.uos2.com
DocumentRoot /var/www/heml_2
</VirtualHost>

<VirtualHost *:8080>
ServerName www.uos3.com
DocumentRoot /var/www/heml_3
</VirtualHost>

三种方法一起使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<VirtualHost 192.168.22.138:80>                                 
# ServerName www.uos1.com
DocumentRoot /var/www/html_1
</VirtualHost>

<VirtualHost 192.168.22.140:80>
# ServerName www.uos2.com
DocumentRoot /var/www/html_2
</VirtualHost>

<VirtualHost *:80>
ServerName www.uos3.com
DocumentRoot /var/www/html_3
</VirtualHost>
1
2
3
4
5
6
7
8
修改httpd.conf文件,增加如下内容:
Alias /bbs "/forum/htdocs"
<Directory "/forum/htdocs">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Require all granted
</Directory>
上述别名的配置,就是说当你基于你的站点访问http://www.yourdoamin.com/bbs目录下的文件时,会直接从服务器/forum/htdocs目录下访问对应的文件。

LAMP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@UOS /etc/apache2]# apt install -y php-mysql mariadb-server mariadb-client php-fpm libapache2-mod-php php-mysql  
# 安装环境
[root@UOS /etc/apache2]# systemctl restart php7.3-fpm
[root@UOS /etc/apache2]# systemctl enable php7.3-fpm

Synchronizing state of php7.3-fpm.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable php7.3-fpm
[root@UOS /etc/apache2]# systemctl restart mariadb
[root@UOS /etc/apache2]# systemctl enable mariadb
#启动服务
[root@UOS /etc/apache2]# echo "<?php phpinfo(); ?" >> /var/www/html/index.php
# 创建php测试页
[root@UOS /etc/apache2]# systemctl restart apache2
# 重启apache2
[root@UOS /etc/apache2]# curl http://192.168.22.138/index.php
# 客户端验证(F12查看php代码)

掌握多种网络存储:NFS、逻辑卷、vdo

数据库MariaDB

容器Docker

批量系统管理ansible

入侵检测及防护

这里我们以查询系统登陆日志+使用ufw拒绝多次尝试登陆IP的方式,挡住黑客入侵的企图:
首先,找出多次尝试登陆系统的IP:

1
grep "Failed password" /var/log/auth.log | grep -Po "from \K\d+\\. \d+.\d+.\d+" | uniq -c|tee 1

如果最次错误登陆次数大于10次,我们就把这个IP加入ufw的deny规则:

1
cat 1 | awk '$1>10{print "ufw deny "$2}' | sh

我们可以把这些命令规则加入到root的crontab里,周期性的检测和执行,这样就完成了一个简单的入侵检测策略。

找出所有连接过ssh22端口的所有IP,可以执行:

1
grep -ri ufw /var/log/messages | grep DPT=22 | grep -Po ".*SRC=\K\d+\. \d+. \d+. \d+"

配置ssh免密登录

常规

  1. 生成密钥对 使用以下命令,在本地系统上生成公钥和私钥对:
1
ssh-keygen -t rsa

按照提示,设置密钥对文件名和选择一个口令。如果不想要口令的话,可以直接按 Enter 跳过。

  1. 将公钥复制到远程系统 使用以下命令将本地系统生成的公钥添加到远程系统的授权文件中:
1
ssh-copy-id username@remote_host

其中,username 是远程系统的用户名,remote_host 是远程系统的 IP 地址或域名。执行命令后,会询问远程系统的密码。输入正确的密码,按回车键即可。

  1. 测试免密登录 使用以下命令测试免密登录是否成功:
1
ssh username@remote_host

其中,username 和 remote_host 分别是远程系统的用户名和 IP 地址或域名。如果免密登录成功,则无需输入密码即可登录远程系统。

  1. 配置 SSH 服务 为了支持免密登录,需要在远程系统上配置 SSH 服务。首先,确保远程系统上存在 OpenSSH 服务:
1
sudo apt-get install openssh-server

然后,编辑 SSH 服务配置文件 /etc/ssh/sshd_config:

1
sudo nano /etc/ssh/sshd_config

找到以下两行,并将 # 去掉:

1
2
RSAAuthentication yes
PubkeyAuthentication yes

如果没有这两行,可以手动添加。编辑完毕后,保存并关闭文件。

接下来,重启 SSH 服务以应用配置修改:

1
sudo systemctl restart ssh

以上就是在 Linux 操作系统上配置 SSH 免密登录的详细流程。

自动化

1、准备虚拟机
4台虚拟机:
(ansible 192.168.200.10)
(node1 192.168.200.11)
(node2 192.168.200.12)
(node3 192.168.200.13)
2、配置免密登陆和hosts解析
ssh-keygen 一直回车 #生成密钥
ssh-copy-id node1 #拷贝私钥
所有机器都必须是免密登陆
4台主机上面配置/etc/hosts,以ansible主机为例

1
2
3
4
5
vim /etc/hosts
192.168.200.10 ansible
192.168.200.11 node1
192.168.200.12 node2
192.168.200.13 node3

Copy到每台主机:

1
for i in {10..13);do scp /etc/hosts 192.168.200.$i:/etc/hosts;done

3、安装ansible

1
apt install -y ansible

4、配置主机清单

1
2
3
4
5
6
vim /etc/ansible/hosts
[web]
node1
node2
[db]
node3

5、结果测试

1
ansible all -m ping

简单项目

通过模块批量完成主机的功能实现

环境说明:
一台物理机:pserver192.168.1.254
三台虚拟机:
server1 192.168.1.10
server2 192.168.1.20
server3 192.168.1.30
要求:
配置ssh:从物理机可以root免密登陆三台虚拟机;
配置ansible主机列表,serverl在db组,server2和server3在web组;
配置四台服务器的hosts文件,加入四台主机名和IP:
使用ping模块测试四台主机的连通性;
使用apt模块在db组安装mariadb-server;
使用shel1模块获得web组服务器的mac地址;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
---
- name: configure servers
hosts: all
become: yes

tasks:
- name: set up ssh key for root user
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

- name: configure ansible inventory
lineinfile:
path: /etc/ansible/hosts
line: "{{ item }}"
with_items:
- "[db]"
- "server1 ansible_host=192.168.1.10"
- ""
- "[web]"
- "server2 ansible_host=192.168.1.20"
- "server3 ansible_host=192.168.1.30"
- ""
- "[all:vars]"
- "ansible_python_interpreter=/usr/bin/python3"

- name: configure hosts file
lineinfile:
path: /etc/hosts
line: "{{ item }}"
with_items:
- "192.168.1.254 pserver"
- "192.168.1.10 server1"
- "192.168.1.20 server2"
- "192.168.1.30 server3"

- name: test connectivity
hosts: all
become: yes

tasks:
- name: ping hosts
ping:

- name: install mariadb-server
hosts: db
become: yes

tasks:
- name: install mariadb-server
apt:
name: mariadb-server
state: present

- name: get web servers' MAC addresses
hosts: web
become: yes

tasks:
- name: get MAC addresses
shell: "cat /sys/class/net/*/address"
register: result

- name: show MAC addresses
debug:
var: result.stdout_lines

该 playbook 的任务如下:

  1. set up ssh key for root user: 将物理机的 ~/.ssh/id_rsa.pub 文件中的公钥添加到三台虚拟机的 /root/.ssh/authorized_keys 文件中,使 root 用户可以免密码登录。
  2. configure ansible inventory: 在控制节点上配置 Ansible 的 inventory 文件,将 server1 加入 db 组,将 server2 和 server3 加入 web 组,并设置所有主机的解释器为 Python3。
  3. configure hosts file: 配置四台服务器的 hosts 文件,加入四台主机名和 IP。
  4. ping hosts: 使用 ping 模块测试四台主机的连通性。
  5. install mariadb-server: 使用 apt 模块在 db 组安装 MariaDB。
  6. get web servers' MAC addresses: 使用 shell 模块获取 web 组服务器的 MAC 地址,并将输出保存到变量 result 中。
  7. show MAC addresses: 使用 debug 模块显示变量 result 中的内容,即 web 组服务器的 MAC 地址。

编写剧本,根据企业环境部署web服务并进行优化

环境说明:
一台物理机:pserver192.168.1.254
三台虚拟机:
server1 192.168.1.10
server2 192.168.1.20
server3 192.168.1.30
要求:
配置ssh:从物理机可以root免密登陆三台虚拟机;
配置ansible主机列表,serverl在db组,server2和server3在web组;
配置四台服务器的hosts文件,加入四台主机名和IP;
编写web.yaml,在web组服务器中安装apache2;在db组中安装mariadb-server;
并启动安装的服务,检测服务的可用性;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
- name: deploy web service
hosts: web
become: yes

tasks:
- name: set up ssh key for root user
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

- name: configure ansible inventory
lineinfile:
path: /etc/ansible/hosts
line: "{{ item }}"
with_items:
- "[db]"
- "server1 ansible_host=192.168.1.10"
- ""
- "[web]"
- "server2 ansible_host=192.168.1.20"
- "server3 ansible_host=192.168.1.30"

- name: configure hosts file
lineinfile:
path: /etc/hosts
line: "{{ item }}"
with_items:
- "192.168.1.254 pserver"
- "192.168.1.10 server1"
- "192.168.1.20 server2"
- "192.168.1.30 server3"

- name: install apache2
apt:
name: apache2
state: present

- name: start apache2 service
service:
name: apache2
state: started

- name: check apache2 status
uri:
url: http://{{ inventory_hostname }}
return_content: yes
register: result
failed_when: result.status not in [200, 301, 302]
changed_when: false

- name: debug
debug:
var: result

- name: deploy database service
hosts: db
become: yes

tasks:
- name: set up ssh key for root user
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

- name: install mariadb-server
apt:
name: mariadb-server
state: present

- name: start mariadb-server service
service:
name: mariadb
state: started

- name: check mariadb-server status
shell: "systemctl is-active --quiet mariadb"
changed_when: false

该 playbook 的任务如下:

  1. set up ssh key for root user: 将物理机的 ~/.ssh/id_rsa.pub 文件中的公钥添加到三台虚拟机的 /root/.ssh/authorized_keys 文件中,使 root 用户可以免密码登录。
  2. configure ansible inventory: 在控制节点上配置 Ansible 的 inventory 文件,将 server1 加入 db 组,将 server2 和 server3 加入 web 组。
  3. configure hosts file: 配置四台服务器的 hosts 文件,加入四台主机名和 IP。
  4. install apache2: 使用 apt 模块安装 Apache2。
  5. start apache2 service: 使用 service 模块启动 Apache2 服务。
  6. check apache2 status: 使用 uri 模块检测 Apache2 服务的可用性,并将输出保存到变量 result 中。
  7. debug: 使用 debug 模块显示变量 result 中的内容,即检测 Apache2 服务的结果。
  8. install mariadb-server: 使用 apt 模块安装 MariaDB。
  9. start mariadb-server service: 使用 service 模块启动 MariaDB 服务。
  10. check mariadb-server status: 使用 shell 模块检查 MariaDB 服务是否正在运行。

编写剧本,完成嵌套循环,添加多用户

环境说明:
一台物理机:pserver192.168.1.254
三台虚拟机:
server1 192.168.1.10
server2 192.168.1.20
server3 192.168.1.30
要求:
配置ssh:从物理机可以root免密登陆三台虚拟机;
配置ansible主机列表,server1在db组,server2和server3在web组;
配置四台服务器的hosts文件,加入四台主机名和IP;
编写useradd.yaml,使用循环,在所有虚拟机添加uos[1-3]三个用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
---
- name: add users
hosts: all
become: yes

tasks:
- name: set up ssh key for root user
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

- name: configure ansible inventory
lineinfile:
path: /etc/ansible/hosts
line: "{{ item }}"
with_items:
- "[db]"
- "server1 ansible_host=192.168.1.10"
- ""
- "[web]"
- "server2 ansible_host=192.168.1.20"
- "server3 ansible_host=192.168.1.30"

- name: configure hosts file
lineinfile:
path: /etc/hosts
line: "{{ item }}"
with_items:
- "192.168.1.254 pserver"
- "192.168.1.10 server1"
- "192.168.1.20 server2"
- "192.168.1.30 server3"

- name: add users
user:
name: "uos{{ item }}"
state: present
loop: "{{ range(1,4)|list }}"

- name: display users
shell: "cut -d: -f1 /etc/passwd | grep -E '^uos[1-3]$'"
register: users
changed_when: false

- name: debug
debug:
var: users.stdout_lines

  1. set up ssh key for root user: 将物理机的 ~/.ssh/id_rsa.pub 文件中的公钥添加到三台虚拟机的 /root/.ssh/authorized_keys 文件中,使 root 用户可以免密码登录。
  2. configure ansible inventory: 在控制节点上配置 Ansible 的 inventory 文件,将 server1 加入 db 组,将 server2 和 server3 加入 web 组。
  3. configure hosts file: 配置四台服务器的 hosts 文件,加入四台主机名和 IP。
  4. add users: 使用 user 模块循环添加 uos1、uos2 和 uos3 三个用户到所有虚拟机中。
  5. display users: 使用 shell 模块执行命令,在所有虚拟机上获取当前用户列表,并将输出保存到变量 users 中。
  6. debug: 使用 debug 模块显示变量 users 中的内容,即所有虚拟机中存在的 uos1、uos2 和 uos3 三个用户。

编写剧本,完成debug检测

环境说明:
一台物理机:pserver192.168.1.254
三台虚拟机:
server1 192.168.1.10
server2 192.168.1.20
server3 192.168.1.30
要求:
配置ssh:从物理机可以root免密登陆三台虚拟机;
配置ansible主机列表,serverl在db组,server2和server3在web组;
配置四台服务器的hosts文件,加入四台主机名和IP;
编写mac.yaml,取得所有虚拟机mac地址(192.168.1.XX所在网卡)
使用debug var获得命令输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
- name: debug check
hosts: all
become: yes

tasks:
- name: set up ssh key for root user
authorized_key:
user: root
state: present
key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

- name: configure ansible inventory
lineinfile:
path: /etc/ansible/hosts
line: "{{ item }}"
with_items:
- "[db]"
- "server1 ansible_host=192.168.1.10"
- ""
- "[web]"
- "server2 ansible_host=192.168.1.20"
- "server3 ansible_host=192.168.1.30"

- name: configure hosts file
lineinfile:
path: /etc/hosts
line: "{{ item }}"
with_items:
- "192.168.1.254 pserver"
- "192.168.1.10 server1"
- "192.168.1.20 server2"
- "192.168.1.30 server3"

- name: get MAC addresses of virtual machines
command: "ifconfig eth0 | grep 'HWaddr' | awk '{print $NF}'"
register: mac_addresses
ignore_errors: yes
when: "'192.168.1' in inventory_hostname"
changed_when: false
failed_when: false

- name: display MAC addresses
debug:
var: mac_addresses.stdout_lines
when: "'192.168.1' in inventory_hostname"

该 playbook 的任务如下:

  1. set up ssh key for root user: 将物理机的 ~/.ssh/id_rsa.pub 文件中的公钥添加到三台虚拟机的 /root/.ssh/authorized_keys 文件中,使 root 用户可以免密码登录。
  2. configure ansible inventory: 在控制节点上配置 Ansible 的 inventory 文件,将 server1 加入 db 组,将 server2 和 server3 加入 web 组。
  3. configure hosts file: 配置四台服务器的 hosts 文件,加入四台主机名和 IP。
  4. get MAC addresses of virtual machines: 在虚拟机上执行命令,获取其 eth0 网卡的 MAC 地址。使用 register 参数将输出保存到变量 mac_addresses 中,忽略脚本执行过程中的错误。
  5. display MAC addresses: 使用 debug 模块显示变量 mac_addresses 中的内容(即 MAC 地址),仅在运行任务的主机属于 192.168.1.x 网段时执行。

综合项目

1. 快速自动化安装配置DHCP服务器脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
---
- name: install and configure dhcp server
hosts: all
become: yes

tasks:
- name: install dhcp server
shell: sh /dhcp_install.sh #替换为实际脚本路径
register: dhcp_install_result

- name: configure dhcp server
block:
- name: set dhcp listening interface
lineinfile:
path: /etc/default/isc-dhcp-server
regexp: '^INTERFACESv4='
line: 'INTERFACESv4="eth0"' #替换为实际接口名称

- name: set dhcp network settings
copy:
dest: /etc/dhcp/dhcpd.conf
content: |
option domain-name "example.com";
option domain-name-servers 114.114.114.114;
default-lease-time 3600;
max-lease-time 7200;
authoritative;
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.100 192.168.1.200;
option routers 192.168.1.254;
}
when: dhcp_install_result.rc == 0

dhcp_install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

# 安装 isc-dhcp-server 应用
apt-get update && apt-get install -y isc-dhcp-server

# 创建备份文件
cp /etc/default/isc-dhcp-server /etc/default/isc-dhcp-server.bak

# 配置监听接口
sed -i 's/#INTERFACESv4=""/INTERFACESv4="eth0"/' /etc/default/isc-dhcp-server

# 重启 dhcp 服务
systemctl restart isc-dhcp-server.service

上述 playbook 中,包含一个名为 install and configure dhcp server 的 play,该 play 会在所有目标主机上执行安装和配置 DHCP 服务器的任务。

playbook 中的任务分为两个部分:第一个任务用于安装 DHCP 服务器,使用 shell 模块调用指定的 shell 脚本来完成安装。任务使用 register 关键字,将安装结果保存在变量 dhcp_install_result 中。

第二个任务则用于对已安装的 DHCP 服务器进行配置,其中 block 关键字包含多个子任务。首先使用 lineinfile 模块修改服务器监听接口,使用 copy 模块复制配置文件模板,并修改其中的分配网段、IP范围、网关、掩码和 DNS 等参数。当且仅当安装任务成功后,才执行配置任务。

注意:请替换 playbook 中的脚本路径和接口名称为实际的值。另外,由于 DHCP 服务需要在特权模式下运行,所以需要将 playbook 中指定的 become 属性设置为“是”。

2.检查密码,如果用户三次输入密码均错误,则退出脚本

要求:
编写shell脚本,分别从键盘读入用户输入的用户名和密码:
判断用户名为uos,密码为uos_123,不正确则重新输入;
直到3次机会都用完,仍不正确则执行退出exit ll;
正确则显示welcome
要求2:
使用shell模块,使此脚本在server1上运行;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

chance=3
while [ $chance -gt 0 ]; do
read -p "请输入用户名: " username
read -s -p "请输入密码: " password # -s 参数用于隐藏输入内容

if [ "$username" = "uos" ] && [ "$password" = "uos_123" ]; then
echo -e "\nWelcome!"
exit 0
else
echo -e "\n用户名或密码错误,请重试。"
let chance-=1
if [ $chance -gt 0 ]; then
echo "还有 $chance 次机会。"
fi
fi
done

echo "错误次数已达上限,退出."
exit 1

# shell模块执行
ansible server1 -m shell -a '/path/to/login.sh'