[命令] Linux 命令 dmesg (显示开机日志)

内容一:dmesg 简介

Linux 的 kernel 会把开机日子存储在 ring buffer 和 /var/log/message 中,其中 ring buffer 里的开机日志可以使用 dmesg 来显示,如果开机时的提示信息来不及阅读的话,可以通过 dmesg 命令来显示

内容二:dmesg 命令的选项

1) -c 显示 ring buffer 的日志后清空 ring buffer
2) -n 显示日志的层级
3) -cn 显示 ring buffer 的日志后清空 ring buffer,同时显示日志的层级
4) -s 设置缓冲区的大小,默认为 8196 与 ring buffer 的大小一样

内容三:dmesg 命令的使用案例
3.1 显示所有开机日志

# dmesg | less

3.2 显示所有日志,显示 ring buffer 的日志后清空 ring buffer,同时显示日志的层级

# dmesg -cn

3.3 显示所有失败的开机日志

# dmesg | egrep -i "fail|warn|error"

[步骤] SELinux 的启用 (openSUSE & SLE 版) (不建议)

软件准备:

在 SELinuxProject 的官网上下载 SELinux 策略 UseRefpolicy:

https://github.com/SELinuxProject/refpolicy/wiki/UseRefpolicy

注意:

1) 如果使用此文的方法将 openSUSE & SLE 的 SELinux 设置为 Enforcing 则系统将无法设置 IP 地址
2) 如果使用此文的方法开启了 SELinux 并且将所有的布尔(boolean)值开启,则系统将无法关机,开启所有布尔值的方法:# for i in semanage boolean -l | awk '{print $1}'; do echo $i;setsebool -P $i 1; done

正文:

步骤目录:

步骤一:安装 SELinux 组件

步骤二:安装 SELinux 策略
2.1 解压包含 SELinux 策略的压缩包
2.2 将 SELinux 策略移动到 SELinux 配置文件的位置
2.3 进入到和 SELinux 策略相同目录下
2.4 显示 SELinux 策略的安装手册
2.5 创建 SELinux 策略的配置文件
2.6 创建 SELinux 策略
2.7 编译 SELinux 策略
2.8 安装 SELinux 策略

步骤三:配置 SELinux 配置文件
3.1 在 SELinux 配置文件中将 SELinux 设置为 Permissive 状态
3.2 在系统内核中禁止使用 AppArmor 使用 SELinux 并且将 SELinux 状态设置为 Permissive
3.2.1 设置 grub 文件
3.2.2 让刚刚设置的 grub 文件生效
3.3 刷新系统内所有文件的标签

步骤四:重启系统让 SELinux 生效

具体的操作步骤:

步骤一:安装 SELinux 组件

# zypper in libselinux1 libsemanage1 libsepol-devel libsepol1 libselinux-devel mcstrans libselinux1-32bit policycoreutils checkpolicy libsemanage-devel setools-tcl setools-libs setools-java setools-devel setools-console selinux-tools python3-policycoreutils python3-selinux python3-semanage python3-setools restorecond

步骤二:安装 SELinux 策略
2.1 解压包含 SELinux 策略的压缩包

# tar -xvf refpolicy-2.20210203.tar.bz2

(补充:这里以解压 refpolicy-2.20210203.tar.bz2 压缩包为例)

2.2 将 SELinux 策略移动到 SELinux 配置文件的位置

# mv refpolicy /etc/selinux/

2.3 进入到和 SELinux 策略相同目录下

# cd /etc/selinux/refpolicy/

2.4 显示 SELinux 策略的安装手册

# cat INSTALL

2.5 创建 SELinux 策略的配置文件

# make conf

2.6 创建 SELinux 策略

# make policy

2.7 编译 SELinux 策略

# make install

2.8 安装 SELinux 策略

# make load

步骤三:配置 SELinux 配置文件
3.1 在 SELinux 配置文件中将 SELinux 设置为 Permissive 状态

# vim /etc/selinux/config

创建以下内容:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=refpolicy

3.2 在系统内核中禁止使用 AppArmor 使用 SELinux 并且将 SELinux 状态设置为 Permissive
3.2.1 设置 grub 文件

