[命令] Linux 命令 gunzip (打包、解包、压缩和解压文件或目录,压缩包以 xz 作为后缀)

内容一:gunzip 命令简介

1.1 gunzip 命令的格式

# gunzip <option> <file>

1.2 gunzip 命令的常用选项

1) -c or –stdout, write on standard output, keep original files unchanged
2) -d or –decompress, decompress
3) -f or –force, force overwrite of output file and compress links
4) -h or –help, give this help
5) -k or –keep, keep (don’t delete) input files
6) -l or –list, list compressed file contents
7) -n or –no-name, do not save or restore the original name and timestamp
8) -N or –name, save or restore the original name and timestamp
9) -q or –quiet, suppress all warnings
10) -r or –recursive or –rsyncable, operate recursively on directories
11) -t or –test, test compressed file integrity
12) -1 or –fast, compress faster
13) -9 or –best, compress better

内容二:gunzip 命令的使用案例

2.1 案例一:压缩文件

# gunzip test.txt

(补充:这里以压缩文件 test.txt 为例)

2.2 案例二:解压文件

# gunzip -d test.txt.xz

(补充:这里以解压文件 test.txt.zx 为例)

[内容] auditd 使用 (监控文件或目录的变化)

内容一:auditd 的管理

1.1 启动 auditd

1.1.1 启动 auditd
# service auditd restart
1.1.2 查看 auditd 状态
# auditctl -s
1.1.3 设置 auditd 开机自启
# chkconfig auditd on
1.1.4 查看 auditd 开机自启状态
# chkconfig --list auditd

1.2 查看 auditd 的规则

# auditctl -l

1.3 删除 auditd 的规则

1.3.1 永久删除 auditd 的所有规则
1.3.1.1 清空 /etc/audit/audit.rules 文件里的所有内容
# vi /etc/audit/audit.rules

删除里面的所有内容

1.3.1.2 让刚刚的设置生效
# service auditd restart

或者:

# augenrules
1.3.2 临时删除 auditd 的规则
1.3.2.1 临时删除 auditd 的所有规则
# auditctl -D

(补充:此操作重启后失效)

1.3.2.2 临时删除 auditd 的普通规则 (以 -w 开头的规则)
# auditctl -W <policy>

(补充:此操作重启后失效)

1.3.2.3 临时删除 auditd 的使用系统调用和过滤条件的监控规则 (以 -a 开头的规则)
# auditctl -d <policy>

(补充:此操作重启后失效)

1.4 显示 auditd 日志

1.4.1 显示 auditd 的所有日志
# cat /var/log/audit/audit.log
1.4.2 显示某文件或目录的日志
# ausearch -f <file/directory>
1.4.3 显示某关键词的日志
# ausearch -k <key_name>

1.5 生成 auditd 日志报告

# aureport -k

内容二:auditd 的规则

2.1 普通监控规则

2.1.1 普通监控规则的格式
2.1.1.1 普通监控规则的格式
-w <file/directory> -p <previlege> -k <key_name>


补充:
1) 文件名或目录名,需要绝对路径
2) 监控的权限,可以是 rwxa 其中的任意 1 个或多个,r 代表读权限、w 代表写权限,x 代表执行权限,a 代表文件类型
3) 此类日志的关键词

2.1.1.2 添加普通监控规则的格式
2.1.1.2.1 临时添加普通监控规则的格式
# auditctl -w <file/directory> -p <previlege> -k <key_name>
2.1.1.2.2 永久添加普通监控规则的格式
# vim /etc/audit/audit.rules

添加以下内容:

......
-w <file/directory> -p <previlege> -k <key_name>


注意:永久添加的规则后要重启 auditd 服务后才会生效

# service auditd restart

或者:

# augenrules

2.1.2 添加普通监控规则的案例
2.1.2.1 案例一:添加监控 /etc/nginx/nginx.conf 文件的读、写、执行和文件属性变化的规则
# auditctl -w /etc/nginx/nginx.conf

(补充:这里以临时添加监控 /etc/nginx/nginx.conf 文件的读、写、执行和文件属性变化的规则为例)

2.1.2.2 案例二:添加监控 /etc/nginx/nginx.conf 文件的读、写、执行和文件属性变化,并设置关键词为 nginx 的规则
# auditctl -w /etc/nginx/nginx.conf -p rwxa -k 'nginx'

(补充:这里以临时添加监控 /etc/nginx/nginx.conf 文件的读、写、执行和文件属性变化,并设置关键词为 nginx 的规则为例)

2.2 使用系统调用和过滤条件的监控规则

2.2.1 使用系统调用和过滤条件的监控规则的格式
2.2.1.1 使用系统调用和过滤条件的监控规则的格式
-a <action>,<filter> -S <system_call> -F <field>=<value> -k <key_name>


补充:
1) 和 用于确定事件被记录, 的值可以是 always 或者 never, 的值可以是 task、exit、user 或者 exclude
2) 是系统调用,Linux 系统调用的名称在 /usr/include/asm/unistd_64.h 文件中,可以将多个系统调用放在一个规则里,例:-S <system_call> -S <system_call> ……,或者 -S <system_call>,<system_call>……
3) 和 是过略条件,可以将多个过略条件放在一个规则里,-F <field>=<value> -F <field>=<value> ……
4) 是此类日志的关键词

