RHCE-Linux的基础部分笔记

25-10-16(SSH)

1.SSH理论

理论部分参考 跳转

  • 查看 sshd 是否使用

    systemctl status sshd 查询 sshd 服务的运行状态
    netstat -intup | grep sshd 查询 sshd 服务对应的网络连接、监听端口
    ps -aux | grep sshd 查询系统中所有和 sshd 相关的进程详情
  • 原理图

image-20251018135704473

yum whatprocides netstat –查看该命令所需软件(–>下载net-tools)

  • 关键配置:
    • 端口修改(Port,默认 22,建议自定义提高安全性)。
    • 禁用密码认证(PasswordAuthentication no),强制公钥认证。
    • 禁用 root 直接登录(PermitRootLogin no),通过普通用户 sudo 提权。
    • 限制允许登录的用户(AllowUsers)或组(AllowGroups

2.SSH实验

2.1 修改SSH端口

1
2
3
4
5
6
7
8
9
10
11
12
#配置(/etc/ssh/sshd_config)(服务器配置)
[root@ssh ~]# vim /etc/ssh/sshd_config
21 #Port 22 #改端口号
22 Port 666


#关闭防火墙和切换宽容模式
[root@ssh ~]# systemctl --now disable firewalld
[root@ssh ~]# setenforce 0
[root@ssh ~]# getenforce
#重启服务
[root@ssh ~]# systemctl restart sshd

image-20260130002411974

2.2 拒绝root用户远程登录

1
2
3
4
5
6
7
8
9
10
11
12
#配置(/etc/ssh/sshd_config)(服务器配置)
[root@ssh ~]# vim /etc/ssh/sshd_config
#改参数
# PermitRootLogin yes --->
PermitRootLogin no
[root@ssh ~]# systemctl restart sshd
#(------若不生效------)
#配置(/etc/ssh/sshd_config.d/01-permitrootlogin.conf)
[root@ssh ~]# vim /etc/ssh/sshd_config.d/01-permitrootlogin.conf
# PermitRootLogin yes --->
PermitRootLogin no
[root@ssh ~]# systemctl restart sshd

image-20260130003058267

2.3 允许特定用户ssh登录,其余用户无法登录(包括root用户)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#配置(/etc/ssh/sshd_config)(服务器配置)
[root@ssh ~]# vim /etc/ssh/sshd_config
#改参数
#AllowUsers USERNAME --->
AllowUsers zhangsan
#重启服务
[root@ssh ~]# systemctl restart sshd
#注:
#USERNAME需要存在,多个用户之间使用空格隔开

[root@ssh ~]# systemctl restart sshd
[root@ssh ~]# useradd zhangsan
[root@ssh ~]# passwd zhangsan
更改用户 zhangsan 的密码 。
新的密码:
无效的密码: 密码少于 8 个字符
重新输入新的密码:
passwd:所有的身份验证令牌已经成功更新。

image-20260130003746580

2.4 liunx客户端通过密钥登录liunx服务端root用户

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
#服务端(无需操作)
#客户端:
#1.创建密钥对
[root@client ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:RiUr92e99iK9aId8lj91L+LfIPOwMO/GBW8+TLMcN6Q root@client
The key's randomart image is:
+---[RSA 3072]----+
| . . |
| + |
| . + |
| + . .. . |
| S . oo+ |
| . o EBo+|
| oo+X+*=|
| +OO#=.|
| =*B+*=|
+----[SHA256]-----+

#2.将公玥发送至服务端(用户的家目录的.ssh/authorized_keys)
[root@client ~]# ssh-copy-id root@172.25.254.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@172.25.254.129's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'root@172.25.254.129'"
and check to make sure that only the key(s) you wanted were added.

#3.客户端免密登录
[root@client ~]# ssh root@172.25.254.129

ssh-keygen (生成、管理和转换认证密钥) -t (制定类型 RSA)
/root/.ssh/id_rsa —— 私钥文件
/root/.ssh/id_rsa.pub —– 公钥文件

image-20260130004928605

2.5 windows客户端通过秘钥登录linux服务端root用户

上传至autgorized_keys

1
2
3
4
5
6
7
#1、生成公私钥对
ssh-keygen -t rsa
#2、将公钥上传到服务器

#3、测试免密登录
ssh root@192.168.223.5
#4.使用XShell自带的密钥生成密钥对,然后复制到服务器上

​ 加密

公钥<————>私钥

​ 解密

3. SSH协议原理

3.1 连接过程

image-20251012105145531

说明

image-20251012105343249

3.2 两种认证方式

  • 密码认证:通过用户名+密码验证(安全性较低,易受暴力破解)。

image-20251012110434119

  • 公钥认证:基于密钥对(公钥存服务器,私钥存客户端),安全性更高,是推荐方式。

image-20251012110246733

25-10-18(at/crond)

1.计划任务理论

理论部分参考 跳转

2.计划任务

随课记

witch at — 查看at命令所在目录

yum whatprovides */at — 查找命令由哪个软件包提供

at -V —- 查看软件版本

yum install at -y —- 安装at

systemctl start atd.service — 运行at

wall 【内容】 —- 广播内容

at -l —- 查看计划任务

at -d — 删除计划任务

atrm [任务编号] —— 删除计划任务

/var/log/cron —- 日志文件

/var/spool/at

2.1 一次性计划任务【at】

at [HH:MM] [yyyy-mm-dd] #设置一次性计划任务

参数 作用
-l 列出系统上所有用户的at任务,等价于atq
-d 删除或取消一个任务
-v 使用时间格式,列出at任务
-c 可以列出任务之后的命令内容
-f 从文件中读取

2.1.1 实现形式

1
2
3
4
5
6
7
8
9
10
11
12
#设定任务
[root@1010 ~]# at 10;00 #系统时间10点执行
warning: commands will be executed using /bin/sh
at> wall 6666 # 广播6666
at> <EOT> #ctrl d 生成任务
job 1 at Sun Jan 5 9:59:00 2025
#at相关命令
at -l 或 atq --- 查看任务
at -c [编号] --- 查看任务具体内容
atrm [编号] 或 at -d [编号] --- 删除任务
tail -f /var/log/cron --- 查看系统是否执行
或 watch -n [编号] ls -l /root
  • 计划任务保存在目录: /var/spool/at
  • 计划任务的日志:/var/log/cron

注意

at命令的输出结果(标准输出和标准错误输出)不会显示到屏幕上

at命令中执行的命令最好使用绝对路径,不容易报错

过程:输入at 时间 -> 回车 ->输入执行的命令->ctrl+d退出at

at命令只会执行一次,时间过了就不再执行

2.1.2 时间的书写格式

1
2
3
4
5
6
7
8
9
10
11
12
13
#1、指定时间
am、pm 12小时制
HH:MM 24小时制
年月日 06122024 2024-12-06 12/06/2024 06.12.2024
MMDD[CC]YY [CC]YY-MM-DD. MM/DD/[CC]YY DD.MM.[CC]YY
[HH:MM] [yyyy-mm-dd]
#2、模糊时间
noon(12:00) midnight(00:00) teatime(4pm)
#3、相对时间
now +1 min now +2 days
#要在三天后的下午4点运行作业 at 4pm + 3 days
#要在7月31日上午10点运行作业 at 10am Jul 31
#要在明天的凌晨1点运行作业 at 1am tomorrow

2.1.3 黑白名单

超级用户可以在任何情况下使用这些命令。

对于其他用户,使用 at 的权限由文件 /etc/at.allow 和 /etc/at.deny 确定

​ 1、格式:每行一个用户名称,不允许有空白字符

​ 2、系统先检测at.allow,如果at.allow存在,其中的用户允许at,即使at.deny也存 在该用户也会被忽略

​ 3、如果at.allow不存在才检测at.deny,其中用户不允许at,剩下的用户可使用at

​ 4、at.allow at.deny 都不存在,只有root可用at

2.1.4 注意点

  • 权限:不同用户对系统的使用权限不一样,设置任务的时候,该任务的一定要在该用户的权限范围之内

2.2 周期性计划任务【crond】

2.2.1 实现形式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#配置文件
[root@1010 ~]# systemctl status crond.service #查看服务是否启动
[root@1010 ~]# rpm -ql crontabs
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab
/etc/sysconfig/run-parts
/usr/bin/run-parts
/usr/share/licenses/crontabs
/usr/share/licenses/crontabs/COPYING
/usr/share/man/man4/crontabs.4.gz
/usr/share/man/man4/run-parts.4.gz
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
#设定任务

###方法1.编辑系统的crontab 文件,由crond进程管理
[root@1010 ~]# vim /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed


###方法2.以用户身份添加计划任务,并以用户的身份命名的文件保存在/var/spool/cron/目录下
[root@1010 ~]# crontab -e -u root(用户)
* * * * * ls > /file

[root@1010 ~]# ll /var/spool/cron/ # 查看任务编号及数量
总用量 4
-rw-------. 1 root root 26 10月 18 20:11 root

[root@1010 ~]# cat /var/spool/cron/root #查看任务内容
* * * * * ls > /file

#crontab相关命令
crontab -l 列出当前用户的 crontab 文件内容。
crontab -r 删除当前用户的 crontab 文件。
crontab -e 修改任务的执行时间和任务内容。
#可以添加 -u 来选择用户,但是要注意权限问题。

###方法3.包含为不同用户存储的不同类别的任务,系统定时任务的目录,由crond进程管理
#/etc/cron.d/


# 创建一个名为 nginx_maintenance的文件,放在/etc/cron.d/目录下,可以清晰地知道该
# 文件是用于 nginx 服务的维护任务,并且能够快速地找到和修改相关的定时任务配置。

[root@1010 ~]# /etc/cron.d/nginx_maintenance
# 清理旧的日志文件,每周执行一次
0 2 * * 7 root /usr/local/bin/cleanup_nginx_logs.sh
# 检查配置文件的完整性,每天执行一次
0 1 * * * root /usr/local/bin/check_nginx_config.sh

#crontab执行的每一项工作都会被记录到日志:/var/log/cron
tail -f /var/log/cron ## %需要转义
[root@1010 ~]# tail -n 1 /etc/crontab
* 10 * * * root echo 123 > "/file1_`date +\%R`"

2.2.2 时间书写格式

*     *     *     *     *

分 时 日 月 周

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
#特殊符号
*:代表指定时间单位中的每一个时刻。例如,*在分钟字段中表示每分钟。
,:用于分隔列表中的项目。例如,1,3,5 * * * * command 表示每个小时的第1、3、5分钟执行命令。
-:用于定义范围。例如,0 9-17 * * * command 表示从早上9点到下午5点每小时开始时执行命令。
/数字:用于指定步长。例如,*/15 * * * * command 表示每15分钟执行一次命令。

分 时 日 月 周
*/2 */2 */2 */2 *
* * * * */2
* * L 1-6/2 *
* * * * 2
* 8 * * * 每天8点的每一分钟
0 8 * * * 每天8:00执行
13 0 1 * * 每月1日00:13执行
*/2 12 * * * 每天12点每两分钟执行
* 11,23 * * * 每天11点和23点的每一分钟都 执行
* 1-3 * * * 每天的1点、2点、3点的每一分钟都执行

###特殊情况:
#2025年1月5日是星期日,当日期和星期冲突的时候,那么这天和这个星期都会执行

###如果想在每年的2月最后一天凌晨2点执行任务
* * L 2 *

# anacron 能够在系统启动后自动执行定期任务,即使系统在任务预定时间未运行,任务也会在系统下次启动时执行.
[root@1010 ~]# rpm -ql cronie-anacron
/etc/anacrontab

2.2.3 黑白名单

/etc/cron.allow  /etc/cron.deny

cron.allow和cron.deny文件不能用于限制cron的执行;它们只限制crontab的使用。

特别是,限制访问crontab对用户的现有crontab没有影响。其工作将继续直到crontab被删除。
调用crontab的用户必须能够读取cron.allow和cron.deny文件。如果不是这样,那么它们就被视为不存在。

当系统中有 /etc/cron.allow 文件时,只有写入此文件的用户可以使用 crontab 命令,没有写入的用户不能使用crontab 命令。
同样,如果有此/etc/cron.allow文件,/etc/cron.deny 文件会被忽略,因为 /etc/cron.allow 文件的优先级更高
当系统中只有 /etc/cron.deny 文件时,写入此文件的用户不能使用 crontab 命令,没有写入文件的用户可以使用 crontab 命令

crontab 执行的每一项工作都会被 记录到 /var/log/cron 这个日志文件中
当用户使用 crontab 新建工作之后,该项工作就会被记录到 /var/spool/cron/目录里面

2.2.4 扩展 【run-parts】

run-parts:该命令可将后面接的“目录”内的所有文件找出来执行。

假设我们有一个名为 scripts 的目录,其中包含以下可执行脚本:
/scripts/
├── script1.sh
├── script2.sh
└── script3.sh
我们想要在每天的凌晨 1 点执行这些脚本。我们可以这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#1.确保脚本有执行权限。
[root@1010 ~] #chmod +x /scripts/*.sh

#2.设置周期性任务
[root@1010 ~] # vim /etc/crontab
01 * * * * run-parts /scripts

#3.检查
[root@1010 ~] # mkdir /shell
[root@1010 ~] # vim /shell/1.sh
[root@1010 ~] # vim /shell/2.sh
[root@1010 ~] # vim /shell/3.sh
[root@1010 ~] # chmod u+x /shell/*
[root@1010 ~] # vim /etc/crontab
* 11 * * * root run-parts /shell #注意/shell 是目录
#日志追踪
[root@1010 ~] # tail -f /var/log/cron

25-10-23(chrony)

1.ntp理论

理论部分参考 跳转

2.ntp实验

2.1.chrony

chronyc sources — 查看每个时间源更详细的统计信息

date — 显示时间

chrony 的主配置文件:/etc/chrony.conf

概念:

chrony是一个开源的自由软件,它能帮助你保持系统时钟与时钟服务器(NTP)同步,因此让你的时间保持精确。

2.2.安装与查看

yum install chrony -y # 安装

systemctl start chronyd #启动服务

systemctl start chronyd #重启服务

systemctl enable chronyd #开机自启

systemctl status ntp # 查看ntp状态

2.2.1 实验1—-同步时间

(1) 手动修改至错误时间

1
2
[root@1010 ~]# date -s 8:00
2025年 10月 23日 星期四 08:00:00 CST

PixPin_2025-10-23_20-00-36

(2) 编辑配置文件

检查网络

1
[root@1010 ~]# ping ntp.aliyun.com		#检查是否能连通阿里云授时中心

PixPin_2025-10-23_20-04-31

修改/etc/chrony.conf文件

img

1
[root@1010 ~]# vim /etc/chrony.conf

(3) 重启服务

1
2
[root@1010 ~]# systemctl restart  chronyd
[root@1010 ~]# systemctl status chronyd #查看服务状态

PixPin_2025-10-23_20-08-25

(4) 验证

1
[root@1010 ~]# chronyc  sources	# 列出时间源  添加-v(更详细)

PixPin_2025-10-23_20-09-55

与阿里云地址相同,同步成功

1
2
[root@1010 ~]# timedatectl   status
[root@1010 ~]# date

PixPin_2025-10-23_20-11-04

2.2.2 实验2—搭建本地时间同步服务器

服务端配置(192.168.232.3)
(1) 网络检查(ping客户端)
1
[root@1010 ~]# ping 192.168.232.131

PixPin_2025-10-23_20-21-49

(2) 编辑配置文件(已安装服务)
1
[root@1010 ~]# vim /etc/chrony.conf

PixPin_2025-10-23_20-19-26

基于实验一已经配置阿里云的时间源的情况下,允许客户端同步服务端的时间源

1
[root@1010 ~]# systemctl restart chronyd	#重启服务
客户端(192.168.232.131)
(1) 网络检查
1
[root@hym ~]# ping 192.168.232.3

PixPin_2025-10-23_20-25-38

(2) 软件安装
1
2
[root@node1 ~]# yum  install  chrony  -y   
[root@hym ~]# systemctl status chronyd # 查看服务状态
(3) 配置文件(时间源改为服务端的)
1
2
[root@hym ~]# vim  /etc/chrony.conf 
[root@hym ~]# systemctl restart chronyd #重启服务

PixPin_2025-10-23_20-29-40

(4) 检查
1
2
3
[root@hym ~]# chronyc  sources
[root@1010 ~]# timedatectl status
[root@1010 ~]# date

PixPin_2025-10-23_20-32-31

25-10-24(Nginx)

1 Nginx理论

理论部分参考 跳转

2 Nginx实验

安装

1
[root@1010 ~]# yum install nginx -y

启动

1
[root@1010 ~]# systemctl start nginx.service

systemctl restart nginx.service 重新启动Nginx服务

systemctl status nginx.service 查看运行状态

核心配置:/etc/nginx/nginx.conf

2.1 实验1:Nginx基础

1
2
3
4
5
6
7
8
#安装
[root@1010 ~]# yum install nginx -y
#服务启动
[root@1010 ~]# systemctl start nginx.service
#关闭防火墙
[root@1010 ~]# systemctl --now disable firewalld.service
#设置兼容模式
[root@1010 ~]# setenforce 0

默认目录为 /usr/share/nginx/html

1
2
#测试
[root@1010 ~]# curl 192.168.232.3

PixPin_2025-10-24_23-06-42

2.2 实验2:修改端口、默认目录、默认文件访问web页面

多端口访问同一web页面

1
2
3
4
5
#编辑配置文件(/etc/nginx/nginx.conf)
[root@1010 ~]# vim /etc/nginx/nginx.conf

listen 80;
listen 81; #添加81端口

PixPin_2025-10-24_23-12-03

1
2
3
4
5
6
7
8
#重启Nginx服务
[root@1010 ~]# systemctl restart nginx.service
#防火墙
[root@1010 ~]# systemctl --now disable firewalld
#兼容模式
[root@1010 ~]# setenforce 0
#监听端口
[root@1010 ~]# netstat -lntup | grep nginx

PixPin_2025-10-24_23-20-52

1
2
3
#测试
[root@1010 ~]# curl 192.168.232.3:80
[root@1010 ~]# curl 192.168.232.3:81
PixPin_2025-10-24_23-42-35

自定义默认目录

1
2
3
4
#编辑配置文件
[root@1010 ~]# vim /etc/nginx/nginx.conf
# root /usr/share/nginx/html;
root /web;

PixPin_2025-10-24_23-46-53

1
2
3
[root@1010 ~]# mkdir /web
[root@1010 ~]# systemctl restart nginx.service
[root@1010 ~]# vim /web/index.html

PixPin_2025-10-24_23-50-09

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#测试
[root@1010 ~]# curl 192.168.232.3:80
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html>
<meta charset="utf-8">

<body>
<div style="text-align:center;">
<img src="https://ts1.tc.mm.bing.net/th/id/R-C.987f582c510be58755c4933cda68d525? rik=C0D21hJDYvXosw&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc% 2ftx%2fwallpaper%2f1305%2f16%2fc4%2f20990657_ 1368686545122.jpg&ehk=netN2qzcCVS4ALUQfDOwxAwFcy41oxC%2b0xTFvOYy5ds% 3d&risl=&pid=ImgRaw&r=0" height="600px" width="800px">
</img>
<div>测试图像</div>
</div>
</body>
</html>
PixPin_2025-10-24_23-52-31

通过不同文件访问web页面

1
2
3
4
5
6
7
#编辑配置文件
[root@1010 ~]# vim /etc/nginx/default.d/web.conf
location /html {
root /web;
index 1.html;
error_log /web/error/web_error.log;
}
1
2
3
4
5
6
[root@1010 ~]# mkdir /web
[root@1010 ~]# mkdir /web/html
[root@1010 ~]# mkdir /web/error
[root@1010 ~]# echo '/web/html mulu xia' > /web/html/1.html
#重启服务
[root@1010 ~]# systemctl restart nginx
1
2
#查看目录结构
[root@1010 ~]# tree /web
PixPin_2025-10-25_11-25-24
1
2
3
4
#测试
[root@1010 ~]# curl 192.168.232.3/html/
/web/html mulu xia

PixPin_2025-10-25_11-19-53

3.3 实验三:基于不同端口(目录、IP、域名)访问不同页面

3.3.1 基于不同端口访问不同页面

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
#方法1:(不覆盖主配置root)
#配置编辑文件
[root@1010 ~]# vim /etc/nginx/conf.d/port.conf
server {
listen 80;
location / {
index port80.html;
}
}
server {
listen 81;
location / {
index port81.html;
}
}
#创建对应html
[root@1010 ~]# cd /usr/share/nginx/html/
[root@1010 html]# echo "This is port 80" > port80.html
[root@1010 html]# echo "This is port 81" > port81.html
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl 192.168.232.3:80
This is port 80
[root@1010 ~]# curl 192.168.232.3:81
This is port 81

PixPin_2025-10-25_15-18-27

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
#方法2:(设置root参数覆盖主配置文件)
#修改配置文件
[root@1010 ~]# vim /etc/nginx/conf.d/port.conf
server {
listen 80;
location / {
root /web;
index port80.html;
}
}
server {
listen 81;
location / {
root /web;
index port81.html;
}
}
#创建对应html
[root@1010 web]# echo 'This is /web 80' > port80.html
[root@1010 web]# echo 'This is /web 81' > port81.html
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl 192.168.232.3:80
This is /web 80
[root@1010 ~]# curl 192.168.232.3:81
This is /web 81

PixPin_2025-10-25_15-13-30

3.3.2 基于不同目录访问不同页面

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
#编辑配置文件
[root@1010 ~]# vim /etc/nginx/conf.d/dir.conf
server {
listen 82;
root /web;
location /dir1 {
index index1.html;
}
location /dir2 {
index index2.html;
}
}
#创建文件
[root@1010 ~]# mkdir /web/dir1 /web/dir2
[root@1010 ~]# tree /web
/web
├── dir1
├── dir2
├── error
│   └── web_error.log
├── html
│   └── 1.html
├── index.html
├── port80.html
└── port81.html

4 directories, 5 files
[root@1010 ~]# echo 'location ---> /web/dir1 ' > /web/dir1/index1.html
[root@1010 ~]# echo 'location ---> /web/dir2 ' > /web/dir2/index2.html
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl 192.168.232.3:82/dir1/
location ---> /web/dir1
[root@1010 ~]# curl 192.168.232.3:82/dir2/
location ---> /web/dir2

PixPin_2025-10-25_15-41-01

root /web 可放在location外面或里面;

3.3.3 基于不同IP访问不同页面

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
#添加IP并激活
[root@1010 ~]#nmtui
#编辑配置文件
[root@1010 ~]# vim /etc/nginx/conf.d/ip.con
server{
listen 192.168.232.3:83;
root /web;
location /dir1 {
index index1.html;
}
}
server{
listen 192.168.232.4:83;
root /web;
location /dir2 {
index index2.html;
}
}
#创建目录与html文件
#延续上面实验的目录,不再重新创建
#修改html内容
[root@1010 ~]# echo '192.168.232.3:83 location ---> /web/dir1 ' > /web/dir1/index1.html
[root@1010 ~]# echo '192.168.232.4:83 location ---> /web/dir2 ' > /web/dir2/index2.html
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl 192.168.232.3:83/dir1/
192.168.232.3:83 location ---> /web/dir1
[root@1010 ~]# curl 192.168.232.4:83/dir2/
192.168.232.4:83 location ---> /web/dir2

PixPin_2025-10-25_15-54-22

3.3.4 基于不同域名访问不同页面

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
#编辑配置文件
[root@1010 ~]# vim /etc/nginx/conf.d/www.conf
server {
listen 84;
root /web;
server_name hym.com;
location /dir1 {
index index3.html;
}
}
server {
listen 84;
root /web;
server_name hym.cn;
location /dir2 {
index index4.html;
}
}
#创建html
[root@1010 ~]# echo 'hym.com ---> dir1/' > /web/dir1/index3.html
[root@1010 ~]# echo 'hym.cn ---> dir2/' > /web/dir2/index4.html
#域名映射
#liunx
[root@1010 ~]# vim /etc/hosts
192.168.232.3 hym.com hym.cn
#windows
C:\Windows\System32\drivers\etc\hosts
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl hym.com:84/dir1/
hym.com ---> dir1/
[root@1010 ~]# curl hym.cn:84/dir2/
hym.cn ---> dir2/

PixPin_2025-10-25_16-30-39

25-10-29(Web)

1.Web理论

理论部分参考 跳转

2.Web实验:

2.1.安装

1
[root@1010 ~]# dnf install httpd-tools-2.4.57-5.el9.x86_64

2.2.访问控制

2.2.1.基于不同用户的访问控制访问同一页面

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
#创建访问用户
[root@1010 ~]# htpasswd -cb /etc/nginx/conf.d/auth_passwd hua1 passwd
Adding password for user hua1
[root@1010 ~]# htpasswd -b /etc/nginx/conf.d/auth_passwd hua2 passwd
Adding password for user hua2
[root@1010 ~]# cat /etc/nginx/conf.d/auth_passwd
hua1:$apr1$abKnNyKn$t4P9lZdmWRurMgmzASRn60
hua2:$apr1$hS73hLM8$z8YEsjzZdRpLfocEJ5KXh.
#配置基本文件
[root@1010 ~]# vim /etc/nginx/conf.d/access.conf
server {
listen 192.168.232.3:80;
root /web;
location / {
auth_basic on;
auth_basic_user_file /etc/nginx/conf.d/auth_passwd;
}
}
server {
listen 192.168.232.4:80;
root /web;
location / {
auth_basic on;
auth_basic_user_file /etc/nginx/conf.d/auth_passwd;
}
}
PixPin_2025-10-29_20-23-33
1
2
3
4
5
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl 192.168.232.3:80 -u hua1
[root@1010 ~]# curl 192.168.232.3:80 -u hua2

(本机)

PixPin_2025-10-29_20-27-30

(浏览器)

PixPin_2025-10-29_20-28-25 PixPin_2025-10-29_20-28-32

2.2.2.基于不同的用户认证访问 不同的页面

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
[root@1010 ~]# tree /web
/web
├── access
│   └── access.html
├── dir1
│   ├── index1.html
│   └── index3.html
├── dir2
│   ├── index2.html
│   └── index4.html
├── error
│   └── web_error.log
├── html
│   └── 1.html
├── index.html
├── port80.html
└── port81.html

5 directories, 10 files

[root@1010 ~]# htpasswd -cb /etc/nginx/conf.d/auth_passwd1 hua3 passwd
Adding password for user hua3
[root@1010 ~]# tree /etc/nginx/conf.d/
/etc/nginx/conf.d/
├── access.conf
├── auth_passwd
├── auth_passwd1
├── dir.conf
├── ip.conf
├── port.conf
└── www.conf

0 directories, 7 files


[root@1010 ~]# vim /etc/nginx/conf.d/access.conf
server {
listen 192.168.232.3:80;
root /web;
location / {
auth_basic on;
auth_basic_user_file /etc/nginx/conf.d/auth_passwd;
}
}
server {
listen 192.168.232.4:80;
root /web;
location /html {
auth_basic on;
auth_basic_user_file /etc/nginx/conf.d/auth_passwd1;
index 1.html;
}
}


[root@1010 ~]# systemctl restart nginx

#测试
[root@1010 ~]# curl 192.168.232.3:80 -u hua2
Enter host password for user 'hua2':
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html>
<meta charset="utf-8">

<body>
<div style="text-align:center;">
<img src="https://ts1.tc.mm.bing.net/th/id/R-C.987f582c510be58755c4933cda68d525? rik=C0D21hJDYvXosw&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc% 2ftx%2fwallpaper%2f1305%2f16%2fc4%2f20990657_ 1368686545122.jpg&ehk=netN2qzcCVS4ALUQfDOwxAwFcy41oxC%2b0xTFvOYy5ds% 3d&risl=&pid=ImgRaw&r=0" height="600px" width="800px">
</img>
<div>测试图像</div>
</div>
</body>
</html>
[root@1010 ~]# curl 192.168.232.4:80/html/ -u hua3
Enter host password for user 'hua3':
/web/html mulu xia


(本机)

PixPin_2025-10-29_20-43-33

(浏览器)

PixPin_2025-10-29_20-44-03

2.2.3 基于源ip的访问控制

1
2
3
4
5
6
7
8
9
10
11
12
#编辑配置文件
[root@1010 ~]# vim /etc/nginx/conf.d/access.conf
server {
listen 192.168.232.3:80;
root /web;
location / {
allow 192.168.232.1;
deny all;
# auth_basic on;
# auth_basic_user_file /etc/nginx/conf.d/auth_passwd;
}
}

PixPin_2025-10-29_20-47-35

1
2
3
4
5
6
7
8
9
10
11
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
[root@1010 ~]# curl 192.168.232.3:80
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
PixPin_2025-10-29_20-48-39

2.3 https

2.3.1 搭建nginx+ssl的加密认证web服务器

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
#创建私钥
[root@1010 ~]# openssl genrsa -out /web/https/1.key
#基于私玥创建证书
[root@1010 ~]# openssl req -utf8 -new -key /web/https/1.key -x509 -days 100 -out /web/https/ssl.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HuNan
Locality Name (eg, city) [Default City]:Changsha
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:www.hym.com
Email Address []:

#编辑配置文件
[root@1010 ~]# vim /etc/nginx/conf.d/openss.conf
server {
listen 192.168.232.3:443 ssl ;
root /web ;
ssl_certificate "/web/https/ssl.crt";
ssl_certificate_key "/web/https/1.key";
location / {
index index.html;
}
}
#重启服务
[root@1010 ~]# systemctl restart nginx
#测试
PixPin_2025-10-29_20-59-57 PixPin_2025-10-29_21-00-08

25-10-31(DNS)

1.DNS理论

理论部分参考 跳转

2.DNF实验

2.1.安装

1
2
3
#查询软件包名
[root@1010 ~]# dnf search name DNS
[root@1010 ~]# dnf install bind bind-utils -y

2.2.正向解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#修改核心配置文件 /etc/named.conf
[root@1010 ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
allow-query { any; };
};

logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};

zone "." IN {
type hint;
file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

PixPin_2025-11-05_17-33-15

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#配置
[root@1010 ~]# vim /etc/named.rfc1912.zones
zone "hym.com" IN {
type master;
file "hym.com.zone";
allow-update { none; };

};
#创建数据文件,存储dns记录
[root@1010 ~]# cd /var/named/
[root@1010 ~]# cp -a named.localhost hym.com.zone
[root@1010 ~]# vim hym.com.zone
$TTL 1D
@ IN SOA ns.hym.com. admin.hym.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns.hym.com.
ns A 192.168.232.3
www A 192.168.232.4
blog CNAME www.hym.com

PixPin_2025-11-05_18-24-13

1
2
3
4
5
#添加新IP
nmcli connection modify ens160 +ipv4.addresses 172.25.0.100/24
nmcli connection down ens160
nmcli connection up ens160

1
2
3
4
5
6
7
8
9
10
#重启服务
[root@1010 ~]# systemctl restart named
#测试
[root@1010 ~]# host www.hym.com 192.168.232.3
Using domain server:
Name: 192.168.232.3
Address: 192.168.232.3#53
Aliases:

www.hym.com has address 192.168.232.4

PixPin_2025-11-05_19-14-46

2.3.反向解析

1
2
3
4
5
6
7
#修改区域配置文件
[root@1010 ~]# vim /etc/named.rfc1912.zones
zone "232.168.192.in-addr.arpa" IN {
type master;
file "192.168.232.zone";
allow-update { none; };
};

PixPin_2025-11-05_19-27-14

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#编辑数据文件
[root@1010 named]# cd /var/named/
[root@1010 named]# cp -a named.loopback 192.168.232.zone
[root@1010 named]# vim 192.168.232.zone
$TTL 1D
@ IN SOA ns2.hym.com. admin.hym.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns2.hym.com.

4 PTR ns2.hym.com.
5 PTR xixi.hym.com.
6 PTR haha.hym.com.
7 PTR huhu.hym.com.

PixPin_2025-11-05_19-25-59

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
#重启服务
[root@1010 named]# systemctl restart named

#测试
[root@1010 named]# host 192.168.232.6 192.168.232.4
Using domain server:
Name: 192.168.232.4
Address: 192.168.232.4#53
Aliases:

6.232.168.192.in-addr.arpa domain name pointer haha.hym.com.
[root@1010 named]# host 192.168.232.5 192.168.232.4
Using domain server:
Name: 192.168.232.4
Address: 192.168.232.4#53
Aliases:

5.232.168.192.in-addr.arpa domain name pointer xixi.hym.com.
[root@1010 named]# host 192.168.232.7 192.168.232.4
Using domain server:
Name: 192.168.232.4
Address: 192.168.232.4#53
Aliases:

7.232.168.192.in-addr.arpa domain name pointer huhu.hym.com.

PixPin_2025-11-05_19-25-07

2.4.主从同步

2.4.1.完全同步

主:192.168.232.133 从:192.168.232.3

前置:关闭防火墙,调至宽容模式

1
2
systemctl --now disable firewalld
setenforce 0

修改配置文件

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
#主机
[root@hym ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
allow-query { any; };
################################################################
allow-transfer { 192.168.232.3; }; #允许从机转移dns记录
#################################################################
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};

zone "." IN {
type hint;
file "named.ca";
};
############################################
zone "hym.com." IN {
type master;
file "hym.com.zone";
};

zone "232.168.192.in-addr.arpa" IN {
type master;
file "192.168.232.zone";
};
#############################################
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";



#从机
[root@1010 ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
directory "/var/named";
allow-query { any; };
};

logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};

zone "." IN {
type hint;
file "named.ca";
};
#########################################
zone "hym.com." IN {
type slave;
masters { 192.168.232.133; }; #主
file "slaves/hym.com.zone";
};

zone "232.168.192.in-addr.arpa" IN {
type slave;
masters { 192.168.232.133; }; #主
file "slaves/192.168.232.zone";
};
##########################################
include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";

创建数据文件(主机)

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
[root@hym ~]# vim /var/named/hym.com.zone
$TTL 1D
@ IN SOA ns.hym.com. admin.hym.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns.hym.com.

ns A 192.168.232.133
ns2 A 192.168.232.134
www A 192.168.232.135
xixi A 192.168.232.136
blog CNAME www.hym.com.

[root@hym ~]# vim /var/named/192.168.232.zone
$TTL 1D
@ IN SOA ns2.hym.com. admin.hym.com. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS ns2.hym.com.
133 PTR ns.hym.com.
134 PTR ns2.hym.com.
135 PTR www.hym.com.
136 PTR xixi.hym.com.
137 PTR haha.hym.com.

重启服务

1
2
3
4
#主
[root@hym ~]# systemctl restart named
#从
[root@1010 ~]# systemctl restart named

测试(从机)

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
#查看文件变化
[root@1010 ~]# tree /var/named/
/var/named/
├── 192.168.232.zone
├── data
│   └── named.run
├── dynamic
│   ├── managed-keys.bind
│   └── managed-keys.bind.jnl
├── hym.com.zone
├── named.ca
├── named.empty
├── named.localhost
├── named.loopback
└── slaves
├── 192.168.232.zone
└── hym.com.zone

3 directories, 11 files
#使用host测试
[root@1010 ~]# host 192.168.232.136 192.168.232.3
Using domain server:
Name: 192.168.232.3
Address: 192.168.232.3#53
Aliases:

136.232.168.192.in-addr.arpa domain name pointer xixi.hym.com.
[root@1010 ~]# host xixi.hym.com 192.168.232.3
Using domain server:
Name: 192.168.232.3
Address: 192.168.232.3#53
Aliases:

xixi.hym.com has address 192.168.232.136

PixPin_2025-11-06_17-49-24

2.4.2.增量同步(正反向解析同理)

添加记录(主机)

1
2
[root@hym ~]# vim /var/named/hym.com.zone
haha A 192.168.232.137 ;新增加

PixPin_2025-11-06_16-17-04

1
2
#重启服务
[root@hym ~]# systemctl restart named

进行增量同步并测试(从机)

1
2
3
4
5
6
7
8
9
10
11
#同步
[root@1010 ~]# rndc refresh hym.com.
zone refresh queued
#测试
[root@1010 ~]# host haha.hym.com 192.168.232.3
Using domain server:
Name: 192.168.232.3
Address: 192.168.232.3#53
Aliases:

haha.hym.com has address 192.168.232.137

PixPin_2025-11-06_16-18-59

25-11-6(NFS)

1.NFS理论

理论部分参考 跳转

2.NFS实验

2.1.安装(两端)

1
[root@hym ~]# dnf install nfs-utils rpcbind -y

2.2.服务端配置

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
#编辑配置文件
[root@hym ~]# vim /etc/exports
/server/dir1 *(rw,root_squash)
/server/dir2 *(rw,all_squash,anonuid=10000,anongid=10000)
#创建对应目录
[root@hym ~]# mkdir /server
[root@hym ~]# mkdir /server/dir{1,2}
[root@hym ~]# tree /server/
/server/
├── dir1
└── dir2
#给予权限
[root@hym ~]# chmod 755 -R /server/
#根据配置文件进行补充,比如用户权限
[root@hym ~]# useradd -u 10000 hym
[root@hym ~]# id hym
用户id=10000(hym) 组id=10000(hym) 组=10000(hym)
[root@hym ~]# setfacl -m u:hym:rwx /server/dir2
#导出
[root@hym ~]# exportfs -ar
#启动服务
[root@hym ~]# systemctl start nfs-server
#验证共享配置
[root@hym ~]# showmount -e localhost
Export list for localhost:
/server/dir2 *
/server/dir1 *

PixPin_2025-11-06_23-09-50

2.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
#测试服务端共享
[root@1010 ~]# showmount -e 192.168.232.133
Export list for 192.168.232.133:
/server/dir2 *
/server/dir1 *
#创建本地挂载点
[root@1010 ~]# mkdir /bendi
[root@1010 ~]# mkdir /bendi/dir{1,2}
[root@1010 ~]# tree /bendi/
/bendi/
├── dir1
└── dir2
#挂载
[root@1010 ~]# mount -t nfs 192.168.232.133:/server/dir1 /bendi/dir1
[root@1010 ~]# mount -t nfs 192.168.232.133:/server/dir2 /bendi/dir2
#查看挂载状态
[root@1010 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 866M 0 866M 0% /dev/shm
tmpfs 347M 5.1M 342M 2% /run
/dev/mapper/rhel-root 17G 1.7G 15G 11% /
/dev/nvme0n1p2 960M 217M 744M 23% /boot
/dev/nvme0n1p1 599M 7.0M 592M 2% /boot/efi
/dev/sr0 9.9G 9.9G 0 100% /mnt
tmpfs 174M 0 174M 0% /run/user/0
192.168.232.133:/server/dir1 17G 1.7G 15G 11% /bendi/dir1
192.168.232.133:/server/dir2 17G 1.7G 15G 11% /bendi/dir2
#测试
[root@1010 ~]# echo "dir1" > /bendi/dir1/test.txt
-bash: /bendi/dir1/test.txt: 权限不够
[root@1010 ~]# ll -d /bendi/dir1
drwxr-xr-x. 2 root root 6 11月 6 21:53 /bendi/dir1
[root@1010 ~]# echo "dir2" > /bendi/dir2/test.txt
[root@1010 ~]# ll /bendi/dir2/test.txt
-rw-r--r--. 1 10000 10000 5 11月 6 21:48 /bendi/dir2/test.txt
####dir1的权限不足,只能查看,dir2的权限给了rw所以客户端能够创建文件
####dir1的权限为可读,dir2目录hym用户权限为可读可写
###dir1需要写权限时,可在服务端修改对应目录权限

PixPin_2025-11-06_22-16-46

2.4.开机挂载

1
2
3
4
5
6
7
#编辑配置文件
[root@1010 ~]# vim /etc/fstab
192.168.232.133:/server/dir1 /bendi/dir1 nfs defaults 0 0
192.168.232.133:/server/dir2 /bendi/dir2 nfs defaults 0 0
#立即生效
[root@1010 ~]# mount -a

PixPin_2025-11-06_22-49-25

PixPin_2025-11-06_22-49-12

2.5.动态挂载(无进程时会自动卸载)

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
#客户端安装 autofs
[root@1010 ~]# yum install autofs -y
#编辑主配置文件
[root@1010 ~]# vim /etc/auto.misc
/nfs_auto /etc/auto.misc
#编辑子配置文件
[root@1010 ~]# vim /etc/auto.misc
dir1 -fstype=nfs 192.168.232.133:/server/dir1
dir2 -fstype=nfs 192.168.232.133:/server/dir2
#启动服务
[root@1010 ~]# systemctl start autofs
#访问挂载点实现动态挂载
[root@1010 ~]# ls /nfs_auto/dir1
hy
[root@1010 ~]# ls /nfs_auto/dir2
test.txt
#查看挂载状态
[root@1010 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 866M 0 866M 0% /dev/shm
tmpfs 347M 6.5M 340M 2% /run
/dev/mapper/rhel-root 17G 1.7G 15G 11% /
/dev/nvme0n1p2 960M 217M 744M 23% /boot
/dev/nvme0n1p1 599M 7.0M 592M 2% /boot/efi
/dev/sr0 9.9G 9.9G 0 100% /mnt
tmpfs 174M 0 174M 0% /run/user/0
192.168.232.133:/server/dir1 17G 1.7G 15G 11% /bendi/dir1
192.168.232.133:/server/dir2 17G 1.7G 15G 11% /bendi/dir2
192.168.232.133:/server/dir1 17G 1.7G 15G 11% /nfs_auto/dir1
192.168.232.133:/server/dir2 17G 1.7G 15G 11% /nfs_auto/dir2

PixPin_2025-11-06_22-57-35

PixPin_2025-11-06_22-59-45

PixPin_2025-11-06_23-02-31

1
2
3
#修改卸载时间(默认300s)
[root@1010 ~]# vim /etc/autofs.conf
timeout = 300

PixPin_2025-11-06_23-07-52

25-11-13(SELiunx)

1.SELiunx理论

理论部分参考 跳转

SELinux 是一种强制访问控制(MAC)安全机制,作为 Linux 内核的安全模块,它弥补了传统 Linux 自主访问控制的缺陷,从内核层面强化系统安全。

工作过程

img

三种工作模式

模式 说明
enforcing 强制模式,正常加载并执行策略,拦截违规访问并记录日志
permissive 宽容模式,不拦截违规操作,仅记录日志,常用于调试策略
disabled 关闭模式,SELinux 完全不生效

2.SELiunx实验

2.1.实验1:SELiunx端口限制实践

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#前置
systemctl start nginx
setroubleshoot # 提供解决方案
#检查
[root@server ~]# systemctl status nginx
#查看
[root@server ~]# ps -axZ | grep nginx
[root@server ~]# systemctl stop nginx
[root@server ~]# semanage port -l | grep http
#修改配置文件
[root@server ~]# vim /etc/nginx/nginx.conf
server {
listen 84;
}
#启动服务
[root@server ~]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
[root@server ~]# dnf install setroubleshoot
#查看日志
[root@server ~]# tail -f /var/log/messages

PixPin_2025-11-21_00-17-36

1
2
3
4
#根据日志进行配置
[root@server ~]# semanage port -a -t http_port_t -p tcp 82
[root@server ~]# semanage port -l | grep http
[root@server ~]# systemctl restart nginx

2.2.实验2:SELiunx类型限制实践-管理安全上下文

1
2
3
4
5
6
7
8
9
10
11
12
13
#更改SELinux模式为Permissive
[root@server ~]# setenforce 0
[root@server ~]# getenforce
Permissive

#创建网页页面
[root@server ~]# mkdir /web
[root@server ~]# echo “SELiunx 82” > /web/index.html

#修改nginx配置
[root@server ~]# vim /etc/nginx/nginx.conf
listen 82;
root /web;

PixPin_2025-11-21_23-34-19

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#重启服务
[root@server ~]# systemctl restart nginx.service

#修改SELinux模式为Enforcing
[root@server ~]# setenforce 1
[root@server ~]# getenforce
Enforcing

#查看日志
[root@server ~]# tail -f /var/log/messages

#测试curl网页
[root@server ~]# curl 192.168.232.133:82
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
#发现失败,返回403

PixPin_2025-11-21_23-39-36

1
#查看日志报错

PixPin_2025-11-21_23-40-49

1
2
3
4
5
6
7
8
9
10
#根据日志报错修改index文件标签
[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/web/index.html'

#查看文件
[root@server ~]# restorecon -R -v /web/index.html
Relabeled /web/index.html from unconfined_u:object_r:default_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

#重新测试
[root@server ~]# curl 192.168.232.133:82
SELiunx 82

2.3.实验3:SELiunx类型限制实践

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
#安装与启动
[root@server ~]# dnf install nginx -y
[root@server ~]# yum install setroubleshoot -y
[root@server ~]# systemctl start nginx
[root@server ~]# systemctl start setroubleshootd.service

[root@server ~]# mkdir -p /web/http
[root@server ~]# echo "SELinux 84" > /web/http/index.html
[root@server ~]# vim /etc/nginx/nginx.conf
server {
listen 84
root /web/http;
}
[root@server ~]# systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xeu nginx.service" for details.
[root@server ~]# semanage port -a -t http_port_t -p tcp 84
[root@server ~]# systemctl restart nginx.service

[root@server ~]# curl 192.168.232.133:84
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

[root@server ~]# semanage fcontext -a -t httpd_sys_content_t '/web/http/index.html'
[root@server ~]# systemctl restart nginx.service
[root@server ~]# curl 192.168.232.133:84
SELinux 84

25-11-15(iptables/firewall)

1.防火墙理论

理论部分参考 跳转

1.1.iptables基本命令

安装iptables

1
2
3
4
5
6
7
8
#1、RHEL9默认使用firewalld,需先禁用firewalld并安装iptables服务:
[root@hym ~]# systemctl stop firewalld
[root@hym ~]# systemctl disable --now firewalld
[root@hym ~]# systemctl mask firewalld
#2、安装iptables
dnf install iptables-services -y
systemctl enable --now iptables.service
systemctl status iptables.service

增删改查

1
2
3
4
5
6
7
#1.增加
#1.1追加到末尾
iptables -A INPUT -p tcp --dport 22 -j ACCEPT #允许SSH通过TCP 22端口(追加到INPUT链末尾)
#1.2插入规则到链的指定位置
iptables -I INPUT 2 -s 192.168.1.0/24 -j DROP #在INPUT链的第2条位置插入规则,丢弃来自192.168.1.0/24网段的所有流量
#1.3指定表(如filter表)插入规则
iptables -t filter -I FORWARD -d 192.168.10.182 -p tcp --dport 80:443 -j ACCEPT #允许目标为192.168.10.182的TCP 80-443端口流量通过FORWARD链
1
2
3
#2.删除
iptables -t 表名 -F 链名 #清空指定链
iptables -t 表名 -F #清空当前表的所有链(默认 `filter` 表)
1
2
3
4
5
6
#3.查询
iptables -L -t filter -n #查看指定表
iptables -L -nv -t filter --line-numbers #查看所有链上的 指定表( -t fileter )的所有规则

vim /etc/sysconfig/iptables #规则链存储文件

相关命令

1
2
3
4
5
6
7
8
9
10
11
#查看指定表
[root@localhost ~]# iptables -L -t mangle -n
[root@localhost ~]# iptables -L -t nat -n
[root@localhost ~]# iptables -L -t raw -n
[root@localhost ~]# iptables -L -t filter -n

#规则链存储文件
vim /etc/sysconfig/iptables

#查看所有链上的 指定表( -t fileter )的所有规则
iptables -L -nv -t filter --line-numbers

2.防火墙实验

2.1.firewall实验

2.1.1.firewalld区域添加http服务,使其为放行状态

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
#安装nginxa
[root@server ~]# dnf install -y nginx
[root@server ~]# echo "firewalld" > /usr/share/nginx/html/index.html
[root@server ~]# systemctl restart nginx.service

[root@server ~]# firewall-cmd --get-default-zone
public
[root@server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client ssh
ports:
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

[root@server ~]# firewall-cmd --permanent --zone=public --add-service=http
success
[root@server ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp
success
[root@server ~]# firewall-cmd --reload
success

[root@server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http ssh
ports: 80/tcp
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

2.1.2.某些服务需要编辑zone文件才能添加服务,添加nginx服务

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
 firewall-cmd   --add-service=nginx  --permanent# 查看所有区域支持的服务
[root@server ~]# firewall-cmd --get-services

[root@server ~]# firewall-cmd --permanent --add-service=nginx
Error: INVALID_SERVICE: Zone 'public': 'nginx' not among existing services


[root@server ~]# cp -a /usr/lib/firewalld/services/http.xml /usr/lib/firewalld/services/nginx.xml
[root@server ~]# vim /usr/lib/firewalld/services/nginx.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>nginx</short>
<description></description>
<port protocol="tcp" port="80"/>
<port protocol="tcp" port="443"/>
</service>

[root@server ~]# firewall-cmd --permanent --add-service=nginx
success
[root@server ~]# firewall-cmd --reload
success
[root@server ~]# firewall-cmd --zone=public --list-service
cockpit dhcpv6-client http nginx ssh
[root@server ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http nginx ssh
ports: 80/tcp
protocols:
forward: yes
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:

2.1.3.禁止192.168.48.0/24 网段的地址进行ping

1
2
3
4
5
6
7
[root@server ~]# man firewalld.richlanguage

[root@server ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.232.0/24" protocol value="icmp" reject'
success
[root@server ~]# firewall-cmd --reload
success
#测试

PixPin_2025-11-22_14-24-30

2.1.4.端口转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@server ~]# dnf install net-tools -y
[root@server ~]# netstat -lntup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1783/nginx: master
tcp6 0 0 :::80 :::* LISTEN 1783/nginx: master
[root@server ~]# vim /etc/nginx/nginx.conf
[root@server ~]# setenforce 0
[root@server ~]# systemctl restart nginx.service
[root@server ~]# vim /etc/nginx/nginx.conf
[root@server ~]# setenforce 0
[root@server ~]# systemctl res
rescue reset-failed restart
[root@server ~]# systemctl restart nginx.service
[root@server ~]# firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.232.1" forward-port port="80" protocol="tcp" to-port="99"'
success
[root@server ~]# firewall-cmd --reload
success

PixPin_2025-11-22_14-46-55

25-12-3(LNMP)

1.LNMP理论

理论部分参考 跳转

2.LNMP搭建实验

2.1.编写脚本

2.1.1.基础配置脚本sys.sh

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
#sys.sh
#!/bin/bash
#1.安装基础软件
install_app() {
yum install httpd mariadb-server php php-fpm php-mysqlnd nginx -y >/dev/null
yum install zip -y >/dev/null
}
#2.创建目录
dir_create() {
mkdir -p /web/ssl /web/www
}
#3.创建https证书
ssl() {
openssl genrsa --out /web/ssl/ssl.key
openssl req -utf8 -new -key /web/ssl/ssl.key -x509 -days 100 -out /web/ssl/ssl.crt \
-subj "/C=CN/ST=HuNan/L=Changsha/O=./OU=./CN=www.hym.com/emailAddress=admin@hym.com" >/dev/null
}
#4.http前置
http() {
unzip Dis* -d /web/www >/dev/null && echo "解压成功" || echo "解压失败"
cd /web/www/upload/
chmod -R 777 config/ uc_* data
}
#关闭安全措施
safe() {
setenforce 0
systemctl disable --now firewalld >/dev/null
}

2.1.2.应用配置脚本app.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#app.sh
#!/bin/bash
#Nginx
nginx_config() {
mv /root/nginx.conf /etc/nginx/conf.d/nginx.conf >/dev/null 2>&1
systemctl restart nginx >/dev/null 2>&1
}
#mysql
mysql_config() {
systemctl start mariadb >/dev/null 2>&1
mysql -u root -e "
alter user 'root'@'localhost' identified by 'passwd';
create database luntan;
flush privileges; " >/dev/null 2>&1 \
&& echo "你的数据库名为luntan,密码为passwd"
}

2.1.3.运行脚本do.sh

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
#do.sh
#!/bin/bash
source sys.sh
source app.sh
#关闭防火墙
echo "=============="
safe
if [ $? -eq 0 ];then
echo 防火墙关闭 成功
else
echo 防火墙关闭 失败
fi
#1.软件安装
echo "=============="
install_app
if [ $? -eq 0 ];then
echo 软件安装 成功
else
echo 软件安装 失败
fi
#2.创建目录
echo "=============="
dir_create
if [ $? -eq 0 ];then
echo 目录创建 成功
else
echo 目录创建 失败
fi
#3.创建https证书
echo "=============="
ssl
if [ $? -eq 0 ];then
echo 证书创建 成功
else
echo 证书创建 失败
fi
#4.http搭建
echo "=============="
http
if [ $? -eq 0 ];then
echo http配置 成功
else
echo http配置 失败
fi
#5.nginx配置
echo "=============="
nginx_config
if [ $? -eq 0 ];then
echo nginx配置 成功
else
echo nginx配置 失败
fi
#6.mysql配置
echo "=============="
mysql_config
if [ $? -eq 0 ];then
echo mysql配置 成功
else
echo mysql配置 失败
fi
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
root /web/www/upload;
include /etc/nginx/default.d/php.conf;
}
server {
listen 443 ssl http2;
root /web/www/upload;
include /etc/nginx/default.d/php.conf;

ssl_certificate "/web/ssl/ssl.crt";
ssl_certificate_key "/web/ssl/ssl.key";
}

2.2.运行

2.2.1.准备文件

PixPin_2025-12-05_17-08-59

2.2.2.运行shell脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@server ~]# bash do.sh 
==============
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
防火墙关闭 成功
==============
软件安装 成功
==============
目录创建 成功
==============
证书创建 成功
==============
解压成功
http配置 成功
==============
nginx配置 成功
==============
你的数据库名为luntan,密码为passwd
mysql配置 成功

PixPin_2025-12-05_17-16-50

2.3.测试

PixPin_2025-12-05_17-17-42

PixPin_2025-12-05_17-18-19

25-12-13(SHELL)

1.SHELL理论

01 入门: 02 变量: 03 条件测试: 04 流程判断: 05 函数: 06 数组: 07 grep: 08 sed: 09 awk:
跳转 跳转 跳转 跳转 跳转 跳转 跳转 跳转 跳转

2.SHELL实验:

2.1.实验1:提示用户输入用户名称,判断用户是否存在。

附加要求:无论存在与否都初始化用户密码为redhat,如果用户不存在就创建用户,然后初始化密码为redhat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@hym ~]# vim /check_user.sh
#!/bin/bash
passwd_init() {
echo "正在初始化密码..."
passwd $name <<end >/dev/null 2>&1
redhat
redhat
end
echo "初始化成功,初始密码为redhat"
}

read -p "请输入用户名称:" name
id $name >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "$name 已存在"
passwd_init
else
echo "正在创建用户$name"
useradd $name
echo "用户$name创建成功."
passwd_init
fi

PixPin_2026-01-13_23-06-17

测试

PixPin_2026-01-13_23-10-39

2.1 实验2:创建一个简单的系统信息报告脚本,输出基本的系统状态信息

附加:当前登录用户名称、报告生成时间、磁盘使用情况、内存使用情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@hym shell]# vim sys.sh
#!/bin/bash

#输出报告
echo "====系统基本信息===="
echo "当前用户:"
whoami
echo ""
echo "----磁盘使用情况----"
df -h
echo "----内存使用情况----"
free -h
echo "生成时间:"
date
echo "====报告结束===="

PixPin_2026-01-14_20-52-19

2.2 实验3:检查文件

检查文件 /etc/passwd是否存在 并且 可读,如果两个条件都满足,则输出”文件存在且可读”,否则输出相应错误信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@hym shell]# vim check.sh
#!/bin/bash
file="/etc/passw"

if [ -e "$file" ] && [ -r "$file" ] ; then
echo "$file 存在且可读"
else

[ -e "$file" ] || "$file 不存在"

[ -r "$file" ] || "$file 不可读"
fi


#测试
[root@hym shell]# bash check.sh
/etc/passwd 存在且可读

[root@hym shell]# bash check.sh
check.sh:行4: /etc/passw 不存在: 没有那个文件或目录
check.sh:行5: /etc/passw 不可读: 没有那个文件或目录

25-12-20(Ansible)

1.Ansible理论

理论部分参考 跳转

2.Ansible实验:

2.1.用ansible命令行实现nginx部署修改端口号

2.1.1.建立连接
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
[root@client ~]vim /hosts
[server]
192.168.232.134
[root@client ~]vim /auth.yml
---
- hosts: server # 目标主机组(对应你清单里的[newserver])
gather_facts: no # 关闭事实收集(首次推送公钥时,受控节点可能未配置Python依赖)
vars:
ansible_ssh_pass: "passwd"
ansible_host_key_checking: False
tasks:
- name: 给root用户添加SSH公钥(Ansible管理)
authorized_key:
user: root # 指定目标用户(必填)
state: present # 确保公钥存在(添加)
key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}" # 读取本地公钥
[root@client ~]# ansible-playbook -i /hosts /auth.yml

PLAY [server] ******************************************************************

TASK [给root用户添加SSH公钥(Ansible管理)] ************************************
changed: [192.168.232.134]

PLAY RECAP *********************************************************************
192.168.232.134 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.1.2.创建目录
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@client ~]# vim file.sh

#!/bin/bash

mkdir /ansible

mkdir -p /ansible/roles/warehouse/{vars,tasks}

mkdir -p /ansible/roles/safety/{vars,tasks}

mkdir -p /ansible/roles/web/{vars,tasks}

touch /ansible/site.yml

PixPin_2025-12-20_19-31-39

2.1.3.编写相关配置
1
2
3
4
5
6
7
8
9
10
11
[root@client ~]# cat /ansible/site.yml 

---
- hosts: 192.168.232.134
gather_facts: true
become: yes
roles:
- warehouse
- safety
- web

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
[root@client ~]# vim /ansible/roles/warehouse/tasks/main.yml

---
- name: 挂载
mount:
path: /mnt
src: /dev/sr0
state: mounted
fstype: iso9660
- name: 配置仓库源1
yum_repository:
file: cangku
name: base
description: base
state: present
enabled: yes
gpgcheck: no
baseurl: /mnt/BaseOS
- name: 配置仓库源2
yum_repository:
file: cangku
name: app
description: app
state: present
enabled: yes
gpgcheck: no
baseurl: /mnt/AppStream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@client ~]# vim /ansible/roles/safety/tasks/main.yml

---
- name: SELinx设置宽容模式
ansible.builtin.selinux:
policy: targeted
state: permissive
- name: 放行89端口
ansible.builtin.firewalld:
port: 89/tcp
permanent: true
immediate: true
state: enabled


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@client ~]# vim /ansible/roles/web/tasks/main.yml

---
- name: 安装nginx
yum:
name: nginx
state: present
- name: 启动nginx
service:
name: nginx
enabled: true
- name: 修改端口号
lineinfile:
path: /etc/nginx/nginx.conf
regexp: '^(\s*)listen\s.*;'
line: '\1listen 89;'
backup: yes
backrefs: yes
- name: 重启配置
systemd:
name: nginx
state: restarted
2.1.4.测试

image-20251220194237778