# vim /etc/default/grub

在这一行里:

GRUB_CMDLINE_LINUX_DEFAULT="......"
添加以下内容:
GRUB_CMDLINE_LINUX_DEFAULT="...... security=selinux selinux=1 enforcing=0"

3.2.2 让刚刚设置的 grub 文件生效

# grub2-mkconfig -o /boot/grub2/grub.cfg

3.3 刷新系统内所有文件的标签

# restorecon -Rp /

步骤四:重启系统让 SELinux 生效

# reboot

参考文献:

https://documentation.suse.com/sles/15-SP2/html/SLES-all/cha-selinux.html

[内容] Linux SELinux 状态的设置

内容目录:

内容一:SELinux 的状态

内容二:永久切换 SELinux 状态
2.1 将 SELinux 永久切换至 Disabled 状态
2.1.1 修改 SELinux 配置文件
2.1.2 重启系统
2.1.3 显示 SELinux 状态
2.2 将 SELinux 永久切换至 Permissive 状态
2.2.1 修改 SELinux 配置文件
2.2.2 重启系统
2.2.3 显示 SELinux 状态
2.3 将 SELinux 永久切换至 Enforcing 状态
2.3.1 修改 SELinux 配置文件
2.3.2 重启系统
2.3.3 显示 SELinux 状态

内容三:临时切换 SELinux 状态
3.1 临时切换到 Permissive 状态
3.1.1 临时切换到 Permissive 状态
3.1.2 显示 SELinux 状态
3.2 临时切换到 Enforcing 状态
3.2.1 临时切换到 Enforcing 状态
3.2.2 显示 SELinux 状态

具体的内容:

内容一:SELinux 的状态

1) Disabled:完全关闭 SELinux
2) Permissive:即使违反了策略也依旧可以执行,但是违反策略的记录会被记录在日志中
3) Enforcing:如果违反了策略就不能之行

内容二:永久切换 SELinux 状态
2.1 将 SELinux 永久切换至 Disabled 状态
2.1.1 修改 SELinux 配置文件

# vim /etc/selinux/config

将以下内容:

......
SELINUX=......
......

修改为:

......
SELINUX=disabled
......

2.1.2 重启系统

# reboot

2.1.3 显示 SELinux 状态

# getenforce 
Disabled

2.2 将 SELinux 永久切换至 Permissive 状态
2.2.1 修改 SELinux 配置文件

# vim /etc/selinux/config

将以下内容:

......
SELINUX=......
......

修改为:

......
SELINUX=permissive
......

2.2.2 重启系统

# reboot

2.2.3 显示 SELinux 状态

# getenforce 
Permissive

2.3 将 SELinux 永久切换至 Enforcing 状态
2.3.1 修改 SELinux 配置文件

# vim /etc/selinux/config

将以下内容:

......
SELINUX=......
......

修改为:

......
SELINUX=enforcing
......

2.3.2 重启系统

# reboot

2.3.3 显示 SELinux 状态

# getenforce 
Enforcing

内容三:临时切换 SELinux 状态
3.1 临时切换到 Permissive 状态
3.1.1 临时切换到 Permissive 状态

# setenfoce 0


注意:
1) 系统重启后失效
2) 只能从 Enforcing 状态切换到 Permissive 状态

3.1.2 显示 SELinux 状态

# getenforce 
Permissive

3.2 临时切换到 Enforcing 状态
3.2.1 临时切换到 Enforcing 状态

# setenfoce 1


注意:
1) 系统重启后失效
2) 只能从 Permissive 状态切换到 Enforcing 状态

3.2.2 显示 SELinux 状态

# getenforce 
Enforcing

[实验] Linux 远程日志的集中管理和分文件存储 (通过 Rsyslog 实现)

步骤目录:

步骤一:规划拓扑
1.1 服务器列表
1.2 服务器列表简介

步骤二:系统环境要求

步骤三:配置 rsyslog 服务端
3.1 修改 rsyslog 配置文件的案例
3.1.1 案例一
3.1.2 案例二
3.1.3 案例三
3.1.4 案例四
3.1.5 案例五
3.2 重启 rsyslog 服务

