Appearance
15. 故障排查与性能优化
本章节指导在 RHEL 8.10(主机名 ZSLinux)上使用 SecureCRT 进行故障排查和性能优化,配合 SecureFX 传输日志文件。内容涵盖故障排查流程(日志分析、系统调用跟踪)、性能监控(sar, iostat, top)和系统优化(swappiness, I/O 调度器、cgroups),以 SSH(端口 2222,参考第 6 章)等案例为例,全面实用,适合初学者快速掌握和运维人员日常维护。所有操作在 ZSLinux 环境中测试,确保实验一致性。
15.1 前提条件
- RHEL 8.10 已安装(参考第 1 章),主机名设置为
ZSLinux. - 使用 SecureCRT 登录(SSH2 协议,端口 2222,参考第 6 章).
- 使用 SecureFX 传输文件(SFTP 协议).
- 具有 root 或 sudo 权限.
- 系统已订阅并启用 RHEL 和 EPEL 仓库(参考第 4 和 7 章).
sshd服务已配置(参考第 6 章).
15.2 故障排查
15.2.1 日志分析
- 查看系统和服务日志:bash
journalctl -u sshd # 查看 SSH 服务日志 journalctl -xe # 显示系统日志,定位末尾并带扩展信息 dmesg | grep error # 查看内核日志中的错误信息
15.2.2 系统调用跟踪
- 使用
strace跟踪服务:bashstrace -p $(pidof sshd) -o sshd_trace.log # 跟踪 sshd 进程的系统调用 cat sshd_trace.log # 查看跟踪日志
15.2.3 收集系统信息
- 生成
sosreport:bashdnf install -y sos # 安装 sos 工具 sosreport --batch # 生成系统诊断报告(存储在 /var/tmp)
15.2.4 故障排查案例
- SSH 连接失败:bash
systemctl status sshd # 检查 SSH 服务状态 ss -tunlp | grep 2222 # 检查端口 2222 是否监听 firewall-cmd --list-all # 检查防火墙规则 journalctl -u sshd --since "1 hour ago" # 查看最近 1 小时 SSH 日志 ausearch -m avc -ts recent # 检查 SELinux 拒绝日志 - 高 CPU 使用率:bash
top # 实时监控,按 P 键按 CPU 排序 ps aux | sort -nrk 3 | head # 查找 CPU 占用最高的进程 - 磁盘空间不足:bash
df -h # 查看分区使用情况 du -sh /var/* | sort -hr # 查找 /var 占用空间大的目录 - 网络延迟:bash
ping -c 4 google.com # 测试连通性和延迟 mtr google.com # 诊断网络路径和延迟
15.3 性能监控与优化
15.3.1 性能监控
- 安装并使用监控工具:bash
dnf install -y sysstat # 安装 sysstat 工具集(sar, iostat) - 监控 CPU 和磁盘:bash
sar -u 1 5 # 每秒收集 CPU 使用率,5 次 iostat -x 1 5 # 每秒收集磁盘 I/O 统计,5 次 vmstat 1 5 # 每秒收集系统资源统计,5 次
15.3.2 系统性能优化
- 调整
swappiness:bashsysctl -w vm.swappiness=10 # 临时设置 swappiness 为 10 vim /etc/sysctl.d/99-swappiness.conf # 永久设置 # 添加: vm.swappiness=10 - 优化 I/O 调度器:bash
tuned-adm profile virtual-guest # 应用虚拟机优化配置 tuned-adm active # 验证当前 profile - 配置 cgroups 资源限制:bash
dnf install -y libcgroup-tools # 安装 cgroups 工具 cgcreate -g cpu:/limitgroup # 创建 CPU 限制组 cgset -r cpu.shares=512 limitgroup # 设置 CPU 配额 cgexec -g cpu:limitgroup /usr/bin/custom-script.sh # 在限制组运行脚本
15.4 验证
- 验证故障排查:bash
systemctl is-active sshd # 检查 SSH 服务状态 df -h # 验证磁盘空间 ping -c 4 google.com # 验证网络连通性 - 验证性能监控:bash
sar -u 1 1 # 检查 CPU 使用率 iostat -x 1 1 # 检查磁盘 I/O free -m # 检查内存和 swap 使用情况 - 验证优化效果:bash
sysctl vm.swappiness # 检查 swappiness 值 tuned-adm active # 检查 tuned profile cgget -g cpu:/limitgroup # 检查 cgroups 配置 - 测试 SSH 连接:bash
ssh -p 2222 testuser@ZSLinux # 测试 SSH 连接(参考第 6 章) - 常见问题:
- SSH 连接失败:检查服务(
systemctl status sshd),防火墙(firewall-cmd --list-all),或 SELinux(ausearch -m avc)。 - 高 CPU 使用率:使用
top或ps aux | sort -nrk 3定位进程。 - 磁盘空间不足:清理
/var/log或使用fdupes(参考第 14 章)。 - 性能优化无效:确认
tuned-admprofile 或重启服务。
- SSH 连接失败:检查服务(
15.5 实践任务
- 使用 SecureCRT 登录
ZSLinux,模拟 SSH 连接失败并排查(检查日志、防火墙、SELinux)。 - 使用
top和iostat监控 CPU 和磁盘 I/O,记录高负载进程。 - 配置
swappiness为 10 并验证效果。 - 使用
sosreport生成系统诊断报告并传输至本地。
15.6 自测问题
- 问题:如何排查服务失败?
- 答案:
journalctl -u <service>,systemctl status <service>
- 答案:
- 问题:如何找到高 CPU 进程?
- 答案:
top或ps aux | sort -nrk 3 | head
- 答案:
- 问题:如何调整
swappiness永久生效?- 答案:编辑
/etc/sysctl.d/99-swappiness.conf,添加vm.swappiness=10
- 答案:编辑