2.2.1.2 添加使用系统调用和过滤条件的监控规则的格式
2.2.1.2.1 临时添加使用系统调用和过滤条件的监控规则的格式
# auditctl -a <action>,<filter> -S <system_call> -F <field>=<value> -k <key_name>
2.2.1.2.2 永久添加使用系统调用和过滤条件的监控规则的格式
# vim /etc/audit/audit.rules

添加以下内容:

......
-a <action>,<filter> -S <system_call> -F <field>=<value> -k <key_name>


注意:永久添加的规则后要重启 auditd 服务后才会生效

# service auditd restart

或者:

# augenrules

2.2.2 使用系统调用和过滤条件的监控规则的案例
2.2.2.1 案例一:监控所有 UID 大于 1000 的用户的删除操作,并设置关键词为 delete
# auditctl -a always,exit -S rmdir -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=unset -F auid!=4294967295 -k delete

(补充:这里以临时监控所有 UID 大于 1000 的用户删除操作为例)

(注意:这里的 -F auid!=4294967295 是为了排除 login UID 没有被设置的用户)

2.2.2.2 案例二:监控所有 UID 大于 1000 的用户的文件删除操作,并设置关键词为 delete
# auditctl -a always,exit -S unlink -S unlinkat -S rename -S renameat -F auid>=1000 -F auid!=unset -F auid!=4294967295 -k delete

(补充:这里以临时监控所有 UID 大于 1000 的用户删除操作为例)

(注意:这里的 -F auid!=4294967295 是为了排除 login UID 没有被设置的用户)

2.2.2.3 案例三:监控所有网络连接
# auditctl -a always,exit -F arch=b64 -S socket
# auditctl -a always,exit -F arch=b64 -S connect
# auditctl -a always,exit -F arch=b64 -S sendmmsg
# auditctl -a always,exit -F arch=b64 -S sendmsg
# auditctl -a always,exit -F arch=b64 -S bind
# auditctl -a always,exit -F arch=b64 -S recvmsg
# auditctl -a always,exit -F arch=b64 -S close

(补充:这里以监控所有网络连接为例)

[命令] Linux 命令 locate (查找文件)

内容一:locate 命令的安装

1.1 安装 locate 命令

# dnf install mlocate

1.2 更新 locate 数据库

# updatedb


注意:如果不更新 locate 数据库,在使用 locate 命令后可能会报错

locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

内容二:locate 命令的选项

1) -b 或者 –basename 只显示使用指定模式匹配名称的条目
2) -c 或者 –count 只显示找的数量
3) -e 或者 –existing 只显示存在的条目
4) -i 或者 –ignore-case 查找时忽略大小写
5) -r 或者 –regexp 使用正则表达式
6) –regex 使用扩展正则表达式

内容三:locate 命令的案例

3.1 案例一:查找包含 passwd 的文件

# locate passwd

3.2 案例二:在 /etc/ 目录下查找以 sh 开头的文件

# locate /etc/sh

3.3 案例三:在当前目录下查找以 r 开头的文件并忽略大小写

# locate -i ~/r

3.4 案例四:使用正则表达式超找包含 1.txt 或 2.txt 的文件

# locate -r [1-2].txt

Linux 启动顺序

第一步启动:BIOS/UEFI

BIOS (Basic Input Output System) 即基本输入输出系统。

UEFI (Unified Extensible Firmware Interface) 即可扩展固件接口。

它们的主要作用是为计算机提供直接、底层的硬件控制和设置。UEFT 是 BIOS 的升级替代方案。

第二步启动:MBR/GPT

MBR (Master Boot Record) 即主引导记录。MBR 通常和 BIOS 搭配,最大分区容量不能超过 2T,最多可以有 3 个主分区,1 个扩展分区,不过扩展分区里可以有多个逻辑分区。

GPT (GUID Partition Table) 即全局唯一标识分区表。GPT 通常和 UEFI 搭配。

它们都是读取硬盘时最先读取的分区,里面有各自的启动代码。

第三步启动:GRUB2

GRUB2 (GRand Unified Bootloader version 2) 即多操作系统启动程序,可以选择系统分区上不同的系统内核,同时也可以向启动的内核传递参数。

第四步启动:initrd/Kernel

initrd (init ramdisk) 即初始化内存盘,作用是:
1) 提供开机必备的单 kernel 文件 (vmlinuz) 没有提供的驱动模块。
2) 通过引导加载程序加载内存,将内存视为临时根 “/” 目录,从中运行程序,之后再将根 “/” 目录转移到硬盘上真正的根 “/” 目录上。


注意:系统启动 initrd 的时间默认上限是 90 秒钟,如果 initrd 加载的时间超过 90 秒钟,则系统会报以下错误:

Warning: dracut-initqueue timeout - starting timeout scripts

(补充链接:Linux 解决启动时,报错 “Warning: dracut-initqueue timeout – starting timeout scripts”

第五步启动:systemdprocess

systemdprocess,即启动各类程序和进程。