步骤四:配置 rsyslog 客户端
4.1 修改 rsyslog 客户端的案例
4.2 重启 rsyslog 服务

步骤五:测试 rsyslog 服务
5.1 在 rsyslog 客户端上发送日志
5.2 在 rsyslog 服务端上显示日志

具体的操作步骤:

步骤一:规划拓扑
1.1 服务器列表

rsyslog 服务端 IP 地址:192.168.0.11
rsyslog 客户端 IP 地址:192.168.0.12

1.2 服务器列表简介

rsyslog 客户端将日志发送给 rsyslog 服务端,并实现分文件存储

步骤二:系统环境要求

1) 所有服务器的系统都需要是 CentOS 8 版本
2) 所有服务器都要关闭防火墙
3) 所有服务器都要关闭 SELinux
4) 需要按照拓扑图给对应的服务器配置好 IP 地址和主机名
5) 所有服务器都要可以相互 ping 通自己和对方的 IP 地址和主机名

步骤三:配置 rsyslog 服务端
3.1 修改 rsyslog 配置文件的案例
3.1.1 案例一

(只在 rsyslog 服务端执行以下步骤)

# vim /etc/rsyslog.conf

添加以下内容:

......
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")


补充:
1) 使用 UDP 的 514 端口接收日志
2) 使用 TCP 的 514 端口接收日志
3) 此配置会将所有 rsyslog 客户端的日志存储在 rsyslog 服务端的 /var/log/message 里

3.1.2 案例二

(只在 rsyslog 服务端执行以下步骤)

# vim /etc/rsyslog.conf

添加以下内容:

......
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")

$template DynFile,"/var/log/%HOSTNAME%.log"
*.* -?DynFile
& ~


补充:
1) 使用 UDP 的 514 端口接收日志
2) 使用 TCP 的 514 端口接收日志
3) 此配置会将每个 rsyslog 客户端的日志单独存储在 /var/log/ 下的一个文件里,此文件会以其所属系统名命名

3.1.3 案例三

(只在 rsyslog 服务端执行以下步骤)

# vim /etc/rsyslog.conf

添加以下内容:

......
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")

$template DynFile,"/var/log/remotelog/%HOSTNAME%-%$YEAR%-%$MONTH%-%$DAY%-messages.log"
*.* -?DynFile
& ~


补充:
1) 使用 UDP 的 514 端口接收日志
2) 使用 TCP 的 514 端口接收日志
3) 此配置会将每个 rsyslog 客户端的日志单独存储在 /var/log/remotelog 下的一个文件里,此文件会以其创建时间和其所属系统名命名

3.1.4 案例四

(只在 rsyslog 服务端执行以下步骤)

# vim /etc/rsyslog.conf

添加以下内容:

......
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")

$template DynFile,"/var/log/remotelog/%HOSTNAME%-%$YEAR%-%$MONTH%-%$DAY%-messages.log"
:fromhost-ip,!isequal,"127.0.0.1" -?DynFile
& ~


补充:
1) 使用 UDP 的 514 端口接收日志
2) 使用 TCP 的 514 端口接收日志
3) 此配置会将每个 rsyslog 客户端的日志单独存储在 /var/log/ 下的一个文件里,此文件会以其创建时间和其所属系统名命名
4) 此配置不会单独存储 rsyslog 服务端的日志

3.1.5 案例五

(只在 rsyslog 服务端执行以下步骤)

# vim /etc/rsyslog.conf

添加以下内容:

......
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")

$template DynFile,"/var/log/syslog/system-%HOSTNAME%/messages.log"
*.* -?DynFile
& ~


补充:
1) 使用 UDP 的 514 端口接收日志
2) 使用 TCP 的 514 端口接收日志
3) 此配置会将每个 rsyslog 客户端的日志单独存储在 /var/log/ 下某个目录的 messages 文件里,此目录会以其所属系统名命名

3.2 重启 rsyslog 服务

(只在 rsyslog 服务端执行以下步骤)

# systemctl restart rsyslog


