运维 Linux 个人笔记 RHCE-Linux的基础部分笔记 Kaitumei 2026-02-01 2026-02-02 25-10-16(SSH) 1.SSH理论 理论部分参考 跳转
查看 sshd 是否使用
systemctl status sshd
查询 sshd 服务的运行状态
netstat -intup | grep sshd
查询 sshd 服务对应的网络连接、监听端口
ps -aux | grep sshd
查询系统中所有和 sshd 相关的进程详情
原理图
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 [root@ssh ~]# vim /etc/ssh/sshd_config 21 22 Port 666 [root@ssh ~]# systemctl --now disable firewalld [root@ssh ~]# setenforce 0 [root@ssh ~]# getenforce [root@ssh ~]# systemctl restart sshd
2.2 拒绝root用户远程登录 1 2 3 4 5 6 7 8 9 10 11 12 [root@ssh ~]# vim /etc/ssh/sshd_config PermitRootLogin no [root@ssh ~]# systemctl restart sshd [root@ssh ~]# vim /etc/ssh/sshd_config.d/01-permitrootlogin.conf PermitRootLogin no [root@ssh ~]# systemctl restart sshd
2.3 允许特定用户ssh登录,其余用户无法登录(包括root用户) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [root@ssh ~]# vim /etc/ssh/sshd_config AllowUsers zhangsan [root@ssh ~]# systemctl restart sshd [root@ssh ~]# systemctl restart sshd [root@ssh ~]# useradd zhangsan [root@ssh ~]# passwd zhangsan 更改用户 zhangsan 的密码 。 新的密码: 无效的密码: 密码少于 8 个字符 重新输入新的密码: passwd:所有的身份验证令牌已经成功更新。
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 [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. [root@client ~]# ssh root@172.25.254.129
ssh-keygen (生成、管理和转换认证密钥) -t (制定类型 RSA) /root/.ssh/id_rsa —— 私钥文件 /root/.ssh/id_rsa.pub —– 公钥文件
2.5 windows客户端通过秘钥登录linux服务端root用户
上传至autgorized_keys
1 2 3 4 5 6 7 ssh-keygen -t rsa ssh root@192.168.223.5
加密
公钥<————>私钥
解密
3. SSH协议原理 3.1 连接过程
说明
3.2 两种认证方式
密码认证:通过用户名+密码验证(安全性较低,易受暴力破解)。
公钥认证:基于密钥对(公钥存服务器,私钥存客户端),安全性更高,是推荐方式。
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 warning: commands will be executed using /bin/sh at> wall 6666 at> <EOT> job 1 at Sun Jan 5 9:59:00 2025 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 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] noon(12:00) midnight(00:00) teatime(4pm) now +1 min now +2 days
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 [root@1010 ~]# vim /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root [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 -l 列出当前用户的 crontab 文件内容。 crontab -r 删除当前用户的 crontab 文件。 crontab -e 修改任务的执行时间和任务内容。 [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 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点的每一分钟都执行 * * L 2 * [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 [root@1010 ~] [root@1010 ~] 01 * * * * run-parts /scripts [root@1010 ~] [root@1010 ~] [root@1010 ~] [root@1010 ~] [root@1010 ~] [root@1010 ~] * 11 * * * root run-parts /shell [root@1010 ~]
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
(2) 编辑配置文件
检查网络
1 [root@1010 ~]# ping ntp.aliyun.com
修改/etc/chrony.conf文件
1 [root@1010 ~]# vim /etc/chrony.conf
(3) 重启服务
1 2 [root@1010 ~]# systemctl restart chronyd [root@1010 ~]# systemctl status chronyd
(4) 验证
1 [root@1010 ~]# chronyc sources
与阿里云地址相同,同步成功
1 2 [root@1010 ~]# timedatectl status [root@1010 ~]# date
2.2.2 实验2—搭建本地时间同步服务器 服务端配置(192.168.232.3) (1) 网络检查(ping客户端) 1 [root@1010 ~]# ping 192.168.232.131
(2) 编辑配置文件(已安装服务) 1 [root@1010 ~]# vim /etc/chrony.conf
基于实验一已经配置阿里云的时间源的情况下,允许客户端同步服务端的时间源
1 [root@1010 ~]# systemctl restart chronyd
客户端(192.168.232.131) (1) 网络检查 1 [root@hym ~]# ping 192.168.232.3
(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
(4) 检查 1 2 3 [root@hym ~]# chronyc sources [root@1010 ~]# timedatectl status [root@1010 ~]# date
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
2.2 实验2:修改端口、默认目录、默认文件访问web页面 多端口访问同一web页面 1 2 3 4 5 [root@1010 ~]# vim /etc/nginx/nginx.conf listen 80; listen 81;
1 2 3 4 5 6 7 8 [root@1010 ~]# systemctl restart nginx.service [root@1010 ~]# systemctl --now disable firewalld [root@1010 ~]# setenforce 0 [root@1010 ~]# netstat -lntup | grep nginx
1 2 3 [root@1010 ~]# curl 192.168.232.3:80 [root@1010 ~]# curl 192.168.232.3:81
自定义默认目录 1 2 3 4 [root@1010 ~]# vim /etc/nginx/nginx.conf root /web;
1 2 3 [root@1010 ~]# mkdir /web [root@1010 ~]# systemctl restart nginx.service [root@1010 ~]# vim /web/index.html
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>
通过不同文件访问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
1 2 3 4 [root@1010 ~]# curl 192.168.232.3/html/ /web/html mulu xia
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 [root@1010 ~]# vim /etc/nginx/conf.d/port.conf server { listen 80; location / { index port80.html; } } server { listen 81; location / { index port81.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
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@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; } } [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
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
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 [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; } } [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
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; } } [root@1010 ~]# echo 'hym.com ---> dir1/' > /web/dir1/index3.html [root@1010 ~]# echo 'hym.cn ---> dir2/' > /web/dir2/index4.html [root@1010 ~]# vim /etc/hosts 192.168.232.3 hym.com hym.cn 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/
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; } }
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
(本机)
(浏览器)
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
(本机)
(浏览器)
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; } }
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>
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 #测试
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 [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" ;
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; }; }; [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
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
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; }; };
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.
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.
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; }; }; 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 [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
2.4.2.增量同步(正反向解析同理) 添加记录(主机)
1 2 [root@hym ~]# vim /var/named/hym.com.zone haha A 192.168.232.137 ;新增加
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
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 *
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
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
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 [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
1 2 3 [root@1010 ~]# vim /etc/autofs.conf timeout = 300
25-11-13(SELiunx) 1.SELiunx理论 理论部分参考 跳转
SELinux 是一种强制访问控制(MAC)安全机制,作为 Linux 内核的安全模块,它弥补了传统 Linux 自主访问控制的缺陷,从内核层面强化系统安全。
工作过程
三种工作模式
模式
说明
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
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 [root@server ~]# setenforce 0 [root@server ~]# getenforce Permissive [root@server ~]# mkdir /web [root@server ~]# echo “SELiunx 82” > /web/index.html [root@server ~]# vim /etc/nginx/nginx.conf listen 82; root /web;
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 [root@server ~]# setenforce 1 [root@server ~]# getenforce Enforcing [root@server ~]# tail -f /var/log/messages [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>
1 2 3 4 5 6 7 8 9 10 [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 [root@hym ~]# systemctl stop firewalld [root@hym ~]# systemctl disable --now firewalld [root@hym ~]# systemctl mask firewalld dnf install iptables-services -y systemctl enable --now iptables.service systemctl status iptables.service
增删改查
1 2 3 4 5 6 7 iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -I INPUT 2 -s 192.168.1.0/24 -j DROP iptables -t filter -I FORWARD -d 192.168.10.182 -p tcp --dport 80:443 -j ACCEPT
1 2 3 iptables -t 表名 -F 链名 iptables -t 表名 -F
1 2 3 4 5 6 iptables -L -t filter -n iptables -L -nv -t filter --line-numbers 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 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 [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
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
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 install_app () { yum install httpd mariadb-server php php-fpm php-mysqlnd nginx -y >/dev/null yum install zip -y >/dev/null } dir_create () { mkdir -p /web/ssl /web/www } 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 } 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 nginx_config () { mv /root/nginx.conf /etc/nginx/conf.d/nginx.conf >/dev/null 2>&1 systemctl restart nginx >/dev/null 2>&1 } 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 source sys.shsource app.shecho "==============" safe if [ $? -eq 0 ];then echo 防火墙关闭 成功 else echo 防火墙关闭 失败 fi echo "==============" install_app if [ $? -eq 0 ];then echo 软件安装 成功 else echo 软件安装 失败 fi echo "==============" dir_create if [ $? -eq 0 ];then echo 目录创建 成功 else echo 目录创建 失败 fi echo "==============" ssl if [ $? -eq 0 ];then echo 证书创建 成功 else echo 证书创建 失败 fi echo "==============" http if [ $? -eq 0 ];then echo http配置 成功 else echo http配置 失败 fi echo "==============" nginx_config if [ $? -eq 0 ];then echo nginx配置 成功 else echo nginx配置 失败 fi 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.准备文件
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配置 成功
2.3.测试
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 passwd_init () { echo "正在初始化密码..." passwd $name <<end >/dev/null 2>&1 redhat redhat end echo "初始化成功,初始密码为redhat" } read -p "请输入用户名称:" nameid $name >/dev/null 2>&1if [ $? -eq 0 ]; then echo "$name 已存在" passwd_init else echo "正在创建用户$name " useradd $name echo "用户$name 创建成功." passwd_init fi
测试
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 echo "====系统基本信息====" echo "当前用户:" whoami echo "" echo "----磁盘使用情况----" df -hecho "----内存使用情况----" free -h echo "生成时间:" date echo "====报告结束===="
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 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 gather_facts: no 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 mkdir /ansiblemkdir -p /ansible/roles/warehouse/{vars,tasks}mkdir -p /ansible/roles/safety/{vars,tasks}mkdir -p /ansible/roles/web/{vars,tasks}touch /ansible/site.yml
2.1.3.编写相关配置 1 2 3 4 5 6 7 8 9 10 11 [root@client ~ ] --- - 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 ~ ] --- - 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 ~ ] --- - 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 ~ ] --- - 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.测试