linux/linux系统管理.md
2025-06-19 09:49:48 +08:00

1553 lines
49 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

centos官网[https://centos.org/](https://centos.org/)
rockylinux官网[https://rockylinux.org/zh-CN](https://rockylinux.org/zh-CN)
nginx官网[https://nginx.org/](https://nginx.org/)
apache官网[https://apache.org/](https://apache.org/)
<font style="color:rgb(51, 51, 51);">linux发行版本 centos redhat ubuntu debian</font>
## <font style="color:rgb(51, 51, 51);">第一章linux系统管理</font>
### linux的目录结构及文件类型
目录结构
+ <font style="color:rgb(51, 51, 51);">which获取命令对应的二进制文件存放位置</font>
+ <font style="color:rgb(51, 51, 51);"> bin命令文件 二进制文件 程序文件(普通用户)</font>
+ <font style="color:rgb(51, 51, 51);">sbin命令文件 二进制文件 程序文件(管理员用户)</font>
+ <font style="color:rgb(51, 51, 51);">root默认存放root用户的家目录 ~/当前用户家目录</font>
+ <font style="color:rgb(51, 51, 51);">home默认存放普通用户的家目录</font>
+ <font style="color:rgb(51, 51, 51);">var默认存放有变化的文件、比如日志</font>
+ <font style="color:rgb(51, 51, 51);">mnt默认挂载</font>
+ <font style="color:rgb(51, 51, 51);">dev默认存放设备文件</font>
+ <font style="color:rgb(51, 51, 51);"> proc虚拟文件</font>
+ <font style="color:rgb(51, 51, 51);">etc默认存放配置文件的目录</font>
+ <font style="color:rgb(51, 51, 51);"> tmp临时文件</font>
文件类型
+ <font style="color:rgb(51, 51, 51);">— 普通文件 (文本文件,二进制文件,压缩文件,电影,图片)</font>
+ <font style="color:rgb(51, 51, 51);">d 目录文件 (蓝色)</font>
+ <font style="color:rgb(51, 51, 51);">b 设备文件块设备存储设备硬盘U盘/dev/sda,/dev/sda1</font>
+ <font style="color:rgb(51, 51, 51);">c 设备文件(字符设备)打印机,终端,/dev/tty1,/dev/zero</font>
+ <font style="color:rgb(51, 51, 51);">s 套接字文件</font>
+ <font style="color:rgb(51, 51, 51);">p 管道文件</font>
+ <font style="color:rgb(51, 51, 51);">l 链接文件(深蓝色)</font>
### vim编辑器操作及使用
<font style="color:rgb(51, 51, 51);">编辑模式使用i键进入编辑模式可以修改</font>
<font style="color:rgb(51, 51, 51);">命令模式:默认进入命令模式,该模式可以看到文件内容,不能编辑</font>
<font style="color:rgb(51, 51, 51);">可视模式:第一列注释 ctrl + v shift+i # Esc两下</font>
<font style="color:rgb(51, 51, 51);">尾行命令模式wq!</font>
<font style="color:rgb(51, 51, 51);"></font>
| GG | <font style="color:rgb(51, 51, 51);">光标移动到最后一行</font> |
| --- | --- |
| <font style="color:rgb(51, 51, 51);">gg</font> | <font style="color:rgb(51, 51, 51);">光标到行首</font> |
| <font style="color:rgb(51, 51, 51);">dd</font> | <font style="color:rgb(51, 51, 51);">删除光标所在行</font> |
| <font style="color:rgb(51, 51, 51);">dG</font> | <font style="color:rgb(51, 51, 51);">删除光标的下面的行</font> |
| <font style="color:rgb(51, 51, 51);">u</font> | <font style="color:rgb(51, 51, 51);">撤销</font> |
| <font style="color:rgb(51, 51, 51);">yy</font> | <font style="color:rgb(51, 51, 51);">复制</font> |
| <font style="color:rgb(51, 51, 51);">p</font> | <font style="color:rgb(51, 51, 51);">粘贴</font> |
| <font style="color:rgb(51, 51, 51);">D</font> | <font style="color:rgb(51, 51, 51);">删除光标后的内容</font> |
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">vi编辑器查找替换</font>
<font style="color:rgb(51, 51, 51);">语法格式行s/替换的内容/替换成谁/</font>
<font style="color:rgb(51, 51, 51);">查找 在命令模式下输入“/”进入行尾命令模式 要查找的内容 /root</font>
<font style="color:rgb(51, 51, 51);"> n从上往下查看 N从下往上查看</font>
```plain
替换:%s/root/ROOT/ 从首行到最后一行替换,只能替换每一行第一个匹配到的
%s/root/ROOT/g 从首行到最后一行,只要匹配到全部替换
510s/.*/#&/ 5行到10行每一行开头加一个#
49s/^#// 4行到9行每行开头的#删除
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">临时查看和取消行号</font>
```plain
set nu
set nonu
set list 查看空格
set ic 不区分大小写
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">永久设定行号</font>
```plain
/etc/vimrc里面加 set nu
```
如果出现非正常关闭文件
```plain
非正常关闭,再次编辑文件以.swp结尾的隐藏文件
解决方案q退出 然后删除.swp结尾的
```
### Linux用户和组管理
<font style="color:rgb(51, 51, 51);">特性:多用户多任务 多个用户在同一个系统中同一时间执行的不同的任务,他们互相不影响</font>
<font style="color:rgb(51, 51, 51);">权限管理机制:每个用户拥有自己的权限</font>
<font style="color:rgb(51, 51, 51);">默认的自带的超级管理用户root
</font>
<font style="color:rgb(51, 51, 51);">用户管理</font>
+ <font style="color:rgb(51, 51, 51);">useradd 用户名字 </font>**<font style="color:rgb(51, 51, 51);">创建用户</font>**
+ <font style="color:rgb(51, 51, 51);">useradd -u 1000 用户名字 </font>**<font style="color:rgb(51, 51, 51);">指定用户uid</font>**
+ <font style="color:rgb(51, 51, 51);">useradd -d 用户名字 /opt/xiaoshun </font>**<font style="color:rgb(51, 51, 51);">指定用户家目录</font>**
+ <font style="color:rgb(51, 51, 51);">useradd -s /bin/sh 用户名字 </font>**<font style="color:rgb(51, 51, 51);">指定登录shell</font>**
+ <font style="color:rgb(51, 51, 51);">useradd -r 用户名称 </font>**<font style="color:rgb(51, 51, 51);">创建系统用户</font>**
设置用户密码
```plain
passwd 用户名字 用户设置密码
```
<font style="color:rgb(51, 51, 51);"></font>
切换用户
```plain
su - 用户名字
```
获取当前使用的用户名
```plain
id -un
```
修改用户
+ <font style="color:rgb(51, 51, 51);">usermod -s /sbin/nologin 用户名 </font>**<font style="color:rgb(51, 51, 51);">禁止用户登录</font>**
+ <font style="color:rgb(51, 51, 51);"> usermod -u 用户名 1000 </font>**<font style="color:rgb(51, 51, 51);">修改已创用户uid</font>**
+ <font style="color:rgb(51, 51, 51);">usermod -l 新的用户名 旧的用户名 </font>**<font style="color:rgb(51, 51, 51);">修改用户名</font>**
+ <font style="color:rgb(51, 51, 51);">usermod -L 用户名 </font>**<font style="color:rgb(51, 51, 51);">锁定密码</font>**
+ <font style="color:rgb(51, 51, 51);">usermod -U 用户名 </font>**<font style="color:rgb(51, 51, 51);">解开密码</font>**
+ <font style="color:rgb(51, 51, 51);">usermod -G 用户2 用户1 </font>**<font style="color:rgb(51, 51, 51);">用户1加入到用户2组里用户1有两个组给用户附加组</font>**
+ <font style="color:rgb(51, 51, 51);">usermod -aG 组名 用户名 </font>**<font style="color:rgb(51, 51, 51);">用户附加组 修改用户属组</font>**
+ <font style="color:rgb(51, 51, 51);">userdel -r 用户 </font>**<font style="color:rgb(51, 51, 51);">删除用户</font>**
**<font style="color:rgb(51, 51, 51);"></font>**
**<font style="color:rgb(51, 51, 51);">添加用户发生变化的文件</font>**
```plain
/var/spool/mail 邮箱
/etc/group 存放组信息配置文件
/home 家目录
/etc/shadow 存放用户密码的(第二列密码是经过加密处理的)
/etc/passwd 系统中的所有用户的信息
```
**<font style="color:rgb(51, 51, 51);"></font>**
**<font style="color:rgb(51, 51, 51);">用户组管理</font>**
<font style="color:rgb(51, 51, 51);">一个用户至少包含一个组</font>
用户组管理
+ <font style="color:rgb(51, 51, 51);">groupadd 组名 创建组</font>
+ <font style="color:rgb(51, 51, 51);">groups 组名 查看用户组</font>
+ <font style="color:rgb(51, 51, 51);">groupmod -n 新组名 旧组名 修改组名字</font>
+ <font style="color:rgb(51, 51, 51);">groupmod -g 新GID 组名 修改GID</font>
+ <font style="color:rgb(51, 51, 51);">gpasswd -a 用户 组名 添加用户到组</font>
+ <font style="color:rgb(51, 51, 51);">gpasswd -M 用户1用户2 组名 添加多用户到组</font>
+ <font style="color:rgb(51, 51, 51);">gpasswd -d 用户 组名 从组删除用户</font>
+ <font style="color:rgb(51, 51, 51);">groupdel 组名 删除组</font>
<font style="color:rgb(51, 51, 51);"></font>
用户提权
<font style="color:rgb(51, 51, 51);">让普通用户具备管理员权限,执行管理员命令</font>
```plain
root ALL=(ALL) ALL
```
<font style="color:rgb(51, 51, 51);">超级管理员用户 root 标识 UID 0</font>
<font style="color:rgb(51, 51, 51);">系统用户 标识 UID 1-999 包含14和999 0</font><font style="color:rgb(51, 51, 51);"><</font><font style="color:rgb(51, 51, 51);">UID<999</font>
<font style="color:rgb(51, 51, 51);">普通用户 标识 UID UID>=1000</font>
### linux系统管理权限
基本权限UGO
+ U 代表user 用户 所有者
+ G 代表Group 组 所属组
+ O 代表Other 其他人
-rw-r--r-- 1 user root 945 Jan 22 18:53 passwd
<font style="color:rgb(51, 51, 51);">对应的所有者user 对应的所属组root</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">基本权限针对文件</font>
+ r 读 4 <font style="color:rgb(51, 51, 51);">读取文件的内容 cat vim/vi</font>
+ <font style="color:rgb(51, 51, 51);">w 写 2 编辑该文件 vi vim写入文件</font>
+ <font style="color:rgb(51, 51, 51);">x 执行 1 执行该文件(脚本) bash sh ./ /</font>
<font style="color:rgb(51, 51, 51);"> 就算一个文件没有执行权限我们可以使用bash或者sh的命令执行文件但是如果一个文件没有执行权限不能用./或者执行文件</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">文件的默认权限是644</font>
<font style="color:rgb(51, 51, 51);">目录的默认权限是755</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">权限设置</font>
**<font style="color:rgb(51, 51, 51);">修改基本权限chmod</font>**
**<font style="color:rgb(51, 51, 51);">数字方式</font>**<font style="color:rgb(51, 51, 51);">chmod 564 tiantian //这个文件权限:所有者读和执行,所属组读和写,其他人读</font>
**<font style="color:rgb(51, 51, 51);">字母方式:</font>**<font style="color:rgb(51, 51, 51);"> chmod u+x tiantian //增加给tiantian文件所有者增加x权限</font>
<font style="color:rgb(51, 51, 51);"></font>
**<font style="color:rgb(51, 51, 51);">将/opt/dir目录下的所有内容设置权限为777包括dir1目录下的所有文件也包括了dir目录</font>**
```plain
chmod 777 /opt/dir -R
```
**<font style="color:rgb(51, 51, 51);">将/opt/dir/目录下所有内容设置权限为644包括dir1目录下的所有文件不包括dir目录</font>**
```plain
chmod 644 /opt/dir/\* -R
```
<font style="color:rgb(51, 51, 51);">修改所有者所属组chown</font>
**<font style="color:rgb(51, 51, 51);">修改tiantian文件所有者为user1000,所属组为group2000</font>**
```plain
chown user1000,group2000 tiantian
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">高级权限</font>
<font style="color:rgb(51, 51, 51);">suid权限给命令文件设置权限当给命令文件设置了suid权限后无论那个用户执行该命令都会变成这个命令文件所有者的用户去执行 符合 s 对象 u 文件</font>
```plain
chmod u+s 命令文件
```
**<font style="color:rgb(51, 51, 51);"></font>**
<font style="color:rgb(51, 51, 51);">sgid权限组继承权限 对象 g 符号s 针对目录</font>
<font style="color:rgb(51, 51, 51);">目录的所属组假设是hr如果设定了sgid权限那么在该目录下创建的新文件都会继承目录的属组hr</font>
```plain
chmod g+s 目录名
```
**<font style="color:rgb(51, 51, 51);"></font>**
<font style="color:rgb(51, 51, 51);">sticky权限防止别人误删除 符号 t 对象 o 针对目录</font>
```plain
chmod o+t 目录名
```
umask
<font style="color:rgb(51, 51, 51);">umask 权限掩码 创建文件的时候文件默认权限跟umask有关联 默认情况下umask的值为022 所以创建的文件默认权限644目录默认权限755</font>
<font style="color:rgb(51, 51, 51);">修改umaskumask + 掩码 最大777 最小000</font>
<font style="color:rgb(51, 51, 51);">文件默认权限=0666-0022=0644</font>
<font style="color:rgb(51, 51, 51);">目录默认权限=0777-0022=0755</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">文件属性</font>
<font style="color:rgb(51, 51, 51);">a能看 能追加 不能删除</font>
<font style="color:rgb(51, 51, 51);">i只能看其他啥也干不了</font>
<font style="color:rgb(51, 51, 51);">chattr + -</font>
<font style="color:rgb(51, 51, 51);">lsattr查看</font>
<font style="color:rgb(51, 51, 51);"></font>
### <font style="color:rgb(51, 51, 51);">linux进程使用管理</font>
<font style="color:rgb(51, 51, 51);">ps aux</font>
<font style="color:rgb(51, 51, 51);">第一列:进程的拥有者</font>
<font style="color:rgb(51, 51, 51);">第二列PID 进程ID 唯一标识一个进程</font>
<font style="color:rgb(51, 51, 51);">第三列cpu使用的百分比</font>
<font style="color:rgb(51, 51, 51);">第四列mem内存使用率</font>
<font style="color:rgb(51, 51, 51);">第七列:终端</font>
<font style="color:rgb(51, 51, 51);">第八列:进程的状态</font>
+ <font style="color:rgb(51, 51, 51);"> Ss s进程的领导者父进程</font>
+ <font style="color:rgb(51, 51, 51);"> S< <优先级较高的进程</font>
+ <font style="color:rgb(51, 51, 51);">SN N优先级较低的进程</font>
+ <font style="color:rgb(51, 51, 51);">R+ +表示是前台的进程组</font>
+ <font style="color:rgb(51, 51, 51);">Sl 以线程的方式运行</font>
<font style="color:rgb(51, 51, 51);">最后一列:进程的名字</font>
<font style="color:rgb(51, 51, 51);">按照cpu使用率从大到小</font>
```shell
ps aux --sort -%cpu | less
```
**<font style="color:rgb(51, 51, 51);"></font>**
<font style="color:rgb(51, 51, 51);">按照mem使用率从大到小</font>
```shell
ps aux --sort -%mem |less
```
<font style="color:rgb(51, 51, 51);">按照cpu使用率从大到小查看前四行去掉USER重定向到/tmp/1.txt上</font>
```shell
ps aux --sort -%cpu | head -4 | grep -v USER > /tmp/1.txt
```
获取服务的pid
```shell
pidof dockerd
```
<font style="color:rgb(51, 51, 51);">w who 都可以看到谁正在远程链接我能够获取到对方ip地址、终端编号、对应的用户</font>
<font style="color:rgb(51, 51, 51);">w 还可以看服务器的运行时间、用户数量、平均负载</font>
<font style="color:rgb(51, 51, 51);">uptime 看cpu平均负载</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">动态进程top</font>
<font style="color:rgb(51, 51, 51);">快捷键P将cpu使用率排序 M将%mem使用率排序 R排序反转</font>
<font style="color:rgb(51, 51, 51);">操作系统两种cpu状态 内核态 用户态</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">top的整体信息</font>
```shell
top分为上下两部分上面是机器整体负载情况下面是单个进程的负载情况
load average代表cpu每1分钟5分钟15分钟的平均负载
us 用户空间占用cpu的百分比
sy 内核空间占用cpu的百分比
ni 调整过优先级的进程占用cpu的百分比
id cpu空闲百分比
wa io等待占用cpu百分比
hi 硬中断占用cpu百分比
si 软中断占用cpu的百分比
```
<font style="color:rgb(51, 51, 51);">free -m/h 看内存的使用率</font>
<font style="color:rgb(51, 51, 51);">total 总量 used使用量 free空闲量 buff/cache 缓存中的内存</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">网络进程</font>
<font style="color:rgb(51, 51, 51);">netstat -auntpl</font>
<font style="color:rgb(51, 51, 51);">ss -auntpl</font>
+ <font style="color:rgb(51, 51, 51);"> -a所有</font>
+ <font style="color:rgb(51, 51, 51);">-u查看upd进程</font>
+ <font style="color:rgb(51, 51, 51);">-ppid</font>
+ <font style="color:rgb(51, 51, 51);">-n显示数字</font>
+ <font style="color:rgb(51, 51, 51);">-l监听</font>
+ <font style="color:rgb(51, 51, 51);">-t查看tcp进程</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">显示所有的 TCP 连接和监听端口,以及与之相关联的进程信息</font>
```shell
ss -antpl
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">显示所有的 TCP 连接和监听端口,以及与之相关联的进程信息</font>
```shell
netstat -antpl
```
<font style="color:rgb(51, 51, 51);"> </font>
<font style="color:rgb(51, 51, 51);">显示所有的 UDP 连接和监听端口,以及与之相关联的进程信息</font>
```shell
ss -tnupl
```
数据传输
TCP<font style="color:rgb(51, 51, 51);">传输控制协议</font>
<font style="color:rgb(51, 51, 51);"> udp用户数据报协议</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">tcp状态</font>
+ <font style="color:rgb(51, 51, 51);">CLOSED(关闭状态)初始状态表示TCP连接未建立</font>
+ <font style="color:rgb(51, 51, 51);">LISTEN(监听状态)。服务器等待客户端连接的状态</font>
+ <font style="color:rgb(51, 51, 51);">SYN SENT(同步已发送状态)。客户端发送连接请求后等待服务器确认的状态。</font>
+ <font style="color:rgb(51, 51, 51);">SYN RECEIVED(同步已接收状态)。服务器接收到客户端连接请求并发送确认后的状态。</font>
+ <font style="color:rgb(51, 51, 51);">ESTABLISHED(已建立状态)。表示TCP连接已成功建立双方可以进行数据传输。</font>
+ <font style="color:rgb(51, 51, 51);">FIN WAIT 1(等待对方FIN报文状态)。表示TCP连接的一方发送了关闭连接请求。</font>
+ <font style="color:rgb(51, 51, 51);">FIN WAIT 2(等待对方关闭连接请求状态)。表示TCP连接的一方等待对方发送关闭连接请求。</font>
+ <font style="color:rgb(51, 51, 51);">TIME WAIT(等待状态)。表示TCP连接关闭后的等待状态用于确保数据的可靠传输。</font>
+ <font style="color:rgb(51, 51, 51);">CLOSE WAIT(等待关闭状态)。表示TCP连接的一方接收到了对方的FIN报文但尚未发送ACK。1</font>
+ <font style="color:rgb(51, 51, 51);">LAST ACK(最后确认状态)。表示TCP连接关闭前的最后确认状态等待对方的FIN报文。3</font>
+ <font style="color:rgb(51, 51, 51);">CLOSING(关闭状态)。表示TCP连接正在进行关闭过程</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">kill</font>
+ <font style="color:rgb(51, 51, 51);">kill -1 重新加载进程或重新加载配置文件</font>
+ <font style="color:rgb(51, 51, 51);">kill -9 给进程发送一个强行终止的信号</font>
+ <font style="color:rgb(51, 51, 51);">kill -15 正常杀死</font>
+ <font style="color:rgb(51, 51, 51);">kill -18 激活进程 //挂起在次激活时服务对应进程的PID会改变不能</font>
+ <font style="color:rgb(51, 51, 51);">kill -19 挂起进程</font>
pkill
+ <font style="color:rgb(51, 51, 51);">pkill -9 -t pts/2 //指定终端干掉</font>
+ <font style="color:rgb(51, 51, 51);">pkill -9 -u 用户 //干掉指定用户进程</font>
### linux磁盘分区管理
<font style="color:rgb(51, 51, 51);">机械硬盘HDD</font>
<font style="color:rgb(51, 51, 51);">固态硬盘SSD</font>
<font style="color:rgb(51, 51, 51);">分布式存储 HDFS CEPH</font>
<font style="color:rgb(51, 51, 51);">云存储</font>
<font style="color:rgb(51, 51, 51);">对象存储 minio OSS</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">raid 0</font>
```shell
raid 0分为2种分别为单盘0和全盘0
raid全盘0 最少需要2块盘 磁盘空间利用率为100% 没有冗余没有容错
raid0把连续的数据分散到多个磁盘上存取
raid单盘0 只需要一块磁盘 常用于新服务器安装操作系统
```
raid 1
```shell
raid 1 只能2块磁盘做。
镜像raid通过磁盘数据镜像实现数据冗余在成对的独立磁盘上产生互为备份的数据
优点:提供数据冗余,支持容错,读速度快
缺点:写速度慢,磁盘利用率不高
磁盘利用率50%
```
raid 5
```shell
raid5最少需要3块磁盘。它的校验数据分布在阵列中的所有磁盘上。
优点1.可以找回丢失的数据2.冗余磁盘当某一块磁盘坏掉后冗余磁盘会自动替换上去3.读写速度高4.磁盘利用率高
缺点1.磁盘越多安全性能越差
利用率 n-1/n
```
raid 6
```shell
raid6 两块存校验位。RAID6引入双重校验的概念
最少需要四块磁盘,它可以保护阵列中同时出现两个磁盘失效时,阵列仍能够继续工作,不会发生数据丢失。
优点1.容错:允许两块磁盘同时坏掉。读快。
缺点1.写入速度差
2.成本高
3 磁盘利用率 n-2/n
应用场景:对数据安全级别要求比较高的企业
```
<font style="color:rgb(51, 51, 51);">raid1+raid5 全都是机械硬盘</font>
<font style="color:rgb(51, 51, 51);">raid1+raid5 机械盘+固态盘 混搭</font>
<font style="color:rgb(51, 51, 51);">raid1用机械装系统</font>
<font style="color:rgb(51, 51, 51);">raid5用固态存数据</font>
<font style="color:rgb(51, 51, 51);">raid1+ssd</font><font style="color:rgb(51, 51, 51);">_</font><font style="color:rgb(51, 51, 51);">raid5</font>
<font style="color:rgb(51, 51, 51);">raid 阵列选型: 根据服务特点和数据相关做有冗余的阵列数据要求极高的就做最高的安全性raid。</font>
<font style="color:rgb(51, 51, 51);">根据业务特点数据重要程度去选择冗余度高和低的raid阵列</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">磁盘分区</font>
<font style="color:rgb(51, 51, 51);">lsblk 查看磁盘分区</font>
<font style="color:rgb(51, 51, 51);">MBR fdisk 命令最多分4个主分区</font>
<font style="color:rgb(51, 51, 51);">GPT gidsk 命令可以分128个主分区</font>
<font style="color:rgb(51, 51, 51);">磁盘大于2T不可以使用fdisk命令必须要用gdisk命令可以分128个主分区</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">fdisk</font>
+ <font style="color:rgb(51, 51, 51);">n添加分区</font>
+ <font style="color:rgb(51, 51, 51);"> p主分区</font>
+ <font style="color:rgb(51, 51, 51);"> e扩展分区</font>
+ w保存退出
+ q退出不保存
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">gdisk</font>
+ <font style="color:rgb(51, 51, 51);"> n添加分区1-128</font>
+ <font style="color:rgb(51, 51, 51);"> p: 查看分区</font>
+ <font style="color:rgb(51, 51, 51);"> d删除分区</font>
+ <font style="color:rgb(51, 51, 51);">w保存退出</font>
+ <font style="color:rgb(51, 51, 51);"> q退出不保存</font>
<font style="color:rgb(51, 51, 51);">文件系统</font>
+ linuxxfs ext4 ext3 nfs
+ windowNTFS FAT32
格式化:磁盘或磁盘分区设定文件系统类型
<font style="color:rgb(51, 51, 51);"> </font><font style="color:rgb(51, 51, 51);">磁盘有空间本身容量100G现在还显示10G但是无法存放数据ext家族文件系统inodes耗尽造成无法存储文件</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">格式化方式</font>
+ <font style="color:rgb(51, 51, 51);">mkfs.xfs -f 设备名称</font>
+ <font style="color:rgb(51, 51, 51);">mkfs.ext4 设备名称</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">临时挂载(重启失效)</font>
+ mount 设备名称 挂载点(目录)
+ umount 设备名称 挂载点 (目录)
<font style="color:rgb(51, 51, 51);">mount -o rw centos-7-x86 64-Minima1-2009.iso /opt/centos/ 把镜像挂载在opt/centos目录上 rw是读写权限</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">永久挂载</font>
```plain
第一列设备设备名称。设备uuid标识
第二列:挂载点(本质就是目录)
第三列文件系统类型xfs ext4 nfs
第四列: 属性 rw remount defaults
第五列+第六列: 0 0 不备份 不检查
```
```plain
/dev/sdc /opt/1 ext4 defaults 0 0
```
uuid挂载
blkid /dev/sda
/dev/sda: PTUUID="770490bd" PTTYPE="dos"
```plain
UUID="25d22b48-8e46-494b-a12c-ae80a4297808" /opt/1 xfs defaults 0 0
```
物理卷
+ <font style="color:rgb(51, 51, 51);">pvcreate /dev/磁盘名称(sdb) </font>**<font style="color:rgb(51, 51, 51);">创建物理卷</font>**
+ <font style="color:rgb(51, 51, 51);">pvremove /dev/磁盘名称(sdb) </font>**<font style="color:rgb(51, 51, 51);">删除物理卷</font>**
+ <font style="color:rgb(51, 51, 51);">pvs </font>**<font style="color:rgb(51, 51, 51);">查看物理卷</font>**
+ <font style="color:rgb(51, 51, 51);">pvmove /dev/sdb /dev/sdd </font>**<font style="color:rgb(51, 51, 51);">数据迁移 sdb-->sdd</font>**
**<font style="color:rgb(51, 51, 51);"></font>**
卷组
+ <font style="color:rgb(51, 51, 51);">vgcreate 卷组名vg1/dev/磁盘名(sdb) </font>**<font style="color:rgb(51, 51, 51);">创建卷组</font>**
+ <font style="color:rgb(51, 51, 51);">vgextend 卷组vg1 /dev/磁盘名(sdb) </font>**<font style="color:rgb(51, 51, 51);">将物理卷加入到卷组</font>**
+ <font style="color:rgb(51, 51, 51);">vgremove 卷组名(vg1) </font>**<font style="color:rgb(51, 51, 51);">删除卷组</font>**
+ <font style="color:rgb(51, 51, 51);">vgreduce 卷组名vg1 /dev/磁盘名(sdb) </font>**<font style="color:rgb(51, 51, 51);">将物理卷从卷组中移除</font>**
+ <font style="color:rgb(51, 51, 51);">vgs </font>**<font style="color:rgb(51, 51, 51);">查看卷组</font>**
**<font style="color:rgb(51, 51, 51);"></font>**
<font style="color:rgb(51, 51, 51);">逻辑卷</font>
+ <font style="color:rgb(51, 51, 51);">lvcreate -L 磁盘容量大小(2G) -n 逻辑卷名(lv1) 卷组名(vg1) </font>**<font style="color:rgb(51, 51, 51);">创建逻辑卷</font>**
+ <font style="color:rgb(51, 51, 51);">lvremove /dev/卷组名(vg1)/逻辑卷名(lv1) </font>**<font style="color:rgb(51, 51, 51);">删除逻辑卷</font>**
+ <font style="color:rgb(51, 51, 51);"> lvextend -L 磁盘容量(+5G) -n /dev/卷组名(vg1)/逻辑卷名(lv1) </font>**<font style="color:rgb(51, 51, 51);">逻辑卷扩容</font>**
+ <font style="color:rgb(51, 51, 51);">lvreduce -L 20G /dev/centos/root </font>**<font style="color:rgb(51, 51, 51);">自定义逻辑卷的容量</font>**
+ <font style="color:rgb(51, 51, 51);">lvs 查看逻辑卷</font>
扩容/分区
+ **<font style="color:rgb(51, 51, 51);">pvcreate /dev/sdb 创建物理卷</font>**
+ **<font style="color:rgb(51, 51, 51);">vgextend centos /dev/sdb 将物理卷加入centos卷组</font>**
+ **<font style="color:rgb(51, 51, 51);">lvextend -L +5G /dev/centos/root 逻辑卷扩容5G</font>**
+ **<font style="color:rgb(51, 51, 51);">xfs_growfs 要扩容的lv的path(/dev/centos/root) 整合文件系统</font>**
<font style="color:rgb(51, 51, 51);">扩容完要整合文件系统</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">准备一个正在被使用逻辑卷并且文件系统ext4</font>
```plain
- lvcreate -L 2G -n lv2 vg1
- mkfs.ext4 /dev/vg1/lv2
- mkdir /opt/lv2
- vi /etc/fstab /dev/vg1/lv2 /opt/lv2 ext4 defaults 0 0
- mount -a 验证 df -Th
- lvextend -L 5G /dev/vg1/lv2
- 判断文件系统为ext4 所以使用
- resize2fs /dev/vg1/lv2
```
<font style="color:rgb(51, 51, 51);">逻辑卷格式化流程</font>
+ <font style="color:rgb(51, 51, 51);"> 先取消挂载</font>
+ <font style="color:rgb(51, 51, 51);">在删除逻辑卷</font>
+ <font style="color:rgb(51, 51, 51);">删除卷组</font>
+ <font style="color:rgb(51, 51, 51);">删除物理卷</font>
+ <font style="color:rgb(51, 51, 51);">格式化</font>
swap<font style="color:rgb(51, 51, 51);">交换分区 防止内存溢出造成系统奔溃</font>
swap分区大小设置规则
<font style="color:rgb(51, 51, 51);">内存小于4G时推荐不少于2GB的swap空间</font>
<font style="color:rgb(51, 51, 51);">内存4GB</font><font style="color:rgb(51, 51, 51);">~</font><font style="color:rgb(51, 51, 51);">16GB推荐不少于4GB的swap空间</font>
<font style="color:rgb(51, 51, 51);">内存16GB</font><font style="color:rgb(51, 51, 51);">~</font><font style="color:rgb(51, 51, 51);">64GB推荐不少于8GB的swap空间</font>
<font style="color:rgb(51, 51, 51);">内存64GB~256GB推荐不少于16GB的swap空间</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">制作swap分区</font>
<font style="color:rgb(51, 51, 51);">先用fdisk做一个分区</font>
```plain
mkswap /dev/sdb 格式化分区
swapon /dev/sdb 激活swap分区
swapoff /dev/sdb 关闭swap分区
```
文件链接
软链接
+ <font style="color:rgb(51, 51, 51);">ln -s 源文件(/roo/zhangsan) 目的地(/opt)</font>
+ <font style="color:rgb(51, 51, 51);">普通文件和目录都支持软连接</font>
+ <font style="color:rgb(51, 51, 51);">软连接源文件跟链接文件inode号不同</font>
+ <font style="color:rgb(51, 51, 51);">软连接源文件删除后,链接文件不可用</font>
+ <font style="color:rgb(51, 51, 51);">软连接可以跨分区</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">硬链接</font>
+ <font style="color:rgb(51, 51, 51);">ln 源文件 目的地</font>
+ <font style="color:rgb(51, 51, 51);">文件可以创建硬链接,目录不可以</font>
+ <font style="color:rgb(51, 51, 51);">硬链接inode不变</font>
+ <font style="color:rgb(51, 51, 51);">硬链接源文件删除后,链接文件可以使用</font>
+ <font style="color:rgb(51, 51, 51);">硬链接不可以跨分区</font>
<font style="color:rgb(51, 51, 51);"></font>
### <font style="color:rgb(51, 51, 51);">linux文件find查找</font>
<font style="color:rgb(51, 51, 51);">find 位置/路径 条件 值 动作</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">条件</font>
+ <font style="color:rgb(51, 51, 51);">-name 文件名字</font>
+ <font style="color:rgb(51, 51, 51);"> -type 文件类型 普通文件 f 其他和文件类型一样</font>
+ <font style="color:rgb(51, 51, 51);">-size 文件大小 +5M 大于5M 5M 在5M范围内</font>
+ <font style="color:rgb(51, 51, 51);"> -user 查找所有者</font>
+ <font style="color:rgb(51, 51, 51);"> -group 查找属组</font>
+ <font style="color:rgb(51, 51, 51);">-perm 权限 指定 644 755</font>
+ <font style="color:rgb(51, 51, 51);"> -mtime 修改时间 atime 访问时间 ctime 改变时间 mtime 修改时间</font>
多条件
+ <font style="color:rgb(51, 51, 51);">-a and // 条件都要满足</font>
+ <font style="color:rgb(51, 51, 51);">-o or //条件满足其中一个</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">动作</font>
_<font style="color:rgb(51, 51, 51);"></font>_
_<font style="color:rgb(51, 51, 51);">find /opt -name "3" -exec mv {} /mnt ;</font>_
+ <font style="color:rgb(51, 51, 51);"> -exec ; 格式</font>
+ <font style="color:rgb(51, 51, 51);"> {} 承接查到的内容</font>
+ <font style="color:rgb(51, 51, 51);">mv 文件操作命令</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">find /opt/ -name "*2" |xargs -i mv {} /mnt</font>
linux压缩解压缩
window .rar .zip
linux.zip .tar.gz tar.bz2 .tar.xz
<font style="color:rgb(51, 51, 51);">打包压缩tar czf 路径/压缩包的名字 压缩的文件</font>
```shell
tar czf /opt/docker-ce.tar.gz docker/
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">解压缩tar xzf 压缩包名字</font>
<font style="color:rgb(51, 51, 51);">指定位置解压: -C</font>
<font style="color:rgb(51, 51, 51);">tar tf 不解压直接查看内容</font>
```shell
tar xzf docker-ce.tar.gz -C /root/
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">查看/etc目录下所有以.conf结尾的文件并进行压缩压缩到/opt目录下命名为conf.tar.g</font>
```shell
find /etc -name "\*.conf" |xargs -i tar rf /opt/conf-1.tar.gz{}
```
<font style="color:rgb(51, 51, 51);"></font>
zip包
<font style="color:rgb(51, 51, 51);">解压缩unzip 压缩包名字</font>
<font style="color:rgb(51, 51, 51);">指定位置解压: unzip -d /opt/ nextcloud-22.0.0.zip</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">gunzip</font>
<font style="color:rgb(51, 51, 51);">解压.gz结尾的包 如果没有gunzip,使用yum -y install gzip</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);"></font>
### <font style="color:rgb(51, 51, 51);">linux软件包管理</font>
rpm包管理
rpm网站[https://rpmfind.net/](https://rpmfind.net/)
<font style="color:rgb(51, 51, 51);">二进制包 .rpm结尾 有依赖关系 </font>
+ <font style="color:rgb(51, 51, 51);"> rpm -qa | grep 服务名 </font>**<font style="color:rgb(51, 51, 51);">找安装包</font>**
+ <font style="color:rgb(51, 51, 51);">rpm -ql 服务名 </font>**<font style="color:rgb(51, 51, 51);">启动服务</font>**
+ <font style="color:rgb(51, 51, 51);">rpm -e s1-5.02-1.e17.x86 64.rpm </font>**<font style="color:rgb(51, 51, 51);">卸载</font>**
+ <font style="color:rgb(51, 51, 51);">rpm -qa </font>**<font style="color:rgb(51, 51, 51);">获取已经安装的所有包</font>**<font style="color:rgb(51, 51, 51);"> </font>
```shell
软件名字 版本号 发行版本 平台架构 后缀
```
yum软件包管理
+ <font style="color:rgb(51, 51, 51);"> install</font>
+ <font style="color:rgb(51, 51, 51);">remove</font>
+ <font style="color:rgb(51, 51, 51);">reinstall 重新安装</font>
+ <font style="color:rgb(51, 51, 51);">provides 查找包名</font>
+ <font style="color:rgb(51, 51, 51);"> info 查找包的详细信息</font>
+ <font style="color:rgb(51, 51, 51);">update 更新服务</font>
+ <font style="color:rgb(51, 51, 51);">list 列出所有包</font>
+ <font style="color:rgb(51, 51, 51);">repolist 列出所有的仓库</font>
+ <font style="color:rgb(51, 51, 51);">clean all 清除缓存</font>
+ <font style="color:rgb(51, 51, 51);">makecache fast 快速生成缓存</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">yum list | grep show</font>
```shell
yum list --showduplicates docker-ce服务名全称 列出你可以安装的版本都有哪些
yum -y install docker-ce-3:20.10.12-3.el7 指定版本安装
```
内网下载
```shell
dnf -y install --downloadonly --downloaddir=docker-ce/ docker-ce-3:20.10.24-3.el9.x86_64
--downloadonly 只下载不安装
--downloaddir 指定安装目录
dnf -y install ./*rpm 内网直接安装
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">yum仓库</font>
```shell
[base]
name=CentOS-\$releasever - Base - mirrors.aliyun.com 指定仓库的名字
baseurl=<http://mirrors.aliyun.com/centos/> 仓库地址
enabled=1 1开启 0关闭 仓库是否启用
gpgcheck=1 是否校验 1开启 0关闭
gpgkey=<http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7>
```
源码包
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">源码编译安装nginx</font>
```shell
源码编译安装nginx https://nginx.org/ nginx的官网
```
<font style="color:rgb(51, 51, 51);">1.获取nginx的源码包</font>
```shell
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar xf nginx-1.24.0.tar.gz
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">2.编译安装准备所需要的环境</font>
```shell
yum -y install apr apr-util ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make
```
<font style="color:rgb(51, 51, 51);">3.配置</font>
```shell
./configure --prefix=指定位置(/opt/nginx
```
<font style="color:rgb(51, 51, 51);">4.编译</font>
```shell
make
```
5.安装
```shell
make install
```
测试
```shell
获取项目源码包wget <http://10.9.12.206:30120/directlink/1/safeware/love.zip>
安装解压工具yum -y install unzip
解压源码包unzip love.zip
拷贝源码到网站发布目录下cp -r love/\* /opt/nginx/html/
启动nginx : /opt/nginx/sbin/nginx 执行的是安装目录下的sbin目录下nginx的文件
关闭nginx killall -9 nginx
```
## 第二章linux网络协议
### linux网卡解析
### DNS解析
1. **<font style="color:rgb(36, 41, 47);">输入网址</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">用户在浏览器中输入网址(</font>[例如www.example.com](http://xn--www-uc0ep96b.example.com/)<font style="color:rgb(36, 41, 47);">)并按下回车。</font>
2. **<font style="color:rgb(36, 41, 47);">本地 DNS 缓存查询</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">计算机首先检查本地 DNS 缓存,看看是否已经保存了该域名对应的 IP 地址。</font>
3. **<font style="color:rgb(36, 41, 47);">向本地 DNS 服务器发起请求</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">如果本地 DNS 缓存中没有找到对应的 IP 地址,计算机会向本地 DNS 服务器发起请求。</font>
4. **<font style="color:rgb(36, 41, 47);">递归查询开始</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">本地 DNS 服务器开始递归查询过程,向根域名服务器发起查询请求。</font>
5. **<font style="color:rgb(36, 41, 47);">向根域名服务器查询</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">根域名服务器知道整个互联网的域名分布情况,但它不知道具体域名对应的 IP 地址。根域名服务器会将查询转发给顶级域名服务器。</font>
6. **<font style="color:rgb(36, 41, 47);">向顶级域名服务器查询</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">顶级域名服务器知道每个顶级域名(</font>[如.com](http://xn--bvs.com/)<font style="color:rgb(36, 41, 47);">、.net、.org等的权威 DNS 服务器地址。本地 DNS 服务器向顶级域名服务器查询该域名的权威 DNS 服务器的 IP 地址。</font>
7. **<font style="color:rgb(36, 41, 47);">向权威 DNS 服务器查询</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">顶级域名服务器返回权威 DNS 服务器的 IP 地址后,本地 DNS 服务器向权威 DNS 服务器发起查询请求,请求该域名对应的 IP 地址。</font>
8. **<font style="color:rgb(36, 41, 47);">返回 IP 地址</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">权威 DNS 服务器查询到该域名对应的 IP 地址后,将其返回给本地 DNS 服务器。</font>
9. **<font style="color:rgb(36, 41, 47);">返回给客户端</font>**<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);">本地 DNS 服务器收到 IP 地址后,将其缓存,并将其返回给客户端(例如您的计算机)。客户端收到 IP 地址后,就可以使用它来建立与目标网站的连接,开始浏览网页或进行其他网络活动。</font>
<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);"></font>
<font style="color:rgb(36, 41, 47);"></font>
## 第三章linux常用命令指南
### linux重定向
+ <font style="color:rgb(51, 51, 51);">0 标准输入</font>
+ <font style="color:rgb(51, 51, 51);"> 1 标准正确输出 </font>
+ <font style="color:rgb(51, 51, 51);"> 2 标准错误输出</font>
+ > 覆盖
+ >> 追加
<font style="color:rgb(51, 51, 51);">1> 标准正确输出重定向覆盖</font>
<font style="color:rgb(51, 51, 51);">1>> 标准正确输出重定向</font>
<font style="color:rgb(51, 51, 51);">2> 标准错误输出重定向覆盖</font>
<font style="color:rgb(51, 51, 51);">2>> 标准错误输出重定向追加</font>
<font style="color:rgb(51, 51, 51);">& === 1+2 混合输出</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">重定向案例</font>
```shell
cat >>/opt/tiantian.b <\<eof
tiantian
tianjin
hello
hi
eof
```
<font style="color:rgb(51, 51, 51);">| 管道的作用 前一条命令执行后的结果交给后一条的执行</font>
### linux三剑客
+ <font style="color:rgb(51, 51, 51);">cat 参数-n显示行号文件名</font>
+ <font style="color:rgb(51, 51, 51);">head 指定行数 从前往后 默认看前10行</font>
+ <font style="color:rgb(51, 51, 51);"> tail 指定行数 从后往前 默认看后10行</font>
+ <font style="color:rgb(51, 51, 51);">tail -f 实时的查看</font>
+ <font style="color:rgb(51, 51, 51);"> more less 翻页查看</font>
<font style="color:rgb(51, 51, 51);">sort 排序 默认从小到大</font>
+ -r 反转
+ <font style="color:rgb(51, 51, 51);">-n 按照数值排序</font>
+ <font style="color:rgb(51, 51, 51);">-k 指定字段(按列分)</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">uniq 去重复</font><font style="color:rgb(51, 51, 51);">(只能对一个字段,必须要排序在去重复)</font>
+ -c 去重
awk 字符处理工具
<font style="color:rgb(51, 51, 51);">cat 1.txt | awk -F':' 'NR==10{print $5}' ##NR指定目标所在行</font>
<font style="color:rgb(51, 51, 51);">awk </font>`<font style="color:rgb(51, 51, 51);background-color:rgb(243, 244, 244);">$NF 最后一列 $</font>`<font style="color:rgb(51, 51, 51);">`(NF-1) 倒数第二列</font>
+ -F 指定分隔符
<font style="color:rgb(51, 51, 51);">grep 过滤</font>
+ -v 反向查找,只打印不匹配的行
+ -i 忽略大小写进行匹配
+ -n 显示匹配行的行号
wc 统计
+ -l 统计行数
<font style="color:rgb(51, 51, 51);">access.log 日志文件 nginx访问日志</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">获取到所有的ip地址</font>
```shell
cat access.log | awk '{print \$1}'
```
<font style="color:rgb(51, 51, 51);">获取到所有ip的个数统计某一网站访问次数统计某一网站pv量</font>
```shell
cat access.log | awk '(print \$1)' | wc -l
```
<font style="color:rgb(51, 51, 51);">获取所有的IP地址去重统计某个网站访问的人有那些</font>
```shell
cat access.log | awk '{print \$1}' | sort -n | uniq
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">统计某一网站访问人数uv</font>
```shell
cat access.log | awk '{print \$1}' | sort -n | uniq | wc -l
```
<font style="color:rgb(51, 51, 51);">每个ip地址的个数每次ip访问的次数</font>
```shell
cat access.log | awk '{print \$1}' | sort -n | uniq
```
<font style="color:rgb(51, 51, 51);">获取访问次数前三的人/ip地址</font>
```shell
cat access.log | awk '{print \$1}' | sort | uniq -c | sort -rn | head -3
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);"></font>
### linux基础命令
ls参数使用
```bash
ls -l :显示文件的详细信息 或者 使用 ll
ls -lh :以单位显示
ls -i 显示文件的inode号
ls -a :显示隐藏文件
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">date时间使用</font>
<font style="color:rgb(51, 51, 51);">date:查看时间和日期、修改时间和日期</font>
```bash
获取当前日期date +%F F日期
获取当前时间date +%H:%M:%S H时 M分 S
获取周几: date +%w w
获取日期+时间 date +%F-%T
touch `date +%F` 创建当前时间的文件
date -s 3:00 //指定时间
```
## 第四章linux常用服务管理
### ntp时间服务
<font style="color:rgb(51, 51, 51);">timedatectl set-timezone Asia/Shanghai 设置时区为上海</font>
安装时间服务
```shell
yum -y install ntp
```
修改配置文件
```shell
restrict default nomodify ##不允许客户端登录,也不允许客户端修改
server 127.127.1.0 ##使用本地的bios时间自己跟自己同步
fudge 127.127.1.0 stratum 10 ##定义级别范围0-16越小越精准
```
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">启动服务</font>
```shell
systemctl start ntpd && systemctl enable ntpd
```
同步阿里云时间
```bash
ntpdate ntp.aliyun.com
```
### 计划任务服务
一次性计划任务
安装一次性计划任务服务
```bash
yum -y install at && systemctl start atd
```
时间:只能指定某一个时间点
任务/命令/动作
<font style="color:rgb(51, 51, 51);">100 5/1/2024 ##小时:分钟 月:日:年</font>
案例
```bash
[root@localhost ~]# at 8:01
at> touch /opt/test
at> <EOT> ##ctrl +d 结束计划任务创建
job 2 at Thu Apr 10 08:01:00 2025
```
at -l 查看计划任务
at -d 任务序列号
循环性计划任务
<font style="color:rgb(51, 51, 51);">crontab执行时间计算 (</font>[https://tool.lu/crontab/](https://tool.lu/crontab/)<font style="color:rgb(51, 51, 51);">)</font>
<font style="color:rgb(51, 51, 51);"></font>
<font style="color:rgb(51, 51, 51);">crond服务</font>
```bash
systemctl status crond ## 默认开机自启
```
<font style="color:rgb(51, 51, 51);"> </font><font style="color:rgb(51, 51, 51);">crontab -e 创建计划任务</font>
<font style="color:rgb(51, 51, 51);"> crontab -l 查看计划任务</font>
<font style="color:rgb(51, 51, 51);"> crontab -r 删除计划任务</font>
<font style="color:rgb(51, 51, 51);">/var/spool/cron 里面存放了循环计划任务的文件 以用户命名</font>
<font style="color:rgb(51, 51, 51);"></font>
### <font style="color:rgb(51, 51, 51);">ftp服务</font>
服务端:
安装vsftp服务
```bash
yum -y install vsftpd
```
启动服务并设置开机自启
```bash
systemctl start vsftpd && systemctl enable vsftpd
## 浏览器或者文件资源管理器访问测试
```
客户端:
安装lftp服务
```bash
yum -y install lftp
```
连接服务端
```bash
lftp 192.168.198.129
```
基本概要
```bash
/var/ftp/ ## 对外共享目录,要共享的文件放到该目录下
/var/log/xferlog ## 用户的上传和下载都会被该文件记录 vsftpd日志文件
mirror ## 下载目录
get ## 下载文件
```
<font style="color:rgb(51, 51, 51);">开启匿名用户上传模式</font>
```bash
mkdir /var/ftp/date
chmod 777 /var/ftp/date/
```
<font style="color:rgb(51, 51, 51);">修改文件实现上传功能</font>
```bash
anon_upload_enable=YES # 是否允许匿名用户上传文件
anon_mkdir_write_enable=YES # 是否允许匿名用户允许创建目录
```
验证上传功能
```bash
lftp 192.168.198.129
put +文件名(绝对路径)上传的文件本地有
mirror -R 目录名(绝对路径) 上传的目录本地有
注意:
1.对外共享目录自身不能使用777权限否则会报错
2.上传文件前先要进入对应的目录下,否则无法上传
3.所有服务器都要关闭防火墙和SELinux
```
### 密钥远程管理服务
ssh服务
/etc/ssh/sshd_config 服务端配置文件
/etc/ssh/ssh_config 客户端配置文件
/root/.ssh/ 密钥存放的位置
ssh-keygen 生成密钥
+ id_rsa 私钥
+ id_rsa.pub 公钥
+ <font style="color:rgb(36, 41, 47);">authorized_keys 存放对方的公钥</font>
拷贝私钥到B机器
```plain
scp id_rsa 192.168.0.1:/root
```
给192.168.0.1机器拷贝密钥(默认是公钥)
```plain
ssh-copy-id 192.168.0.1
```
scp远程拷贝
```plain
scp -r 源文件/源目录 192.168.0.1:/root
```