补充:
1) 使用 UDP 的 514 端口接收日志
2) 使用 TCP 的 514 端口接收日志
3) 需要在 3.1 修改 rsyslog 配置文件的案例中任选其一完成后在执行此步骤

步骤四:配置 rsyslog 客户端
4.1 修改 rsyslog 客户端的案例

(只在 rsyslog 客户端执行以下步骤)

# vim /etc/rsyslog.conf

添加以下内容:

......
*.emerg;*.alert;*.crit;*.err;*.warning;*.notice;*.info  @192.168.0.11

或者:

......
*.emerg;*.alert;*.crit;*.err;*.warning;*.notice;*.info  @@192.168.0.11


补充:
1) 这里的 192.168.0.11 是指 rsyslog 服务端的 IP 地址
2) IP 地址前一个 “@” 符后是指使用 UDP 端口 514 传输日志
3) IP 地址前两个 “@” 符后是指使用 TCP 端口 514 传输日志

4.2 重启 rsyslog 服务

(只在 rsyslog 客户端执行以下步骤)

# systemctl restart rsyslog

步骤五:测试 rsyslog 服务
5.1 在 rsyslog 客户端上发送日志

(只在 rsyslog 客户端执行以下步骤)

# logger "This is our test log"

(补充:这里以发送 This is our test log 信息为例)

5.2 在 rsyslog 服务端上显示日志

(只在 rsyslog 服务端执行以下步骤)

# cat /var/log/* | grep test

[步骤] Linux Kdump 的开启 (用于收集内核崩溃时的信息)

步骤一:允许 Kdump 使用内存
1.1 方法一:在 /etc/default/grub 文件里修改 crashkernel 参数
1.1.1 在 /etc/default/grub 文件里修改 crashkernel 参数

# vim /etc/default/grub

在这一行里:

.....
GRUB_CMDLINE_LINUX_DEFAULT="......"
.....

确保有:

.....
GRUB_CMDLINE_LINUX="crashkernel=auto......"
.....

并确保此文件其他地方没有和 crashkernel= 相关的参数

(补充:这里的 auto 代表系统会根据内存大小自动设置一个值,也可以指定一个值,例如:crashkernel=crashkernel=128M,high、crashkernel=crashkernel=256M,high 等等,但是建议设置成 crashkernel=crashkernel=512M,high)

1.1.2 让刚刚修改的内核参数生效

# grub2-mkconfig -o /boot/grub2/grub.cfg
# reboot

1.2 方法二:通过 yast 工具修改 crashkernel 参数
1.2.1 通过 yast 工具修改 crashkernel 参数

# yast kdump

之后将 –Start-Up 中的 Kdump Low Memory [MiB] (72 – 3069) 修改为 256,将 –Start-Up 中的 Kdump High Memory [MiB] (0 – 7168) 修改为 512,之后再选择 [ OK ]

(补充:当 Kdump Low Memory 设置为 256,Kdump High Memory 设置为 512 时更易触发 Kdump)

(注意:方法二只有 openSUSE & SUSE 可以使用)

1.2 让刚刚修改的内核参数生效

# reboot

步骤二:修改 Kdump 的配置信息
2.1 修改 Kdump 的配置文件

# vim /etc/kdump.conf

将以下内容:

......
path /var/crash
core_collector makedumpfile -l --message-level 1 -d 31
......

修改为:

......
path /var/crash
core_collector makedumpfile -c -l --message-level 1 -d 31
default reboot
......


补充:
1) path /var/crash
2) -c 参数会对搜集的内核崩溃时的信息进行压缩
3) default reboot 参数会让 KDUMP 收集完内核崩溃时的信息后重启

2.2 让刚刚修改的 Kdump 配置文件生效

# systemctl enable --now kdump.service

步骤三:测试 Kdump
3.1 造成系统内核崩溃

# echo 1 > /proc/sys/kernel/sysrq ; echo c > /proc/sysrq-trigger

(注意:此时系统会自动崩溃并重启)

3.2 显示 Kdump 生成内核崩溃信息

# ls /var/crash/<date>/vmcore

(补充:这里的内核崩溃信息存放目录 /var/crash/,是刚刚在 /etc/kdump.conf 文件里指定的)